-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchallenge_test.go
61 lines (52 loc) · 1.18 KB
/
challenge_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
package challenge
import (
"math/rand"
"testing"
"time"
)
func runWinnerTest(t *testing.T, gophers []gopher) {
if len(gophers) >= 5 {
rand.Seed(time.Now().UTC().UnixNano())
winner := rand.Intn(len(gophers))
t.Logf("The winner is: %s!", gophers[winner].key())
} else {
t.Log("Not enough participants")
}
}
func TestWinner(t *testing.T) {
kazanGophers := []gopher{}
nizhnyGophers := []gopher{}
// keys is a set of all participants keys.
// Needed to detect collisions.
keys := map[string]bool{}
// Check whether all fields are set correctly.
for i, g := range gophers {
if g.name == "" {
t.Errorf("gopher[%d] name is empty", i)
continue
}
if g.post == "" {
t.Errorf("gopher[%d] (%s) post is empty", i, g.name)
continue
}
if keys[g.key()] {
t.Errorf("gopher[%d] (%s) is a duplicate", i, g.name)
continue
}
keys[g.key()] = true
if g.tester {
continue
}
if g.nizhnyNovgorod {
nizhnyGophers = append(nizhnyGophers, g)
} else {
kazanGophers = append(kazanGophers, g)
}
}
t.Run("Kazan", func(t *testing.T) {
runWinnerTest(t, kazanGophers)
})
t.Run("NizhnyNovgorod", func(t *testing.T) {
runWinnerTest(t, nizhnyGophers)
})
}