Skip to content

Commit 9ca25ce

Browse files
committedSep 22, 2022
Native: add more tests for Swift Set and Dictionary used in Kotlin
(cherry picked from commit 38f2b20)
1 parent 1244679 commit 9ca25ce

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed
 

‎kotlin-native/backend.native/tests/framework/stdlib/stdlib.kt

+66-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
package stdlib
99

10+
import kotlin.test.*
11+
1012
fun <K, V> isEmpty(map: Map<K, V>) = map.isEmpty()
1113

1214
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) {
6163
}
6264
}
6365

64-
fun gc() = kotlin.native.internal.GC.collect()
66+
fun gc() = kotlin.native.internal.GC.collect()
67+
68+
// Note: this method checks only some of the operations (namely the ones modified recently,
69+
// and thus required additional tests).
70+
// More tests are absolutely needed.
71+
@Throws(Throwable::class)
72+
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
73+
fun testSet(set: Set<String>) {
74+
set as kotlin.native.internal.KonanSet<String> // Smart cast to access getElement below.
75+
val setAny: Set<Any?> = set
76+
77+
assertTrue(set.contains("a"))
78+
assertTrue(set.contains("c"))
79+
assertFalse(set.contains("h"))
80+
assertFalse(setAny.contains(1))
81+
82+
83+
assertEquals("a", set.getElement("a"))
84+
assertNull(set.getElement("aa"))
85+
assertNull((setAny as kotlin.native.internal.KonanSet<Any?>).getElement(1))
86+
}
87+
88+
// Note: this method checks only some of the operations (namely the ones modified recently,
89+
// and thus required additional tests).
90+
// More tests are absolutely needed.
91+
@Throws(Throwable::class)
92+
fun testMap(map: Map<String, Int>) {
93+
val mapAny: Map<String, Any?> = map
94+
val mapKeysAny: Set<Any?> = map.keys
95+
val mapEntriesAny: Set<Map.Entry<Any?, Any?>> = map.entries
96+
97+
assertTrue(map.containsKey("a"))
98+
assertTrue(map.keys.contains("b"))
99+
assertTrue(map.containsKey("g"))
100+
assertFalse(map.containsKey("0"))
101+
assertFalse(mapKeysAny.contains(1))
102+
103+
assertTrue(map.containsValue(1))
104+
assertTrue(map.values.contains(2))
105+
assertTrue(map.containsValue(7))
106+
assertFalse(map.containsValue(8))
107+
assertFalse(mapAny.containsValue("8"))
108+
109+
assertEquals(2, map.get("b"))
110+
assertEquals(4, map.get("d"))
111+
assertNull(map.get("h"))
112+
113+
val referenceMap = (0 until 7).map { ('a' + it).toString() to (it + 1) }.toMap()
114+
assertEquals(referenceMap.hashCode(), map.hashCode())
115+
assertEquals(referenceMap, map)
116+
assertEquals(map, referenceMap)
117+
118+
assertEquals(28, map.entries.sumBy { it.value })
119+
120+
assertTrue(map.entries.contains(createMapEntry("e", 5)))
121+
assertTrue(map.entries.contains(createMapEntry("g", 7)))
122+
assertFalse(map.entries.contains(createMapEntry("e", 7)))
123+
assertFalse(map.entries.contains(createMapEntry("e", 10)))
124+
assertFalse(map.entries.contains(createMapEntry("10", 5)))
125+
assertFalse(map.entries.contains(createMapEntry("10", 10)))
126+
assertFalse(mapEntriesAny.contains(createMapEntry(5, "e")))
127+
}
128+
129+
private fun <K, V> createMapEntry(key: K, value: V) = mapOf(key to value).entries.single()

‎kotlin-native/backend.native/tests/framework/stdlib/stdlib.swift

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class StdlibTests : TestProvider {
5050
TestCase(name: "TestMutableMap", method: withAutorelease(testMutableMap)),
5151
TestCase(name: "TestKotlinMutableSetInit", method: withAutorelease(testKotlinMutableSetInit)),
5252
TestCase(name: "TestKotlinMutableDictionaryInit", method: withAutorelease(testKotlinMutableDictionaryInit)),
53+
TestCase(name: "TestSwiftSetInKotlin", method: withAutorelease(testSwiftSetInKotlin)),
54+
TestCase(name: "TestSwiftDictionaryInKotlin", method: withAutorelease(testSwiftDictionaryInKotlin)),
5355
]
5456
providers.append(self)
5557
}
@@ -459,4 +461,12 @@ class StdlibTests : TestProvider {
459461
StdlibKt.gc() // To reproduce https://github.com/JetBrains/kotlin-native/issues/3259
460462
}
461463

464+
func testSwiftSetInKotlin() throws {
465+
try StdlibKt.testSet(set: ["a", "b", "c", "d", "e", "f", "g"])
466+
}
467+
468+
func testSwiftDictionaryInKotlin() throws {
469+
try StdlibKt.testMap(map: ["a" : 1, "b" : 2, "c" : 3, "d" : 4, "e" : 5, "f" : 6, "g" : 7])
470+
}
471+
462472
}

0 commit comments

Comments
 (0)
Please sign in to comment.