-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmin_heap_test.go
170 lines (136 loc) · 3.4 KB
/
min_heap_test.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package heap_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/thenativeweb/codingcircle/heap"
)
func TestNew(t *testing.T) {
t.Run("Creates an empty heap.", func(t *testing.T) {
h := heap.New[int]()
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 0)
})
}
func TestInsert(t *testing.T) {
t.Run("Inserts a value into the heap.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(23, 1)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 1)
assert.Equal(t, []heap.Item[int]{
{Value: 23, Priority: 1},
}, h.Items())
})
t.Run("Inserts multiple values into the heap.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(42, 2)
h.Insert(17, 3)
h.Insert(23, 1)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 3)
assert.Equal(t, []heap.Item[int]{
{Value: 23, Priority: 1},
{Value: 17, Priority: 3},
{Value: 42, Priority: 2},
}, h.Items())
})
}
func TestRemove(t *testing.T) {
t.Run("Removes a value from the heap.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(23, 1)
err := h.Remove(0)
assert.NoError(t, err)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 0)
})
t.Run("Removes multiple values from the heap.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(42, 2)
h.Insert(17, 3)
h.Insert(23, 1)
err := h.Remove(1)
assert.NoError(t, err)
err = h.Remove(1)
assert.NoError(t, err)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 1)
assert.Equal(t, []heap.Item[int]{
{Value: 23, Priority: 1},
}, h.Items())
})
}
func TestUpdate(t *testing.T) {
t.Run("Updates a value in the heap.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(42, 2)
h.Insert(17, 3)
h.Insert(23, 1)
err := h.Update(2, 0)
assert.NoError(t, err)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 3)
assert.Equal(t, []heap.Item[int]{
{Value: 42, Priority: 0},
{Value: 17, Priority: 3},
{Value: 23, Priority: 1},
}, h.Items())
})
}
func TestPeek(t *testing.T) {
t.Run("Returns the minimum value.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(42, 2)
h.Insert(17, 3)
h.Insert(23, 1)
assert.Equal(t, 23, h.Peek().Value)
assert.Equal(t, 1, h.Peek().Priority)
})
t.Run("Returns nil if the heap is empty.", func(t *testing.T) {
h := heap.New[int]()
assert.Nil(t, h.Peek())
})
}
func TestComplex(t *testing.T) {
t.Run("Complex test.", func(t *testing.T) {
h := heap.New[int]()
h.Insert(27, 6)
h.Insert(9, 3)
h.Insert(17, 5)
h.Insert(2, 1)
h.Insert(12, 4)
h.Insert(8, 2)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 6)
assert.Equal(t, []heap.Item[int]{
{Value: 2, Priority: 1},
{Value: 9, Priority: 3},
{Value: 8, Priority: 2},
{Value: 27, Priority: 6},
{Value: 12, Priority: 4},
{Value: 17, Priority: 5},
}, h.Items())
err := h.Remove(3)
assert.NoError(t, err)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 5)
assert.Equal(t, []heap.Item[int]{
{Value: 2, Priority: 1},
{Value: 9, Priority: 3},
{Value: 8, Priority: 2},
{Value: 17, Priority: 5},
{Value: 12, Priority: 4},
}, h.Items())
err = h.Update(3, -1)
assert.NoError(t, err)
assert.True(t, h.IsValid())
assert.Len(t, h.Items(), 5)
assert.Equal(t, []heap.Item[int]{
{Value: 17, Priority: -1},
{Value: 2, Priority: 1},
{Value: 8, Priority: 2},
{Value: 9, Priority: 3},
{Value: 12, Priority: 4},
}, h.Items())
})
}