From 38f2b20e1a5196031a6faed4fddd59dcda6f915d Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Wed, 21 Sep 2022 18:00:47 +0200 Subject: [PATCH] Native: add more tests for Swift Set and Dictionary used in Kotlin --- .../tests/framework/stdlib/stdlib.kt | 67 ++++++++++++++++++- .../tests/framework/stdlib/stdlib.swift | 10 +++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/kotlin-native/backend.native/tests/framework/stdlib/stdlib.kt b/kotlin-native/backend.native/tests/framework/stdlib/stdlib.kt index 65d0d297f99..e2f810ea61f 100644 --- a/kotlin-native/backend.native/tests/framework/stdlib/stdlib.kt +++ b/kotlin-native/backend.native/tests/framework/stdlib/stdlib.kt @@ -7,6 +7,8 @@ package stdlib +import kotlin.test.* + fun isEmpty(map: Map) = map.isEmpty() fun getKeysAsSet(map: Map) = map.keys @@ -61,4 +63,67 @@ data class TripleVars(var first: T, var second: T, var third: T) { } } -fun gc() = kotlin.native.internal.GC.collect() \ No newline at end of file +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) { + set as kotlin.native.internal.KonanSet // Smart cast to access getElement below. + val setAny: Set = 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).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) { + val mapAny: Map = map + val mapKeysAny: Set = map.keys + val mapEntriesAny: Set> = 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 createMapEntry(key: K, value: V) = mapOf(key to value).entries.single() diff --git a/kotlin-native/backend.native/tests/framework/stdlib/stdlib.swift b/kotlin-native/backend.native/tests/framework/stdlib/stdlib.swift index 298048677cd..1fc490b0baa 100644 --- a/kotlin-native/backend.native/tests/framework/stdlib/stdlib.swift +++ b/kotlin-native/backend.native/tests/framework/stdlib/stdlib.swift @@ -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]) + } + }