-
Notifications
You must be signed in to change notification settings - Fork 1
/
gof_tests.pl
172 lines (143 loc) · 5.41 KB
/
gof_tests.pl
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
% prolog
:- load_files(gof).
:- use_module(library(plunit)).
:- begin_tests(gof_tests).
test(dead_stays_dead) :-
cell_new_state(dead_cell, 0, dead_cell),
cell_new_state(dead_cell, 1, dead_cell),
cell_new_state(dead_cell, 4, dead_cell),
cell_new_state(dead_cell, 4, dead_cell),
cell_new_state(dead_cell, 5, dead_cell),
cell_new_state(dead_cell, 6, dead_cell),
cell_new_state(dead_cell, 7, dead_cell),
cell_new_state(dead_cell, 8, dead_cell).
test(dead_3_neighbour_lives) :-
cell_new_state(dead_cell, 3, live_cell).
test(live_cell_dies) :-
cell_new_state(live_cell, 0, dead_cell),
cell_new_state(live_cell, 1, dead_cell),
cell_new_state(live_cell, 4, dead_cell),
cell_new_state(live_cell, 5, dead_cell),
cell_new_state(live_cell, 6, dead_cell),
cell_new_state(live_cell, 7, dead_cell),
cell_new_state(live_cell, 8, dead_cell).
test(live_cell_stays_alive) :-
cell_new_state(live_cell, 2, live_cell),
cell_new_state(live_cell, 3, live_cell).
test(cell_north) :-
north(cell(0, 0), cell(0, -1)),
north(cell(0, -1), cell(0, -2)),
north(cell(1, 0), cell(1, -1)).
test(cell_south) :-
south(cell(0, 0), cell(0, 1)),
south(cell(0, -1), cell(0, 0)),
south(cell(1, 0), cell(1, 1)).
test(cell_west) :-
west(cell(0, 0), cell(-1, 0)),
west(cell(1, 0), cell(0, 0)),
west(cell(1, 1), cell(0, 1)).
test(cell_east) :-
east(cell(0, 0), cell(1, 0)),
east(cell(-1, 0), cell(0, 0)),
east(cell(-1, 1), cell(0, 1)).
test(cell_south_east) :-
south_east(cell(0, 0), cell(1, 1)),
south_east(cell(1, 1), cell(2, 2)).
test(cell_south_west) :-
south_west(cell(0, 0), cell(-1, 1)),
south_west(cell(1, 1), cell(0, 2)).
test(cell_north_west) :-
north_west(cell(0, 0), cell(-1, -1)),
north_west(cell(1, 1), cell(0, 0)).
test(cell_north_east) :-
north_east(cell(0, 0), cell(1, -1)),
north_east(cell(1, 1), cell(2, 0)).
test(cell_neighbours) :-
neighbours(cell(0, 0), Neighbours),
length(Neighbours, 8),
member(cell(-1, -1), Neighbours),
member(cell(0, -1), Neighbours),
member(cell(1, -1), Neighbours),
member(cell(1, 0), Neighbours),
member(cell(1, 1), Neighbours),
member(cell(0, 1), Neighbours),
member(cell(-1, 1) ,Neighbours),
member(cell(-1, 0), Neighbours).
test(add_cell_to_world) :-
empty_world(Empty),
world_add_cell(Empty, cell(0, 0), world(LiveCells, DeadCells)),
member(cell(0, 0), LiveCells),
length(LiveCells, 1),
length(DeadCells, 8),
member(cell(-1, -1), DeadCells),
member(cell(0, -1), DeadCells),
member(cell(1, -1), DeadCells),
member(cell(1, 0), DeadCells),
member(cell(1, 1), DeadCells),
member(cell(0, 1), DeadCells),
member(cell(-1, 1), DeadCells),
member(cell(-1, 0), DeadCells).
test(add_two_distant_cells) :-
empty_world(Empty),
world_add_cells(Empty, [cell(3, 6), cell(7, 10)], world(LiveAfter2ndInsertion, DeadAfter2ndInsertion)),
member(cell(3, 6), LiveAfter2ndInsertion),
member(cell(7, 10), LiveAfter2ndInsertion),
length(LiveAfter2ndInsertion, 2),
length(DeadAfter2ndInsertion, 16).
test(add_multiple_cells) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 7), cell(3, 6)], world(LiveAfter2ndInsertion, DeadAfter2ndInsertion)),
member(cell(3, 6), LiveAfter2ndInsertion),
member(cell(4, 7), LiveAfter2ndInsertion),
length(LiveAfter2ndInsertion, 2),
length(DeadAfter2ndInsertion, 12).
test(count_two_live_neighbours_of_live_cell) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 7), cell(3, 6), cell(5, 6)], NewWorld),
live_neighbours(NewWorld, cell(4, 7), LiveNeighbours),
length(LiveNeighbours, 2),
member(cell(5, 6), LiveNeighbours),
member(cell(3, 6), LiveNeighbours).
test(count_three_live_neighbours_of_dead_cell) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 7), cell(3, 6), cell(5, 6)], NewWorld),
live_neighbours(NewWorld, cell(4, 6), LiveNeighbours),
length(LiveNeighbours, 3),
member(cell(5, 6), LiveNeighbours),
member(cell(4, 7), LiveNeighbours),
member(cell(3, 6), LiveNeighbours).
test(evolve_single_cell_results_empty_world) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 7)], NewWorld),
evolve(NewWorld, EvolvedWorld),
empty_world(EvolvedWorld).
test(evolve_2_square_is_stable) :-
empty_world(Empty),
world_add_cells(Empty, [cell(1, 3), cell(2, 3), cell(1, 4), cell(2, 4)], NewWorld),
evolve(NewWorld, world(LiveCells, DeadCells)),
length(LiveCells, 4),
length(DeadCells, 12).
test(evolve_tree_inline_will_rotate) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 1), cell(4, 2), cell(4, 3)], NewWorld),
evolve(NewWorld, world(LiveCells, DeadCells)),
length(LiveCells, 3),
length(DeadCells, 12).
test(prints_world_one_char) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 1), cell(4, 2), cell(4, 3)], NewWorld),
world_to_string(NewWorld, world_window(3, 1, 1, 1), S), !, S = " \n".
test(prints_world_two_chars) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 1), cell(4, 2), cell(4, 3)], NewWorld),
world_to_string(NewWorld, world_window(3, 1, 1, 2), S), !, S = " 0\n".
test(prints_world_three_chars) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 1), cell(4, 2), cell(4, 3)], NewWorld),
evolve(NewWorld, EvolveWorld),
world_to_string(EvolveWorld, world_window(3, 2, 1, 3), S), !, S = "000\n".
test(prints_9x9_window) :-
empty_world(Empty),
world_add_cells(Empty, [cell(4, 1), cell(4, 2), cell(4, 3)], NewWorld),
world_to_string(NewWorld, world_window(3, 1, 3, 3), S), !, S = " 0 \n 0 \n 0 \n".
:- end_tests(gof_tests).