-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_test.go
74 lines (69 loc) · 1.46 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
package gdk
import (
"fmt"
"testing"
)
type player struct {
level int
name string
}
func TestNewHeap(t *testing.T) {
h := NewHeap([]player{}, func(p1, p2 player) int {
return p1.level - p2.level
})
t.Run("TestNewHeap EmptyCheck", func(t *testing.T) {
//want := player{}
var want player
if got := h.Pop(); got != want {
t.Errorf("got %v, want %v", got, want)
}
})
for i := 100; i > 0; i-- {
h.Push(player{i, fmt.Sprintf("name%d", i)})
}
t.Run("TestNewHeap Sort", func(t *testing.T) {
if got := h.Pop(); got.level != 1 {
t.Errorf("got %v, want 1", got.level)
}
})
}
func TestCopy(t *testing.T) {
h := NewHeap([]*player{}, func(p1, p2 *player) int {
return p1.level - p2.level
})
t.Run("nil check", func(t *testing.T) {
if got := h.Pop(); got != nil {
t.Errorf("got %v, want nil", got)
}
})
h.Push(&player{1, "math"})
h.Push(&player{2, "mat"})
h2 := h.Copy()
h2.Push(&player{3, "ma"})
t.Run("Len Check", func(t *testing.T) {
if h.Len() != 2 {
t.Errorf("got %v, want 2", h.Len())
}
if h2.Len() != 3 {
t.Errorf("got %v, want 3", h2.Len())
}
})
t.Run("Pop check", func(t *testing.T) {
data := h.Pop()
if data.level != 1 {
t.Errorf("got %v, want 1", data.level)
}
data = h2.Pop()
if data.level != 1 {
t.Errorf("got %v, want 1", data.level)
}
data = h.Pop()
if data.level != 2 {
t.Errorf("got %v, want 2", data.level)
}
data = h.Pop()
if data != nil {
t.Errorf("got %v, want nil", data)
}
})
}