Software /
code /
prosody
Comparison
tests/test_util_queue.lua @ 6732:b5cf091d8c45
tests: Add tests for util.queue
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 03 Jun 2015 15:53:27 +0100 |
child | 7507:11e8c605a591 |
comparison
equal
deleted
inserted
replaced
6731:d4a6c9ee4bc5 | 6732:b5cf091d8c45 |
---|---|
1 local new = require "util.queue".new; | |
2 | |
3 local q = new(10); | |
4 | |
5 assert(q.size == 10); | |
6 assert(q:count() == 0); | |
7 | |
8 assert(q:push("one")); | |
9 assert(q:push("two")); | |
10 assert(q:push("three")); | |
11 | |
12 for i = 4, 10 do | |
13 print("pushing "..i) | |
14 assert(q:push("hello")); | |
15 assert(q:count() == i, "count is not "..i.."("..q:count()..")"); | |
16 end | |
17 assert(q:push("hello") == nil, "queue overfull!"); | |
18 assert(q:push("hello") == nil, "queue overfull!"); | |
19 assert(q:pop() == "one", "queue item incorrect"); | |
20 assert(q:pop() == "two", "queue item incorrect"); | |
21 assert(q:push("hello")); | |
22 assert(q:push("hello")); | |
23 assert(q:pop() == "three", "queue item incorrect"); | |
24 assert(q:push("hello")); | |
25 assert(q:push("hello") == nil, "queue overfull!"); | |
26 assert(q:push("hello") == nil, "queue overfull!"); | |
27 | |
28 assert(q:count() == 10, "queue count incorrect"); | |
29 | |
30 for i = 1, 10 do | |
31 assert(q:pop() == "hello", "queue item incorrect"); | |
32 end | |
33 | |
34 assert(q:count() == 0, "queue count incorrect"); | |
35 | |
36 assert(q:push(1)); | |
37 for i = 1, 1001 do | |
38 assert(q:pop() == i); | |
39 assert(q:count() == 0); | |
40 assert(q:push(i+1)); | |
41 assert(q:count() == 1); | |
42 end | |
43 assert(q:pop() == 1002); | |
44 assert(q:push(1)); | |
45 for i = 1, 1000000 do | |
46 q:pop(); | |
47 q:push(i+1); | |
48 end | |
49 | |
50 -- Test queues that purge old items when pushing to a full queue | |
51 local q = new(10, true); | |
52 | |
53 for i = 1, 10 do | |
54 q:push(i); | |
55 end | |
56 | |
57 assert(q:count() == 10); | |
58 | |
59 assert(q:push(11)); | |
60 assert(q:count() == 10); | |
61 assert(q:pop() == 2); -- First item should have been purged | |
62 | |
63 for i = 12, 32 do | |
64 assert(q:push(i)); | |
65 end | |
66 | |
67 assert(q:count() == 10); | |
68 assert(q:pop() == 23); |