~hokiegeek/life

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

import "testing"

func TestTrackerSetTest(t *testing.T) {
	tracker := newTracker()

	// Ok, set the value
	loc := Location{X: 42, Y: 24}
	tracker.Set(loc)

	// Test that it exists
	if !tracker.Test(loc) {
		t.Fatal("Added location unexpectedly tested false")
	}
}

func TestTrackerTestError(t *testing.T) {
	tracker := newTracker()

	if tracker.Test(Location{X: 0, Y: 0}) {
		t.Error("Unexpectedly tested true a location that does not exist in structure")
	}
}

func TestTrackerRemove(t *testing.T) {
	tracker := newTracker()

	// Ok, set the value
	loc := Location{X: 42, Y: 24}
	tracker.Set(loc)

	tracker.Remove(loc)

	// Test that it exists
	if tracker.Test(loc) {
		t.Fatal("Unexpectedly tested true a location that was removed")
	}
}

func TestTrackerRemoveError(t *testing.T) {
	tracker := newTracker()

	// Ok, set the value
	loc := Location{X: 42, Y: 24}
	tracker.Set(loc)

	loc = Location{X: 0, Y: 0}
	if tracker.Remove(loc) {
		t.Fatal("Unexpectedly able to remove a location that doesn't exist")
	}
}

func TestTrackerGetAll(t *testing.T) {
	tracker := newTracker()

	// Create the expected values
	expectedLocations := make([]Location, 3)
	expectedLocations[0] = Location{X: 42, Y: 42}
	expectedLocations[1] = Location{X: 11, Y: 11}
	expectedLocations[2] = Location{X: 12, Y: 34}

	// Ok, set the values
	for _, l := range expectedLocations {
		tracker.Set(l)
	}

	actualLocations := tracker.GetAll()

	if len(actualLocations) != len(expectedLocations) {
		t.Fatalf("Received %d locations when I expected %d\n", len(actualLocations), len(expectedLocations))
	}

	// Check that all returned locations were expected
	for _, actual := range actualLocations {
		found := false
		for _, expected := range expectedLocations {
			if actual.Equals(&expected) {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("Returned location %s was not in list of test locations\n", actual.String())
			break
		}
	}

	// Check that all expected locations were returned
	for _, expected := range expectedLocations {
		found := false
		for _, actual := range actualLocations {
			if expected.Equals(&actual) {
				found = true
				break
			}
		}
		if !found {
			t.Fatalf("Expected location %s was not in list of returned locations\n", expected.String())
			break
		}
	}
}

func TestTrackerCount(t *testing.T) {
	tracker := newTracker()

	// Create the expected values
	expectedLocations := make([]Location, 3)
	expectedLocations[0] = Location{X: 42, Y: 42}
	expectedLocations[1] = Location{X: 11, Y: 11}
	expectedLocations[2] = Location{X: 12, Y: 34}

	// Ok, set the values
	for _, l := range expectedLocations {
		tracker.Set(l)
	}

	// Test the counter
	expectedCount := len(expectedLocations)
	count := tracker.Count()
	if count != expectedCount {
		t.Fatalf("Retrieved count of %d instead of expected %d\n", count, expectedCount)
	}

	// Now remove a location and try again
	tracker.Remove(expectedLocations[2])
	expectedCount--
	count = tracker.Count()
	if count != expectedCount {
		t.Fatalf("Retrieved count of %d instead of expected %d after remove a location\n", count, expectedCount)
	}
}

// vim: set foldmethod=marker: