-
Notifications
You must be signed in to change notification settings - Fork 1
/
midori_test.go
123 lines (104 loc) · 3.6 KB
/
midori_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
package main
import (
"github.com/chiro/parrot/random"
"testing"
)
func TestMidoriAddRandomCell(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 2, 0, 2}, {2, 2, 0, 0}, {0, 2, 0, 0}, {0, 0, 0, 0}}
var r random.Gen = &random.Std{}
r.SetRange([]int{2, 4})
var sim Midori = Midori{encode2(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
before := sim.GetAvailableCells()
sim.AddRandomCell()
after := sim.GetAvailableCells()
if before-1 != after {
t.Errorf("got %v, want %v\n", after, before-1)
}
}
func TestEncodeDecode(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}
var r random.Gen = &random.Std{}
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
if initial != sim.GetBoard() {
t.Errorf("got %v\nwant %v\n", sim.GetBoard(), initial)
}
}
func TestMidoriGet(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}
var r random.Gen = &random.Std{}
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
for y := 0; y < 4; y++ {
for x := 0; x < 4; x++ {
if sim.get(x, y) != initial[y][x] {
t.Errorf("got %v, want %v, at (%v,%v)\n", sim.get(x, y), initial[y][x], x, y)
}
}
}
}
func TestMidoriSet(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}
var r random.Gen = &random.Std{}
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
sim.set(0, 0, 2)
if sim.get(0, 0) != 2 {
t.Errorf("got %v, want %v\n", sim.get(0, 0), 2)
}
sim.set(3, 3, 8)
if sim.get(3, 3) != 8 {
t.Errorf("got %v, want %v\n", sim.get(3, 3), 8)
}
}
func TestMidoriMoveUp(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 1, 0, 1}, {1, 1, 0, 2}, {0, 1, 0, 3}, {0, 0, 0, 4}}
var r random.Gen = &random.Std{}
r.SetRange([]int{2, 4})
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
sim.Move(Up)
var expected [4][4]int = [4][4]int{{1, 2, 0, 1}, {0, 1, 0, 2}, {0, 0, 0, 3}, {0, 0, 0, 4}}
if sim.Grid != encode(&expected) {
t.Errorf("got %v\nwant %v\n", sim.GetBoard(), expected)
t.Errorf("got %v\nwant %v", sim.Grid, encode(&expected))
}
}
func TestMidoriMoveRight(t *testing.T) {
var initial [4][4]int = [4][4]int{{1, 1, 4, 5}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
var r random.Gen = &random.Std{}
r.SetRange([]int{2, 4})
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
sim.Move(Right)
var expected [4][4]int = [4][4]int{{0, 2, 4, 5}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
if sim.Grid != encode(&expected) {
t.Errorf("got %v\n", sim.GetBoard())
t.Errorf("got %v\nwant %v", sim.Grid, encode(&expected))
}
}
func TestMidoriGetAvailableCells(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}
var r random.Gen = &random.Std{}
r.SetRange([]int{2, 4})
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
if sim.GetAvailableCells() != 11 {
t.Errorf("got %v, want %v\n", sim.GetAvailableCells(), 11)
}
x, _ := sim.getRandomAvailableCell()
if x == -1 {
t.Errorf("can't find empty cells!\n")
}
}
func TestMidoriGetMaxCell(t *testing.T) {
var initial [4][4]int = [4][4]int{{0, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 0, 0}, {0, 0, 3, 0}}
var r random.Gen = &random.Std{}
r.SetRange([]int{2, 4})
var sim Midori = Midori{encode(&initial), 0, false, r.GetGenerator()}
sim.Initialize()
if sim.GetMaxTile() != 8 {
t.Errorf("got %v, want %v\n", sim.GetMaxTile(), 8)
}
}