main
1#include <stdio.h>
2#include <stdlib.h>
3#include "main.h"
4
5void test_world_create() {
6 int width = 3;
7 int height = 3;
8 World *world = world_create(width, height);
9
10 assert(width == world->width);
11 assert(height == world->height);
12}
13
14void test_world_create_should_create_all_cells(){
15 World *world = world_create(1, 1);
16 assert(false == world_cell_at(world, 0)->alive);
17}
18
19void any_live_cell_with_fewer_than_two_live_neighbours_dies_as_if_caused_by_under_population() {
20 /*{ 'x', ' ', ' ' },*/
21 /*{ ' ', ' ', ' ' },*/
22 /*{ ' ', ' ', ' ' },*/
23 World *world = world_create(3, 3);
24 cell_change_life(world_cell_at(world, 0), true);
25
26 World *new_world = world_evolve(world);
27 assert(false == world_cell_at(new_world, 0)->alive);
28 assert(false == world_cell_at(new_world, 8)->alive);
29}
30
31void any_live_cell_with_two_live_neighbours_lives_on_to_the_next_generation() {
32 /*{ 'x', 'x', 'x' },*/
33 /*{ ' ', ' ', ' ' },*/
34 /*{ ' ', ' ', ' ' },*/
35 World *world = world_create(3, 3);
36 cell_change_life(world_cell_at(world, 0), true);
37 cell_change_life(world_cell_at(world, 1), true);
38 cell_change_life(world_cell_at(world, 2), true);
39
40 World* new_world = world_evolve(world);
41 assert(true == world_cell_at(new_world, 1)->alive);
42}
43
44void any_live_cell_with_three_live_neighbours_lives_on_to_the_next_generation() {
45 /*{ 'x', ' ', 'x' },*/
46 /*{ ' ', 'x', ' ' },*/
47 /*{ ' ', ' ', ' ' },*/
48 World *world = world_create(3, 3);
49 cell_change_life(world_cell_at(world, 0), true);
50 cell_change_life(world_cell_at(world, 2), true);
51 cell_change_life(world_cell_at(world, 4), true);
52
53 World* new_world = world_evolve(world);
54 assert(true == world_cell_at(new_world, 1)->alive);
55}
56
57void any_live_cell_with_more_than_three_live_neighbours_dies_as_if_by_overcrowding() {
58 /*{ ' ', 'x', ' ' },*/
59 /*{ 'x', 'x', 'x' },*/
60 /*{ ' ', 'x', ' ' },*/
61 World *world = world_create(3, 3);
62 cell_change_life(world_cell_at(world, 1), true);
63 cell_change_life(world_cell_at(world, 3), true);
64 cell_change_life(world_cell_at(world, 4), true);
65 cell_change_life(world_cell_at(world, 5), true);
66 cell_change_life(world_cell_at(world, 7), true);
67
68 assert(5 == world_neighbours(world, 0));
69 assert(4 == world_neighbours(world, 1));
70 assert(4 == world_neighbours(world, 4));
71
72 World* new_world = world_evolve(world);
73 assert(false == world_cell_at(new_world, 4)->alive);
74}
75
76void any_dead_cell_with_exactly_three_live_neighbours_becomes_a_live_cell_as_if_by_reproduction(){
77 /*{ ' ', 'x', ' ' },*/
78 /*{ 'x', ' ', 'x' },*/
79 /*{ ' ', ' ', ' ' },*/
80 World *world = world_create(3, 3);
81 cell_change_life(world_cell_at(world, 1), true);
82 cell_change_life(world_cell_at(world, 3), true);
83 cell_change_life(world_cell_at(world, 5), true);
84
85 World* new_world = world_evolve(world);
86 assert(true == world_cell_at(new_world, 4)->alive);
87}
88
89void it_returns_the_correct_number_of_living_neighbors() {
90 /*{ 'x', ' ', 'x' },*/
91 /*{ ' ', ' ', ' ' },*/
92 /*{ 'x', ' ', 'x' },*/
93 World *world = world_create(3, 3);
94 cell_change_life(world_cell_at(world, 0), true);
95 cell_change_life(world_cell_at(world, 2), true);
96 cell_change_life(world_cell_at(world, 6), true);
97 cell_change_life(world_cell_at(world, 8), true);
98
99 assert(world_neighbours(world, 0) == 3);
100 assert(world_neighbours(world, 1) == 4);
101 assert(world_neighbours(world, 2) == 3);
102 assert(world_neighbours(world, 3) == 4);
103 assert(world_neighbours(world, 4) == 4);
104 assert(world_neighbours(world, 5) == 4);
105 assert(world_neighbours(world, 6) == 3);
106 assert(world_neighbours(world, 7) == 4);
107 assert(world_neighbours(world, 8) == 3);
108}
109
110void world_tests() {
111 run_test(test_world_create);
112 run_test(test_world_create_should_create_all_cells);
113 run_test(any_live_cell_with_fewer_than_two_live_neighbours_dies_as_if_caused_by_under_population);
114 run_test(any_live_cell_with_two_live_neighbours_lives_on_to_the_next_generation);
115 run_test(any_live_cell_with_three_live_neighbours_lives_on_to_the_next_generation);
116 run_test(any_live_cell_with_more_than_three_live_neighbours_dies_as_if_by_overcrowding);
117 run_test(any_dead_cell_with_exactly_three_live_neighbours_becomes_a_live_cell_as_if_by_reproduction);
118 run_test(it_returns_the_correct_number_of_living_neighbors);
119}
120