summaryrefslogtreecommitdiff
path: root/go/allergies/allergies_test.go
blob: a8b2995a7b93619e40bd7990dd4dd63218b02b5b (plain)
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
package allergies

import (
	"fmt"
	"sort"
	"testing"
)

var allergiesTests = []struct {
	expected []string
	input    int
}{
	{[]string{}, 0},
	{[]string{"eggs"}, 1},
	{[]string{"peanuts"}, 2},
	{[]string{"strawberries"}, 8},
	{[]string{"eggs", "peanuts"}, 3},
	{[]string{"eggs", "shellfish"}, 5},
	{[]string{"strawberries", "tomatoes", "chocolate", "pollen", "cats"}, 248},
	{[]string{"eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"}, 255},
	{[]string{"eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats"}, 509},
}

func TestAllergies(t *testing.T) {
	for _, test := range allergiesTests {
		actual := Allergies(test.input)
		sort.Strings(actual)
		sort.Strings(test.expected)
		if fmt.Sprintf("%s", actual) != fmt.Sprintf("%s", test.expected) {
			t.Fatalf("FAIL: Allergies(%d): expected %s, actual %s", test.input, test.expected, actual)
		} else {
			t.Logf("PASS: Allergic to %v", test.expected)
		}
	}
}

func BenchmarkAllergies(b *testing.B) {
	b.StopTimer()
	for _, test := range allergicToTests {
		b.StartTimer()

		for i := 0; i < b.N; i++ {
			Allergies(test.i)
		}
		b.StopTimer()
	}
}

var allergicToTests = []struct {
	expected bool
	i        int
	allergen string
}{
	{false, 0, "peanuts"},
	{false, 0, "cats"},
	{false, 0, "strawberries"},
	{true, 1, "eggs"},
	{true, 5, "eggs"},
}

func TestAllergicTo(t *testing.T) {
	for _, test := range allergicToTests {
		actual := AllergicTo(test.i, test.allergen)
		if actual != test.expected {
			t.Fatalf("FAIL: AllergicTo(%d, %s): expected %t, actual %t", test.i, test.allergen, test.expected, actual)
		} else {
			t.Logf("PASS: AllergicTo(%d, %s) %t", test.i, test.allergen, actual)
		}
	}
}

func BenchmarkAllergicTo(b *testing.B) {
	b.StopTimer()
	for _, test := range allergicToTests {
		b.StartTimer()

		for i := 0; i < b.N; i++ {
			AllergicTo(test.i, test.allergen)
		}
		b.StopTimer()
	}
}