/* * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license * that can be found in the LICENSE file. */ @file:Suppress("UNUSED") @file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class) package stdlib import kotlin.test.* fun isEmpty(map: Map) = map.isEmpty() fun getKeysAsSet(map: Map) = map.keys fun getKeysAsList(map: Map) = map.keys.toList() fun toMutableMap(map: HashMap) = map.toMutableMap() fun getFirstElement(collection: Collection) = collection.first() class GenericExtensionClass> (private val holder: T?) { fun getFirstKey(): K? = holder?.entries?.first()?.key fun getFirstValue() : V? { holder?.entries?.forEach { e -> println("KEY: ${e.key} VALUE: ${e.value}") } return holder?.entries?.first()?.value } } fun createPair(): Pair, GenericExtensionClass>> { val l = createLinkedMap() val g = GenericExtensionClass(l) return Pair(l, g) } fun createLinkedMap() = linkedMapOf() fun createTypedMutableMap() = linkedMapOf() fun addSomeElementsToMap(map: MutableMap) { map.put(key = "XYZ", value = 321) map.put(key = "TMP", value = 451) } fun list(vararg elements: Any?): Any = listOf(*elements) fun set(vararg elements: Any?): Any = setOf(*elements) fun map(vararg keysAndValues: Any?): Any = mutableMapOf().apply { (0 until keysAndValues.size step 2).forEach {index -> this[keysAndValues[index]] = keysAndValues[index + 1] } } fun emptyMutableList(): Any = mutableListOf() fun emptyMutableSet(): Any = mutableSetOf() fun emptyMutableMap(): Any = mutableMapOf() data class TripleVals(val first: T, val second: T, val third: T) data class TripleVars(var first: T, var second: T, var third: T) { override fun toString(): String { return "[$first, $second, $third]" } } fun gc() = kotlin.native.runtime.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) { val setAny: Set = set assertTrue(set.contains("a")) assertTrue(set.contains("c")) assertFalse(set.contains("h")) assertFalse(setAny.contains(1)) val konanSet = set as kotlin.native.internal.KonanSet assertEquals("a", konanSet.getElement("a")) assertNull(konanSet.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()