-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
250 lines (225 loc) · 6.64 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const should = require('should')
const Queue = require('./index')
describe('Test LoopQueue constructor', function () {
it('constructs without args', () => {
const queue = new Queue()
should(queue.length).equal(0)
should(queue._hash).be.Function()
should(queue._loops).be.false()
should(queue._capacity).be.false()
})
it('constructs with (hash)', () => {
const noop = () => {}
const queue = new Queue(noop)
should(queue._hash).equal(noop)
should(queue._loops).be.false()
should(queue._capacity).be.false()
})
it('constructs with (loops)', () => {
const queue = new Queue(1)
should(queue._hash).be.Function()
should(queue._loops).equal(1)
should(queue._capacity).be.false()
})
it('constructs with (loops, hash)', () => {
const noop = () => {}
const queue = new Queue(1, noop)
should(queue._hash).equal(noop)
should(queue._loops).equal(1)
should(queue._capacity).be.false()
})
it('constructs with (loops, capacity)', () => {
const queue = new Queue(1, 2)
should(queue._hash).be.Function()
should(queue._loops).equal(1)
should(queue._capacity).equal(2)
})
it('constructs with (loops, capacity, hash)', () => {
const noop = () => {}
const queue = new Queue(1, 2, noop)
should(queue._hash).equal(noop)
should(queue._loops).equal(1)
should(queue._capacity).equal(2)
})
it('constructs with (0, 0, hash)', () => {
const noop = () => {}
const queue = new Queue(0, 0, noop)
should(queue._hash).equal(noop)
should(queue._loops).be.false()
should(queue._capacity).be.false()
})
})
describe('Test LoopQueue.next', () => {
it('loop in the insertion order', () => {
const queue = new Queue()
queue.push(3)
queue.push(1)
queue.push(2)
should(queue.next()).equal(3)
should(queue.next()).equal(1)
should(queue.next()).equal(2)
should(queue.next()).equal(3)
should(queue.next()).equal(1)
should(queue.next()).equal(2)
should(queue.next()).equal(3)
})
it('loop continue even when next is changed', () => {
const queue = new Queue()
queue.push(3)
queue.push(1)
queue.push(2)
should(queue.next()).equal(3)
should(queue.remove(1)).be.true()
should(queue.next()).equal(2)
should(queue.next()).equal(3)
should(queue.next()).equal(2)
should(queue.push(4)).equal(4)
should(queue.next()).equal(4)
should(queue.next()).equal(3)
})
it('loop then predicate to remove', () => {
const queue = new Queue()
const predicate = item => item === 2
queue.push(1)
queue.push(2)
queue.push(3)
should(queue.next(predicate)).equal(1)
should(queue.length).equal(3)
should(queue.next(predicate)).equal(2)
should(queue.length).equal(2)
should(queue.next(true)).equal(3)
should(queue.length).equal(1)
should(queue.next(false)).equal(1)
should(queue.length).equal(1)
should(queue.next()).equal(1)
should(queue.length).equal(1)
should(queue.next(true)).equal(1)
should(queue.length).equal(0)
should(queue.next()).be.undefined()
})
it('loop then remove if reach loops limit', () => {
const queue = new Queue(2)
queue.push(1)
queue.push(2)
should(queue.next()).equal(1)
should(queue.length).equal(2)
should(queue.next()).equal(2)
should(queue.length).equal(2)
should(queue.next()).equal(1)
should(queue.length).equal(1)
should(queue.next()).equal(2)
should(queue.length).equal(0)
should(queue.next()).be.undefined()
})
})
describe('Test LoopQueue .push .remove .clear', () => {
it('push return object reference hash', () => {
const queue = new Queue()
const obj = {}
should(queue.push(0)).be.exactly(0)
should(queue.push(null)).be.null()
should(queue.push(obj)).equal(obj)
should(queue.push(obj)).not.equal({})
})
it('push accept null but undefined', () => {
const queue = new Queue()
should(queue.push()).be.undefined()
should(queue.length).equal(0)
should(queue.push(null)).equal(null)
should(queue.length).equal(1)
})
it('push overwrite exiting of same, do not grow the queue', () => {
const queue = new Queue()
should(queue.push(null)).be.null()
should(queue.length).equal(1)
should(queue.push(null)).be.null()
should(queue.length).equal(1)
})
it('remove returns true when found', () => {
const queue = new Queue()
queue.push(5)
should(queue.remove(2)).be.false()
should(queue.remove(5)).be.true()
})
it('support to clear entire queue', () => {
const queue = new Queue()
queue.push(1)
queue.push(2)
queue.clear()
queue.length.should.equal(0)
})
})
describe('Test LoopQueue with custom hash', () => {
it('push overwrites item of same hash', () => {
const queue = new Queue(item => item.id)
const ob1 = { id: 1 }
const ob2 = { id: 1 }
should(queue.push(ob1)).equal(1)
should(queue.length).equal(1)
should(queue.push(ob2)).equal(1)
should(queue.length).equal(1)
const item = queue.get(1)
should(item).not.equal(ob1)
should(item).equal(ob2)
should(queue.get(2)).be.undefined()
})
it('remove by custom hash', () => {
const queue = new Queue(item => item.id)
queue.push({ id: 1 })
should(queue.remove(1)).be.true()
})
})
describe('Test LoopQueue.shift', () => {
it('shift oldest item', () => {
const queue = new Queue()
queue.push(1)
queue.push(2)
should(queue.shift()).equal(1)
should(queue.shift()).equal(2)
should(queue.length).equal(0)
should(queue.shift()).be.undefined()
})
it('push and shift once reach capacity', () => {
const queue = new Queue(false, 1)
const original = queue.shift
queue.shift = function mock () {
const item = original.call(this)
should(queue.length).equal(0)
return item
}
queue.push(1)
queue.push(2) // force shift of 1
should(queue.shift()).equal(2)
})
})
describe('Test LoopQueue iterator', () => {
it('is iterable', () => {
const queue = new Queue()
queue.push(1)
queue.push(null)
let counter = 0
for (const item of new Queue()) {
switch (++counter) {
case '1': item.should.equal(1); break
case '2': should(item).be.null(); break
}
}
})
it('is iterable event if empty', done => {
for (const item of new Queue()) {
done('should not call' + item)
}
done()
})
it('support multiple concurrent iterators', () => {
const queue = new Queue()
queue.push(1)
queue.push(2)
const it1 = queue[Symbol.iterator]()
const it2 = queue.values()
it1.next()
it1.next()
it2.next().done.should.be.false()
it1.next().done.should.be.true()
})
})