JS stdlib: added missed tests for Map and fix HashMap implementation.

This commit is contained in:
Zalim Bashorov
2014-01-21 15:36:31 +04:00
parent 15a727c6ed
commit ceab4347ec
3 changed files with 125 additions and 40 deletions
@@ -81,7 +81,7 @@ public final class TopLevelFIF extends CompositeFIF {
@NotNull @NotNull
@Override @Override
protected String operation() { protected String operation() {
return "get"; return "get_s9cetl$";
} }
@Nullable @Nullable
+8 -8
View File
@@ -234,7 +234,7 @@
return oldValue; return oldValue;
}; };
this.get = function (key) { this.get_s9cetl$ = function (key) {
checkKey(key); checkKey(key);
var hash = hashingFunction(key); var hash = hashingFunction(key);
@@ -252,7 +252,7 @@
return null; return null;
}; };
this.containsKey = function (key) { this.containsKey_s9cetl$ = function (key) {
checkKey(key); checkKey(key);
var bucketKey = hashingFunction(key); var bucketKey = hashingFunction(key);
@@ -262,7 +262,7 @@
return bucket ? bucket.containsKey_s9cetl$(key) : false; return bucket ? bucket.containsKey_s9cetl$(key) : false;
}; };
this.containsValue = function (value) { this.containsValue_s9cetl$ = function (value) {
checkValue(value); checkValue(value);
var i = buckets.length; var i = buckets.length;
while (i--) { while (i--) {
@@ -306,7 +306,7 @@
return result; return result;
}; };
this.remove = function (key) { this.remove_s9cetl$ = function (key) {
checkKey(key); checkKey(key);
var hash = hashingFunction(key), bucketIndex, oldValue = null; var hash = hashingFunction(key), bucketIndex, oldValue = null;
@@ -347,7 +347,7 @@
}; };
this.putAll = function (hashtable, conflictCallback) { this.putAll_s9c68p$ = function (hashtable, conflictCallback) {
var entries = hashtable._entries(); var entries = hashtable._entries();
var entry, key, value, thisValue, i = entries.length; var entry, key, value, thisValue, i = entries.length;
var hasConflictCallback = (typeof conflictCallback == FUNCTION); var hasConflictCallback = (typeof conflictCallback == FUNCTION);
@@ -450,7 +450,7 @@ Kotlin.ComplexHashMap = Kotlin.HashMap;
return false; return false;
}, },
get: function (key) { get_s9cetl$: function (key) {
return this.map[key]; return this.map[key];
}, },
put_5yfy9u$: function (key, value) { put_5yfy9u$: function (key, value) {
@@ -461,7 +461,7 @@ Kotlin.ComplexHashMap = Kotlin.HashMap;
} }
return prevValue; return prevValue;
}, },
remove: function (key) { remove_s9cetl$: function (key) {
var prevValue = this.map[key]; var prevValue = this.map[key];
if (prevValue !== undefined) { if (prevValue !== undefined) {
delete this.map[key]; delete this.map[key];
@@ -566,7 +566,7 @@ Kotlin.PrimitiveHashSet = Kotlin.createClassNow(Kotlin.AbstractCollection,
}; };
this.remove = function (o) { this.remove = function (o) {
return hashTable.remove(o) ? o : null; return hashTable.remove_s9cetl$(o) ? o : null;
}; };
this.contains = function (o) { this.contains = function (o) {
+112 -27
View File
@@ -12,7 +12,7 @@ class MapJsTest {
val VALUES = array(0, 1, 2, 3).toList() val VALUES = array(0, 1, 2, 3).toList()
test fun getOrElse() { test fun getOrElse() {
val data = HashMap<String, Int>() val data = emptyMap()
val a = data.getOrElse("foo"){2} val a = data.getOrElse("foo"){2}
assertEquals(2, a) assertEquals(2, a)
@@ -22,7 +22,7 @@ class MapJsTest {
} }
test fun getOrPut() { test fun getOrPut() {
val data = HashMap<String, Int>() val data = emptyMutableMap()
val a = data.getOrPut("foo"){2} val a = data.getOrPut("foo"){2}
assertEquals(2, a) assertEquals(2, a)
@@ -32,30 +32,90 @@ class MapJsTest {
assertEquals(1, data.size()) assertEquals(1, data.size())
} }
test fun emptyMapGet() {
val map = emptyMap()
assertEquals(null, map.get("foo"), """failed on map.get("foo")""")
assertEquals(null, map["bar"], """failed on map["bar"]""")
}
test fun mapGet() {
val map = createTestMap()
for (i in KEYS.indices) {
assertEquals(VALUES[i], map.get(KEYS[i]), """failed on map.get(KEYS[$i])""")
assertEquals(VALUES[i], map[KEYS[i]], """failed on map[KEYS[$i]]""")
}
assertEquals(null, map.get("foo"))
}
/* TODO: fix after switch to use compiled stdlib (need drop js.Map<K,V>.set(V))
test fun mapPut() {
val map = emptyMutableMap()
map.put("foo", 1)
assertEquals(1, map["foo"])
assertEquals(null, map["bar"])
map["bar"] = 2
assertEquals(1, map["foo"])
assertEquals(2, map["bar"])
map["foo"] = 0
assertEquals(0, map["foo"])
assertEquals(2, map["bar"])
}
*/
test fun sizeAndEmptyForEmptyMap() {
val data = emptyMap()
assertTrue(data.isEmpty())
assertTrue(data.empty)
assertEquals(0, data.size())
assertEquals(0, data.size)
}
test fun sizeAndEmpty() { test fun sizeAndEmpty() {
val data = HashMap<String, Int>() val data = createTestMap()
assertTrue{ data.empty }
assertEquals(data.size, 0) assertFalse(data.isEmpty())
assertFalse(data.empty)
assertEquals(KEYS.size, data.size())
assertEquals(KEYS.size, data.size)
} }
// #KT-3035 // #KT-3035
test fun emptyHashMapValues() { test fun emptyMapValues() {
val emptyMap = HashMap<String, Int>() val emptyMap = emptyMap()
assertTrue(emptyMap.values().isEmpty()) assertTrue(emptyMap.values().isEmpty())
} }
test fun hashMapValues() { test fun mapValues() {
val map = createTestHashMap() val map = createTestMap()
assertEquals(VALUES, map.values().toSortedList()) assertEquals(VALUES, map.values().toSortedList())
} }
test fun hashMapKeySet() { test fun mapKeySet() {
val map = createTestHashMap() val map = createTestMap()
assertEquals(KEYS.toSortedList(), map.keySet().toSortedList()) assertEquals(KEYS.toSortedList(), map.keySet().toSortedList())
} }
test fun hashMapContainsValue() { test fun mapContainsKey() {
val map = createTestHashMap() val map = createTestMap()
assertTrue(map.containsKey(KEYS[0]) &&
map.containsKey(KEYS[1]) &&
map.containsKey(KEYS[2]) &&
map.containsKey(KEYS[3]))
assertFalse(map.containsKey("foo") ||
map.containsKey(1))
}
test fun mapContainsValue() {
val map = createTestMap()
assertTrue(map.containsValue(VALUES[0]) && assertTrue(map.containsValue(VALUES[0]) &&
map.containsValue(VALUES[1]) && map.containsValue(VALUES[1]) &&
@@ -63,28 +123,38 @@ class MapJsTest {
map.containsValue(VALUES[3])) map.containsValue(VALUES[3]))
assertFalse(map.containsValue("four") || assertFalse(map.containsValue("four") ||
map.containsValue("five")) map.containsValue(5))
} }
test fun hashMapSize() { test fun mapPutAll() {
val map = createTestHashMap() val map = createTestMap()
assertEquals(KEYS.size, map.size) val newMap = emptyMutableMap()
}
test fun hashMapPutAll() {
val map = createTestHashMap()
val newMap = HashMap<String, Int>()
newMap.putAll(map) newMap.putAll(map)
assertEquals(KEYS.size, newMap.size) assertEquals(KEYS.size, newMap.size)
} }
test fun mapRemove() {
val map = createTestMutableMap()
val last = KEYS.size() - 1
val first = 0
val mid = KEYS.size() / 2
fun createTestHashMap(): HashMap<String, Int> { assertEquals(KEYS.size(), map.size())
val map = HashMap<String, Int>()
for (i in KEYS.indices) { assertEquals(null, map.remove("foo"))
map.put(KEYS[i], VALUES[i]) assertEquals(VALUES[mid], map.remove(KEYS[mid]))
assertEquals(null, map.remove(KEYS[mid]))
assertEquals(VALUES[last], map.remove(KEYS[last]))
assertEquals(VALUES[first], map.remove(KEYS[first]))
assertEquals(KEYS.size() - 3, map.size())
} }
return map
test fun mapClear() {
val map = createTestMutableMap()
assertFalse(map.isEmpty())
map.clear()
assertTrue(map.isEmpty())
} }
/* /*
@@ -232,4 +302,19 @@ class MapJsTest {
} }
*/ */
// Helpers
fun emptyMap(): Map<String, Int> = HashMap<String, Int>()
fun emptyMutableMap(): MutableMap<String, Int> = HashMap<String, Int>()
fun createTestMap(): Map<String, Int> = createTestHashMap()
fun createTestMutableMap(): MutableMap<String, Int> = createTestHashMap()
fun createTestHashMap(): HashMap<String, Int> {
val map = HashMap<String, Int>()
for (i in KEYS.indices) {
map.put(KEYS[i], VALUES[i])
}
return map
}
} }