~hokiegeek/life

ref: c41a449353d445a06ec7334b540afb793d577cd8 life/life_test.go -rw-r--r-- 3.4 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
package life

import (
	"testing"
	"time"
)

func TestLifeCreation(t *testing.T) {
	dims := Dimensions{Height: 3, Width: 3}
	strategy, err := New(
		dims,
		NeighborsAll,
		Blinkers,
		ConwayTester(),
		SimultaneousProcessor)
	if err != nil {
		t.Fatalf("Unable to create strategy: %s\n", err)
	}

	expected, _ := newPond(Dimensions{Height: 3, Width: 3}, newTracker(), NeighborsAll)
	expected.SetOrganisms([]Location{Location{X: 0, Y: 1}, Location{X: 1, Y: 1}, Location{X: 2, Y: 1}})

	if !strategy.pond.Equals(expected) {
		t.Fatalf("Actual board\n%s\ndoes not match expected\n%s\n", strategy.pond.String(), expected.String())
	}
}

func TestLifeProcess(t *testing.T) {
	dims := Dimensions{Height: 3, Width: 3}
	strategy, err := New(
		dims,
		NeighborsAll,
		Blinkers,
		ConwayTester(),
		SimultaneousProcessor)
	if err != nil {
		t.Fatalf("Unable to create strategy: %s\n", err)
	}

	seededpond, err := strategy.pond.Clone()
	if err != nil {
		t.Fatalf("Unable to clone pond: %s\n", err)
	}

	strategy.process()

	if seededpond.Equals(strategy.pond) {
		t.Fatal("pond did not change after processing")
	}

	// Check statistics
	if strategy.Generations != 1 {
		t.Errorf("Tracked %d generations when there should only be one\n", strategy.Generations)
	}
}

func TestLifeStart(t *testing.T) {
	t.Skip("whoops")
	dims := Dimensions{Height: 3, Width: 3}
	strategy, err := New(
		dims,
		NeighborsAll,
		func(dimensions Dimensions, offset Location) []Location {
			return Random(dimensions, offset, 85)
		},
		ConwayTester(),
		SimultaneousProcessor)
	if err != nil {
		t.Fatalf("Unable to create strategy: %s\n", err)
	}

	seededpond, err := strategy.pond.Clone()
	if err != nil {
		t.Fatalf("Unable to clone pond: %s\n", err)
	}

	stop := strategy.Start(nil)

	// t.Log(strategy.String())

	time.Sleep(time.Millisecond * 10)
	stop()
	// t.Log(strategy.String())

	processedpond := strategy.pond

	if seededpond.Equals(processedpond) {
		t.Fatal("pond did not change after processing")
	}

	// Check statistics
	if strategy.Generations < 2 {
		t.Errorf("Tracked %d generations when there should be two or more\n", strategy.Generations)
	}
}

func TestLifeGeneration(t *testing.T) {
	dims := Dimensions{Height: 3, Width: 3}
	strategy, err := New(
		dims,
		NeighborsAll,
		Blinkers,
		ConwayTester(),
		SimultaneousProcessor)
	if err != nil {
		t.Fatalf("Unable to create strategy: %s\n", err)
	}

	expectedNumLiving := 3
	expectedGen := 31
	gen := strategy.Generation(expectedGen)

	if gen.Num != expectedGen {
		t.Errorf("Retrieved %d generations instead of %d\n", gen.Num, expectedGen)
	}

	if len(gen.Living) != expectedNumLiving {
		t.Fatalf("Retrieved %d living organisms instead of %d\n", len(gen.Living), expectedNumLiving)
	}
}

func TestLifeString(t *testing.T) {
	dims := Dimensions{Height: 3, Width: 3}
	strategy, err := New(
		dims,
		NeighborsAll,
		Blinkers,
		ConwayTester(),
		SimultaneousProcessor)
	if err != nil {
		t.Fatalf("Unable to create strategy: %s\n", err)
	}

	if len(strategy.String()) <= 0 {
		t.Error("String function unexpectly returned an empty string")
	}
}

func TestLifeDimensions(t *testing.T) {
	dims := Dimensions{Height: 3, Width: 3}
	life, err := New(
		dims,
		NeighborsAll,
		Blinkers,
		ConwayTester(),
		SimultaneousProcessor)
	if err != nil {
		t.Fatalf("Unable to create strategy: %s\n", err)
	}

	retrievedDims := life.Dimensions()

	if !retrievedDims.Equals(&dims) {
		t.Error("Retrieved dimensions did not match expected dimensions")
	}
}

// vim: set foldmethod=marker: