Native: add more tests for Swift Set and Dictionary used in Kotlin

This commit is contained in:
Svyatoslav Scherbina
2022-09-21 18:00:47 +02:00
committed by Space
parent 39c73e25a4
commit 38f2b20e1a
2 changed files with 76 additions and 1 deletions
@@ -7,6 +7,8 @@
package stdlib
import kotlin.test.*
fun <K, V> isEmpty(map: Map<K, V>) = map.isEmpty()
fun <K, V> getKeysAsSet(map: Map<K, V>) = map.keys
@@ -61,4 +63,67 @@ data class TripleVars<T>(var first: T, var second: T, var third: T) {
}
}
fun gc() = kotlin.native.internal.GC.collect()
fun gc() = kotlin.native.internal.GC.collect()
// Note: this method checks only some of the operations (namely the ones modified recently,
// and thus required additional tests).
// More tests are absolutely needed.
@Throws(Throwable::class)
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
fun testSet(set: Set<String>) {
set as kotlin.native.internal.KonanSet<String> // Smart cast to access getElement below.
val setAny: Set<Any?> = set
assertTrue(set.contains("a"))
assertTrue(set.contains("c"))
assertFalse(set.contains("h"))
assertFalse(setAny.contains(1))
assertEquals("a", set.getElement("a"))
assertNull(set.getElement("aa"))
assertNull((setAny as kotlin.native.internal.KonanSet<Any?>).getElement(1))
}
// Note: this method checks only some of the operations (namely the ones modified recently,
// and thus required additional tests).
// More tests are absolutely needed.
@Throws(Throwable::class)
fun testMap(map: Map<String, Int>) {
val mapAny: Map<String, Any?> = map
val mapKeysAny: Set<Any?> = map.keys
val mapEntriesAny: Set<Map.Entry<Any?, Any?>> = map.entries
assertTrue(map.containsKey("a"))
assertTrue(map.keys.contains("b"))
assertTrue(map.containsKey("g"))
assertFalse(map.containsKey("0"))
assertFalse(mapKeysAny.contains(1))
assertTrue(map.containsValue(1))
assertTrue(map.values.contains(2))
assertTrue(map.containsValue(7))
assertFalse(map.containsValue(8))
assertFalse(mapAny.containsValue("8"))
assertEquals(2, map.get("b"))
assertEquals(4, map.get("d"))
assertNull(map.get("h"))
val referenceMap = (0 until 7).map { ('a' + it).toString() to (it + 1) }.toMap()
assertEquals(referenceMap.hashCode(), map.hashCode())
assertEquals(referenceMap, map)
assertEquals(map, referenceMap)
assertEquals(28, map.entries.sumBy { it.value })
assertTrue(map.entries.contains(createMapEntry("e", 5)))
assertTrue(map.entries.contains(createMapEntry("g", 7)))
assertFalse(map.entries.contains(createMapEntry("e", 7)))
assertFalse(map.entries.contains(createMapEntry("e", 10)))
assertFalse(map.entries.contains(createMapEntry("10", 5)))
assertFalse(map.entries.contains(createMapEntry("10", 10)))
assertFalse(mapEntriesAny.contains(createMapEntry(5, "e")))
}
private fun <K, V> createMapEntry(key: K, value: V) = mapOf(key to value).entries.single()
@@ -50,6 +50,8 @@ class StdlibTests : TestProvider {
TestCase(name: "TestMutableMap", method: withAutorelease(testMutableMap)),
TestCase(name: "TestKotlinMutableSetInit", method: withAutorelease(testKotlinMutableSetInit)),
TestCase(name: "TestKotlinMutableDictionaryInit", method: withAutorelease(testKotlinMutableDictionaryInit)),
TestCase(name: "TestSwiftSetInKotlin", method: withAutorelease(testSwiftSetInKotlin)),
TestCase(name: "TestSwiftDictionaryInKotlin", method: withAutorelease(testSwiftDictionaryInKotlin)),
]
providers.append(self)
}
@@ -459,4 +461,12 @@ class StdlibTests : TestProvider {
StdlibKt.gc() // To reproduce https://github.com/JetBrains/kotlin-native/issues/3259
}
func testSwiftSetInKotlin() throws {
try StdlibKt.testSet(set: ["a", "b", "c", "d", "e", "f", "g"])
}
func testSwiftDictionaryInKotlin() throws {
try StdlibKt.testMap(map: ["a" : 1, "b" : 2, "c" : 3, "d" : 4, "e" : 5, "f" : 6, "g" : 7])
}
}