-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
111 lines (101 loc) · 2.49 KB
/
main_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
package main
import (
"fmt"
"math/rand"
"strings"
"testing"
)
type args struct {
val int
oldVal int
newVal int
}
type testObj struct {
name string
args args
want any
wantErr bool
}
func TestInsert(t *testing.T) {
bTree := NewBTree()
tests := make([]testObj, 0)
for i := 0; i < 2*T-1; i++ {
name := fmt.Sprintf("success_leaf_add_%v", i)
tests = append(tests, unitTestMaker(name, args{val: rand.Intn(10000)}, nil))
}
for i := 0; i < 2*T-1; i++ {
name := fmt.Sprintf("success_node_add_%v", i)
tests = append(tests, unitTestMaker(name, args{val: rand.Intn(10000)}, nil))
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := bTree.Insert(tt.args.val); (err != nil) != tt.wantErr {
t.Errorf("bTree.Insert() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
bTree.PrintTree()
}
func unitTestMaker(testName string, testAgrs args, want any) testObj {
var test testObj
isSuccess := true
test.name = testName
test.wantErr = isSuccess
if strings.Contains(testName, "success") {
test.wantErr = !isSuccess
}
test.args = testAgrs
test.want = want
return test
}
func Test_bTree_Search(t *testing.T) {
bTree := NewBTree()
tests := make([]testObj, 0)
nums := make([]int, 0)
for i := 0; i < 3*T; i++ {
x := rand.Intn(100)
nums = append(nums, x)
bTree.Insert(x)
}
bTree.PrintTree()
for i := 0; i < len(nums); i++ {
name := fmt.Sprintf("success_find_%v", i+1)
tests = append(tests, unitTestMaker(name, args{val: nums[i]}, true))
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := bTree.Search(tt.args.val)
if (err != nil) != tt.wantErr {
t.Errorf("bTree.Search() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("bTree.Search() = %v, want %v", got, tt.want)
}
})
}
}
func TestBTree_Update(t *testing.T) {
bTree := NewBTree()
nums := make([]int, 0)
for i := 0; i < 3*T; i++ {
x := rand.Intn(100)
nums = append(nums, x)
bTree.Insert(x)
}
// bTree.PrintTree()
fmt.Printf("Nums : %v\n", nums)
tests := make([]testObj, 0)
for i := 0; i < 5; i++ {
name := fmt.Sprintf("success_update_%v", i+1)
tests = append(tests, unitTestMaker(name, args{oldVal: nums[i], newVal: rand.Intn(50) + 100}, true))
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := bTree.Update(tt.args.oldVal, tt.args.newVal); (err != nil) != tt.wantErr {
t.Errorf("BTree.Update() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
bTree.PrintTree()
}