Skip to content

Commit

Permalink
Native: add more tests for Swift Set and Dictionary used in Kotlin
Browse files Browse the repository at this point in the history
(cherry picked from commit 38f2b20)
  • Loading branch information
SvyatoslavScherbina committed Sep 22, 2022
1 parent 1244679 commit 9ca25ce
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
67 changes: 66 additions & 1 deletion kotlin-native/backend.native/tests/framework/stdlib/stdlib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
10 changes: 10 additions & 0 deletions kotlin-native/backend.native/tests/framework/stdlib/stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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])
}

}

0 comments on commit 9ca25ce

Please sign in to comment.