2 local queue
= require
"util.queue";
4 describe("util.queue", function()
5 describe("#new()", function()
6 it("should work", function()
9 local q
= queue
.new(10);
11 assert.are
.equal(q
.size
, 10);
12 assert.are
.equal(q
:count(), 0);
14 assert.is_true(q
:push("one"));
15 assert.is_true(q
:push("two"));
16 assert.is_true(q
:push("three"));
19 assert.is_true(q
:push("hello"));
20 assert.are
.equal(q
:count(), i
, "count is not "..i
.."("..q
:count()..")");
22 assert.are
.equal(q
:push("hello"), nil, "queue overfull!");
23 assert.are
.equal(q
:push("hello"), nil, "queue overfull!");
24 assert.are
.equal(q
:pop(), "one", "queue item incorrect");
25 assert.are
.equal(q
:pop(), "two", "queue item incorrect");
26 assert.is_true(q
:push("hello"));
27 assert.is_true(q
:push("hello"));
28 assert.are
.equal(q
:pop(), "three", "queue item incorrect");
29 assert.is_true(q
:push("hello"));
30 assert.are
.equal(q
:push("hello"), nil, "queue overfull!");
31 assert.are
.equal(q
:push("hello"), nil, "queue overfull!");
33 assert.are
.equal(q
:count(), 10, "queue count incorrect");
36 assert.are
.equal(q
:pop(), "hello", "queue item incorrect");
39 assert.are
.equal(q
:count(), 0, "queue count incorrect");
40 assert.are
.equal(q
:pop(), nil, "empty queue pops non-nil result");
41 assert.are
.equal(q
:count(), 0, "popping empty queue affects count");
43 assert.are
.equal(q
:peek(), nil, "empty queue peeks non-nil result");
44 assert.are
.equal(q
:count(), 0, "peeking empty queue affects count");
46 assert.is_true(q
:push(1));
48 assert.are
.equal(q
:pop(), i
);
49 assert.are
.equal(q
:count(), 0);
50 assert.is_true(q
:push(i
+1));
51 assert.are
.equal(q
:count(), 1);
53 assert.are
.equal(q
:pop(), 1002);
54 assert.is_true(q
:push(1));
56 assert.are
.equal(q
:pop(), i
);
57 assert.is_true(q
:push(i
+1));
59 assert.are
.equal(q
:pop(), 1001);
60 assert.are
.equal(q
:count(), 0);
64 -- Test queues that purge old items when pushing to a full queue
65 local q
= queue
.new(10, true);
71 assert.are
.equal(q
:count(), 10);
73 assert.is_true(q
:push(11));
74 assert.are
.equal(q
:count(), 10);
75 assert.are
.equal(q
:pop(), 2); -- First item should have been purged
76 assert.are
.equal(q
:peek(), 3);
79 assert.is_true(q
:push(i
));
82 assert.are
.equal(q
:count(), 10);
83 assert.are
.equal(q
:pop(), 23);
88 local q
= queue
.new(10, true);
95 for item
in q
:items() do
97 assert.are
.equal(item
, i
, "unexpected item returned by iterator")
103 describe("consume()", function ()
104 it("should work", function ()
105 local q
= queue
.new(10);
110 for i
in q
:consume() do
112 assert(q
:count() == (5-i
));
117 it("should work even if items are pushed in the loop", function ()
118 local q
= queue
.new(10);
123 for i
in q
:consume() do
126 assert(q
:count() == (5-i
));
128 assert(q
:count() == (6-i
));