-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.js
139 lines (113 loc) · 2.04 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const test = require('brittle')
const FIFO = require('./')
test('basic', function (t) {
const q = new FIFO()
const values = [
1,
4,
4,
0,
null,
{},
Math.random(),
'',
'hello',
9,
1,
4,
5,
6,
7,
null,
null,
0,
0,
15,
52.2,
null
]
t.is(q.shift(), undefined)
t.ok(q.isEmpty())
t.is(q.length, 0)
for (const value of values) q.push(value)
while (!q.isEmpty()) {
t.is(q.shift(), values.shift())
t.is(q.length, values.length)
}
t.is(q.shift(), undefined)
t.ok(q.isEmpty())
})
test('long length', function (t) {
const q = new FIFO()
const len = 0x8f7
for (let i = 0; i < len; i++) q.push(i)
t.is(q.length, len)
let shifts = 0
while (!q.isEmpty()) {
q.shift()
shifts++
}
t.is(shifts, len)
t.is(q.length, 0)
})
test('clear', function (t) {
const q = new FIFO()
q.push('a')
q.push('a')
q.clear()
t.is(q.shift(), undefined)
t.is(q.length, 0)
for (let i = 0; i < 50; i++) {
q.push('a')
}
q.clear()
t.is(q.shift(), undefined)
t.is(q.length, 0)
})
test('basic length', function (t) {
const q = new FIFO()
q.push('a')
t.is(q.length, 1)
q.push('a')
t.is(q.length, 2)
q.shift()
t.is(q.length, 1)
q.shift()
t.is(q.length, 0)
q.shift()
t.is(q.length, 0)
})
test('peek', function (t) {
const q = new FIFO()
q.push('a')
t.is(q.length, 1)
t.is(q.peek(), 'a')
t.is(q.peek(), 'a')
q.push('b')
t.is(q.length, 2)
t.is(q.peek(), 'a')
t.is(q.peek(), 'a')
t.is(q.shift(), 'a')
t.is(q.peek(), 'b')
t.is(q.peek(), 'b')
t.is(q.shift(), 'b')
t.is(q.peek(), undefined)
t.is(q.peek(), undefined)
})
test('peek edgecase', function (t) {
const q = new FIFO(4)
q.push('a')
q.push('b')
q.push('c')
q.push('d')
q.push('e')
t.is(q.peek(), q.shift())
t.is(q.peek(), q.shift())
t.is(q.peek(), q.shift())
t.is(q.peek(), q.shift())
t.is(q.peek(), q.shift())
t.is(q.peek(), q.shift())
})
test('invalid hwm', function (t) {
t.exception(() => new FIFO(3))
})