Skip to content

Commit 503ba4d

Browse files
committed
Tests for (Mutable)BinaryHeap with custom ordering
1 parent a0b1431 commit 503ba4d

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

test/test_binheap.jl

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
@testset "make heap" begin
66
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
7+
vs2 = collect(enumerate(vs))
8+
ordering = Base.Order.By(last)
79

810
@testset "construct heap" begin
911
BinaryHeap{Int, Base.ForwardOrdering}()
@@ -20,13 +22,18 @@
2022
BinaryMaxHeap{Int}(vs)
2123
BinaryMaxHeap(vs)
2224

25+
BinaryHeap{eltype(vs2)}(ordering)
26+
BinaryHeap{eltype(vs2)}(ordering, vs2)
27+
BinaryHeap(ordering, vs2)
28+
2329
@test true
2430
end
2531

2632
@testset "implicit conversion" begin
2733
BinaryHeap{Float64, Base.ForwardOrdering}(vs)
2834
BinaryMinHeap{Float64}(vs)
2935
BinaryMaxHeap{Float64}(vs)
36+
BinaryHeap{Tuple{Int, Float64}}(ordering, vs2)
3037

3138
@test true
3239
end
@@ -61,6 +68,16 @@
6168
@test sizehint!(h, 100) === h
6269
end
6370

71+
@testset "make custom ordering heap" begin
72+
h = BinaryHeap(ordering, vs2)
73+
74+
@test length(h) == 10
75+
@test !isempty(h)
76+
@test first(h) == (2, 1)
77+
@test isheap([(2, 1), (4, 2), (3, 3), (1, 4), (10, 7), (6, 9), (7, 10), (8, 14), (9, 8), (5, 16)], ordering)
78+
@test sizehint!(h, 100) === h
79+
end
80+
6481
@testset "extract all" begin
6582
@test sort(vs) == extract_all!(BinaryMinHeap(vs))
6683
@test reverse(sort(vs)) == extract_all_rev!(BinaryMinHeap(vs))
@@ -95,7 +112,6 @@
95112
@test isequal(extract_all!(hmin), [1, 2, 3, 4, 7, 8, 9, 10, 14, 16])
96113
@test isempty(hmin)
97114
end
98-
99115
end
100116

101117
@testset "push! hmax" begin
@@ -127,6 +143,36 @@
127143
@test isempty(hmax)
128144
end
129145
end
146+
147+
@testset "push! custom ordering" begin
148+
heap = BinaryHeap{Tuple{Int,Int}}(ordering)
149+
@test length(heap) == 0
150+
@test isempty(heap)
151+
152+
ss = Any[
153+
[(1, 4)],
154+
[(2, 1), (1, 4)],
155+
[(2, 1), (1, 4), (3, 3)],
156+
[(2, 1), (4, 2), (3, 3), (1, 4)],
157+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16)],
158+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9)],
159+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9), (7, 10)],
160+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9), (7, 10), (8, 14)],
161+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9), (7, 10), (8, 14), (9, 8)],
162+
[(2, 1), (4, 2), (3, 3), (1, 4), (10, 7), (6, 9), (7, 10), (8, 14), (9, 8), (5, 16)]]
163+
164+
for i = 1 : length(vs2)
165+
push!(heap, vs2[i])
166+
@test length(heap) == i
167+
@test !isempty(heap)
168+
@test isequal(heap.valtree, ss[i])
169+
end
170+
171+
@testset "pop! custom ordering" begin
172+
@test isequal(extract_all!(heap), sort(vs2, order=ordering))
173+
@test isempty(heap)
174+
end
175+
end
130176
end
131177

132178
end

test/test_mutable_binheap.jl

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ end
5353
@testset "MutableBinheap" begin
5454

5555
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
56+
vs2 = collect(enumerate(vs))
57+
ordering = Base.Order.By(last)
5658

5759
@testset "construct heap" begin
5860
MutableBinaryHeap{Int, Base.ForwardOrdering}()
@@ -69,6 +71,10 @@ end
6971
MutableBinaryMaxHeap{Int}(vs)
7072
MutableBinaryMaxHeap(vs)
7173

74+
MutableBinaryHeap{eltype(vs2)}(ordering)
75+
MutableBinaryHeap{eltype(vs2)}(ordering, vs2)
76+
MutableBinaryHeap(ordering, vs2)
77+
7278
@test true
7379
end
7480

@@ -103,6 +109,17 @@ end
103109
@test sizehint!(h, 100) === h
104110
end
105111

112+
@testset "make mutable binary custom ordering heap" begin
113+
h = MutableBinaryHeap(ordering, vs2)
114+
115+
@test length(h) == 10
116+
@test !isempty(h)
117+
@test first(h) == (2, 1)
118+
@test isequal(list_values(h), vs2)
119+
@test isequal(heap_values(h), [(2, 1), (4, 2), (3, 3), (1, 4), (10, 7), (6, 9), (7, 10), (8, 14), (9, 8), (5, 16)])
120+
@test sizehint!(h, 100) === h
121+
end
122+
106123
@testset "hmin / push! / pop!" begin
107124
hmin = MutableBinaryMinHeap{Int}()
108125
@test length(hmin) == 0
@@ -163,7 +180,37 @@ end
163180
# test pop!
164181
@test isequal(extract_all!(hmax), [16, 14, 10, 9, 8, 7, 4, 3, 2, 1])
165182
@test isempty(hmax)
183+
end
166184

185+
@testset "Custom ordering push! / pop!" begin
186+
heap = MutableBinaryHeap{Tuple{Int,Int}}(ordering)
187+
@test length(heap) == 0
188+
@test isempty(heap)
189+
190+
# test push!
191+
ss = Any[
192+
[(1, 4)],
193+
[(2, 1), (1, 4)],
194+
[(2, 1), (1, 4), (3, 3)],
195+
[(2, 1), (4, 2), (3, 3), (1, 4)],
196+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16)],
197+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9)],
198+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9), (7, 10)],
199+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9), (7, 10), (8, 14)],
200+
[(2, 1), (4, 2), (3, 3), (1, 4), (5, 16), (6, 9), (7, 10), (8, 14), (9, 8)],
201+
[(2, 1), (4, 2), (3, 3), (1, 4), (10, 7), (6, 9), (7, 10), (8, 14), (9, 8), (5, 16)]]
202+
for i = 1 : length(vs2)
203+
ia = push!(heap, vs2[i])
204+
@test ia == i
205+
@test length(heap) == i
206+
@test !isempty(heap)
207+
@test isequal(list_values(heap), vs2[1:i])
208+
@test isequal(heap_values(heap), ss[i])
209+
end
210+
211+
# test pop!
212+
@test isequal(extract_all!(heap), sort(vs2, order=ordering))
213+
@test isempty(heap)
167214
end
168215

169216
@testset "hybrid push! and pop!" begin

0 commit comments

Comments
 (0)