Commit 5f89d25

mo khan <mo@mokhan.ca>
2014-10-27 03:11:44
add spec for overcrowding.
1 parent 1f4e69b
Changed files (2)
gol.c
@@ -48,7 +48,7 @@ char* evolve(char* world) {
   new_world[number_of_cells] = 0;
   for (int i = 0; i < number_of_cells; i++) {
     int neighbours = living_neighbours_for(world, i);
-    new_world[i] = (neighbours >= 2) ? 'x' : ' ';
+    new_world[i] = (neighbours >= 2 && neighbours <= 3) ? 'x' : ' ';
   }
   return new_world;
 }
gol_test.c
@@ -48,6 +48,18 @@ static char* any_live_cell_with_three_live_neighbours_lives_on_to_the_next_gener
   return 0;
 }
 
+static char* any_live_cell_with_more_than_three_live_neighbours_dies_as_if_by_overcrowding() {
+  printf("TEST: %s\n", __func__);
+  char world[3][3] = {
+    { ' ', 'x', ' ' },
+    { 'x', 'x', 'x' },
+    { ' ', 'x', ' ' },
+  };
+  char* new_world = evolve(*world);
+  assert_equal(new_world[4] == ' ', "dies because has 4 neighbors");
+  return 0;
+}
+
 static char* it_returns_the_correct_number_of_living_neighbors() {
   printf("TEST: %s\n", __func__);
   char world[3][3] = {
@@ -73,6 +85,7 @@ static char* all_tests() {
   run_test(any_live_cell_with_fewer_than_two_live_neighbours_dies_as_if_caused_by_under_population);
   run_test(any_live_cell_with_two_live_neighbours_lives_on_to_the_next_generation);
   run_test(any_live_cell_with_three_live_neighbours_lives_on_to_the_next_generation);
+  run_test(any_live_cell_with_more_than_three_live_neighbours_dies_as_if_by_overcrowding);
   return 0;
 }