-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
124 lines (93 loc) · 2.12 KB
/
test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import test from 'ava';
var nanoq = require('./nanoq');
var data=[], w=10000;
while(--w) data.push((1000 * Math.random())>>0);
test('Typed Array 8bit', t => {
let q = new nanoq(255);
t.is(Object.prototype.toString.call(q.tree), '[object Uint8Array]');
});
test('Typed Array 16bit', t => {
let q = new nanoq(256);
t.is(Object.prototype.toString.call(q.tree), '[object Uint16Array]');
});
test('Typed Array 32bit', t => {
let q = new nanoq(65536);
t.is(Object.prototype.toString.call(q.tree), '[object Uint32Array]');
});
test('Js Array', t => {
let q = new nanoq();
t.is(Object.prototype.toString.call(q.tree), '[object Array]');
t.pass();
});
test('Sorted default comparator', t => {
let q = new nanoq(); // nanoq is a minheap by default
for (let i=0; i<data.length; i++) q.push(data[i]);
var last = -1;
for (let i=0; i<data.length; i++)
{
let p = q.pop();
t.true(p >= last);
last = p;
}
t.pass();
});
test('Sorted minheap comparator', t => {
let q = new nanoq(0, (a, b) => a > b);
for (let i=0; i<data.length; i++) q.push(data[i]);
var last = -1;
for (let i=0; i<data.length; i++)
{
let p = q.pop();
t.true(p >= last);
last = p;
}
t.pass();
});
test('Sorted maxheap comparator', t => {
let q = new nanoq(0, (a, b) => a < b);
for (let i=0; i<data.length; i++) q.push(data[i]);
var last = Number.MAX_VALUE;
for (let i=0; i<data.length; i++)
{
let p = q.pop();
t.true(p <= last);
last = p;
}
t.pass();
});
test('Sorted strings', t => {
let q = new nanoq();
q.push("dog");
q.push("cat");
q.push("more cats");
q.push("yet more cats");
t.is(q.pop(), "cat");
t.is(q.pop(), "dog");
t.is(q.pop(), "more cats");
t.is(q.pop(), "yet more cats");
t.pass();
});
test('`pop()` underflow', t => {
let q = new nanoq();
q.push(1);
q.push(2);
q.push(3);
q.pop();
q.pop();
q.pop();
t.is(q.pop(), null);
});
test('peek', t => {
let q = new nanoq();
q.push(100);
q.push(101);
q.push(1);
t.is(q.peek(), 1);
});
test('length', t => {
let q = new nanoq();
let num = 500 + ((Math.random() * 500)>>0);
for (let i=0; i<num; i++)
q.push((Math.random()* 1000)>>0);
t.is(q.length(), num);
});