util.x509: Nameprep commonName once
[prosody.git] / spec / util_queue_spec.lua
blobd73f523d0c5168406c232646c3c442f7694ca33a
2 local queue = require "util.queue";
4 describe("util.queue", function()
5 describe("#new()", function()
6 it("should work", function()
8 do
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"));
18 for i = 4, 10 do
19 assert.is_true(q:push("hello"));
20 assert.are.equal(q:count(), i, "count is not "..i.."("..q:count()..")");
21 end
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");
35 for _ = 1, 10 do
36 assert.are.equal(q:pop(), "hello", "queue item incorrect");
37 end
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));
47 for i = 1, 1001 do
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);
52 end
53 assert.are.equal(q:pop(), 1002);
54 assert.is_true(q:push(1));
55 for i = 1, 1000 do
56 assert.are.equal(q:pop(), i);
57 assert.is_true(q:push(i+1));
58 end
59 assert.are.equal(q:pop(), 1001);
60 assert.are.equal(q:count(), 0);
61 end
64 -- Test queues that purge old items when pushing to a full queue
65 local q = queue.new(10, true);
67 for i = 1, 10 do
68 q:push(i);
69 end
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);
78 for i = 12, 32 do
79 assert.is_true(q:push(i));
80 end
82 assert.are.equal(q:count(), 10);
83 assert.are.equal(q:pop(), 23);
84 end
87 -- Test iterator
88 local q = queue.new(10, true);
90 for i = 1, 10 do
91 q:push(i);
92 end
94 local i = 0;
95 for item in q:items() do
96 i = i + 1;
97 assert.are.equal(item, i, "unexpected item returned by iterator")
98 end
99 end
101 end);
102 end);
103 describe("consume()", function ()
104 it("should work", function ()
105 local q = queue.new(10);
106 for i = 1, 5 do
107 q:push(i);
109 local c = 0;
110 for i in q:consume() do
111 assert(i == c + 1);
112 assert(q:count() == (5-i));
113 c = i;
115 end);
117 it("should work even if items are pushed in the loop", function ()
118 local q = queue.new(10);
119 for i = 1, 5 do
120 q:push(i);
122 local c = 0;
123 for i in q:consume() do
124 assert(i == c + 1);
125 if c < 3 then
126 assert(q:count() == (5-i));
127 else
128 assert(q:count() == (6-i));
131 c = i;
133 if c == 3 then
134 q:push(6);
137 assert.equal(c, 6);
138 end);
139 end);
140 end);