Compare commits

...

1 Commits

Author SHA1 Message Date
Ilya Gorbunov
e891725b9a Fix unordered set/map behavior expectations 2020-04-20 21:36:21 +03:00
6 changed files with 49 additions and 33 deletions

View File

@@ -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 @@ class AbstractCollectionsTest {
assertTrue("ok" in set)
compare(set.toSet(), set) {
setBehavior()
setBehavior(ordered = true)
}
}
@@ -72,7 +72,7 @@ class AbstractCollectionsTest {
assertEquals(listOf(42), map.values.toList())
compare(map.toMap(), map) {
mapBehavior()
mapBehavior(ordered = true)
}
}
@@ -142,7 +142,7 @@ class AbstractCollectionsTest {
assertEquals(setOf("element", "test"), set)
compare(set.storage, set) {
setBehavior()
setBehavior(ordered = true)
}
}
@@ -161,7 +161,7 @@ class AbstractCollectionsTest {
assertTrue(map.containsValue('f'.toInt()))
compare(map.storage, map) {
mapBehavior()
mapBehavior(ordered = true)
}
}
}

View File

@@ -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 @@ public fun <T> CompareContext<Iterator<T>>.iteratorBehavior() {
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 @@ 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({ 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() }
}
}

View File

@@ -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 @@ class CollectionTest {
}
@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() {

View File

@@ -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 @@ class PrimitiveMapJsTest : MapJsTest() {
@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() {

View File

@@ -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 @@ class PrimitiveSetJsTest : SetJsTest() {
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) }
}
}

View File

@@ -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) }
}
}
}