Fix unordered set/map behavior expectations
diff --git a/libraries/stdlib/test/collections/AbstractCollectionsTest.kt b/libraries/stdlib/test/collections/AbstractCollectionsTest.kt index d0e0e02..cda1ab2 100644 --- a/libraries/stdlib/test/collections/AbstractCollectionsTest.kt +++ b/libraries/stdlib/test/collections/AbstractCollectionsTest.kt
@@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -48,7 +48,7 @@ assertTrue("ok" in set) compare(set.toSet(), set) { - setBehavior() + setBehavior(ordered = true) } } @@ -72,7 +72,7 @@ assertEquals(listOf(42), map.values.toList()) compare(map.toMap(), map) { - mapBehavior() + mapBehavior(ordered = true) } } @@ -142,7 +142,7 @@ assertEquals(setOf("element", "test"), set) compare(set.storage, set) { - setBehavior() + setBehavior(ordered = true) } } @@ -161,7 +161,7 @@ assertTrue(map.containsValue('f'.toInt())) compare(map.storage, map) { - mapBehavior() + mapBehavior(ordered = true) } } } \ No newline at end of file
diff --git a/libraries/stdlib/test/collections/CollectionBehaviors.kt b/libraries/stdlib/test/collections/CollectionBehaviors.kt index 087a011..3d7839a 100644 --- a/libraries/stdlib/test/collections/CollectionBehaviors.kt +++ b/libraries/stdlib/test/collections/CollectionBehaviors.kt
@@ -1,14 +1,15 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ package test.collections.behaviors import test.collections.CompareContext +import kotlin.test.assertEquals public fun <T> CompareContext<List<T>>.listBehavior() { - equalityBehavior() + equalityBehavior(withToString = true) collectionBehavior() compareProperty({ listIterator() }, { listIteratorBehavior() }) compareProperty({ listIterator(0) }, { listIteratorBehavior() }) @@ -62,14 +63,27 @@ propertyFails { next() } } -public fun <T> CompareContext<Set<T>>.setBehavior(objectName: String = "") { - equalityBehavior(objectName) - collectionBehavior(objectName) - compareProperty({ iterator() }, { iteratorBehavior() }) +public fun <T> CompareContext<Iterator<T>>.unorderedIteratorBehavior() { + propertyEquals { hasNext() } + val expectedValues = mutableListOf<T>() + val actualValues = mutableListOf<T>() + while (expected.hasNext()) { + expectedValues.add(expected.next()) + actualValues.add(actual.next()) + propertyEquals { hasNext() } + } + propertyFails { next() } + assertEquals(expectedValues.groupingBy { it }.eachCount(), actualValues.groupingBy { it }.eachCount()) } -public fun <K, V> CompareContext<Map<K, V>>.mapBehavior() { - equalityBehavior() +public fun <T> CompareContext<Set<T>>.setBehavior(objectName: String = "", ordered: Boolean = false) { + equalityBehavior(objectName, withToString = ordered) + collectionBehavior(objectName) + compareProperty({ iterator() }, { if (ordered) iteratorBehavior() else unorderedIteratorBehavior() }) +} + +public fun <K, V> CompareContext<Map<K, V>>.mapBehavior(ordered: Boolean = false) { + equalityBehavior(withToString = ordered) propertyEquals { size } propertyEquals { isEmpty() } @@ -82,16 +96,18 @@ propertyEquals { containsValue(values.firstOrNull()) } propertyEquals { get(null as Any?) } - compareProperty({ keys }, { setBehavior("keySet") }) - compareProperty({ entries }, { setBehavior("entrySet") }) + compareProperty({ keys }, { setBehavior("keySet", ordered) }) + compareProperty({ entries }, { setBehavior("entrySet", ordered) }) compareProperty({ values }, { collectionBehavior("values") }) } -public fun <T> CompareContext<T>.equalityBehavior(objectName: String = "") { +public fun <T> CompareContext<T>.equalityBehavior(objectName: String = "", withToString: Boolean = true) { val prefix = objectName + if (objectName.isNotEmpty()) "." else "" equals(objectName) propertyEquals(prefix + "hashCode") { hashCode() } - propertyEquals(prefix + "toString") { toString() } + if (withToString) { + propertyEquals(prefix + "toString") { toString() } + } }
diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index 74cf9ff..e840f26 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt
@@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -1076,15 +1076,15 @@ } @Test fun specialSets() { - compare(linkedSetOf<Int>(), setOf<Int>()) { setBehavior() } - compare(hashSetOf<Double>(), emptySet<Double>()) { setBehavior() } - compare(listOf("value").toMutableSet(), setOf("value")) { setBehavior() } + compare(linkedSetOf<Int>(), setOf<Int>()) { setBehavior(ordered = true) } + compare(hashSetOf<Double>(), emptySet<Double>()) { setBehavior(ordered = true) } + compare(listOf("value").toMutableSet(), setOf("value")) { setBehavior(ordered = true) } } @Test fun specialMaps() { - compare(hashMapOf<String, Int>(), mapOf<String, Int>()) { mapBehavior() } - compare(linkedMapOf<Int, String>(), emptyMap<Int, String>()) { mapBehavior() } - compare(linkedMapOf(2 to 3), mapOf(2 to 3)) { mapBehavior() } + compare(hashMapOf<String, Int>(), mapOf<String, Int>()) { mapBehavior(ordered = true) } + compare(linkedMapOf<Int, String>(), emptyMap<Int, String>()) { mapBehavior(ordered = true) } + compare(linkedMapOf(2 to 3), mapOf(2 to 3)) { mapBehavior(ordered = true) } } @Test fun toStringTest() {
diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index 609d051..695d6ce 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt
@@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -52,11 +52,11 @@ @Test fun compareBehavior() { val specialJsStringMap = stringMapOf<Any>() specialJsStringMap.put("k1", "v1") - compare(genericHashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior() } + compare(genericHashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior(ordered = false) } val specialJsNumberMap = HashMap<Int, Any>(4) specialJsNumberMap.put(5, "v5") - compare(genericHashMapOf(5 to "v5"), specialJsNumberMap) { mapBehavior() } + compare(genericHashMapOf(5 to "v5"), specialJsNumberMap) { mapBehavior(ordered = false) } } @Test fun putNull() {
diff --git a/libraries/stdlib/test/js/SetJsTest.kt b/libraries/stdlib/test/js/SetJsTest.kt index a03f2ef..8774a21 100644 --- a/libraries/stdlib/test/js/SetJsTest.kt +++ b/libraries/stdlib/test/js/SetJsTest.kt
@@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -53,11 +53,11 @@ fun compareBehavior() { val specialJsStringSet = HashSet<String>() specialJsStringSet.add("kotlin") - compare(genericHashSetOf("kotlin"), specialJsStringSet) { setBehavior() } + compare(genericHashSetOf("kotlin"), specialJsStringSet) { setBehavior(ordered = false) } val specialJsNumberSet = HashSet<Double>() specialJsNumberSet.add(3.14) - compare(genericHashSetOf(3.14), specialJsNumberSet) { setBehavior() } + compare(genericHashSetOf(3.14), specialJsNumberSet) { setBehavior(ordered = false) } } }
diff --git a/libraries/stdlib/test/text/StringTest.kt b/libraries/stdlib/test/text/StringTest.kt index 4afbaf1..f3e945b 100644 --- a/libraries/stdlib/test/text/StringTest.kt +++ b/libraries/stdlib/test/text/StringTest.kt
@@ -1,5 +1,5 @@ /* - * Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. */ @@ -1655,7 +1655,7 @@ @Test fun toHashSet() { - compare(hashSetOf('A', 'B', 'C'), "ACAABBAC".toHashSet()) { setBehavior() } + compare(hashSetOf('A', 'B', 'C'), "ACAABBAC".toHashSet()) { setBehavior(ordered = false) } buildString { repeat(100) { append('1') } @@ -1664,7 +1664,7 @@ append('4') repeat(100) { append('5') } }.let { - compare(hashSetOf('1', '2', '3', '4', '5'), it.toHashSet()) { setBehavior() } + compare(hashSetOf('1', '2', '3', '4', '5'), it.toHashSet()) { setBehavior(ordered = false) } } } }