-
Notifications
You must be signed in to change notification settings - Fork 0
/
red_black_tree_test.go
150 lines (108 loc) · 3.26 KB
/
red_black_tree_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 datt_test
import (
"testing"
"github.com/raphadam/datt"
)
func TestRedBlackTreeLen(t *testing.T) {
t.Run("len on empty tree return 0", func(t *testing.T) {
rb := datt.NewRedBlackTree[int, string](datt.CompareOrdered[int])
got := rb.Len()
AssertEqual(t, got, 0)
})
t.Run("len on multiple insert must increase", func(t *testing.T) {
rb := datt.NewRedBlackTree[int, string](datt.CompareOrdered[int])
rb.Insert(125, "pierre")
rb.Insert(51, "henry")
rb.Insert(12, "john")
rb.Insert(-10, "alice")
got := rb.Len()
AssertEqual(t, got, 4)
})
}
func TestRedBlackTreeHas(t *testing.T) {
t.Run("does not contain", func(t *testing.T) {
rb := datt.NewRedBlackTree[int, string](datt.CompareOrdered[int])
got := rb.Has(10)
AssertEqual(t, got, false)
})
t.Run("tree with one element", func(t *testing.T) {
rb := datt.NewRedBlackTree[int, string](datt.CompareOrdered[int])
rb.Insert(10, "alice")
got := rb.Has(10)
AssertEqual(t, got, true)
})
t.Run("insert with bigger key", func(t *testing.T) {
rb := datt.NewRedBlackTree[int, string](datt.CompareOrdered[int])
rb.Insert(10, "alice")
rb.Insert(20, "bob")
got := rb.Has(20)
AssertEqual(t, got, true)
})
}
func TestRedBlackTreeGet(t *testing.T) {
t.Run("empty redblacktree", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
_, ok := rb.Get("alice")
AssertEqual(t, ok, false)
})
t.Run("one element rbt", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("alice", 25)
got, ok := rb.Get("alice")
AssertEqual(t, ok, true)
AssertEqual(t, got, 25)
})
t.Run("two elements with second lower", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("bob", 33)
rb.Insert("alice", 25)
got, ok := rb.Get("alice")
AssertEqual(t, ok, true)
AssertEqual(t, got, 25)
})
t.Run("three elements left", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("ken", 67)
rb.Insert("bob", 33)
rb.Insert("alice", 25)
got, ok := rb.Get("alice")
AssertEqual(t, ok, true)
AssertEqual(t, got, 25)
})
t.Run("three elements right", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("alice", 25)
rb.Insert("bob", 33)
rb.Insert("ken", 67)
got, ok := rb.Get("ken")
AssertEqual(t, ok, true)
AssertEqual(t, got, 67)
})
t.Run("three elements cross", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("alice", 25)
rb.Insert("ken", 67)
rb.Insert("bob", 33)
got, ok := rb.Get("bob")
AssertEqual(t, ok, true)
AssertEqual(t, got, 33)
})
t.Run("override root", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("bob", 33)
rb.Insert("bob", 28)
got, ok := rb.Get("bob")
AssertEqual(t, ok, true)
AssertEqual(t, got, 28)
})
t.Run("override any", func(t *testing.T) {
rb := datt.NewRedBlackTree[string, int](datt.CompareOrdered[string])
rb.Insert("bob", 33)
rb.Insert("alice", 26)
rb.Insert("ken", 68)
rb.Insert("ken", 69)
got, ok := rb.Get("ken")
AssertEqual(t, ok, true)
AssertEqual(t, got, 69)
})
}