-
-
Notifications
You must be signed in to change notification settings - Fork 654
/
allergies_test.go
73 lines (62 loc) · 1.63 KB
/
allergies_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
package allergies
import (
"reflect"
"testing"
)
func TestAllergies(t *testing.T) {
for _, test := range listTests {
t.Run(test.description, func(t *testing.T) {
if actual := Allergies(test.score); !sameSliceElements(actual, test.expected) {
t.Fatalf("Allergies(%d) = %#v, want: %#v", test.score, actual, test.expected)
}
})
}
}
func BenchmarkAllergies(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
for _, test := range allergicToTests {
Allergies(test.input.score)
}
}
}
func TestAllergicTo(t *testing.T) {
for _, test := range allergicToTests {
t.Run(test.description, func(t *testing.T) {
actual := AllergicTo(test.input.score, test.input.allergen)
if actual != test.expected {
t.Fatalf("AllergicTo(%d, %q) = %t, want: %t", test.input.score, test.input.allergen, actual, test.expected)
}
})
}
}
func BenchmarkAllergicTo(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
for _, test := range allergicToTests {
AllergicTo(test.input.score, test.input.allergen)
}
}
}
// stringSet is a set of strings
type stringSet map[string]bool
// sameSliceElements checks if the slices have the same number of elements
// regardless of their order
func sameSliceElements(a, b []string) bool {
if len(a) != len(b) {
return false
}
return reflect.DeepEqual(sliceSet(a), sliceSet(b))
}
// sliceSet creates a new stringSet from a slice of strings
func sliceSet(list []string) stringSet {
set := make(stringSet)
for _, elem := range list {
set[elem] = true
}
return set
}