-
Notifications
You must be signed in to change notification settings - Fork 0
/
state_test.go
80 lines (69 loc) · 2.25 KB
/
state_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
package main
import "testing"
func TestGameShouldAlwaysFinish(t *testing.T) {
// This test is going to be a tad slow
if testing.Short() {
t.Skip("skipping test in short mode.")
}
// generate a map
mapList, _ := generateMap(10, rng)
state := newGame(mapList, 3, rng)
state.run(10000)
// just finishing this test is enough
}
func TestSimpleBridge(t *testing.T) {
// 2 cities with A -> B
cityA := &city{name: "A", north: "B", numLinks: 1}
cityB := &city{name: "B"}
cities := map[string]*city{"A": cityA, "B": cityB}
listCities := []*city{cityA, cityB}
// alien in A
alien1 := &alien{name: "1", atCity: cityA}
cityA.aliens = []*alien{alien1}
// create state
state := gameState{cities: cities, listCities: listCities, aliens: []*alien{alien1}, rng: rng}
state.run(1)
if len(cityA.aliens) != 0 || len(cityB.aliens) != 1 || alien1.atCity != cityB {
t.Fatalf("Alien hasn't moved correctly")
}
}
func TestStuck(t *testing.T) {
// 2 cities with A <-!-B
cityA := &city{name: "A", north: "B", numLinks: 1}
cityB := &city{name: "B"}
cities := map[string]*city{"A": cityA, "B": cityB}
listCities := []*city{cityA, cityB}
// alien in B
alien1 := &alien{name: "1", atCity: cityB}
cityB.aliens = []*alien{alien1}
aliens := []*alien{alien1}
// create state
state := gameState{cities: cities, listCities: listCities, aliens: aliens, rng: rng}
state.run(1)
if len(cityA.aliens) != 0 || len(cityB.aliens) != 1 || alien1.atCity != cityB {
t.Fatalf("Alien has moved when it shouldn't have")
}
}
func TestTwoAliens(t *testing.T) {
// 2 cities with A <-> B
cityA := &city{name: "A", north: "B", numLinks: 1}
cityB := &city{name: "B", south: "A", numLinks: 1}
cities := map[string]*city{"A": cityA, "B": cityB}
listCities := []*city{cityA, cityB}
// aliens in both A and B
alien1 := &alien{name: "1", atCity: cityA}
alien2 := &alien{name: "2", atCity: cityB}
cityA.aliens = []*alien{alien1}
cityB.aliens = []*alien{alien2}
aliens := []*alien{alien1, alien2}
// create state
state := gameState{cities: cities, listCities: listCities, aliens: aliens, rng: rng}
state.run(1)
allGood := true
allGood = allGood || cityA.destroyed
allGood = allGood || cityB.destroyed
allGood = allGood || state.deadAliens == 2
if !allGood {
t.Fatalf("simple state has failed")
}
}