forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap_test.go
150 lines (137 loc) · 3.1 KB
/
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
package heap_test
import (
"github.com/TheAlgorithms/Go/structure/heap"
"reflect"
"testing"
)
type testInt int
func (u testInt) Less(o testInt) bool {
return u < o
}
type testStudent struct {
Name string
Score int64
}
func (u testStudent) Less(o testStudent) bool {
if u.Score == o.Score {
return u.Name < o.Name
}
return u.Score > o.Score
}
func TestHeap_Empty(t *testing.T) {
tests := []struct {
name string
want bool
}{
{name: "empty", want: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h := heap.New[testInt]()
if got := h.Empty(); got != tt.want {
t.Errorf("Empty() = %v, want %v", got, tt.want)
}
})
}
}
type testOpType int
const (
testPush = 1
testPop = 2
testTop = 3
testEmpty = 4
)
type testOp[T any] struct {
typ testOpType
x T
isEmpty bool
}
type testStruct[T any] struct {
name string
ops []testOp[T]
}
func TestHeapExample1(t *testing.T) {
tests1 := []testStruct[testInt]{
{
name: "example 1",
ops: []testOp[testInt]{
{typ: testEmpty, isEmpty: true},
{typ: testPush, x: 10},
{typ: testEmpty, isEmpty: false},
{typ: testTop, x: 10},
{typ: testPop},
{typ: testEmpty, isEmpty: true},
{typ: testPush, x: 9},
{typ: testPush, x: 8},
{typ: testPop},
{typ: testPush, x: 3},
{typ: testTop, x: 3},
{typ: testPush, x: 2},
{typ: testTop, x: 2},
{typ: testPush, x: 4},
{typ: testPush, x: 6},
{typ: testPush, x: 5},
{typ: testTop, x: 2},
{typ: testPop},
{typ: testTop, x: 3},
{typ: testPop},
{typ: testPop},
{typ: testTop, x: 5},
{typ: testEmpty, isEmpty: false},
},
},
}
testFunc(t, tests1, testInt.Less)
}
func TestHeapExample2(t *testing.T) {
tests1 := []testStruct[testStudent]{
{
name: "example 2",
ops: []testOp[testStudent]{
{typ: testPush, x: testStudent{Name: "Alan", Score: 87}},
{typ: testPush, x: testStudent{Name: "Bob", Score: 98}},
{typ: testTop, x: testStudent{Name: "Bob", Score: 98}},
{typ: testPop},
{typ: testPush, x: testStudent{Name: "Carl", Score: 70}},
{typ: testTop, x: testStudent{Name: "Alan", Score: 87}},
},
},
}
testFunc(t, tests1, testStudent.Less)
}
func testFunc[T any](t *testing.T, tests []testStruct[T], less func(a, b T) bool) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
h, err := heap.NewAny[T](less)
if err != nil {
t.Errorf("New Heap err %v", err)
}
for i, op := range tt.ops {
switch op.typ {
case testPush:
oldSize := h.Size()
h.Push(op.x)
newSize := h.Size()
if oldSize+1 != newSize {
t.Errorf("op %d testPush %v failed", i, op.x)
}
case testPop:
oldSize := h.Size()
h.Pop()
newSize := h.Size()
if oldSize-1 != newSize {
t.Errorf("op %d testPop %v failed", i, op.x)
}
case testTop:
if got := h.Top(); !reflect.DeepEqual(got, op.x) {
t.Errorf("op %d testTop %v, want %v", i, got, op.x)
}
case testEmpty:
if got := h.Empty(); got != op.isEmpty {
t.Errorf("op %d Empty() = %v, want %v", i, got, op.isEmpty)
}
}
}
})
}
}