~hokiegeek/life

ref: c41a449353d445a06ec7334b540afb793d577cd8 life/pond_test.go -rw-r--r-- 8.5 KiB View raw
c41a4493HokieGeek Refactored to host on sr.ht and converted to a mod 10 months ago
                                                                                
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package life

import "testing"

func TestLocationString(t *testing.T) {
	loc := Location{X: 1, Y: 1}
	if len(loc.String()) == 0 {
		t.Error("The Location String() function unexpectedly returned an empty string")
	}
}

func TestDimensionsString(t *testing.T) {
	dims := Dimensions{Height: 1, Width: 1}
	if len(dims.String()) == 0 {
		t.Error("The Dimensions Strings() function unexpectedly returned an empty string")
	}
}

func TestDimensionsEquals(t *testing.T) {
	dims := Dimensions{Height: 42, Width: 42}

	// Check that they are equal
	if !dims.Equals(&dims) {
		t.Fatal("Stupid equals function failed identity check")
	}

	// Check where height is not equal
	notEqual := Dimensions{Height: 44, Width: 42}
	if dims.Equals(&notEqual) {
		t.Fatalf("Equals function says that %d is equal to %d\n", dims.Height, notEqual.Height)
	}

	// Check where width is not equal
	notEqual = Dimensions{Height: 42, Width: 22}
	if dims.Equals(&notEqual) {
		t.Fatalf("Equals function says that %d is equal to %d\n", dims.Height, notEqual.Height)
	}

	// Check where neither is equal
	notEqual = Dimensions{Height: 44, Width: 22}
	if dims.Equals(&notEqual) {
		t.Fatalf("Equals function says that %s is equal to %s\n", dims.String(), notEqual.String())
	}
}

func TestNeighborSelectorString(t *testing.T) {
	var selector neighborsSelector

	selector = NeighborsAll
	if len(selector.String()) <= 0 || selector.String() != "All" {
		t.Error("Unexpectedly retrieved empty string from Status object")
	}

	selector = NeighborsOrthogonal
	if len(selector.String()) <= 0 || selector.String() != "Orthogonal" {
		t.Error("Unexpectedly retrieved empty string from pondselector object")
	}

	selector = NeighborsOblique
	if len(selector.String()) <= 0 || selector.String() != "Oblique" {
		t.Error("Unexpectedly retrieved empty string from pondselector object")
	}
}

func TestCreateCreateError(t *testing.T) {
	size := Dimensions{Width: 0, Height: 0}
	_, err := newPond(size, newTracker(), NeighborsAll)
	if err == nil {
		t.Fatal("Unexpectedly successful at creating pond of 0 capacity")
	}
}

func TestPondSettingInitialPatterns(t *testing.T) {
	pond, err := newPond(Dimensions{Height: 3, Width: 3}, newTracker(), NeighborsAll)
	if err != nil {
		t.Fatal("Unable to create pond")
	}

	// Create a pattern and call the pond's init function
	initialLiving := make([]Location, 3)
	initialLiving[0] = Location{X: 0, Y: 0}
	initialLiving[1] = Location{X: 1, Y: 1}
	initialLiving[2] = Location{X: 2, Y: 2}

	pond.SetOrganisms(initialLiving)

	// Check each expected value
	for _, loc := range initialLiving {
		if !pond.isOrganismAlive(loc) {
			t.Fatalf("Seed organism is not alive!: %s\n", loc.String())
		}
	}
}

func TestPondNeighborSelectionOrthogonal(t *testing.T) {
	pond, err := newPond(Dimensions{Height: 3, Width: 3}, newTracker(), NeighborsOrthogonal)
	if err != nil {
		t.Fatalf("Unable to create pond: %s\n", err)
	}

	expected := make([]Location, 4)
	expected[0] = Location{X: 1, Y: 0}
	expected[1] = Location{X: 0, Y: 1}
	expected[2] = Location{X: 2, Y: 1}
	expected[3] = Location{X: 1, Y: 2}

	var actual []Location
	actual, err = pond.GetNeighbors(Location{X: 1, Y: 1})
	if err != nil {
		t.Fatalf("Unable to retrieve neighbors: %s\n", err)
	}

	if len(actual) != len(expected) {
		t.Fatalf("Retrieved %d neighbors but expected %d\n", len(actual), len(expected))
	}
}

func TestPondNeighborSelectionOblique(t *testing.T) {
	pond, err := newPond(Dimensions{Height: 3, Width: 3}, newTracker(), NeighborsOblique)
	if err != nil {
		t.Fatalf("Unable to create pond: %s\n", err)
	}

	expected := make([]Location, 4)
	expected[0] = Location{X: 0, Y: 0}
	expected[1] = Location{X: 2, Y: 0}
	expected[2] = Location{X: 0, Y: 2}
	expected[3] = Location{X: 2, Y: 2}

	var actual []Location
	actual, err = pond.GetNeighbors(Location{X: 1, Y: 1})
	if err != nil {
		t.Fatalf("Unable to retrieve neighbors: %s\n", err)
	}

	if len(actual) != len(expected) {
		t.Fatalf("Retrieved %d neighbors but expected %d\n", len(actual), len(expected))
	}
}

