|
| 1 | +// |
| 2 | +// CodingDojo18Tests.m |
| 3 | +// CodingDojo18Tests |
| 4 | +// |
| 5 | +// Created by Marián Černý on 31.07.14. |
| 6 | +// Copyright (c) 2014 Pematon, s.r.o. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | + |
| 11 | +#import "DLWorld.h" |
| 12 | +#import "DLCell.h" |
| 13 | + |
| 14 | +@interface CodingDojo18Tests : XCTestCase |
| 15 | + |
| 16 | +@end |
| 17 | + |
| 18 | +@implementation CodingDojo18Tests |
| 19 | + |
| 20 | +// Any live cell with fewer than two live neighbours dies, as if caused by under-population. |
| 21 | +// Any live cell with two or three live neighbours lives on to the next generation. |
| 22 | +// Any live cell with more than three live neighbours dies, as if by overcrowding. |
| 23 | +// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. |
| 24 | + |
| 25 | + |
| 26 | +- (void)cellThatIsAlive:(BOOL)alive withNeighbours:(NSInteger)neighbours shouldDie:(BOOL)shouldDie |
| 27 | +{ |
| 28 | + DLCell *cell = [[DLCell alloc] initWithAlive:alive]; |
| 29 | + XCTAssertEqual([cell shouldBeDeadInNextGenerationWithNeighboursCount:neighbours], shouldDie, @"Alive cell %d with %d neighbours should die %d", (int)alive, (int)neighbours, (int)shouldDie); |
| 30 | +} |
| 31 | + |
| 32 | +- (void)aliveCellWithNeighbours:(NSInteger)neighbours shouldDie:(BOOL)shouldDie |
| 33 | +{ |
| 34 | + [self cellThatIsAlive:YES withNeighbours:neighbours shouldDie:shouldDie]; |
| 35 | +} |
| 36 | + |
| 37 | +- (void)deadCellWithNeighbours:(NSInteger)neighbours shouldDie:(BOOL)shouldDie |
| 38 | +{ |
| 39 | + [self cellThatIsAlive:NO withNeighbours:neighbours shouldDie:shouldDie]; |
| 40 | +} |
| 41 | + |
| 42 | +// Any live cell with fewer than two live neighbours dies, as if caused by under-population. |
| 43 | +- (void)testUnderPopulation |
| 44 | +{ |
| 45 | + [self aliveCellWithNeighbours:0 shouldDie:YES]; |
| 46 | + [self aliveCellWithNeighbours:1 shouldDie:YES]; |
| 47 | +} |
| 48 | + |
| 49 | +// Any live cell with two or three live neighbours lives on to the next generation. |
| 50 | +- (void)testSurvival |
| 51 | +{ |
| 52 | + [self aliveCellWithNeighbours:2 shouldDie:NO]; |
| 53 | + [self aliveCellWithNeighbours:3 shouldDie:NO]; |
| 54 | +} |
| 55 | + |
| 56 | +// Any live cell with more than three live neighbours dies, as if by overcrowding. |
| 57 | +- (void)testOvercrowding |
| 58 | +{ |
| 59 | + [self aliveCellWithNeighbours:4 shouldDie:YES]; |
| 60 | + [self aliveCellWithNeighbours:5 shouldDie:YES]; |
| 61 | + [self aliveCellWithNeighbours:6 shouldDie:YES]; |
| 62 | + [self aliveCellWithNeighbours:7 shouldDie:YES]; |
| 63 | + [self aliveCellWithNeighbours:8 shouldDie:YES]; |
| 64 | +} |
| 65 | + |
| 66 | +// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction. |
| 67 | +- (void)testReproduction |
| 68 | +{ |
| 69 | + [self deadCellWithNeighbours:3 shouldDie:NO]; |
| 70 | + |
| 71 | + [self deadCellWithNeighbours:2 shouldDie:YES]; |
| 72 | + [self deadCellWithNeighbours:4 shouldDie:YES]; |
| 73 | +} |
| 74 | + |
| 75 | +- (void)testCanCreateWorld |
| 76 | +{ |
| 77 | + DLWorld *world = [[DLWorld alloc] init]; |
| 78 | + |
| 79 | + XCTAssertNotNil(world, @"there is world"); |
| 80 | +} |
| 81 | + |
| 82 | +- (void)testWorldHasDeadCellsByDefault |
| 83 | +{ |
| 84 | + DLWorld *world = [[DLWorld alloc] init]; |
| 85 | + |
| 86 | + DLCell *cell = [world cellAtX:0 andY:0]; |
| 87 | + |
| 88 | + XCTAssertFalse(cell.alive, @"cell should be dead"); |
| 89 | +} |
| 90 | + |
| 91 | +@end |
0 commit comments