-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
132 lines (112 loc) · 3.83 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"testing"
)
func TestSetStrings(t *testing.T) {
set := NewSet("red", "green", "yellow", "red")
checkInt(t, 3, set.Size())
checkBool(t, true, set.Contains("red"))
checkBool(t, true, set.Contains("green"))
checkBool(t, true, set.Contains("yellow"))
checkBool(t, false, set.Contains("blue"))
checkContains(t, set.Slice(), "red", "green", "yellow")
checkString(t, "green, red, yellow", set.String())
set.Add("blue")
checkInt(t, 4, set.Size())
checkBool(t, true, set.Contains("blue"))
checkContains(t, set.Slice(), "red", "green", "yellow", "blue")
checkString(t, "blue, green, red, yellow", set.String())
set.Add("green")
checkInt(t, 4, set.Size())
set.Remove("red")
checkInt(t, 3, set.Size())
checkContains(t, set.Slice(), "green", "yellow", "blue")
checkString(t, "blue, green, yellow", set.String())
set.Remove("red")
checkInt(t, 3, set.Size())
}
func TestSetAddress(t *testing.T) {
set := NewSet(
Address{"Bob", "Water Street 17", 12345},
Address{"Alice", "Behind the Tower 3", 54321},
Address{"Charlie", "Green Road 13", 44452},
)
checkInt(t, 3, set.Size())
checkBool(t, true, set.Contains(Address{"Bob", "Water Street 17", 12345}))
checkBool(t, true, set.Contains(Address{"Alice", "Behind the Tower 3", 54321}))
checkBool(t, true, set.Contains(Address{"Charlie", "Green Road 13", 44452}))
checkBool(t, false, set.Contains(Address{"Zoe", "Parker Street", 57823}))
checkContains(t, set.Slice(),
Address{"Bob", "Water Street 17", 12345},
Address{"Alice", "Behind the Tower 3", 54321},
Address{"Charlie", "Green Road 13", 44452})
set.Add(Address{"Zoe", "Parker Street", 57823})
checkInt(t, 4, set.Size())
checkBool(t, true, set.Contains(Address{"Zoe", "Parker Street", 57823}))
checkContains(t, set.Slice(),
Address{"Bob", "Water Street 17", 12345},
Address{"Alice", "Behind the Tower 3", 54321},
Address{"Charlie", "Green Road 13", 44452},
Address{"Zoe", "Parker Street", 57823})
set.Add(Address{"Charlie", "Green Road 13", 44452})
checkInt(t, 4, set.Size())
set.Remove(Address{"Bob", "Water Street 17", 12345})
checkInt(t, 3, set.Size())
checkContains(t, set.Slice(),
Address{"Alice", "Behind the Tower 3", 54321},
Address{"Charlie", "Green Road 13", 44452},
Address{"Zoe", "Parker Street", 57823})
set.Remove(Address{"Bob", "Water Street 17", 12345})
checkInt(t, 3, set.Size())
checkString(t, "Alice | Behind the Tower 3 | 54321, "+
"Charlie | Green Road 13 | 44452, "+
"Zoe | Parker Street | 57823", set.String())
// Since there are no restrictions on the element type specific element types
// can be mixed which is often not wanted.
set.Add(42)
// Example for missing type information at compile time:
for _, element := range set.Slice() {
address, ok := element.(Address)
if ok {
fmt.Printf("name: %v street: %v zip: %v\n", address.name, address.street, address.zip)
} else {
fmt.Println("element is not an address but something different:", element)
}
}
}
func checkContains(t *testing.T, slice []interface{}, expectedElements ...interface{}) {
t.Helper()
// Since our use case covers rather small element counts, searching with a nested
// loop is fast enough.
for _, expected := range expectedElements {
found := false
for _, element := range slice {
if expected == element {
found = true
break
}
}
if !found {
t.Fatalf("Expected to find %v but slice is %v", expected, slice)
}
}
}
func checkBool(t *testing.T, expected, actual bool) {
t.Helper()
if expected != actual {
t.Fatalf("Expected %v but got %v", expected, actual)
}
}
func checkString(t *testing.T, expected, actual string) {
t.Helper()
if expected != actual {
t.Fatalf("Expected %v but got %v", expected, actual)
}
}
func checkInt(t *testing.T, expected, actual int) {
t.Helper()
if expected != actual {
t.Fatalf("Expected %v but got %v", expected, actual)
}
}