From f78493e192daf4639b91352909e4029b9970fcdb Mon Sep 17 00:00:00 2001 From: sanine Date: Wed, 8 Nov 2023 00:47:06 -0600 Subject: implement basic sensing & refactor world/cells -> lattice --- src/world/lattice.test.js | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/world/lattice.test.js (limited to 'src/world/lattice.test.js') diff --git a/src/world/lattice.test.js b/src/world/lattice.test.js new file mode 100644 index 0000000..7c71d04 --- /dev/null +++ b/src/world/lattice.test.js @@ -0,0 +1,99 @@ +'use strict'; + +import { lattice_update, lattice_valid, lattice_apply } from './lattice.js'; + + +test("growth update rule", () => { + const lattice = [[ + { type: 'empty', flags: {} }, + { type: 'empty', flags: {} }, + { type: 'plant', flags: {} }, + ]]; + const update_rules = { + plant: () => {}, + empty: (lattice, x, y) => { + if (lattice[y][x+1].type === 'plant') { + return { world_updates: [{ x, y, from: 'empty', to: 'plant' }] }; + } + }, + }; + + expect(lattice_update(lattice, update_rules)).toEqual([ + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' }] }, + ]); + + lattice[0][1] = { type: 'plant' }; + + expect(lattice_update(lattice, update_rules)).toEqual([ + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' }] }, + ]); + + lattice[0][0] = { type: 'plant' }; + + expect(lattice_update(lattice, update_rules)).toEqual([]); +}); + + +test("growth update rule applied", () => { + const lattice = [[ + { type: 'empty', flags: {} }, + { type: 'empty', flags: {} }, + { type: 'plant', flags: {} }, + ]]; + expect(lattice_apply(lattice, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([[ + { type: 'empty', flags: {} }, + { type: 'plant', flags: {} }, + { type: 'plant', flags: {} }, + ]]); + + expect(lattice_apply(lattice, [ + { world_updates: [{ x: 2, y: 0, from: 'plant', to: 'empty' } ]}, + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant' } ]}, + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant' } ]}, + ])).toEqual([[ + { type: 'plant', flags: {} }, + { type: 'plant', flags: {} }, + { type: 'empty', flags: {} }, + ]]); +}); + + +test("check proposals agains lattice for validity", () => { + const lattice = [[ { type: 'empty' }, { type: 'empty' }, { type: 'plant' } ]]; + expect(lattice_valid(lattice, { world_updates: [{ x: -1, y: 0, from: 'blah', to: 'blah' }] })).toBe(false); + expect(lattice_valid(lattice, { world_updates: [{ x: 0, y: 0, from: 'blah', to: 'blah' }] })).toBe(false); + expect(lattice_valid(lattice, { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'blah' }] })).toBe(true); + expect(lattice_valid(lattice, { world_updates: [{ x: 2, y: 0, from: 'empty', to: 'blah' }] })).toBe(false); + expect(lattice_valid(lattice, { world_updates: [{ x: 2, y: 1, from: 'empty', to: 'blah' }] })).toBe(false); +}); + + +test("proposals update cell flags appropriately", () => { + const lattice = [ + [ + { type: 'empty', flags: { step: 1} }, + { type: 'empty', flags: {} }, + { type: 'plant', flags: { foo: 'bar' } }, + ] + ]; + + // flags are reset each time step + expect(lattice_apply(lattice, [{ world_updates:[{ x: 1, y: 0, from: 'empty', to: 'plant' }]}])).toEqual([[ + { type: 'empty', flags: {} }, + { type: 'plant', flags: {} }, + { type: 'plant', flags: {} }, + ]]); + + // flags are combined when updating + expect(lattice_apply(lattice, [ + { world_updates: [{ x: 1, y: 0, flags: { foo: 'bar' } } ]}, + { world_updates: [{ x: 1, y: 0, from: 'empty', to: 'plant', flags: { baz: 'baz' } } ]}, + { world_updates: [{ x: 0, y: 0, from: 'empty', to: 'plant', flags: { foo: 'foo' } } ]}, + { world_updates: [{ x: 0, y: 0, flags: { beep: 'boop' } } ]}, + ])).toEqual([[ + { type: 'plant', flags: { foo: 'foo', beep: 'boop' } }, + { type: 'plant', flags: { foo: 'bar', baz: 'baz' } }, + { type: 'plant', flags: {} }, + ]]); + +}); -- cgit v1.2.1