Added some tests for JS implementation of HashMap

Fixed JS implementation of HashMap#putAll
This commit is contained in:
Zalim Bashorov
2013-03-11 15:46:51 +04:00
parent 7f583cc798
commit 322d35ac2f
2 changed files with 36 additions and 5 deletions
+2 -2
View File
@@ -339,7 +339,7 @@
}; };
this.each = function (callback) { this.each = function (callback) {
var entries = that.entries(), i = entries.length, entry; var entries = that._entries(), i = entries.length, entry;
while (i--) { while (i--) {
entry = entries[i]; entry = entries[i];
callback(entry[0], entry[1]); callback(entry[0], entry[1]);
@@ -348,7 +348,7 @@
this.putAll = function (hashtable, conflictCallback) { this.putAll = 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);
while (i--) { while (i--) {
+34 -3
View File
@@ -6,8 +6,9 @@ import java.util.*
import org.junit.Test as test import org.junit.Test as test
class MapJsTest { class MapJsTest {
val KEYS = array("zero", "one", "two", "three") //TODO: replace `array(...).toList()` to `listOf(...)`
val VALUES = array(0, 1, 2, 3) val KEYS = array("zero", "one", "two", "three").toList()
val VALUES = array(0, 1, 2, 3).toList()
test fun getOrElse() { test fun getOrElse() {
val data = HashMap<String, Int>() val data = HashMap<String, Int>()
@@ -44,9 +45,39 @@ class MapJsTest {
test fun hashMapValues() { test fun hashMapValues() {
val map = createTestHashMap() val map = createTestHashMap()
assertEquals(VALUES.toList(), map.values().toSortedList()) assertEquals(VALUES, map.values().toSortedList())
} }
test fun hashMapKeySet() {
val map = createTestHashMap()
assertEquals(KEYS.toSortedList(), map.keySet().toSortedList())
}
test fun hashMapContainsValue() {
val map = createTestHashMap()
assertTrue(map.containsValue(VALUES[0]) &&
map.containsValue(VALUES[1]) &&
map.containsValue(VALUES[2]) &&
map.containsValue(VALUES[3]))
assertFalse(map.containsValue("four") ||
map.containsValue("five"))
}
test fun hashMapSize() {
val map = createTestHashMap()
assertEquals(KEYS.size, map.size)
}
test fun hashMapPutAll() {
val map = createTestHashMap()
val newMap = HashMap<String, Int>()
newMap.putAll(map)
assertEquals(KEYS.size, newMap.size)
}
fun createTestHashMap(): HashMap<String, Int> { fun createTestHashMap(): HashMap<String, Int> {
val map = HashMap<String, Int>() val map = HashMap<String, Int>()
for (i in KEYS.indices) { for (i in KEYS.indices) {