Reformat stdlib tests and samples
#KT-5558
This commit is contained in:
@@ -10,8 +10,8 @@ import test.collections.CompareContext
|
||||
public fun <T> CompareContext<List<T>>.listBehavior() {
|
||||
equalityBehavior()
|
||||
collectionBehavior()
|
||||
compareProperty( { listIterator() }, { listIteratorBehavior() })
|
||||
compareProperty( { listIterator(0) }, { listIteratorBehavior() })
|
||||
compareProperty({ listIterator() }, { listIteratorBehavior() })
|
||||
compareProperty({ listIterator(0) }, { listIteratorBehavior() })
|
||||
|
||||
propertyFails { listIterator(-1) }
|
||||
propertyFails { listIterator(size + 1) }
|
||||
@@ -24,8 +24,8 @@ public fun <T> CompareContext<List<T>>.listBehavior() {
|
||||
propertyEquals { indexOf(elementAtOrNull(0)) }
|
||||
propertyEquals { lastIndexOf(elementAtOrNull(0)) }
|
||||
|
||||
propertyFails { subList(0, size + 1)}
|
||||
propertyFails { subList(-1, 0)}
|
||||
propertyFails { subList(0, size + 1) }
|
||||
propertyFails { subList(-1, 0) }
|
||||
propertyEquals { subList(0, size) }
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ public fun <K, V> CompareContext<Map<K, V>>.mapBehavior() {
|
||||
propertyEquals { size }
|
||||
propertyEquals { isEmpty() }
|
||||
|
||||
(object {}).let { propertyEquals { containsKey(it as Any?) } }
|
||||
(object {}).let { propertyEquals { containsKey(it as Any?) } }
|
||||
|
||||
if (expected.isEmpty().not())
|
||||
propertyEquals { contains(keys.first()) }
|
||||
@@ -82,13 +82,13 @@ public fun <K, V> CompareContext<Map<K, V>>.mapBehavior() {
|
||||
propertyEquals { containsValue(values.firstOrNull()) }
|
||||
propertyEquals { get(null as Any?) }
|
||||
|
||||
compareProperty( { keys }, { setBehavior("keySet") } )
|
||||
compareProperty( { entries }, { setBehavior("entrySet") } )
|
||||
compareProperty( { values }, { collectionBehavior("values") })
|
||||
compareProperty({ keys }, { setBehavior("keySet") })
|
||||
compareProperty({ entries }, { setBehavior("entrySet") })
|
||||
compareProperty({ values }, { collectionBehavior("values") })
|
||||
}
|
||||
|
||||
public fun <T> CompareContext<T>.equalityBehavior(objectName: String = "") {
|
||||
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
|
||||
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
|
||||
equals(objectName)
|
||||
propertyEquals(prefix + "hashCode") { hashCode() }
|
||||
propertyEquals(prefix + "toString") { toString() }
|
||||
@@ -96,11 +96,11 @@ public fun <T> CompareContext<T>.equalityBehavior(objectName: String = "") {
|
||||
|
||||
|
||||
public fun <T> CompareContext<Collection<T>>.collectionBehavior(objectName: String = "") {
|
||||
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
|
||||
propertyEquals (prefix + "size") { size }
|
||||
propertyEquals (prefix + "isEmpty") { isEmpty() }
|
||||
val prefix = objectName + if (objectName.isNotEmpty()) "." else ""
|
||||
propertyEquals(prefix + "size") { size }
|
||||
propertyEquals(prefix + "isEmpty") { isEmpty() }
|
||||
|
||||
(object {}).let { propertyEquals { contains(it as Any?) } }
|
||||
(object {}).let { propertyEquals { contains(it as Any?) } }
|
||||
propertyEquals { contains(firstOrNull()) }
|
||||
propertyEquals { containsAll(this) }
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import test.*
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.test.*
|
||||
|
||||
public fun <T> compare(expected: T, actual: T, block:CompareContext<T>.() -> Unit) {
|
||||
public fun <T> compare(expected: T, actual: T, block: CompareContext<T>.() -> Unit) {
|
||||
CompareContext(expected, actual).block()
|
||||
}
|
||||
|
||||
@@ -26,7 +26,11 @@ public class CompareContext<out T>(public val expected: T, public val actual: T)
|
||||
public fun <P> propertyEquals(message: String = "", getter: T.() -> P) {
|
||||
assertEquals(expected.getter(), actual.getter(), message)
|
||||
}
|
||||
public fun propertyFails(getter: T.() -> Unit) { assertFailEquals({expected.getter()}, {actual.getter()}) }
|
||||
|
||||
public fun propertyFails(getter: T.() -> Unit) {
|
||||
assertFailEquals({ expected.getter() }, { actual.getter() })
|
||||
}
|
||||
|
||||
public fun <P> compareProperty(getter: T.() -> P, block: CompareContext<P>.() -> Unit) {
|
||||
compare(expected.getter(), actual.getter(), block)
|
||||
}
|
||||
|
||||
@@ -58,16 +58,16 @@ class GroupingTest {
|
||||
|
||||
val elements = listOf("foo", "bar", "flea", "zoo", "biscuit")
|
||||
val result = elements.groupingBy { it.first() }
|
||||
.fold({ k, _ -> Collector<Char, String>(k)}, { _, acc, e -> acc.accumulateIfEven(e) })
|
||||
.fold({ k, _ -> Collector<Char, String>(k) }, { _, acc, e -> acc.accumulateIfEven(e) })
|
||||
|
||||
val ordered = result.values.sortedBy { it.key }.map { it.toPair() }
|
||||
assertEquals(listOf('b' to emptyList(), 'f' to listOf("flea"), 'z' to emptyList()), ordered)
|
||||
|
||||
val moreElements = listOf("fire", "zero")
|
||||
val result2 = moreElements.groupingBy { it.first() }
|
||||
.foldTo(HashMap(result),
|
||||
{ k, _ -> error("should not be called for $k") },
|
||||
{ _, acc, e -> acc.accumulateIfEven(e) })
|
||||
.foldTo(HashMap(result),
|
||||
{ k, _ -> error("should not be called for $k") },
|
||||
{ _, acc, e -> acc.accumulateIfEven(e) })
|
||||
|
||||
val ordered2 = result2.values.sortedBy { it.key }.map { it.toPair() }
|
||||
assertEquals(listOf('b' to emptyList(), 'f' to listOf("flea", "fire"), 'z' to listOf("zero")), ordered2)
|
||||
@@ -101,7 +101,7 @@ class GroupingTest {
|
||||
assertEquals(mapOf('f' to 2, 'b' to 3, 'a' to 1, 'z' to 2), counts2)
|
||||
}
|
||||
|
||||
/**
|
||||
/*
|
||||
@Test fun sumEach() {
|
||||
val values = listOf("k" to 50, "b" to 20, "k" to 1000 )
|
||||
val summary = values.groupingBy { it.first }.eachSumOf { it.second }
|
||||
|
||||
@@ -14,7 +14,7 @@ fun <T> Iterable<T>.toIterable(): Iterable<T> = Iterable { this.iterator() }
|
||||
class IterableTest : OrderedIterableTests<Iterable<String>>({ iterableOf(*it) }, iterableOf<String>())
|
||||
class SetTest : IterableTests<Set<String>>({ setOf(*it) }, setOf())
|
||||
class LinkedSetTest : OrderedIterableTests<LinkedHashSet<String>>({ linkedSetOf(*it) }, linkedSetOf())
|
||||
class ListTest : OrderedIterableTests<List<String>>( { listOf(*it) }, listOf<String>())
|
||||
class ListTest : OrderedIterableTests<List<String>>({ listOf(*it) }, listOf<String>())
|
||||
class ArrayListTest : OrderedIterableTests<ArrayList<String>>({ arrayListOf(*it) }, arrayListOf<String>())
|
||||
|
||||
abstract class OrderedIterableTests<T : Iterable<String>>(createFrom: (Array<out String>) -> T, empty: T) : IterableTests<T>(createFrom, empty) {
|
||||
@@ -56,10 +56,10 @@ abstract class OrderedIterableTests<T : Iterable<String>>(createFrom: (Array<out
|
||||
assertFails { data.elementAt(-1) }
|
||||
assertFails { empty.elementAt(0) }
|
||||
|
||||
expect("foo") { data.elementAtOrElse(0, {""} )}
|
||||
expect("zoo") { data.elementAtOrElse(-1, { "zoo" })}
|
||||
expect("zoo") { data.elementAtOrElse(2, { "zoo" })}
|
||||
expect("zoo") { empty.elementAtOrElse(0) { "zoo" }}
|
||||
expect("foo") { data.elementAtOrElse(0, { "" }) }
|
||||
expect("zoo") { data.elementAtOrElse(-1, { "zoo" }) }
|
||||
expect("zoo") { data.elementAtOrElse(2, { "zoo" }) }
|
||||
expect("zoo") { empty.elementAtOrElse(0) { "zoo" } }
|
||||
|
||||
expect(null) { empty.elementAtOrNull(0) }
|
||||
|
||||
@@ -239,7 +239,8 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
|
||||
assertEquals(listOf("foo"), foo)
|
||||
}
|
||||
|
||||
@Test fun filterIndexed() {
|
||||
@Test
|
||||
fun filterIndexed() {
|
||||
val result = data.filterIndexed { index, value -> value.first() == ('a' + index) }
|
||||
assertEquals(listOf("bar"), result)
|
||||
}
|
||||
@@ -286,7 +287,7 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
|
||||
assertTrue(data === newData)
|
||||
|
||||
// static types test
|
||||
assertStaticTypeIs<ArrayList<Int>>(arrayListOf(1, 2, 3).onEach { })
|
||||
assertStaticTypeIs<ArrayList<Int>>(arrayListOf(1, 2, 3).onEach { })
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -490,7 +491,10 @@ abstract class IterableTests<T : Iterable<String>>(val createFrom: (Array<out St
|
||||
}
|
||||
|
||||
|
||||
fun <T> Iterable<T>.assertSorted(isInOrder: (T, T) -> Boolean): Unit { this.iterator().assertSorted(isInOrder) }
|
||||
fun <T> Iterable<T>.assertSorted(isInOrder: (T, T) -> Boolean) {
|
||||
this.iterator().assertSorted(isInOrder)
|
||||
}
|
||||
|
||||
fun <T> Iterator<T>.assertSorted(isInOrder: (T, T) -> Boolean) {
|
||||
if (!hasNext()) return
|
||||
var index = 0
|
||||
@@ -498,7 +502,7 @@ fun <T> Iterator<T>.assertSorted(isInOrder: (T, T) -> Boolean) {
|
||||
while (hasNext()) {
|
||||
index += 1
|
||||
val next = next()
|
||||
assertTrue(isInOrder(prev, next), "Not in order at position $index, element[${index-1}]: $prev, element[$index]: $next")
|
||||
assertTrue(isInOrder(prev, next), "Not in order at position $index, element[${index - 1}]: $prev, element[$index]: $next")
|
||||
prev = next
|
||||
}
|
||||
return
|
||||
|
||||
@@ -8,7 +8,8 @@ package test.collections
|
||||
import kotlin.test.*
|
||||
|
||||
class IteratorsTest {
|
||||
@Test fun iterationOverIterator() {
|
||||
@Test
|
||||
fun iterationOverIterator() {
|
||||
val c = listOf(0, 1, 2, 3, 4, 5)
|
||||
var s = ""
|
||||
for (i in c.iterator()) {
|
||||
|
||||
@@ -82,7 +82,7 @@ class ListBinarySearchTest {
|
||||
val list = values.map { IncomparableDataItem(IncomparableDataItem(it)) }
|
||||
|
||||
list.forEachIndexed { index, item ->
|
||||
assertEquals(index, list.binarySearch { comparator.compare(it.value, item.value) } )
|
||||
assertEquals(index, list.binarySearch { comparator.compare(it.value, item.value) })
|
||||
assertEquals(notFound(index), list.binarySearch { comparator.compare(it.value, item.value.pred()) })
|
||||
assertEquals(notFound(index + 1), list.binarySearch { comparator.compare(it.value, item.value.succ()) })
|
||||
|
||||
@@ -102,7 +102,7 @@ class ListBinarySearchTest {
|
||||
val list = values.flatMap { v1 -> values.map { v2 -> Pair(v1, v2) } }
|
||||
|
||||
list.forEachIndexed { index, item ->
|
||||
assertEquals(index, list.binarySearch { compareValuesBy(it, item, { it.first }, { it.second }) })
|
||||
assertEquals(index, list.binarySearch { compareValuesBy(it, item, { it.first }, { it.second }) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,10 +43,10 @@ class ListSpecificTest {
|
||||
assertFails { data.get(-1) }
|
||||
assertFails { empty.get(0) }
|
||||
|
||||
expect("foo") { data.getOrElse(0, {""} )}
|
||||
expect("zoo") { data.getOrElse(-1, { "zoo" })}
|
||||
expect("zoo") { data.getOrElse(2, { "zoo" })}
|
||||
expect("zoo") { empty.getOrElse(0) { "zoo" }}
|
||||
expect("foo") { data.getOrElse(0, { "" }) }
|
||||
expect("zoo") { data.getOrElse(-1, { "zoo" }) }
|
||||
expect("zoo") { data.getOrElse(2, { "zoo" }) }
|
||||
expect("zoo") { empty.getOrElse(0) { "zoo" } }
|
||||
|
||||
expect(null) { empty.getOrNull(0) }
|
||||
|
||||
|
||||
@@ -9,15 +9,15 @@ import kotlin.test.*
|
||||
|
||||
|
||||
class MutableCollectionTest {
|
||||
fun <T, C: MutableCollection<T>> testOperation(before: List<T>, after: List<T>, expectedModified: Boolean, toMutableCollection: (List<T>) -> C)
|
||||
= fun(operation: (C.() -> Boolean)) {
|
||||
val list = toMutableCollection(before)
|
||||
assertEquals(expectedModified, list.operation())
|
||||
assertEquals(toMutableCollection(after), list)
|
||||
}
|
||||
fun <T, C : MutableCollection<T>> testOperation(before: List<T>, after: List<T>, expectedModified: Boolean, toMutableCollection: (List<T>) -> C) =
|
||||
fun(operation: (C.() -> Boolean)) {
|
||||
val list = toMutableCollection(before)
|
||||
assertEquals(expectedModified, list.operation())
|
||||
assertEquals(toMutableCollection(after), list)
|
||||
}
|
||||
|
||||
fun <T> testOperation(before: List<T>, after: List<T>, expectedModified: Boolean)
|
||||
= testOperation(before, after, expectedModified, { it.toMutableList() })
|
||||
fun <T> testOperation(before: List<T>, after: List<T>, expectedModified: Boolean) =
|
||||
testOperation(before, after, expectedModified, { it.toMutableList() })
|
||||
|
||||
|
||||
@Test fun addAll() {
|
||||
|
||||
@@ -90,10 +90,12 @@ public class SequenceTest {
|
||||
|
||||
@Test fun mapIndexedNotNull() {
|
||||
// find which terms are divisible by their index
|
||||
assertEquals(listOf("1/1", "5/5", "144/12", "46368/24", "75025/25"),
|
||||
fibonacci().mapIndexedNotNull { index, value ->
|
||||
if (index > 0 && (value % index) == 0) "$value/$index" else null
|
||||
}.take(5).toList())
|
||||
assertEquals(
|
||||
listOf("1/1", "5/5", "144/12", "46368/24", "75025/25"),
|
||||
fibonacci().mapIndexedNotNull { index, value ->
|
||||
if (index > 0 && (value % index) == 0) "$value/$index" else null
|
||||
}.take(5).toList()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -457,14 +459,14 @@ public class SequenceTest {
|
||||
|
||||
@Test fun flatMapAndTakeExtractTheTransformedElements() {
|
||||
val expected = listOf(
|
||||
'3', // fibonacci(4) = 3
|
||||
'5', // fibonacci(5) = 5
|
||||
'8', // fibonacci(6) = 8
|
||||
'1', '3', // fibonacci(7) = 13
|
||||
'2', '1', // fibonacci(8) = 21
|
||||
'3', '4', // fibonacci(9) = 34
|
||||
'5' // fibonacci(10) = 55
|
||||
)
|
||||
'3', // fibonacci(4) = 3
|
||||
'5', // fibonacci(5) = 5
|
||||
'8', // fibonacci(6) = 8
|
||||
'1', '3', // fibonacci(7) = 13
|
||||
'2', '1', // fibonacci(8) = 21
|
||||
'3', '4', // fibonacci(9) = 34
|
||||
'5' // fibonacci(10) = 55
|
||||
)
|
||||
|
||||
assertEquals(expected, fibonacci().drop(4).flatMap { it.toString().asSequence() }.take(10).toList())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user