aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_util_queue.lua
blob: b0e1fa3d8e52e6ee60ab7c082b1729293802e641 (plain)
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
local new = require "util.queue".new;

local q = new(10);

assert(q.size == 10);
assert(q:count() == 0);

assert(q:push("one"));
assert(q:push("two"));
assert(q:push("three"));

for i = 4, 10 do
	print("pushing "..i)
	assert(q:push("hello"));
	assert(q:count() == i, "count is not "..i.."("..q:count()..")");
end
assert(q:push("hello") == nil, "queue overfull!");
assert(q:push("hello") == nil, "queue overfull!");
assert(q:pop() == "one", "queue item incorrect");
assert(q:pop() == "two", "queue item incorrect");
assert(q:push("hello"));
assert(q:push("hello"));
assert(q:pop() == "three", "queue item incorrect");
assert(q:push("hello"));
assert(q:push("hello") == nil, "queue overfull!");
assert(q:push("hello") == nil, "queue overfull!");

assert(q:count() == 10, "queue count incorrect");

for i = 1, 10 do
	assert(q:pop() == "hello", "queue item incorrect");
end

assert(q:count() == 0, "queue count incorrect");

assert(q:push(1));
for i = 1, 1001 do
	assert(q:pop() == i);
	assert(q:count() == 0);
	assert(q:push(i+1));
	assert(q:count() == 1);
end
assert(q:pop() == 1002);
assert(q:push(1));
for i = 1, 1000000 do
	q:pop();
	q:push(i+1);
end

-- Test queues that purge old items when pushing to a full queue
local q = new(10, true);

for i = 1, 10 do
	q:push(i);
end

assert(q:count() == 10);

assert(q:push(11));
assert(q:count() == 10);
assert(q:pop() == 2); -- First item should have been purged

for i = 12, 32 do
	assert(q:push(i));
end

assert(q:count() == 10);
assert(q:pop() == 23);