diff options
| author | ruki <[email protected]> | 2019-12-07 22:53:56 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-12-07 22:53:56 +0800 |
| commit | cefbcff637ee2652f9572845e9f758e188a45923 (patch) | |
| tree | 541fdf339ecb42ca7b9ce0a78a9953bbd504082d /tests/modules/heap/test.lua | |
| parent | fdaddc5793ee13bf78befd08412c3278bd861360 (diff) | |
add heap
Diffstat (limited to 'tests/modules/heap/test.lua')
| -rw-r--r-- | tests/modules/heap/test.lua | 37 |
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 + |
