forked from monmohan/dsjslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestTST.js
82 lines (74 loc) · 3.01 KB
/
TestTST.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var tstrie = require('../lib/TernarySearchTrie.js'), assert = require('assert'), util = require('util');
(function () {
function testSearchAndInsert() {
var tst = new tstrie();
tst.put("abc", 20).put("z", 30).put("ab", {x:"someObj"}).put("averylongkey", "somevalue");
assert.strictEqual(tst.get("abc"), 20)
assert.strictEqual(tst.get("z"), 30)
assert.deepEqual(tst.get("ab"), {x:"someObj"})
assert.deepEqual(tst.get("averylongkey"), "somevalue")
}
function testMulti() {
var tst = new tstrie();
var para = "This example shows us the power of closures clearly. As you can see, we store i at the outer scope of the object"
" we actually operate on. opening "
"It seems kind of trivial but it is one of those features of trade "
"JavaScript it really pays off to understand as it allows us to implement all sorts of"
" nifty nice little patterns such as this quite easily.";
var keys = para.split(/\s/);
var keyValSet = {}, val;
keys.forEach(function (key) {
val = key '-value';
tst.put(key, val);
keyValSet[key] = val;
})
keys.forEach(function (key) {
assert.deepEqual(tst.get(key), keyValSet[key]);
})
function testDelete() {
var i = 0, deleted = [];
while (i < keys.length) {
console.log('--Deleting-- ' i)
var nkey = keys.shift();
keyValSet[nkey] = null;
tst.delete(nkey);
deleted.push(nkey);
deleted.forEach(function (dKey) {
assert.deepEqual(tst.get(dKey), null);
});
keys.forEach(function (key) {
assert.deepEqual(tst.get(key), keyValSet[key]);
})
i ;
}
}
function testEntrySet() {
var entries=tst.entrySet();
entries.forEach(function (entry) {
var k=entry.key;var v=entry.value;
assert.deepEqual(keyValSet[k],v);
})
}
function testPrefix(){
var prefix=tst.keysWithPrefix('a');
assert.deepEqual(prefix,[ { key: 'at', value: 'at-value' },
{ key: 'actually', value: 'actually-value' },
{ key: 'as', value: 'as-value' },
{ key: 'all', value: 'all-value' },
{ key: 'allows', value: 'allows-value' } ]
)
}
function testNegative(){
assert.doesNotThrow(function(){tst.keysWithPrefix('ox')})
assert.doesNotThrow(function(){tst.keysWithPrefix('a')})
assert.deepEqual(tst.keysWithPrefix('ox'),[])
assert.deepEqual(tst.keysWithPrefix('operate'),[{key:'operate','value':keyValSet['operate']}])
}
testEntrySet()
testPrefix()
testNegative()
testDelete()
}
testSearchAndInsert()
testMulti()
})()