Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cellular-Automata/ConwaysGameOfLife.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ The Game of Life is a cellular automaton devised by the British mathematician Jo

/**
* Generates the next generation for a given state of Conway's Game of Life.
*
* The function is deterministic and has no external dependencies (no time,
* randomness, I/O or shared module state). It reads `cells` but never
* modifies it, and always builds a brand-new grid, so the same input always
* gives the same output, regardless of earlier calls.
* Cells outside the grid are treated as dead (the grid does not wrap).
*
* @param {number[][]} cells rectangular grid of 0 (dead) and 1 (alive)
* @returns {number[][]} a new grid of the same shape with the next generation
* @example newGeneration([[1]]) // [[0]] (a lone cell dies)
*/
export function newGeneration(cells) {
const nextGeneration = []
Expand Down
79 changes: 79 additions & 0 deletions Cellular-Automata/test/ConwaysGameOfLife.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,82 @@ describe('newGeneration', () => {
])
})
})

// Repeatability and input-isolation regressions for newGeneration
// (ConwaysGameOfLife.js line 24). The function has no external dependency
// (no time, randomness, I/O or module state), so the checks are: identical
// output for identical input, no mutation of the input, and no leakage
// between calls. Expected grids were worked out by hand from rules 1-3
// (lines 4-6) before running the code.
describe('newGeneration is deterministic and isolates its input', () => {
// Fresh copies for every use, so no test can affect another.
const glider = () => [
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]
// Live (1,0): 3 neighbours, born. (1,2): 3, survives. (2,1): 3, survives.
// (2,2): 2, survives. (3,1): 3, born. (0,1) and (2,0): 1 each, die.
const gliderNext = [
[0, 0, 0, 0, 0],
[1, 0, 1, 0, 0],
[0, 1, 1, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 0, 0, 0]
]

it('smallest valid input: an empty grid gives an empty grid', () => {
expect(newGeneration([])).toEqual([])
})

it('smallest non-empty grid: a lone live cell dies', () => {
// Rule 3 (line 6): a live cell with 0 neighbours dies.
expect(newGeneration([[1]])).toEqual([[0]])
})

it('ordinary input: one glider step', () => {
expect(newGeneration(glider())).toEqual(gliderNext)
})

it('does not modify the input grid', () => {
const input = glider()
newGeneration(input)
expect(input).toEqual(glider())
})

it('accepts a deeply frozen grid, so it never writes to its input', () => {
// ES modules run in strict mode: writing to a frozen array would throw.
const frozen = Object.freeze(glider().map((row) => Object.freeze(row)))
expect(newGeneration(frozen)).toEqual(gliderNext)
})

it('returns a new grid that shares no rows with the input', () => {
const input = glider()
const output = newGeneration(input)
expect(output).not.toBe(input)
output.forEach((row, i) => expect(row).not.toBe(input[i]))
})

it('same input twice in fresh state gives the same result', () => {
expect(newGeneration(glider())).toEqual(gliderNext)
expect(newGeneration(glider())).toEqual(gliderNext)
})

it('an earlier call with another input cannot change a later result', () => {
const first = newGeneration(glider())
const other = newGeneration([
[1, 1],
[1, 1]
])
expect(other).toEqual([
[1, 1],
[1, 1]
]) // block still life: every cell has 3 neighbours
other[0][0] = 0 // tamper with an earlier result
const later = newGeneration(glider())
expect(later).toEqual(gliderNext)
expect(later).toEqual(first)
})
})
77 changes: 77 additions & 0 deletions work/R50-T005/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# R50-T005 — Repeatable verification of `newGeneration`

Source: `Cellular-Automata/ConwaysGameOfLife.js`, `newGeneration` (line 24 after this change; line 14 at pinned commit `5c39e87`).

## Recorded contract (before any change)

| Aspect | Observation |
| --------------------- | -------------------------------------------------------------------------------------------- |
| Input | `cells`: rectangular grid of `0`/`1`. Cells outside the grid count as dead (no wrap-around). |
| Result | a **new** grid of the same shape (lines 25, 27 create fresh arrays; line 53 returns it). |
| Mutation | none — `cells` is only read (e.g. line 43). |
| External dependencies | **none**. No `Date`, `Math.random`, imports, network, I/O, console or module-level state. |

