tests: Copy stdlib tests from Kotlin/JVM

This patch copies Kotlin/JVM stdlib tests (libraries/stdlib/test)
excepting ones with explicit java dependencies. It also transforms
the tests to use them as box-tests.
This commit is contained in:
Ilya Matveev
2017-04-04 18:20:09 +07:00
committed by ilmat192
parent 5d50943ebd
commit 85827b4880
530 changed files with 11027 additions and 4 deletions
@@ -0,0 +1,26 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
fun box() {
val byName = compareBy<Item> { it.name }
val byRating = compareBy<Item> { it.rating }
val v3 = Item(v1.name, v1.rating + 1)
val v4 = Item(v2.name + "_", v2.rating)
assertTrue((byName then byRating).compare(v1, v2) > 0)
assertTrue((byName then byRating).compare(v1, v3) < 0)
assertTrue((byName thenDescending byRating).compare(v1, v3) > 0)
assertTrue((byRating then byName).compare(v1, v2) < 0)
assertTrue((byRating then byName).compare(v4, v2) > 0)
assertTrue((byRating thenDescending byName).compare(v4, v2) < 0)
}
@@ -0,0 +1,16 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val diff = v1.compareTo(v2)
assertTrue(diff < 0)
}
@@ -0,0 +1,16 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val diff = compareValuesBy(v1, v2, { it.name }, { it.rating })
assertTrue(diff > 0)
}
@@ -0,0 +1,16 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val diff = compareValuesBy(v1, v2, { it.rating }, { it.name })
assertTrue(diff < 0)
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val v1: Item? = v1
val v2: Item? = null
val diff = compareValuesBy(v1, v2) { it?.rating }
assertTrue(diff > 0)
val diff2 = nullsLast(compareBy<Item> { it.rating }.thenBy { it.name }).compare(v1, v2)
assertTrue(diff2 < 0)
}
@@ -0,0 +1,15 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
fun box() {
val diff = compareValuesBy(v1, v1, { it.rating }, { it.name })
assertTrue(diff == 0)
}
@@ -0,0 +1,14 @@
import kotlin.test.*
import kotlin.comparisons.*
fun box() {
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE))
assertEquals(Int.MAX_VALUE, maxOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE))
assertEquals(Long.MAX_VALUE, maxOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
assertEquals(Double.POSITIVE_INFINITY, maxOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
}
@@ -0,0 +1,19 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
fun box() {
assertEquals(v1, maxOf(v1, v2, compareBy { it.name }))
assertEquals(v1, maxOf(v3, v2, v1, compareBy { it.name }))
assertEquals(v2, maxOf(v1, v2, compareBy { it.rating }))
assertEquals(v3, maxOf(v1, v2, v3, compareBy { it.rating }))
}
@@ -0,0 +1,14 @@
import kotlin.test.*
import kotlin.comparisons.*
fun box() {
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE))
assertEquals(Int.MIN_VALUE, minOf(Int.MAX_VALUE, Int.MIN_VALUE, 0))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE))
assertEquals(Long.MIN_VALUE, minOf(Long.MAX_VALUE, Long.MIN_VALUE, 0))
assertEquals(Double.NEGATIVE_INFINITY, minOf(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY))
assertEquals(Double.MIN_VALUE, minOf(Double.POSITIVE_INFINITY, Double.MAX_VALUE, Double.MIN_VALUE))
}
@@ -0,0 +1,19 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
val v3 = Item("apple", 20)
fun box() {
assertEquals(v2, minOf(v1, v2, compareBy { it.name }))
assertEquals(v3, minOf(v3, v2, v1, compareBy { it.name }))
assertEquals(v1, minOf(v1, v2, compareBy { it.rating }))
assertEquals(v1, minOf(v1, v2, v3, compareBy { it.rating }))
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val v1 = "a"
val v2 = "beta"
assertTrue(naturalOrder<String>().compare(v1, v2) < 0)
assertTrue(reverseOrder<String>().compare(v1, v2) > 0)
assertTrue(reverseOrder<Int>() === naturalOrder<Int>().reversed())
assertTrue(naturalOrder<Int>() === reverseOrder<Int>().reversed())
}
@@ -0,0 +1,18 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item> { it.name }
val reversed = comparator.reversed()
assertEquals(comparator.compare(v2, v1), reversed.compare(v1, v2))
assertEquals(comparator, reversed.reversed())
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item> { it.rating }.thenBy { it.name }
val diff = comparator.compare(v1, v2)
assertTrue(diff < 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v1, items[0])
assertEquals(v2, items[1])
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item> { it.rating }.thenByDescending { it.name }
val diff = comparator.compare(v1, v2)
assertTrue(diff < 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v1, items[0])
assertEquals(v2, items[1])
}
@@ -0,0 +1,21 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = Comparator<Item> { a, b -> a.name.compareTo(b.name) }.thenComparator { a, b -> a.rating.compareTo(b.rating) }
val diff = comparator.compare(v1, v2)
assertTrue(diff > 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v2, items[0])
assertEquals(v1, items[1])
}
@@ -0,0 +1,28 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = object : Comparator<Item> {
override fun compare(o1: Item, o2: Item): Int {
return compareValuesBy(o1, o2, { it.name }, { it.rating })
}
override fun equals(other: Any?): Boolean {
return this == other
}
}
val diff = comparator.compare(v1, v2)
assertTrue(diff > 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v2, items[0])
assertEquals(v1, items[1])
}
@@ -0,0 +1,20 @@
import kotlin.test.*
import kotlin.comparisons.*
data class Item(val name: String, val rating: Int) : Comparable<Item> {
public override fun compareTo(other: Item): Int {
return compareValuesBy(this, other, { it.rating }, { it.name })
}
}
val v1 = Item("wine", 9)
val v2 = Item("beer", 10)
fun box() {
val comparator = compareBy<Item>({ it.name }, { it.rating })
val diff = comparator.compare(v1, v2)
assertTrue(diff > 0)
val items = arrayListOf(v1, v2).sortedWith(comparator)
assertEquals(v2, items[0])
assertEquals(v1, items[1])
}