func TestPondNeighborSelectionError(t *testing.T) {
	pond, err := newPond(Dimensions{Height: 1, Width: 1}, newTracker(), NeighborsAll)
	if err != nil {
		t.Fatalf("Unable to create pond: %s\n", err)
	}

	_, err = pond.GetNeighbors(Location{X: 2, Y: 2})
	if err == nil {
		t.Error("Did not encounter error when retrieving neighbors using bogus selector")
	}
}

/* TODO
func TestPondOrganismValue(t *testing.T) {
	expectedVal := 2
	pos := Location{X: 0, Y: 0}
	pond, err := newPond(Dimensions{Height: 1, Width: 1}, newTracker(), NeighborsAll)
	if err != nil {
		t.Fatal("Unable to create pond")
	}
	pond.setOrganismValue(pos, expectedVal)

	actualVal := pond.GetOrganismValue(pos)

	if actualVal != expectedVal {
		t.Fatalf("Retrieved actual value %d instead of expected value %d\n", actualVal, expectedVal)
	}
}
*/

func TestPondString(t *testing.T) {
	dims := Dimensions{Height: 3, Width: 3}
	pond, err := newPond(dims, newTracker(), NeighborsAll)
	if err != nil {
		t.Fatal("Unable to create pond")
	}
	if len(pond.String()) <= 0 {
		t.Error("Unexpectly retrieved empty string from pond string function")
	}
}

func TestPondEquals(t *testing.T) {
	t.Skip("whoops")
	dims := Dimensions{Height: 3, Width: 3}
	pond, err := newPond(dims, newTracker(), NeighborsAll)
	if err != nil {
		t.Fatal("Unable to create pond")
	}

	if !pond.Equals(pond) {
		t.Fatal("Pond Equals failed identity test")
	}

	pond2, err := newPond(dims, newTracker(), NeighborsAll)
	if err != nil {
		t.Fatal("Unable to create pond")
	}

	if pond.Equals(pond2) {
		t.Fatal("Pond Equals returned true with ponds having different neighbor selectors")
	}
}

/*
func testBoardNeighbors(t *testing.T, expected []Location, actual []Location) {
	// Check the results
	if len(actual) != len(expected) {
		t.Fatalf("Number of neighbors (%d) does not match expected (%d)\n", len(actual), len(expected))
	}

	// Check that all expected locations are in the actual list
	for _, expectedLoc := range expected {
		found := false
		for _, actualLoc := range actual {
			if expectedLoc.Equals(&actualLoc) {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("Did not find location %s in actual list\n", expectedLoc.String())
		}
	}

	// Check that the actual list doesn't have any unexpected locations
	for _, actualLoc := range actual {
		found := false
		for _, expectedLoc := range expected {
			if expectedLoc.Equals(&actualLoc) {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("Found location %s in actual list that was not expected\n", actualLoc.String())
		}
	}
}

func TestBoardGetOrthogonalNeighbors(t *testing.T) {
	// Build list of expected locations
	expected := make([]Location, 4)
	expected[0] = Location{X: 1, Y: 0}
	expected[1] = Location{X: 0, Y: 1}
	expected[2] = Location{X: 2, Y: 1}
	expected[3] = Location{X: 1, Y: 2}

	// Initialize a board
	dims := Dimensions{Height: 3, Width: 3}
	board, err := newBoard(dims)
	if err != nil {
		t.Fatalf("board of size %s could not be created\n", dims.String())
	}
	for i := 0; i < len(expected); i++ {
		board.SetValue(expected[i], 0)
	}

	// Retrieve neighbors
	actual := board.GetOrthogonalNeighbors(Location{X: 1, Y: 1})

	testBoardNeighbors(t, expected, actual)
}

func TestBoardGetObliqueNeighbors(t *testing.T) {
	// Build list of expected locations
	expected := make([]Location, 4)
	expected[0] = Location{X: 0, Y: 0}
	expected[1] = Location{X: 2, Y: 0}
	expected[2] = Location{X: 0, Y: 2}
	expected[3] = Location{X: 2, Y: 2}

	// Initialize a board
	dims := Dimensions{Height: 3, Width: 3}
	board, err := newBoard(dims)
	if err != nil {
		t.Fatalf("board of size %s could not be created\n", dims.String())
	}
	for i := 0; i < len(expected); i++ {
		board.SetValue(expected[i], 0)
	}

	// Retrieve neighbors
	actual := board.GetObliqueNeighbors(Location{X: 1, Y: 1})

	testBoardNeighbors(t, expected, actual)
}

func TestBoardGetAllNeighbors(t *testing.T) {
	// Build list of expected locations
	expected := make([]Location, 0)
	for i := 0; i < 4; i++ {
		switch i {
		case 0, 2:
			for j := 0; j < 3; j++ {
				expected = append(expected, Location{X: j, Y: i})
			}
		case 1:
			expected = append(expected, Location{X: 0, Y: 1})
			expected = append(expected, Location{X: 2, Y: 1})
		}
	}

	// Initialize a board
	dims := Dimensions{Height: 3, Width: 3}
	board, err := newBoard(dims)
	if err != nil {
		t.Fatalf("board of size %s could not be created\n", dims.String())
	}
	for i := 0; i < len(expected); i++ {
		board.SetValue(expected[i], 0)
	}

	// Retrieve neighbors
	actual := board.GetAllNeighbors(Location{X: 1, Y: 1})

	testBoardNeighbors(t, expected, actual)
}
*/

// vim: set foldmethod=marker: