~telemachus/algorithms

algorithms/binarysearchtree_test.go -rw-r--r-- 3.1 KiB
c4396785Peter Aronoff Update for golangci-lint 9 months ago
                                                                                
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
package algorithms_test

import (
	"fmt"
	"testing"

	"git.sr.ht/~telemachus/algorithms"
)

func TestNewBST(t *testing.T) {
	tree := algorithms.NewBST()

	if tree == nil {
		t.Errorf("tree is nil, but it shouldn’t be")
	}
	if 0 != tree.Size() {
		t.Errorf("expected tree.Size() to be 0; actual: %d", tree.Size())
	}
	expected := "*algorithms.Tree"
	actual := fmt.Sprintf("%T", tree)
	if expected != actual {
		t.Errorf("expected %s; actual %s", expected, actual)
	}
}

func TestTreeInsert(t *testing.T) {
	tree := algorithms.NewBST()
	tree.Insert(2)
	tree.Insert(10)
	tree.Insert(-15)
	tree.Insert(10)

	if 4 != tree.Size() {
		t.Errorf("inserted three items; actual tree.Size() is %d", tree.Size())
	}
}

func TestTreeSearch(t *testing.T) {
	tree := algorithms.NewBST()
	tree.Insert(2)
	tree.Insert(10)
	tree.Insert(-15)
	tree.Insert(10)

	actual := tree.Search(-15)
	if -15 != actual.Value() {
		t.Errorf("expected %d; actual %d", -15, actual.Value())
	}

	actual = tree.Search(7)
	if nil != actual {
		t.Errorf("expected %#v; actual %#v", nil, actual)
	}

	actual = tree.Search(10)
	if 2 != actual.Count() {
		t.Errorf("expected result to have 10 twice; actual %d", actual.Count())
	}
}

func TestTreeMin(t *testing.T) {
	tree := algorithms.NewBST()
	tree.Insert(2)
	tree.Insert(10)
	tree.Insert(-15)
	tree.Insert(10)

	actual := tree.Min()
	if -15 != actual.Value() {
		t.Errorf("expected a minimum of %d; actual %d", -15, actual.Value())
	}
}

func TestTreeMax(t *testing.T) {
	tree := algorithms.NewBST()
	tree.Insert(2)
	tree.Insert(10)
	tree.Insert(-15)
	tree.Insert(10)
	tree.Insert(1010)

	actual := tree.Max()
	if 1010 != actual.Value() {
		t.Errorf("expected a maximum of %d; actual %d", 1010, actual.Value())
	}
}

func TestTreeSuccessor(t *testing.T) {
	tree := algorithms.NewBST()
	tree.Insert(15)
	tree.Insert(6)
	tree.Insert(3)
	tree.Insert(2)
	tree.Insert(4)
	tree.Insert(7)
	tree.Insert(13)
	tree.Insert(9)
	tree.Insert(18)
	tree.Insert(17)
	tree.Insert(20)

	node := tree.Search(7)
	actual := tree.Successor(node)
	if 9 != actual.Value() {
		t.Errorf("expected 9 as successor; actual %d", actual.Value())
	}

	node = tree.Search(20)
	actual = tree.Successor(node)
	if nil != actual {
		t.Errorf("expected nil as successor; actual %#v", actual)
	}
}

func TestTreePredecessor(t *testing.T) {
	tree := algorithms.NewBST()
	tree.Insert(15)
	tree.Insert(6)
	tree.Insert(3)
	tree.Insert(2)
	tree.Insert(4)
	tree.Insert(7)
	tree.Insert(13)
	tree.Insert(9)
	tree.Insert(18)
	tree.Insert(17)
	tree.Insert(20)

	node := tree.Search(15)
	actual := tree.Predecessor(node)
	if 13 != actual.Value() {
		t.Errorf("expected 15 as predecessor; actual %d", actual.Value())
	}

	node = tree.Search(4)
	actual = tree.Predecessor(node)
	if 3 != actual.Value() {
		t.Errorf("expected 3 as predecessor; actual %d", actual.Value())
	}

	node = tree.Search(9)
	actual = tree.Predecessor(node)
	if 7 != actual.Value() {
		t.Errorf("expected 7 as predecessor; actual %d", actual.Value())
	}

	node = tree.Search(2)
	actual = tree.Predecessor(node)
	if nil != actual {
		t.Errorf("expected nil as predecessor; actual %#v", actual)
	}
}