-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest-conway.js
executable file
·141 lines (129 loc) · 5.46 KB
/
test-conway.js
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
/* jshint undef: true, node: true, esnext: true, asi: true */
/* global describe, it */
'use strict'
var assert = require('assert');
var c = require('../config/constants')
var conway = require('../lib/conway.js');
describe('conway', () => {
describe('generateEmptyWorld', () => {
it('should return a two dimentional array with the correct sizes', () => {
let cells = conway.generateEmptyWorld()
assert(cells.length === c.WIDTH)
assert((cells[0]).length === c.HEIGHT)
})
it('should have only zeros', () => {
let cells = conway.generateEmptyWorld()
let sums = cells.map((e) => e.reduce((sum, value) => sum+value, 0))
let sum = sums.reduce((sum, value) => sum+value, 0)
assert(sum === 0)
})
})
describe('generateRandomWorld', () => {
it('should return a two dimentional array with the correct sizes', () => {
let cells = conway.generateRandomWorld()
assert(cells.length === c.WIDTH)
assert((cells[0]).length === c.HEIGHT)
})
it('should return an array of 1 and 0 only', () => {
let cells = conway.generateRandomWorld()
let only1and0 = (e) => {return ((e === 1) || (e === 0))}
let areValid = cells.map((e) => e.reduce((all, elt) => only1and0(elt) && all, true))
let isValid = areValid.reduce((all, elt) => elt && all, true)
assert(isValid)
})
})
describe('numberOfNeighbours', () => {
it('should correctly count the number of neighbours for a generic cell in an empty world', () => {
let cells = conway.generateEmptyWorld()
let n = conway.numberOfNeighbours(cells, 3, 3)
assert(n === 0)
})
it('should not count itself as a neighbour', () => {
let cells = conway.generateEmptyWorld()
cells[3][3] = 1
let n = conway.numberOfNeighbours(cells, 3, 3)
assert(n === 0)
})
it('should correctly count the number of neighbours for a generic cell with 4 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[2][3] = 1
cells[4][3] = 1
cells[3][2] = 1
cells[3][4] = 1
let n = conway.numberOfNeighbours(cells, 3, 3)
assert(n === 4)
})
it('should correctly count the number of neighbours for a low border cell with no neighbours', () => {
let cells = conway.generateEmptyWorld()
let n = conway.numberOfNeighbours(cells, 0, 0)
assert(n === 0)
})
it('should correctly count the number of neighbours for a low border cell with 3 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[0][1] = 1
cells[1][0] = 1
cells[1][1] = 1
let n = conway.numberOfNeighbours(cells, 0, 0)
assert(n === 3)
})
it('should correctly count the number of neighbours for a high border cell with no neighbours', () => {
let cells = conway.generateEmptyWorld()
let n = conway.numberOfNeighbours(cells, c.WIDTH, c.HEIGHT)
assert(n === 0)
})
it('should correctly count the number of neighbours for a high border cell with 3 neighbours', () => {
let h = c.HEIGHT
let w = c.WIDTH
let cells = conway.generateEmptyWorld()
cells[h-2][w-1] = 1
cells[h-1][w-2] = 1
cells[h-2][w-2] = 1
let n = conway.numberOfNeighbours(cells, h-1, w-1)
assert(n === 3)
})
})
describe('newStatus', () => {
it('should check that life can appear if exactly 3 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[2][3] = 1
cells[4][3] = 1
cells[3][2] = 1
let newStatus = conway.newStatus(cells, 3, 3)
assert(newStatus === 1)
})
it('should check that a cell will die if more than 3 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[3][3] = 1 // The cell we are killing
cells[2][3] = 1
cells[4][3] = 1
cells[3][2] = 1
cells[3][4] = 1
let newStatus = conway.newStatus(cells, 3, 3)
assert(newStatus === 0)
})
it('should check that a cell will die if less than 2 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[3][3] = 1 // The cell we are killing
cells[2][3] = 1
let newStatus = conway.newStatus(cells, 3, 3)
assert(newStatus === 0)
})
it('should check that a cell can stay alive if it has 2 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[3][3] = 1 // The cell we are keeping alive
cells[2][3] = 1
cells[4][3] = 1
let newStatus = conway.newStatus(cells, 3, 3)
assert(newStatus === 1)
})
it('should check that a cell can stay alive if it has 3 neighbours', () => {
let cells = conway.generateEmptyWorld()
cells[3][3] = 1 // The cell we are keeping alive
cells[2][3] = 1
cells[4][3] = 1
cells[3][2] = 1
let newStatus = conway.newStatus(cells, 3, 3)
assert(newStatus === 1)
})
})
});