github.com/iainanderson83/datastructures@v0.0.4-0.20191103204413-889e20b53bcf/binarysearchtree/binarysearchtree_test.go (about) 1 package binarysearchtree 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 ) 8 9 var bst *Node 10 11 func fillTree(bst *Node) { 12 bst.Insert(4, "4") 13 bst.Insert(10, "10") 14 bst.Insert(2, "2") 15 bst.Insert(6, "6") 16 bst.Insert(1, "1") 17 bst.Insert(3, "3") 18 bst.Insert(5, "5") 19 bst.Insert(7, "7") 20 bst.Insert(9, "9") 21 bst.Insert(11, "11") 22 } 23 24 // isSameSlice returns true if the 2 slices are identical 25 func isSameSlice(a, b []string) bool { 26 if a == nil && b == nil { 27 return true 28 } 29 if a == nil || b == nil { 30 return false 31 } 32 if len(a) != len(b) { 33 return false 34 } 35 for i := range a { 36 if a[i] != b[i] { 37 return false 38 } 39 } 40 return true 41 } 42 43 func TestInOrderTraverse(t *testing.T) { 44 bst = &Node{Key: 8, Value: "8"} 45 fillTree(bst) 46 47 var result []string 48 bst.InOrderTraverse(func(key int, value interface{}) { 49 result = append(result, fmt.Sprintf("%s", value)) 50 }) 51 if !isSameSlice(result, []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}) { 52 t.Errorf("Traversal order incorrect, got %v", result) 53 } 54 } 55 56 func TestPreOrderTraverse(t *testing.T) { 57 bst = &Node{Key: 8, Value: "8"} 58 fillTree(bst) 59 60 var result []string 61 bst.PreOrderTraverse(func(key int, value interface{}) { 62 result = append(result, fmt.Sprintf("%s", value)) 63 }) 64 if !isSameSlice(result, []string{"8", "4", "2", "1", "3", "6", "5", "7", "10", "9", "11"}) { 65 t.Errorf("Traversal order incorrect, got %v instead of %v", result, []string{"8", "4", "2", "1", "3", "6", "5", "7", "10", "9", "11"}) 66 } 67 } 68 69 func TestPostOrderTraverse(t *testing.T) { 70 bst = &Node{Key: 8, Value: "8"} 71 fillTree(bst) 72 73 var result []string 74 bst.PostOrderTraverse(func(key int, value interface{}) { 75 result = append(result, fmt.Sprintf("%s", value)) 76 }) 77 if !isSameSlice(result, []string{"1", "3", "2", "5", "7", "6", "4", "9", "11", "10", "8"}) { 78 t.Errorf("Traversal order incorrect, got %v instead of %v", result, []string{"1", "3", "2", "5", "7", "6", "4", "9", "11", "10", "8"}) 79 } 80 } 81 82 func TestMin(t *testing.T) { 83 bst = &Node{Key: 8, Value: "8"} 84 fillTree(bst) 85 86 if fmt.Sprintf("%s", bst.Min().Value) != "1" { 87 t.Errorf("min should be 1") 88 } 89 } 90 91 func TestMax(t *testing.T) { 92 bst = &Node{Key: 8, Value: "8"} 93 fillTree(bst) 94 95 if fmt.Sprintf("%s", bst.Max().Value) != "11" { 96 t.Errorf("max should be 11") 97 } 98 } 99 100 func TestSearch(t *testing.T) { 101 bst = &Node{Key: 8, Value: "8"} 102 fillTree(bst) 103 104 if !bst.Search(1) || !bst.Search(8) || !bst.Search(11) { 105 t.Errorf("search not working") 106 } 107 } 108 109 func TestRemove(t *testing.T) { 110 bst = &Node{Key: 8, Value: "8"} 111 fillTree(bst) 112 113 bst.Remove(1) 114 115 if fmt.Sprintf("%s", bst.Min().Value) != "2" { 116 t.Errorf("min should be 2, got %s", bst.Min().Value) 117 } 118 } 119 120 func TestDates(t *testing.T) { 121 ranges := []struct { 122 BeginDate time.Time 123 EndDate time.Time 124 Value float64 125 }{ 126 { 127 time.Date(2009, time.January, 1, 0, 0, 0, 0, time.UTC), 128 time.Date(2009, time.June, 1, 0, 0, 0, 0, time.UTC), 129 60.5, 130 }, 131 { 132 time.Date(2009, time.June, 1, 0, 0, 0, 1, time.UTC), 133 time.Date(2009, time.December, 1, 0, 0, 0, 0, time.UTC), 134 65.5, 135 }, 136 { 137 time.Date(2009, time.December, 1, 0, 0, 0, 1, time.UTC), 138 time.Date(2010, time.June, 1, 0, 0, 0, 0, time.UTC), 139 70.5, 140 }, 141 { 142 time.Date(2010, time.June, 1, 0, 0, 0, 1, time.UTC), 143 time.Date(2010, time.December, 1, 0, 0, 0, 0, time.UTC), 144 75.5, 145 }, 146 { 147 time.Date(2010, time.December, 1, 0, 0, 0, 1, time.UTC), 148 time.Date(2011, time.June, 1, 0, 0, 0, 0, time.UTC), 149 80.5, 150 }, 151 } 152 153 middle := len(ranges) / 2 154 bst := &Node{Key: int(ranges[middle].BeginDate.Unix()), Value: ranges[middle].Value} 155 156 for i := range ranges { 157 if i == middle { 158 continue 159 } 160 bst.Insert(int(ranges[i].BeginDate.Unix()), ranges[i].Value) 161 } 162 163 out := bst.Nearest(int(time.Date(2009, time.June, 2, 0, 0, 0, 1, time.UTC).Unix())) 164 if out.Value != 65.5 { 165 t.Fatalf("expected %f, got %f", 65.5, out.Value) 166 } 167 }