Life: tests: reduce copypaste

This commit is contained in:
He4eT 2019-09-24 00:46:55 +05:00
commit d5c5a860b6

View file

@ -6,37 +6,50 @@ import {
lifeStep lifeStep
} from '../src/life' } from '../src/life'
test('Count neighbours', t => { /* countNeighbours */
const grid = [
[0, 0, 0],
[0, 0, 1],
[0, 1, 0]]
const cn = countNeighbours(grid) const grid = [
t.deepEqual(cn([0, 0]), [0, 0]) [0, 0, 0],
t.deepEqual(cn([0, 1]), [1, 0]) [0, 0, 1],
t.deepEqual(cn([0, 2]), [1, 0]) [0, 1, 0]]
t.deepEqual(cn([1, 1]), [2, 0])
t.deepEqual(cn([2, 2]), [2, 0])
t.deepEqual(cn([1, 2]), [1, 1])
})
test('Dead or alive: from 0 to...', t => { ;[
t.is(deadOrAlive([0, 0]), 0) [[0, 0], [0, 0]],
t.is(deadOrAlive([2, 0]), 0) [[0, 1], [1, 0]],
t.is(deadOrAlive([3, 0]), 1) [[0, 2], [1, 0]],
t.is(deadOrAlive([4, 0]), 0) [[1, 1], [2, 0]],
t.is(deadOrAlive([8, 0]), 0) [[2, 2], [2, 0]],
}) [[1, 2], [1, 1]]
].forEach(([params, exp]) =>
test('Count neighbours: ' + params, t =>
t.deepEqual(
countNeighbours(grid)(params),
exp)))
test('Dead or alive: from 1 to...', t => { /* deadOrAlive */
t.is(deadOrAlive([0, 1]), 0)
t.is(deadOrAlive([1, 1]), 0) ;[
t.is(deadOrAlive([2, 1]), 1) [[0, 0], 0],
t.is(deadOrAlive([3, 1]), 1) [[2, 0], 0],
t.is(deadOrAlive([4, 1]), 0) [[3, 0], 1],
t.is(deadOrAlive([5, 1]), 0) [[4, 0], 0],
}) [[8, 0], 0]
].forEach(([params, exp]) =>
test('Dead or alive: from 0: ' + params, t =>
t.is(deadOrAlive(params), exp)))
;[
[[0, 1], 0],
[[1, 1], 0],
[[2, 1], 1],
[[3, 1], 1],
[[4, 1], 0],
[[5, 1], 0]
].forEach(([params, exp]) =>
test('Dead or alive: from 1: ' + params, t =>
t.is(deadOrAlive(params), exp)))
/* lifeStep */
test('Life step: block', t => { test('Life step: block', t => {
const block = [ const block = [