Skip to content

Instantly share code, notes, and snippets.

@dshook
Created February 4, 2015 17:57
Show Gist options
  • Select an option

  • Save dshook/8b97a3ea09155a090200 to your computer and use it in GitHub Desktop.

Select an option

Save dshook/8b97a3ea09155a090200 to your computer and use it in GitHub Desktop.
//For demo purposes ofc
//
//globals all over the place and no test harness
//library makes this a bit messy but hope you should get the idea
var TEST_SIZE = 10;
var testBoard = initState(TEST_SIZE);
assertEquals(testBoard.length, TEST_SIZE, 'Correct Number of Rows');
assertEquals(testBoard[0].length, TEST_SIZE, 'Correct Number of Columns');
//init board with some state to test
//a cross pattern in the middle in this case
var crossPattern = [
{x: 4, y: 4},
{x: 4, y: 3},
{x: 4, y: 5},
{x: 3, y: 4},
{x: 5, y: 4},
];
var dotPattern = [
{x: 4, y: 4},
];
initPattern(testBoard, crossPattern);
//test board utils
assertEquals(getSurrounding(testBoard,4,4).length, 8, 'There are 8 surrounding cells in the middle');
assertEquals(getSurrounding(testBoard,0,0).length, 3, 'There are 3 surrounding cells in the corner');
assertEquals(countSurrounding(getSurrounding(testBoard,4,4)), 4, 'There are 4 active cells around the middle');
assertEquals(countSurrounding(getSurrounding(testBoard,0,0)), 0, 'There are 0 active cells around the corner');
//for the game rule tests a test harness would help with the setup/teardown before and after each of these tests
//so one test doesn't interfere with any of the others
testBoard = initState(TEST_SIZE);
initPattern(testBoard, dotPattern);
testBoard.forEach(function(row){
row.forEach(function(cell){
underPop(board, cell);
});
});
updateStep(testBoard);
assertEquals(countSurrounding(getAllCells(testBoard)), 0, 'Underpop killed a cell');
//live test
testBoard = initState(TEST_SIZE);
initPattern(testBoard, crossPattern);
testBoard.forEach(function(row){
row.forEach(function(cell){
live(board, cell);
});
});
updateStep(testBoard);
assertEquals(testBoard[3][4].state, ALIVE, 'Cell with 3 neighbors alive stayed alive');
function assertEquals(a, b, message){
if(a === b){
console.log(message + ' passed');
}else{
console.log(message + ' failed. a=' + a + ' b='+ b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment