Annotate

spec/util_queue_spec.lua @ 10799:763bb2ce3f60

util.pposix,signal: Pass around various OS numbers as integers [Lua 5.3] Passing around PIDs, UIDs etc as integers makes it more sane in Lua 5.3. Getting 1234.0 as PID is silly. Shouldn't change any behavior as these are all integers on the C side and the integral floats are accepted as integers when passed back from Lua into C.
author Kim Alvefur <zash@zash.se>
date Mon, 04 May 2020 21:51:30 +0200
parent 9901:c8b75239846c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 local queue = require "util.queue";
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4 describe("util.queue", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 describe("#new()", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
6 it("should work", function()
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
7
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
8 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local q = queue.new(10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
10
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
11 assert.are.equal(q.size, 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
12 assert.are.equal(q:count(), 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
13
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
14 assert.is_true(q:push("one"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
15 assert.is_true(q:push("two"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
16 assert.is_true(q:push("three"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
17
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
18 for i = 4, 10 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
19 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
20 assert.are.equal(q:count(), i, "count is not "..i.."("..q:count()..")");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
21 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
22 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
23 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
24 assert.are.equal(q:pop(), "one", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
25 assert.are.equal(q:pop(), "two", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
26 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
27 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
28 assert.are.equal(q:pop(), "three", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
29 assert.is_true(q:push("hello"));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
30 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
31 assert.are.equal(q:push("hello"), nil, "queue overfull!");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
32
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
33 assert.are.equal(q:count(), 10, "queue count incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
35 for _ = 1, 10 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
36 assert.are.equal(q:pop(), "hello", "queue item incorrect");
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
37 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 assert.are.equal(q:count(), 0, "queue count incorrect");
8240
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
40 assert.are.equal(q:pop(), nil, "empty queue pops non-nil result");
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
41 assert.are.equal(q:count(), 0, "popping empty queue affects count");
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
42
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
43 assert.are.equal(q:peek(), nil, "empty queue peeks non-nil result");
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
44 assert.are.equal(q:count(), 0, "peeking empty queue affects count");
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 assert.is_true(q:push(1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 for i = 1, 1001 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 assert.are.equal(q:pop(), i);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
49 assert.are.equal(q:count(), 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50 assert.is_true(q:push(i+1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51 assert.are.equal(q:count(), 1);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 assert.are.equal(q:pop(), 1002);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
54 assert.is_true(q:push(1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
55 for i = 1, 1000 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
56 assert.are.equal(q:pop(), i);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
57 assert.is_true(q:push(i+1));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
58 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
59 assert.are.equal(q:pop(), 1001);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
60 assert.are.equal(q:count(), 0);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
61 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
62
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
64 -- Test queues that purge old items when pushing to a full queue
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
65 local q = queue.new(10, true);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
66
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
67 for i = 1, 10 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
68 q:push(i);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
69 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
70
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
71 assert.are.equal(q:count(), 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
72
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
73 assert.is_true(q:push(11));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
74 assert.are.equal(q:count(), 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
75 assert.are.equal(q:pop(), 2); -- First item should have been purged
8240
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
76 assert.are.equal(q:peek(), 3);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
77
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
78 for i = 12, 32 do
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
79 assert.is_true(q:push(i));
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
80 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
81
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
82 assert.are.equal(q:count(), 10);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
83 assert.are.equal(q:pop(), 23);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
84 end
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
85
8240
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
86 do
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
87 -- Test iterator
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
88 local q = queue.new(10, true);
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
89
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
90 for i = 1, 10 do
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
91 q:push(i);
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
92 end
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
93
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
94 local i = 0;
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
95 for item in q:items() do
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
96 i = i + 1;
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
97 assert.are.equal(item, i, "unexpected item returned by iterator")
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
98 end
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
99 end
c803624cae3d spec/util_queue: Add iterator and peek tests for 100% line coverage
Matthew Wild <mwild1@gmail.com>
parents: 8236
diff changeset
100
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
101 end);
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
102 end);
9901
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
103 describe("consume()", function ()
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
104 it("should work", function ()
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
105 local q = queue.new(10);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
106 for i = 1, 5 do
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
107 q:push(i);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
108 end
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
109 local c = 0;
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
110 for i in q:consume() do
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
111 assert(i == c + 1);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
112 assert(q:count() == (5-i));
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
113 c = i;
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
114 end
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
115 end);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
116
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
117 it("should work even if items are pushed in the loop", function ()
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
118 local q = queue.new(10);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
119 for i = 1, 5 do
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
120 q:push(i);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
121 end
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
122 local c = 0;
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
123 for i in q:consume() do
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
124 assert(i == c + 1);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
125 if c < 3 then
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
126 assert(q:count() == (5-i));
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
127 else
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
128 assert(q:count() == (6-i));
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
129 end
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
130
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
131 c = i;
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
132
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
133 if c == 3 then
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
134 q:push(6);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
135 end
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
136 end
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
137 assert.equal(c, 6);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
138 end);
c8b75239846c util.queue: Add 'consume()' convenience iterator
Matthew Wild <mwild1@gmail.com>
parents: 8240
diff changeset
139 end);
8236
4878e4159e12 Port tests to the `busted` test runner
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
140 end);