Because the function is already pure, no dependency seam was introduced (none is needed, so no new
parameter and no change for existing callers). The code change is a JSDoc block stating the
determinism / isolation contract, plus regression tests that lock it down.

## Fixture (expected grids worked out by hand from rules 1–3, lines 4–6)

| Case | Input | Expected |
| ----------------------- | ---------------------------- | -------------------------------------------- |
| Smallest valid input | `[]` | `[]` |
| Smallest non-empty grid | `[[1]]` | `[[0]]` (0 neighbours → dies) |
| Ordinary input (glider) | `.#... / ..#.. / ###.. / … ` | `..... / #.#.. / .##.. / .#... / .....` |
| Other input (block) | `[[1,1],[1,1]]` | `[[1,1],[1,1]]` (each cell has 3 → survives) |

Glider by hand: (1,0) has 3 live neighbours → born; (1,2) 3 → survives; (2,1) 3 → survives;
(2,2) 2 → survives; (3,1) 3 → born; (0,1) and (2,0) have 1 each → die; (1,1) has 5 → stays dead.

## Repeatability / isolation checks

1. Glider run twice from fresh copies → identical results.
2. Glider, then the block (whose returned grid is then tampered with), then the glider again →
still the same result; earlier runs cannot change a later one.
3. Input grid is deep-equal to a fresh copy after the call.
4. A deeply frozen input grid is accepted and gives the expected result. ES modules run in strict
mode, so any write to the input would throw.
5. The output is a different array and shares no row arrays with the input.

The "no dependencies" statement is checked, not just asserted. `evidence.mjs` strips comments from
the real source and scans it for `Date`, `Math`, `performance`, `process`, `require`, `import`,
`fetch`, `console`, timers and browser/global objects, and lists any module-level variables.
Both lists are empty. As a negative control, adding `Math.random()` to a copy of the function made
the scan report `["Math"]` and fail.

All checks run in plain Node/Vitest: no network, account or GUI.

## Sensitivity check (temporary copy only)

A mutant that writes the computed state back into `cells` is applied to a temp copy by `check.py`;
`does not modify the input grid` and 5 other tests fail. The real source is untouched.

Baseline: all expected values matched the unmodified implementation; no defect found.

## How to run

From the repository root:

```sh
python3 work/R50-T005/check.py
```

`check.py` installs dependencies from `package-lock.json`, runs the focused test file, evaluates the fixture with `evidence.mjs` (input, expected, actual and source line for every case), runs each deliberate mutant in a temporary copy, and finally runs the repository test script in non-watch mode (`npm test`, which is `vitest run`). It exits non-zero if any assertion or step fails, and records every command it ran in `commands.log`.

### Recorded run (files in this folder)

| Step | Command | Result |
| ------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------ |
| Install from lockfile | `npm ci --ignore-scripts --no-audit --no-fund` (exit 0) | ok |
| Focused tests | `node_modules/.bin/vitest run Cellular-Automata/test/ConwaysGameOfLife.test.js --reporter=json --outputFile=work/R50-T005/vitest-focused.raw.json` | 9/9 passed |
| Fixture evidence | `node work/R50-T005/evidence.mjs` | 9/9 rows match |
| Mutant: writes the next state back into the input grid | temporary copy | killed by `does not modify the input grid` |
| Repository test script | `npm test -- --reporter=json --outputFile=work/R50-T005/vitest-full.raw.json` | 11978/11978 passed |

Tool versions (`versions.txt`): python 3.12.10; node v26.13.1; npm 11.20.0; platform macOS-27.2.1-arm64-arm-64bit; vitest vitest/1.2.1 darwin-arm64 node-v26.13.1.

Files: `results.json` (all of the above, machine-readable), `vitest-focused.json` (each focused test and its status), `commands.log`, `versions.txt`.
Loading