summaryrefslogtreecommitdiff
path: root/tests/modules/heap/test.lua
blob: fc11a49d2e92299e9d05c882b6737993fcf59574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import("core.base.heap")

function test_cdataheap(t)
    if not xmake.luajit() then
        return
    end
    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