Software /
code /
prosody
Comparison
tests/test_util_queue.lua @ 7507:11e8c605a591
tests: Adapt test for util.queue to the test framework
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 20 Jul 2016 17:24:49 +0200 |
parent | 6732:b5cf091d8c45 |
child | 7508:736dea7b99ec |
comparison
equal
deleted
inserted
replaced
7504:b43cbbbb806f | 7507:11e8c605a591 |
---|---|
1 local new = require "util.queue".new; | |
2 | 1 |
3 local q = new(10); | 2 function new(new) |
3 do | |
4 local q = new(10); | |
4 | 5 |
5 assert(q.size == 10); | 6 assert_equal(q.size, 10); |
6 assert(q:count() == 0); | 7 assert_equal(q:count(), 0); |
7 | 8 |
8 assert(q:push("one")); | 9 assert_is(q:push("one")); |
9 assert(q:push("two")); | 10 assert_is(q:push("two")); |
10 assert(q:push("three")); | 11 assert_is(q:push("three")); |
11 | 12 |
12 for i = 4, 10 do | 13 for i = 4, 10 do |
13 print("pushing "..i) | 14 assert_is(q:push("hello")); |
14 assert(q:push("hello")); | 15 assert_equal(q:count(), i, "count is not "..i.."("..q:count()..")"); |
15 assert(q:count() == i, "count is not "..i.."("..q:count()..")"); | 16 end |
17 assert_equal(q:push("hello"), nil, "queue overfull!"); | |
18 assert_equal(q:push("hello"), nil, "queue overfull!"); | |
19 assert_equal(q:pop(), "one", "queue item incorrect"); | |
20 assert_equal(q:pop(), "two", "queue item incorrect"); | |
21 assert_is(q:push("hello")); | |
22 assert_is(q:push("hello")); | |
23 assert_equal(q:pop(), "three", "queue item incorrect"); | |
24 assert_is(q:push("hello")); | |
25 assert_equal(q:push("hello"), nil, "queue overfull!"); | |
26 assert_equal(q:push("hello"), nil, "queue overfull!"); | |
27 | |
28 assert_equal(q:count(), 10, "queue count incorrect"); | |
29 | |
30 for _ = 1, 10 do | |
31 assert_equal(q:pop(), "hello", "queue item incorrect"); | |
32 end | |
33 | |
34 assert_equal(q:count(), 0, "queue count incorrect"); | |
35 | |
36 assert_is(q:push(1)); | |
37 for i = 1, 1001 do | |
38 assert_equal(q:pop(), i); | |
39 assert_equal(q:count(), 0); | |
40 assert_is(q:push(i+1)); | |
41 assert_equal(q:count(), 1); | |
42 end | |
43 assert_equal(q:pop(), 1002); | |
44 assert_is(q:push(1)); | |
45 for i = 1, 1000000 do | |
46 q:pop(); | |
47 q:push(i+1); | |
48 end | |
49 end | |
50 | |
51 do | |
52 -- Test queues that purge old items when pushing to a full queue | |
53 local q = new(10, true); | |
54 | |
55 for i = 1, 10 do | |
56 q:push(i); | |
57 end | |
58 | |
59 assert_equal(q:count(), 10); | |
60 | |
61 assert_is(q:push(11)); | |
62 assert_equal(q:count(), 10); | |
63 assert_equal(q:pop(), 2); -- First item should have been purged | |
64 | |
65 for i = 12, 32 do | |
66 assert_is(q:push(i)); | |
67 end | |
68 | |
69 assert_equal(q:count(), 10); | |
70 assert_equal(q:pop(), 23); | |
71 end | |
16 end | 72 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); |