| 1 |
var tape = require("tape"), |
| 2 |
hashmap = require("../../lib/topojson/topology/hashmap"); |
| 3 |
|
| 4 |
tape("hashmap can get an object by key", function(test) { |
| 5 |
var map = hashmap(10, hash, equals), |
| 6 |
key = {hash: 1}; |
| 7 |
map.set(key, 42); |
| 8 |
test.equal(map.get(key), 42); |
| 9 |
test.end(); |
| 10 |
}); |
| 11 |
|
| 12 |
tape("hashmap get returns undefined when no key is found", function(test) { |
| 13 |
var map = hashmap(10, hash, equals), |
| 14 |
key = {hash: 1}; |
| 15 |
test.equal(map.get(key), undefined); |
| 16 |
test.end(); |
| 17 |
}); |
| 18 |
|
| 19 |
tape("hashmap get returns the missing value when no key is found", function(test) { |
| 20 |
var map = hashmap(10, hash, equals), |
| 21 |
key = {hash: 1}; |
| 22 |
test.equal(map.get(key, 42), 42); |
| 23 |
test.end(); |
| 24 |
}); |
| 25 |
|
| 26 |
tape("hashmap when a hash collision occurs, get checks that the keys are equal", function(test) { |
| 27 |
var map = hashmap(10, hash, equals), |
| 28 |
keyA = {hash: 1}, |
| 29 |
keyB = {hash: 1}, |
| 30 |
keyC = {hash: 1}; |
| 31 |
map.set(keyA, "A"); |
| 32 |
map.set(keyB, "B"); |
| 33 |
test.equal(map.get(keyA), "A"); |
| 34 |
test.equal(map.get(keyB), "B"); |
| 35 |
test.equal(map.get(keyC), undefined); |
| 36 |
test.end(); |
| 37 |
}); |
| 38 |
|
| 39 |
tape("hashmap can set an object by key", function(test) { |
| 40 |
var map = hashmap(10, hash, equals), |
| 41 |
key = {hash: 1}; |
| 42 |
map.set(key, 42); |
| 43 |
test.equal(map.get(key), 42); |
| 44 |
test.end(); |
| 45 |
}); |
| 46 |
|
| 47 |
tape("hashmap can set an object by key if not already set", function(test) { |
| 48 |
var map = hashmap(10, hash, equals), |
| 49 |
key = {hash: 1}; |
| 50 |
test.equal(map.maybeSet(key, 42), 42); |
| 51 |
test.equal(map.get(key), 42); |
| 52 |
test.equal(map.maybeSet(key, 43), 42); |
| 53 |
test.equal(map.get(key), 42); |
| 54 |
test.end(); |
| 55 |
}); |
| 56 |
|
| 57 |
tape("hashmap set returns the set value", function(test) { |
| 58 |
var map = hashmap(10, hash, equals), |
| 59 |
key = {hash: 1}; |
| 60 |
test.equal(map.set(key, 42), 42); |
| 61 |
test.end(); |
| 62 |
}); |
| 63 |
|
| 64 |
tape("hashmap set throws an error when full", function(test) { |
| 65 |
var map = hashmap(0, hash, equals), // minimum size of 16 |
| 66 |
keys = []; |
| 67 |
for (var i = 0; i < 16; ++i) map.set(keys[i] = {hash: i}, true); |
| 68 |
for (var i = 0; i < 16; ++i) map.set(keys[i], true); // replacing is okay |
| 69 |
test.throws(function() { map.set({hash: 16}, true); }); |
| 70 |
test.end(); |
| 71 |
}); |
| 72 |
|
| 73 |
tape("hashmap when a hash collision occurs, set checks that the keys are equal", function(test) { |
| 74 |
var map = hashmap(10, hash, equals), |
| 75 |
keyA = {hash: 1}, |
| 76 |
keyB = {hash: 1}, |
| 77 |
keyC = {hash: 1}; |
| 78 |
test.equal(map.set(keyA, "A"), "A"); |
| 79 |
test.equal(map.set(keyB, "B"), "B"); |
| 80 |
test.equal(map.get(keyA), "A"); |
| 81 |
test.equal(map.get(keyB), "B"); |
| 82 |
test.equal(map.get(keyC), undefined); |
| 83 |
test.end(); |
| 84 |
}); |
| 85 |
|
| 86 |
tape("hashmap the hash function must return a nonnegative integer, but can be greater than size", function(test) { |
| 87 |
var map = hashmap(10, hash, equals), |
| 88 |
key = {hash: 11}; |
| 89 |
test.equal(map.get(key), undefined); |
| 90 |
test.equal(map.set(key, 42), 42); |
| 91 |
test.equal(map.get(key), 42); |
| 92 |
test.end(); |
| 93 |
}); |
| 94 |
|
| 95 |
|
| 96 |
function hash(o) { |
| 97 |
return o.hash; |
| 98 |
} |
| 99 |
|
| 100 |
function equals(a, b) { |
| 101 |
return a === b; |
| 102 |
} |