summaryrefslogtreecommitdiff
path: root/tests/modules/heap/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2019-12-07 22:53:56 +0800
committerruki <[email protected]>2019-12-07 22:53:56 +0800
commitcefbcff637ee2652f9572845e9f758e188a45923 (patch)
tree541fdf339ecb42ca7b9ce0a78a9953bbd504082d /tests/modules/heap/test.lua
parentfdaddc5793ee13bf78befd08412c3278bd861360 (diff)
add heap
Diffstat (limited to 'tests/modules/heap/test.lua')
-rw-r--r--tests/modules/heap/test.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/modules/heap/test.lua b/tests/modules/heap/test.lua
new file mode 100644
index 000000000..098116f28
--- /dev/null
+++ b/tests/modules/heap/test.lua
@@ -0,0 +1,37 @@
+import("core.base.heap")
+
+function test_cdataheap(t)
+ local h = heap.cdataheap{
+ size = 100,
+ ctype = [[
+ struct {
+ int priority;
+ int order;
+ }
+ ]],
+ cmp = function(a, b)
+ if a.priority == b.priority then
+ return a.order > b.order
+ end
+ return a.priority < b.priority
+ end}
+ h:push{priority = 20, order = 1}
+ h:push{priority = 10, order = 2}
+ h:push{priority = 10, order = 3}
+ h:push{priority = 20, order = 4}
+ t:are_equal(h:pop().order, 3)
+ t:are_equal(h:pop().order, 2)
+ t:are_equal(h:pop().order, 4)
+ t:are_equal(h:pop().order, 1)
+end
+
+function test_valueheap(t)
+ local h = heap.valueheap{cmp = function(a, b)
+ return a.priority < b.priority
+ end}
+ h:push{priority = 20, etc = 'bar'}
+ h:push{priority = 10, etc = 'foo'}
+ t:are_equal(h:pop().priority, 10)
+ t:are_equal(h:pop().priority, 20)
+end
+