/
datastruct_utest.lua
510 lines (421 loc) · 12.5 KB
/
datastruct_utest.lua
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
require("engine/test/bustedhelper")
require("engine/core/datastruct")
describe('circular_buffer', function ()
describe('init', function ()
it('should create an empty circular buffer with given max length', function ()
local cb = circular_buffer(3)
-- interface
assert.is_not_nil(cb)
assert.are_equal(0, #cb)
-- implementation
assert.are_equal(3, cb.max_length)
assert.are_same({}, cb._buffer)
assert.are_equal(1, cb._start_index)
end)
describe('_tostring', function ()
it('should return circular_buffer({self,max_length}, {} for an empty buffer)', function ()
local cb = circular_buffer(3)
assert.are_equal("circular_buffer(3, {})", cb:_tostring())
end)
it('should return circular_buffer({self,max_length}, {content, ...})', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(10)
assert.are_equal("circular_buffer(3, {1, 2, 10})", cb:_tostring())
end)
end)
end)
describe('__eq', function ()
it('should return true when the buffer have exactly the same content for the same max length', function ()
local cb1 = circular_buffer(3)
cb1:push(1)
cb1:push(2)
cb1:push(10)
local cb2 = circular_buffer(3)
cb2:push(1)
cb2:push(2)
cb2:push(10)
assert.is_true(cb1:__eq(cb2))
end)
it('should return true when the buffer have the same circular content for the same max length', function ()
local cb1 = circular_buffer(3)
cb1:push(1)
cb1:push(2)
cb1:push(10)
local cb2 = circular_buffer(3)
cb2:push(999)
cb2:push(999)
cb2:push(1)
cb2:push(2)
cb2:push(10)
assert.is_true(cb1:__eq(cb2))
end)
it('should return false when the contents are different for the same buffer size', function ()
local cb1 = circular_buffer(3)
cb1:push(1)
cb1:push(2)
cb1:push(10)
local cb2 = circular_buffer(3)
cb2:push(1)
cb2:push(2)
cb2:push(999)
assert.is_false(cb1:__eq(cb2))
end)
it('should return false when the buffer sizes are different', function ()
local cb1 = circular_buffer(4)
cb1:push(1)
cb1:push(2)
cb1:push(10)
local cb2 = circular_buffer(4)
cb2:push(1)
cb2:push(2)
cb2:push(10)
cb2:push(999)
assert.is_false(cb1:__eq(cb2))
end)
it('should return false when the max lengths are different', function ()
local cb1 = circular_buffer(3)
cb1:push(1)
cb1:push(2)
cb1:push(10)
local cb2 = circular_buffer(4)
cb2:push(1)
cb2:push(2)
cb2:push(10)
cb2:push(999)
assert.is_false(cb1:__eq(cb2))
end)
it('should return false when the other member is not a circular buffer', function ()
local cb1 = circular_buffer(3)
cb1:push(1)
cb1:push(2)
cb1:push(10)
local cb2 = {
max_length = 3,
buffer = {1, 2, 10},
get = function (self, i)
-- circular_buffer.__eq won't even try to compare cb1 and cb2,
-- so this will actually never be called
return self._buffer[i]
end
}
assert.is_false(cb1:__eq(cb2))
end)
end)
describe('__len', function ()
it('#(^) => 0', function ()
local cb = circular_buffer(2)
assert.are_equal(0, #cb)
end)
it('#(^1, 2) => 2', function ()
local cb = circular_buffer(2)
cb:push(1)
cb:push(2)
assert.are_equal(2, #cb)
end)
it('#(3, ^2) => 2', function ()
local cb = circular_buffer(2)
cb:push(1)
cb:push(2)
cb:push(3)
assert.are_equal(2, #cb)
end)
end)
describe('__ipairs', function ()
it('(^): no iteration at all', function ()
local cb = circular_buffer(2)
for i, v in ipairs(cb) do
-- should never be called
assert.is_true(false)
end
end)
it('(^1): 1 iteration', function ()
local cb = circular_buffer(2)
cb:push(10)
local count = 0
local result_ipairs = {}
for i, v in ipairs(cb) do
count = count + 1
result_ipairs[count] = {i, v}
end
assert.are_same({{1, 10}}, result_ipairs)
end)
it('(4, ^2, 3): iterate 3 times from 2 to 4, cycling', function ()
local cb = circular_buffer(3)
cb:push(10)
cb:push(20)
cb:push(30)
cb:push(40)
local count = 0
local result_ipairs = {}
for i, v in ipairs(cb) do
count = count + 1
result_ipairs[count] = {i, v}
end
assert.are_same({{1, 20}, {2, 30}, {3, 40}}, result_ipairs)
end)
end)
describe('stateless_iter', function ()
it('(^):stateless_iter() => nil', function ()
local cb = circular_buffer(2)
assert.is_nil(cb:stateless_iter(0))
end)
it('(^1):stateless_iter() => 1, nil', function ()
local cb = circular_buffer(2)
cb:push(1)
assert.are_same({1, 1}, {cb:stateless_iter(0)})
assert.is_nil(cb:stateless_iter(1))
end)
it('(4, ^2, 3):stateless_iter() => 2, 3, 4, nil', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
cb:push(4)
assert.are_same({1, 2}, {cb:stateless_iter(0)})
assert.are_same({2, 3}, {cb:stateless_iter(1)})
assert.are_same({3, 4}, {cb:stateless_iter(2)})
assert.is_nil(cb:stateless_iter(3))
end)
end)
describe('rotate_indice', function ()
it('rotate_indice(-2, 3) => 1', function ()
assert.are_equal(1, circular_buffer.rotate_indice(-2, 3))
end)
it('rotate_indice(-1, 3) => 2', function ()
assert.are_equal(2, circular_buffer.rotate_indice(-1, 3))
end)
it('rotate_indice(0, 3) => 3', function ()
assert.are_equal(3, circular_buffer.rotate_indice(0, 3))
end)
it('rotate_indice(1, 3) => 1', function ()
assert.are_equal(1, circular_buffer.rotate_indice(1, 3))
end)
it('rotate_indice(2, 3) => 2', function ()
assert.are_equal(2, circular_buffer.rotate_indice(2, 3))
end)
it('rotate_indice(3, 3) => 3', function ()
assert.are_equal(3, circular_buffer.rotate_indice(3, 3))
end)
it('rotate_indice(4, 3) => 1', function ()
assert.are_equal(1, circular_buffer.rotate_indice(4, 3))
end)
end)
describe('get (oldest indicated by ^)', function ()
it('(^1, 2, 3):get(1) => 1', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
assert.are_equal(1, cb:get(1))
end)
it('(^1, 2, 3):get(3) => 3', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
assert.are_equal(3, cb:get(3))
end)
it('(4, ^2, 3):get(1) => 2', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
cb:push(4)
assert.are_equal(2, cb:get(1))
end)
it('(4, ^2, 3):get(3) => 4', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
cb:push(4)
assert.are_equal(4, cb:get(3))
end)
it('(^1, 2):get(-1) => 2', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
assert.are_equal(2, cb:get(-1))
end)
it('(^1, 2):get(-2) => 1', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
assert.are_equal(1, cb:get(-2))
end)
it('(4, ^2, 3):get(-1) => 4', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
cb:push(4)
assert.are_equal(4, cb:get(-1))
end)
it('(^1, 2, 3):get(0) => nil', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
assert.is_nil(cb:get(0))
end)
it('(^1, 2, 3):get(4) => nil', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
assert.is_nil(cb:get(4))
end)
it('(^1, 2, 3):get(-4) => nil', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
assert.is_nil(cb:get(-4))
end)
end)
describe('is_filled', function ()
it('should return true when the max length has been reached', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(10)
assert.is_true(cb:is_filled())
end)
it('should return true when the max length has been reached and some data overriden', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(10)
cb:push(999)
assert.is_true(cb:is_filled())
end)
it('should return false when the buffer is empty', function ()
local cb = circular_buffer(3)
assert.is_false(cb:is_filled())
end)
it('should return false when the buffer has some elements but max length is not reached', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
assert.is_false(cb:is_filled())
end)
end)
describe('push', function ()
local cb = circular_buffer(3)
after_each(function ()
cb:clear()
end)
describe('(when buffer is empty)', function ()
it('should add a new element at index 1', function ()
local has_replaced = cb:push(1)
-- interface
assert.is_false(has_replaced)
assert.are_equal(1, #cb)
assert.are_equal(1, cb:get(1))
-- implementation
assert.are_same({1}, cb._buffer)
assert.are_equal(1, cb._start_index)
end)
end)
describe('(when buffer has some entries but is not full)', function ()
setup(function ()
cb:push(1)
cb:push(2)
end)
it('should add a new element at the next index', function ()
local has_replaced = cb:push(3)
-- interface
assert.is_false(has_replaced)
assert.are_equal(3, #cb)
assert.are_equal(1, cb:get(1))
assert.are_equal(2, cb:get(2))
assert.are_equal(3, cb:get(3))
-- implementation
assert.are_same({1, 2, 3}, cb._buffer)
assert.are_equal(1, cb._start_index)
end)
end)
describe('(when buffer is full)', function ()
setup(function ()
cb:push(1)
cb:push(2)
cb:push(3)
end)
it('should replace the oldest element, moving oldest to the next element ', function ()
local has_replaced = cb:push(4)
-- interface
assert.is_true(has_replaced)
assert.are_equal(3, #cb)
assert.are_equal(2, cb:get(1))
assert.are_equal(3, cb:get(2))
assert.are_equal(4, cb:get(3))
-- implementation
assert.are_same({4, 2, 3}, cb._buffer)
assert.are_equal(2, cb._start_index)
end)
end)
describe('(when buffer is full again)', function ()
setup(function ()
cb:push(1)
cb:push(2)
cb:push(3)
cb:push(4)
cb:push(5)
cb:push(6)
end)
it('should replace the 1st, oldest element again, moving oldest to the next element ', function ()
local has_replaced = cb:push(7)
-- interface
assert.is_true(has_replaced)
assert.are_equal(3, #cb)
assert.are_equal(5, cb:get(1))
assert.are_equal(6, cb:get(2))
assert.are_equal(7, cb:get(3))
-- implementation
assert.are_same({7, 5, 6}, cb._buffer)
assert.are_equal(2, cb._start_index)
end)
end)
end)
describe('clear', function ()
describe('(when buffer is empty)', function ()
it('should do nothing', function ()
local cb = circular_buffer(3)
cb:clear()
-- interface
assert.are_equal(0, #cb)
-- implementation
assert.are_same({}, cb._buffer)
assert.are_equal(1, cb._start_index)
end)
end)
describe('(when buffer has some entries but is not full)', function ()
it('should clear the elements and oldest index', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:clear()
-- interface
assert.are_equal(0, #cb)
-- implementation
assert.are_same({}, cb._buffer)
assert.are_equal(1, cb._start_index)
end)
end)
describe('(when buffer is full)', function ()
it('should clear all elements and oldest index', function ()
local cb = circular_buffer(3)
cb:push(1)
cb:push(2)
cb:push(3)
cb:clear()
-- interface
assert.are_equal(0, #cb)
-- implementation
assert.are_same({}, cb._buffer)
assert.are_equal(1, cb._start_index)
end)
end)
end)
end)