Software / code / prosody
Comparison
spec/util_queue_spec.lua @ 8236:4878e4159e12
Port tests to the `busted` test runner
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Fri, 15 Sep 2017 17:07:57 -0400 |
| child | 8240:c803624cae3d |
comparison
equal
deleted
inserted
replaced
| 8235:7d9a2c200736 | 8236:4878e4159e12 |
|---|---|
| 1 | |
| 2 local queue = require "util.queue"; | |
| 3 | |
| 4 describe("util.queue", function() | |
| 5 describe("#new()", function() | |
| 6 it("should work", function() | |
| 7 | |
| 8 do | |
| 9 local q = queue.new(10); | |
| 10 | |
| 11 assert.are.equal(q.size, 10); | |
| 12 assert.are.equal(q:count(), 0); | |
| 13 | |
| 14 assert.is_true(q:push("one")); | |
| 15 assert.is_true(q:push("two")); | |
| 16 assert.is_true(q:push("three")); | |
| 17 | |
| 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!"); | |
| 32 | |
| 33 assert.are.equal(q:count(), 10, "queue count incorrect"); | |
| 34 | |
| 35 for _ = 1, 10 do | |
| 36 assert.are.equal(q:pop(), "hello", "queue item incorrect"); | |
| 37 end | |
| 38 | |
| 39 assert.are.equal(q:count(), 0, "queue count incorrect"); | |
| 40 | |
| 41 assert.is_true(q:push(1)); | |
| 42 for i = 1, 1001 do | |
| 43 assert.are.equal(q:pop(), i); | |
| 44 assert.are.equal(q:count(), 0); | |
| 45 assert.is_true(q:push(i+1)); | |
| 46 assert.are.equal(q:count(), 1); | |
| 47 end | |
| 48 assert.are.equal(q:pop(), 1002); | |
| 49 assert.is_true(q:push(1)); | |
| 50 for i = 1, 1000 do | |
| 51 assert.are.equal(q:pop(), i); | |
| 52 assert.is_true(q:push(i+1)); | |
| 53 end | |
| 54 assert.are.equal(q:pop(), 1001); | |
| 55 assert.are.equal(q:count(), 0); | |
| 56 end | |
| 57 | |
| 58 do | |
| 59 -- Test queues that purge old items when pushing to a full queue | |
| 60 local q = queue.new(10, true); | |
| 61 | |
| 62 for i = 1, 10 do | |
| 63 q:push(i); | |
| 64 end | |
| 65 | |
| 66 assert.are.equal(q:count(), 10); | |
| 67 | |
| 68 assert.is_true(q:push(11)); | |
| 69 assert.are.equal(q:count(), 10); | |
| 70 assert.are.equal(q:pop(), 2); -- First item should have been purged | |
| 71 | |
| 72 for i = 12, 32 do | |
| 73 assert.is_true(q:push(i)); | |
| 74 end | |
| 75 | |
| 76 assert.are.equal(q:count(), 10); | |
| 77 assert.are.equal(q:pop(), 23); | |
| 78 end | |
| 79 | |
| 80 end); | |
| 81 end); | |
| 82 end); |