summaryrefslogtreecommitdiff
path: root/src/simulation/actions.test.js
blob: 04111f1cc70284d9e56c22af97202320e61e1216 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
'use strict';

import { actions } from './actions.js';


const [
  move_forward, move_backward, 
  turn_left, turn_right,
  ...rest
] = actions;


test("move forward", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(move_forward.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] },
  ]);
  expect(move_forward.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] },
  ]);
  expect(move_forward.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] },
  ]);
  expect(move_forward.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] },
  ]);

  expect(move_forward.propose(null, n, [0])).toEqual([]);
  expect(move_forward.propose(null, s, [-1])).toEqual([]);
  expect(move_forward.propose(null, e, [0])).toEqual([]);
  expect(move_forward.propose(null, w, [-1])).toEqual([]);
});


test("move backward", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(move_backward.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: 1 }] },
  ]);
  expect(move_backward.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 0, y: -1 }] },
  ]);
  expect(move_backward.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: -1, y: 0 }] },
  ]);
  expect(move_backward.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, x: 1, y: 0 }] },
  ]);

  expect(move_backward.propose(null, n, [0])).toEqual([]);
  expect(move_backward.propose(null, s, [-1])).toEqual([]);
  expect(move_backward.propose(null, e, [0])).toEqual([]);
  expect(move_backward.propose(null, w, [-1])).toEqual([]);
});


test("turn_left", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(turn_left.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] },
  ]);
  expect(turn_left.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] },
  ]);
  expect(turn_left.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] },
  ]);
  expect(turn_left.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] },
  ]);

  expect(turn_left.propose(null, n, [0])).toEqual([]);
  expect(turn_left.propose(null, s, [-1])).toEqual([]);
  expect(turn_left.propose(null, e, [0])).toEqual([]);
  expect(turn_left.propose(null, w, [-1])).toEqual([]);
});


test("turn_right", () => {
  const n = { id: 0, x: 0, y: 0, flags: { orientation: 'n' } };
  const s = { id: 0, x: 0, y: 0, flags: { orientation: 's' } };
  const e = { id: 0, x: 0, y: 0, flags: { orientation: 'e' } };
  const w = { id: 0, x: 0, y: 0, flags: { orientation: 'w' } };

  expect(turn_right.propose(null, n, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'e' } }] },
  ]);
  expect(turn_right.propose(null, s, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'w' } }] },
  ]);
  expect(turn_right.propose(null, e, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 's' } }] },
  ]);
  expect(turn_right.propose(null, w, [1])).toEqual([
    { agent_changes: [{ agent_id: 0, flags: { orientation: 'n' } }] },
  ]);

  expect(turn_right.propose(null, n, [0])).toEqual([]);
  expect(turn_right.propose(null, s, [-1])).toEqual([]);
  expect(turn_right.propose(null, e, [0])).toEqual([]);
  expect(turn_right.propose(null, w, [-1])).toEqual([]);
});