-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathbheap_specs.lua
138 lines (105 loc) · 3.14 KB
/
bheap_specs.lua
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
context('Module BHeap', function()
local BHeap
before(function()
BHeap = require ('jumper.core.bheap')
end)
context('BHeap class', function()
test('BHeap() instantiates a new heap object', function()
assert_equal(getmetatable(BHeap()), BHeap)
end)
test('the new heap is empty', function()
assert_true((BHeap()):empty())
end)
test('items can be pushed inside', function()
local h = BHeap()
h:push(1):push(2):push(3)
assert_equal(h._size, 3)
end)
test('popping returns the lowest element by default (< operator)', function()
local h = BHeap()
h:push(1):push(2):push(0)
assert_equal(h:pop(),0)
assert_equal(h:pop(),1)
assert_equal(h:pop(),2)
assert_nil(h:pop())
end)
test('a heap can be cleared', function()
local h = BHeap()
assert_true(h:empty())
h:push(1):push(2):push(3)
assert_false(h:empty())
h:clear()
assert_true(h:empty())
end)
test('one can define a custom sort function', function()
local sort = function(a,b) return a>b end
local h = BHeap(sort)
h:push(1):push(2):push(3)
assert_equal(h:pop(),3)
assert_equal(h:pop(),2)
assert_equal(h:pop(),1)
assert_nil(h:pop())
end)
test('items pushed can be objects, with a custom sort function', function()
local sortNode = function(a, b) return a.cost < b.cost end
local makeObj = function(cost) return {cost = cost} end
local h = BHeap(sortNode)
h:push(makeObj(1)):push(makeObj(2)):push(makeObj(3))
assert_equal(h:pop().cost,1)
assert_equal(h:pop().cost,2)
assert_equal(h:pop().cost,3)
assert_nil(h:pop())
end)
test('pushing a alue that cannot be compared to the previous ones raises an error', function()
local h = BHeap()
h:push(false)
assert_error(pcall(h.push, h, false))
assert_error(pcall(h.push, h, true))
assert_error(pcall(h.push, h, {}))
assert_error(pcall(h.push, h, function() end))
end)
test('pushing nil does nothing', function()
local h = BHeap()
h:push()
assert_true(h:empty())
h:push(1):push()
assert_false(h:empty())
assert_equal(h._size,1)
end)
test('popping an empty heap returns nil', function()
local h = BHeap()
assert_nil(h:pop())
end)
test('BHeap:heapify() forces a sort of the heap', function()
local h = BHeap()
local sort = function(a,b) return a.value < b.value end
local function makeObj(v) return {value = v} end
local h = BHeap(sort)
local A, B, C = makeObj(1), makeObj(2), makeObj(3)
h:push(A):push(B):push(C)
C.value = 0
h:heapify(C)
local ret = h:pop()
assert_true(ret == C)
assert_equal(ret.value,0)
local ret = h:pop()
assert_true(ret == A)
assert_equal(ret.value,1)
local ret = h:pop()
assert_true(ret == B)
assert_equal(ret.value,2)
h:push(A):push(B):push(C)
C.value, B.value, A.value = 3, 2, 100
h:heapify()
local ret = h:pop()
assert_true(ret == B)
assert_equal(ret.value,2)
local ret = h:pop()
assert_true(ret == C)
assert_equal(ret.value,3)
local ret = h:pop()
assert_true(ret == A)
assert_equal(ret.value,100)
end)
end)
end)