-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-runner.html
103 lines (95 loc) · 2.77 KB
/
test-runner.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Tests</title>
<style>
html,
body {
background-color: black;
}
</style>
</head>
<body>
<script type="module">
import { distance, gt, isInClosedInterval } from "./js/math.js";
import AllTests from "./js/tests.js";
import update from "./js/update.js";
function assertNoDiskPenetration({ entities, position, size }) {
for (let i = 0; i < entities.length - 1; i++) {
const id = entities[i];
for (let j = i + 1; j < entities.length; j++) {
const other = entities[j];
const p1 = position.get(id);
const p2 = position.get(other);
const dCenter = distance(p1.x, p1.y, p2.x, p2.y);
const radiusSum = size.get(id).w / 2 + size.get(other).w / 2;
if (gt(radiusSum, dCenter)) {
return false;
}
}
}
return true;
}
function assertAllDisksInsideArena({
entities,
position,
size,
ARENA_W,
ARENA_H,
}) {
for (const id of entities) {
const pos = position.get(id);
const sz = size.get(id);
const radius = sz.w / 2;
if (!isInClosedInterval(pos.x, radius, ARENA_W - radius)) {
return false;
}
if (!isInClosedInterval(pos.y, radius, ARENA_H - radius)) {
return false;
}
}
return true;
}
const SIM_INVARIANTS = [
assertAllDisksInsideArena,
assertNoDiskPenetration,
];
AllTests.forEach((test) => {
const [CONFIG, init, asserts] = test();
const CONTEXT = init();
console.groupCollapsed(`${test.name}`);
console.log("config", CONFIG);
try {
for (let i = 0; i < 100; i++) {
CONTEXT.iteration += 1;
update(CONTEXT, ...CONFIG);
const failed = asserts
.map((a) => a(CONTEXT))
.some((r) => !!r === false);
if (failed) {
console.groupEnd();
console.error("test halted after failed assertion");
return;
}
for (const assert of SIM_INVARIANTS) {
const ok = assert(CONTEXT);
if (!ok) {
console.groupEnd();
console.error(
`test halted because ${assert.name} does not hold`,
);
return;
}
}
}
} catch (e) {
console.groupEnd();
console.error("test halted after simulation throws", e.stack);
return;
}
console.groupEnd();
});
</script>
</body>
</html>