More standard library operations. (#124)

This commit is contained in:
Nikolay Igotti
2016-12-08 15:44:22 +03:00
committed by GitHub
parent 3bb946e144
commit c9f2e1ca22
11 changed files with 921 additions and 25 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
// TODO: remove kotlin_native.io once overrides are in place.
fun main(args : Array<String>) {
print(readLine())
print(readLine().toString())
}
@@ -13,12 +13,16 @@ fun assertEquals(value1: Any?, value2: Any?) {
println("FAIL")
}
fun assertNotEquals(value1: Any?, value2: Any?) {
if (value1 == value2)
println("FAIL")
}
fun assertEquals(value1: Int, value2: Int) {
if (value1 != value2)
println("FAIL")
}
fun testBasic() {
val m = HashMap<String, String>()
assertTrue(m.isEmpty())
@@ -133,8 +137,6 @@ fun testClear() {
}
}
}
// 'to' not yet working.
/*
fun testEquals() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
@@ -146,12 +148,121 @@ fun testEquals() {
assertEquals(m.keys, expected.keys)
assertEquals(m.values, expected.values)
assertEquals(m.entries, expected.entries)
}
fun testHashCode() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertEquals(expected.hashCode(), m.hashCode())
assertEquals(expected.entries.hashCode(), m.entries.hashCode())
assertEquals(expected.keys.hashCode(), m.keys.hashCode())
assertEquals(listOf("1", "2", "3").hashCode(), m.values.hashCode())
}
fun testToString() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertEquals(expected.toString(), m.toString())
assertEquals(expected.entries.toString(), m.entries.toString())
assertEquals(expected.keys.toString(), m.keys.toString())
assertEquals(expected.values.toString(), m.values.toString())
}
fun testPutEntry() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
val e = expected.entries.iterator().next() as MutableMap.MutableEntry<String, String>
assertTrue(m.entries.contains(e))
assertTrue(m.entries.remove(e))
assertTrue(mapOf("b" to "2", "c" to "3") == m)
assertTrue(m.entries.add(e))
assertTrue(expected == m)
assertFalse(m.entries.add(e))
assertTrue(expected == m)
}
/* Fails due to variance.
fun testRemoveAllEntries() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.entries.removeAll(mapOf("a" to "2", "b" to "3", "c" to "4").entries))
assertEquals(expected, m)
assertTrue(m.entries.removeAll(mapOf("b" to "22", "c" to "3", "d" to "4").entries))
assertNotEquals(expected, m)
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
fun testRetainAllEntries() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.entries.retainAll(expected.entries))
assertEquals(expected, m)
assertTrue(m.entries.retainAll(mapOf("b" to "22", "c" to "3", "d" to "4").entries))
assertEquals(mapOf("c" to "3"), m)
} */
fun testContainsAllValues() {
val m = HashMap(mapOf("a" to "1", "b" to "2", "c" to "3"))
assertTrue(m.values.containsAll(listOf("1", "2")))
assertTrue(m.values.containsAll(listOf("1", "2", "3")))
assertFalse(m.values.containsAll(listOf("1", "2", "3", "4")))
assertFalse(m.values.containsAll(listOf("2", "3", "4")))
}
fun testRemoveValue() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.remove("b"))
assertEquals(expected, m)
assertTrue(m.values.remove("2"))
assertEquals(mapOf("a" to "1", "c" to "3"), m)
}
fun testRemoveAllValues() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.removeAll(listOf("b", "c")))
assertEquals(expected, m)
assertTrue(m.values.removeAll(listOf("b", "3")))
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
fun testRetainAllValues() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
assertFalse(m.values.retainAll(listOf("1", "2", "3")))
assertEquals(expected, m)
assertTrue(m.values.retainAll(listOf("1", "2", "c")))
assertEquals(mapOf("a" to "1", "b" to "2"), m)
}
fun testEntriesIteratorSet() {
val expected = mapOf("a" to "1", "b" to "2", "c" to "3")
val m = HashMap(expected)
val it = m.iterator()
while (it.hasNext()) {
val entry = it.next()
entry.setValue(entry.value + "!")
}
assertNotEquals(expected, m)
assertEquals(mapOf("a" to "1!", "b" to "2!", "c" to "3!"), m)
}
fun main(args : Array<String>) {
testBasic()
testRehashAndCompact()
testClear()
//testEquals()
testEquals()
testHashCode()
testToString()
testPutEntry()
//testRemoveAllEntries()
//testRetainAllEntries()
testContainsAllValues()
testRemoveValue()
testRemoveAllValues()
testRetainAllValues()
testEntriesIteratorSet()
//testDegenerateKeys()
println("OK")
}