Skip to content

Commit

Permalink
Added some tests for JS implementation of HashMap
Browse files Browse the repository at this point in the history
Fixed JS implementation of HashMap#putAll
  • Loading branch information
bashor committed Mar 12, 2013
1 parent 7f583cc commit 322d35a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
4 changes: 2 additions & 2 deletions js/js.translator/testFiles/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@
};

this.each = function (callback) {
var entries = that.entries(), i = entries.length, entry;
var entries = that._entries(), i = entries.length, entry;
while (i--) {
entry = entries[i];
callback(entry[0], entry[1]);
Expand All @@ -348,7 +348,7 @@


this.putAll = function (hashtable, conflictCallback) {
var entries = hashtable.entries();
var entries = hashtable._entries();
var entry, key, value, thisValue, i = entries.length;
var hasConflictCallback = (typeof conflictCallback == FUNCTION);
while (i--) {
Expand Down
37 changes: 34 additions & 3 deletions libraries/stdlib/test/js/MapJsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import java.util.*
import org.junit.Test as test

class MapJsTest {
val KEYS = array("zero", "one", "two", "three")
val VALUES = array(0, 1, 2, 3)
//TODO: replace `array(...).toList()` to `listOf(...)`
val KEYS = array("zero", "one", "two", "three").toList()
val VALUES = array(0, 1, 2, 3).toList()

test fun getOrElse() {
val data = HashMap<String, Int>()
Expand Down Expand Up @@ -44,9 +45,39 @@ class MapJsTest {

test fun hashMapValues() {
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> {
val map = HashMap<String, Int>()
for (i in KEYS.indices) {
Expand Down

0 comments on commit 322d35a

Please sign in to comment.