aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_util_queue.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2015-06-25 18:57:43 +0200
committerKim Alvefur <zash@zash.se>2015-06-25 18:57:43 +0200
commit6e187489a98a403ae236a9595fbd0f80d3ff6490 (patch)
tree1b0aa90c356c8219e4165bd4d5136ea99b34e8f1 /tests/test_util_queue.lua
parent7b1c9ac2f7a58d8d91124196fec60206facc840e (diff)
parent7b391bc37acca3a5ed62477f18c9e03348a057f5 (diff)
downloadprosody-6e187489a98a403ae236a9595fbd0f80d3ff6490.tar.gz
prosody-6e187489a98a403ae236a9595fbd0f80d3ff6490.zip
Merge 0.10->trunk
Diffstat (limited to 'tests/test_util_queue.lua')
-rw-r--r--tests/test_util_queue.lua68
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/test_util_queue.lua b/tests/test_util_queue.lua
new file mode 100644
index 00000000..b0e1fa3d
--- /dev/null
+++ b/tests/test_util_queue.lua
@@ -0,0 +1,68 @@
+local new = require "util.queue".new;
+
+local q = new(10);
+
+assert(q.size == 10);
+assert(q:count() == 0);
+
+assert(q:push("one"));
+assert(q:push("two"));
+assert(q:push("three"));
+
+for i = 4, 10 do
+ print("pushing "..i)
+ assert(q:push("hello"));
+ assert(q:count() == i, "count is not "..i.."("..q:count()..")");
+end
+assert(q:push("hello") == nil, "queue overfull!");
+assert(q:push("hello") == nil, "queue overfull!");
+assert(q:pop() == "one", "queue item incorrect");
+assert(q:pop() == "two", "queue item incorrect");
+assert(q:push("hello"));
+assert(q:push("hello"));
+assert(q:pop() == "three", "queue item incorrect");
+assert(q:push("hello"));
+assert(q:push("hello") == nil, "queue overfull!");
+assert(q:push("hello") == nil, "queue overfull!");
+
+assert(q:count() == 10, "queue count incorrect");
+
+for i = 1, 10 do
+ assert(q:pop() == "hello", "queue item incorrect");
+end
+
+assert(q:count() == 0, "queue count incorrect");
+
+assert(q:push(1));
+for i = 1, 1001 do
+ assert(q:pop() == i);
+ assert(q:count() == 0);
+ assert(q:push(i+1));
+ assert(q:count() == 1);
+end
+assert(q:pop() == 1002);
+assert(q:push(1));
+for i = 1, 1000000 do
+ q:pop();
+ q:push(i+1);
+end
+
+-- Test queues that purge old items when pushing to a full queue
+local q = new(10, true);
+
+for i = 1, 10 do
+ q:push(i);
+end
+
+assert(q:count() == 10);
+
+assert(q:push(11));
+assert(q:count() == 10);
+assert(q:pop() == 2); -- First item should have been purged
+
+for i = 12, 32 do
+ assert(q:push(i));
+end
+
+assert(q:count() == 10);
+assert(q:pop() == 23);