From e740cc41bf5a275b8bd070bfff29bd439bca4be5 Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 20 Jul 2016 17:24:49 +0200 Subject: tests: Adapt test for util.queue to the test framework --- tests/test.lua | 1 + tests/test_util_queue.lua | 112 ++++++++++++++++++++++++---------------------- 2 files changed, 59 insertions(+), 54 deletions(-) (limited to 'tests') diff --git a/tests/test.lua b/tests/test.lua index 78eb0134..bc33bb76 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -27,6 +27,7 @@ function run_all_tests() dotest "util.random" dotest "util.xml" dotest "util.xmppstream" + dotest "util.queue" dotest "net.http.parser" dosingletest("test_sasl.lua", "latin1toutf8"); diff --git a/tests/test_util_queue.lua b/tests/test_util_queue.lua index b0e1fa3d..57fd28e7 100644 --- a/tests/test_util_queue.lua +++ b/tests/test_util_queue.lua @@ -1,68 +1,72 @@ -local new = require "util.queue".new; -local q = new(10); +function new(new) + do + local q = new(10); -assert(q.size == 10); -assert(q:count() == 0); + assert_equal(q.size, 10); + assert_equal(q:count(), 0); -assert(q:push("one")); -assert(q:push("two")); -assert(q:push("three")); + assert_is(q:push("one")); + assert_is(q:push("two")); + assert_is(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!"); + for i = 4, 10 do + assert_is(q:push("hello")); + assert_equal(q:count(), i, "count is not "..i.."("..q:count()..")"); + end + assert_equal(q:push("hello"), nil, "queue overfull!"); + assert_equal(q:push("hello"), nil, "queue overfull!"); + assert_equal(q:pop(), "one", "queue item incorrect"); + assert_equal(q:pop(), "two", "queue item incorrect"); + assert_is(q:push("hello")); + assert_is(q:push("hello")); + assert_equal(q:pop(), "three", "queue item incorrect"); + assert_is(q:push("hello")); + assert_equal(q:push("hello"), nil, "queue overfull!"); + assert_equal(q:push("hello"), nil, "queue overfull!"); -assert(q:count() == 10, "queue count incorrect"); + assert_equal(q:count(), 10, "queue count incorrect"); -for i = 1, 10 do - assert(q:pop() == "hello", "queue item incorrect"); -end + for _ = 1, 10 do + assert_equal(q:pop(), "hello", "queue item incorrect"); + end -assert(q:count() == 0, "queue count incorrect"); + assert_equal(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 + assert_is(q:push(1)); + for i = 1, 1001 do + assert_equal(q:pop(), i); + assert_equal(q:count(), 0); + assert_is(q:push(i+1)); + assert_equal(q:count(), 1); + end + assert_equal(q:pop(), 1002); + assert_is(q:push(1)); + for i = 1, 1000000 do + q:pop(); + q:push(i+1); + end + end --- Test queues that purge old items when pushing to a full queue -local q = new(10, true); + do + -- 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 + for i = 1, 10 do + q:push(i); + end -assert(q:count() == 10); + assert_equal(q:count(), 10); -assert(q:push(11)); -assert(q:count() == 10); -assert(q:pop() == 2); -- First item should have been purged + assert_is(q:push(11)); + assert_equal(q:count(), 10); + assert_equal(q:pop(), 2); -- First item should have been purged -for i = 12, 32 do - assert(q:push(i)); -end + for i = 12, 32 do + assert_is(q:push(i)); + end -assert(q:count() == 10); -assert(q:pop() == 23); + assert_equal(q:count(), 10); + assert_equal(q:pop(), 23); + end +end -- cgit v1.2.3 From 6c54088074936289bc0d5f9d33b5d8934df05dee Mon Sep 17 00:00:00 2001 From: Kim Alvefur Date: Wed, 20 Jul 2016 18:07:10 +0200 Subject: test_util_queue: Reduce iteration count of loop and add asserts --- tests/test_util_queue.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_util_queue.lua b/tests/test_util_queue.lua index 57fd28e7..afa15e59 100644 --- a/tests/test_util_queue.lua +++ b/tests/test_util_queue.lua @@ -42,9 +42,9 @@ function new(new) end assert_equal(q:pop(), 1002); assert_is(q:push(1)); - for i = 1, 1000000 do - q:pop(); - q:push(i+1); + for i = 1, 1000 do + assert_equal(q:pop(), i); + assert_is(q:push(i+1)); end end -- cgit v1.2.3 From 1659995fb562c786705ae4f34e94813b0874b2b7 Mon Sep 17 00:00:00 2001 From: Anton Shestakov Date: Thu, 21 Jul 2016 01:23:32 +0800 Subject: test_util_queue: finish stress-test by emptying the queue --- tests/test_util_queue.lua | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/test_util_queue.lua b/tests/test_util_queue.lua index afa15e59..e215ba33 100644 --- a/tests/test_util_queue.lua +++ b/tests/test_util_queue.lua @@ -46,6 +46,8 @@ function new(new) assert_equal(q:pop(), i); assert_is(q:push(i+1)); end + assert_equal(q:pop(), 1001); + assert_equal(q:count(), 0); end do -- cgit v1.2.3