summaryrefslogtreecommitdiff
path: root/src/simulation/lattice_rules.test.js
blob: e541add34188dfe2b106ee888ffc9b5da98a7018 (plain)
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
import { world_update } from '../world/world.js';
import { lattice_rules } from './lattice_rules.js';


function apply(f, n, x0) {
  if (n == 0) {
    return x0;
  } else {
    return f(apply(f, n-1, x0));
  }
}


test("blinker", () => {
  const L = { type: 'active', flags: {} };
  const D = { type: 'empty', flags: {} };
  const lattice = [
    [ D, D, D, D, D ],
    [ D, D, D, D, D ],
    [ D, L, L, L, D ],
    [ D, D, D, D, D ],
    [ D, D, D, D, D ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
  expect(world_update(world).lattice).toEqual([
    [ D, D, D, D, D ],
    [ D, D, L, D, D ],
    [ D, D, L, D, D ],
    [ D, D, L, D, D ],
    [ D, D, D, D, D ],
  ]);
  expect(world_update(world_update(world)).lattice).toEqual(lattice);
});


test("glider", () => {
  const L = { type: 'active', flags: {} };
  const D = { type: 'empty', flags: {} };
  const lattice = [
    [ D, D, D, D, D, D ],
    [ D, D, D, L, D, D ],
    [ D, L, D, L, D, D ],
    [ D, D, L, L, D, D ],
    [ D, D, D, D, D, D ],
    [ D, D, D, D, D, D ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
  //expect(world_update(world).lattice).toEqual([
  expect(apply(world_update, 1, world).lattice).toEqual([
    [ D, D, D, D, D, D ],
    [ D, D, L, D, D, D ],
    [ D, D, D, L, L, D ],
    [ D, D, L, L, D, D ],
    [ D, D, D, D, D, D ],
    [ D, D, D, D, D, D ],
  ]);
  expect(apply(world_update, 2, world).lattice).toEqual([
    [ D, D, D, D, D, D ],
    [ D, D, D, L, D, D ],
    [ D, D, D, D, L, D ],
    [ D, D, L, L, L, D ],
    [ D, D, D, D, D, D ],
    [ D, D, D, D, D, D ],
  ]);
  expect(apply(world_update, 3, world).lattice).toEqual([
    [ D, D, D, D, D, D ],
    [ D, D, D, D, D, D ],
    [ D, D, L, D, L, D ],
    [ D, D, D, L, L, D ],
    [ D, D, D, L, D, D ],
    [ D, D, D, D, D, D ],
  ]);
  expect(apply(world_update, 4, world).lattice).toEqual([
    [ D, D, D, D, D, D ],
    [ D, D, D, D, D, D ],
    [ D, D, D, D, L, D ],
    [ D, D, L, D, L, D ],
    [ D, D, D, L, L, D ],
    [ D, D, D, D, D, D ],
  ]);
});


test("beehive", () => {
  const L = { type: 'active', flags: {} };
  const D = { type: 'empty', flags: {} };
  const lattice = [
    [ D, D, D, D, D, D ],
    [ D, D, L, L, D, D ],
    [ D, L, D, D, L, D ],
    [ D, D, L, L, D, D ],
    [ D, D, D, D, D, D ],
  ];

  const world = { lattice, lattice_rules, agents: [], senses: [], actions: [], validity: [] };
  //expect(world_update(world).lattice).toEqual([
  expect(apply(world_update, 1, world).lattice).toEqual(lattice);
});