~jameinel/+junk/ants-2011-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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main

import (
	"testing"
)

func TestMap(t *testing.T) {
	m := NewMap(4, 3)
	if m.String() != `. . . 
. . . 
. . . 
. . . 
` {
		t.Errorf("map is wrong size, got `%s`", m)
	}

	loc := m.FromRowCol(3, 2)
	row, col := m.FromLocation(loc)
	if row != 3 || col != 2 {
		t.Errorf("conversion broken, got (%v, %v), wanted (3, 2)", row, col)
	}

	loc2 := m.FromRowCol(3, -1)
	if loc2 != loc {
		t.Errorf("from xy broken, got (%v), wanted (%v)", loc2, loc)
	}

	n := m.FromRowCol(2, 2)
	s := m.FromRowCol(4, 2)
	e := m.FromRowCol(3, 3)
	w := m.FromRowCol(3, 1)

	if n != m.Move(loc, North) {
		t.Errorf("Move north is broken")
	}
	if s != m.Move(loc, South) {
		t.Errorf("Move south is broken")
	}
	if e != m.Move(loc, East) {
		t.Errorf("Move east is broken")
	}
	if w != m.Move(loc, West) {
		t.Errorf("Move west is broken")
	}

	m.AddAnt(n, MY_ANT)
	m.AddAnt(s, ANT_1)
	m.AddAnt(w, MY_HILL)
	m.AddAnt(e, MY_OCCUPIED_HILL)
	m.AddHill(m.FromRowCol(0, 0), HILL_1)
	m.AddHill(m.FromRowCol(1, 0), HILL_1)
	m.AddAnt(m.FromRowCol(1, 0), ANT_1)

	if m.String() != `1 . b 
B . . 
. . a 
A 0 . 
` {
		t.Errorf("map put ants in wrong place, got `%s`", m)
	}
}

func TestNewMapFromDescriptionSize(t *testing.T) {
	desc := `
. . . 
. . . 
. . . 
. . . 
`
	m := NewMapFromDescription(desc)
	if m.Rows != 4 || m.Cols != 3 {
		t.Fatalf("Wrong dimensions, expected 4x3 got %dx%d",
			m.Rows, m.Cols)
	}
}

func TestNewMapFromDescriptionLand(t *testing.T) {
	desc := `
. . . 
.   . 
.   . 
. . . 
`
	m := NewMapFromDescription(desc)
	if m.Rows != 4 || m.Cols != 3 {
		t.Fatalf("Wrong dimensions, expected 4x3 got %dx%d",
			m.Rows, m.Cols)
	}
	if m.grid[m.FromRowCol(1, 1)].item != LAND ||
		m.grid[m.FromRowCol(2, 1)].item != LAND {
		t.Fatalf("Did not find LAND at (1, 1) or (2, 1):\n%s", m)
	}
}

func TestNewMapFromDescriptionMultiple(t *testing.T) {
	desc := `
. . . . . 
.   % . . 
. * a . . 
. A . * . 
. . . . . 
`[1:] // strip the opening blank line
	m := NewMapFromDescription(desc)
	if m.String() != desc {
		t.Fatalf("Invalid map, expected:\n%s got\n%s",
			desc, m)
	}
	if m.Rows != 5 || m.Cols != 5 {
		t.Fatalf("Invalid size %dx%d", m.Rows, m.Cols)
	}
	if len(m.Food) != 2 || m.Food[m.FromRowCol(2, 1)] != true ||
		m.Food[m.FromRowCol(3, 3)] != true {
		t.Fatalf("Bad food locations: %v", m.Food)
	}
	if len(m.Ants) != 2 {
		t.Fatalf("Bad ant locations: %v", m.Ants)
	}
}

func TestMapNodeIsNode(t *testing.T) {
	_ = Node(&MapNode{})
}

func TestMapEdgeIsEdge(t *testing.T) {
	_ = Edge(&MapEdge{})
}

func TestMapNode(t *testing.T) {
	m := NewMap(4, 3)
	foodLoc := m.FromRowCol(2, 1)
	m.AddFood(foodLoc)
	if m.String() != `. . . 
. . . 
. * . 
. . . 
` {
		t.Fatalf("unexpected map, got: `%s`", m)
	}
	mn := &m.grid[foodLoc]
	edges := mn.Edges()
	if edges == nil || len(edges) != 4 ||
		edges[0].Target().(*MapNode).loc != m.FromRowCol(1, 1) ||
		edges[1].Target().(*MapNode).loc != m.FromRowCol(2, 0) ||
		edges[2].Target().(*MapNode).loc != m.FromRowCol(2, 2) ||
		edges[3].Target().(*MapNode).loc != m.FromRowCol(3, 1) {
		t.Fatalf("Incorrect edges, got %v", edges)
	}
}

func TestMapNodeEdgesNotWater(t *testing.T) {
	m := NewMap(4, 3)
	foodLoc := m.FromRowCol(2, 1)
	waterLoc := m.FromRowCol(3, 1)
	m.AddFood(foodLoc)
	m.AddWater(waterLoc)
	if m.String() != `. . . 
. . . 
. * . 
. % . 
` {
		t.Fatalf("unexpected map, got: `%s`", m)
	}
	mn := &m.grid[foodLoc]
	edges := mn.Edges()
	if edges == nil || len(edges) != 3 ||
		edges[0].Target().(*MapNode).loc != m.FromRowCol(1, 1) ||
		edges[1].Target().(*MapNode).loc != m.FromRowCol(2, 0) ||
		edges[2].Target().(*MapNode).loc != m.FromRowCol(2, 2) {
		t.Fatalf("Incorrect edges, got %v", edges)
	}
}

func TestMapNodeEdgesWrapping(t *testing.T) {
	m := NewMap(4, 3)
	tl := m.FromRowCol(0, 0)
	tr := m.FromRowCol(0, 2)
	bl := m.FromRowCol(3, 0)
	br := m.FromRowCol(3, 2)
	edges := m.grid[tl].Edges()
	if edges == nil || len(edges) != 4 ||
		edges[0].Target().(*MapNode).loc != bl ||
		edges[1].Target().(*MapNode).loc != tr ||
		edges[2].Target().(*MapNode).loc != m.FromRowCol(0, 1) ||
		edges[3].Target().(*MapNode).loc != m.FromRowCol(1, 0) {
		t.Fatalf("Incorrect edges for top left, got %v", edges)
	}
	edges = m.grid[tr].Edges()
	if edges == nil || len(edges) != 4 ||
		edges[0].Target().(*MapNode).loc != br ||
		edges[1].Target().(*MapNode).loc != m.FromRowCol(0, 1) ||
		edges[2].Target().(*MapNode).loc != tl ||
		edges[3].Target().(*MapNode).loc != m.FromRowCol(1, 2) {
		t.Fatalf("Incorrect edges for top right, got %v", edges)
	}
	edges = m.grid[bl].Edges()
	if edges == nil || len(edges) != 4 ||
		edges[0].Target().(*MapNode).loc != m.FromRowCol(2, 0) ||
		edges[1].Target().(*MapNode).loc != m.FromRowCol(3, 2) ||
		edges[2].Target().(*MapNode).loc != m.FromRowCol(3, 1) ||
		edges[3].Target().(*MapNode).loc != m.FromRowCol(0, 0) {
		t.Fatalf("Incorrect edges for bottom left, got %v", edges)
	}
	edges = m.grid[br].Edges()
	if edges == nil || len(edges) != 4 ||
		edges[0].Target().(*MapNode).loc != m.FromRowCol(2, 2) ||
		edges[1].Target().(*MapNode).loc != m.FromRowCol(3, 1) ||
		edges[2].Target().(*MapNode).loc != bl ||
		edges[3].Target().(*MapNode).loc != tr {
		t.Fatalf("Incorrect edges for bottom right, got %v", edges)
	}
}

func assertDirection(t *testing.T, m *Map, sr, sc, tr, tc int, d Direction) {
	source_loc := m.FromRowCol(sr, sc)
	target_loc := m.FromRowCol(tr, tc)
	if x := m.DirectionFromLocations(source_loc, target_loc); x != d {
		t.Errorf("Expected direction %s found %s", d, x)
	}
}

func TestDirectionFromLocations(t *testing.T) {
	m := NewMap(4, 3)
	assertDirection(t, m, 0, 0, 0, 1, East)
	assertDirection(t, m, 0, 0, 1, 0, South)
	assertDirection(t, m, 0, 1, 0, 0, West)
	assertDirection(t, m, 1, 0, 0, 0, North)
}

func TestDirectionFromLocationsWrapping(t *testing.T) {
	m := NewMap(4, 3)
	assertDirection(t, m, 0, 2, 0, 0, East)
	assertDirection(t, m, 3, 0, 0, 0, South)
	assertDirection(t, m, 0, 0, 0, 2, West)
	assertDirection(t, m, 0, 0, 3, 0, North)
}

func TestDirectionFromLocationsIllegal(t *testing.T) {
	m := NewMap(4, 4)
	assertDirection(t, m, 0, 2, 0, 0, IllegalMovement)
	assertDirection(t, m, 2, 0, 0, 0, IllegalMovement)
	assertDirection(t, m, 0, 0, 0, 2, IllegalMovement)
	assertDirection(t, m, 0, 0, 2, 0, IllegalMovement)
	assertDirection(t, m, 0, 0, 1, 1, IllegalMovement)
}