Debian Codegraph @

topojson/test/topology/hashset-test.js

raw
1 var tape = require("tape"),
2 hashset = require("../../lib/topojson/topology/hashset");
3
4 tape("hashset can add an object", function(test) {
5 var map = hashset(10, hash, equals),
6 key = {hash: 1};
7 test.equal(map.add(key), true);
8 test.equal(map.has(key), true);
9 test.end();
10 });
11
12 tape("hashset has returns false when no key is found", function(test) {
13 var map = hashset(10, hash, equals),
14 key = {hash: 1};
15 test.equal(map.has(key), false);
16 test.end();
17 });
18
19 tape("hashset when a hash collision occurs, get checks that the keys are equal", function(test) {
20 var map = hashset(10, hash, equals),
21 keyA = {hash: 1},
22 keyB = {hash: 1},
23 keyC = {hash: 1};
24 test.equal(map.add(keyA), true);
25 test.equal(map.add(keyB), true);
26 test.equal(map.has(keyA), true);
27 test.equal(map.has(keyB), true);
28 test.equal(map.has(keyC), false);
29 test.end();
30 });
31
32 tape("hashset add returns true", function(test) {
33 var map = hashset(10, hash, equals),
34 key = {hash: 1};
35 test.equal(map.add(key), true);
36 test.end();
37 });
38
39 tape("hashset add throws an error when full", function(test) {
40 var map = hashset(0, hash, equals), // minimum size of 16
41 keys = [];
42 for (var i = 0; i < 16; ++i) map.add(keys[i] = {hash: i});
43 for (var i = 0; i < 16; ++i) map.add(keys[i]); // replacing is okay
44 test.throws(function() { map.add({hash: 16}); }, /Error/);
45 test.end();
46 });
47
48 tape("hashset the hash function must return a nonnegative integer, but can be greater than size", function(test) {
49 var map = hashset(10, hash, equals),
50 key = {hash: 11};
51 test.equal(map.has(key), false);
52 test.equal(map.add(key), true);
53 test.equal(map.has(key), true);
54 test.end();
55 });
56
57 function hash(o) {
58 return o.hash;
59 }
60
61 function equals(a, b) {
62 return a === b;
63 }