-
Notifications
You must be signed in to change notification settings - Fork 11
/
TestUtilities.js
167 lines (128 loc) · 3.45 KB
/
TestUtilities.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Model } from "../src/api/Model.js";
export let testConfig = {
globals: ""
};
export function setupComplexExample() {
let m = new Model();
let c = m.Converter({
name: "My Converter"
});
let x = m.Stock({
name: "x"
});
m.Stock({
name: "y"
});
m.Stock({
name: "y"
});
let ms = m.Stock({
name: "My Stock"
});
m.Variable({
name: "a"
});
let b = m.Variable({
name: "b"
});
m.Link(x, b);
m.Link(x, c);
m.Flow(x, ms, {
name: "My Flow"
});
return m;
}
/**
* @param {string} eqn
* @param {any} out
* @param {number=} decimals
*/
export function check(eqn, out, decimals = null) {
let m = new Model();
m.timeLength = 2;
m.globals = testConfig.globals;
let variable = m.Variable();
variable.value = eqn;
let res = m.simulate();
let x = res.value(variable);
if (typeof x === "number") {
if (decimals !== null) {
x = +x.toFixed(decimals);
}
}
expect(x).toStrictEqual(out);
}
/**
* @param {string} eqn
* @param {string=} errorMsg
*/
export function failure(eqn, errorMsg = null) {
let m = new Model();
m.timeLength = 2;
m.globals = testConfig.globals;
let variable = m.Variable();
variable.value = eqn;
if (errorMsg) {
expect(() => m.simulate()).toThrow(errorMsg);
} else {
expect(() => m.simulate()).toThrow();
}
}
/**
* Checks simulation equality including agent locations and states for equality.
*
* @param {import("../src/Simulator").ResultsType} a
* @param {import("../src/Simulator").ResultsType} b
*/
export function areResultsDifferent(a, b) {
if (a === null && b === null) {
return false;
}
if (a.errorCode !== b.errorCode) {
return `Different error code: ${a.errorCode} "${a.error ? a.error.slice(0, 60) : a.error}..." ${b.errorCode} "${b.error ? b.error.slice(0, 60) : b.error}..."`;
}
if (a.error !== b.error) {
// generally skip this test
// return `Different error text: "${a.error ? a.error.slice(0, 60) : a.error}..." "${b.error ? b.error.slice(0, 60) : b.error}..."`;
}
if (a.periods !== b.periods) {
return `Different periods: "${a.periods}" "${b.periods}"`;
}
for (let i = 0; i < a.periods; i++) {
let dA = a.data[i];
let dB = b.data[i];
for (let key in dA) {
let oA = dA[key];
let oB = dB[key];
if (!oA.current) {
if (("" + oA) !== ("" + oB)) {
return `Different value at "${key}": "${oA}" "${oB}"`;
}
} else {
for (let j = 0; j < oA.current.length; j++) {
let pA = oA.current[j];
let pB = oB.current[j];
if (pA.instanceId !== pB.instanceId) {
return "Different instance id";
}
if ("" + pA.location.items[0] !== "" + pB.location.items[0]) {
return `Different location x: "${pA.location.items[0]}" "${pB.location.items[0]}"`;
}
if ("" + pA.location.items[1] !== "" + pB.location.items[1]) {
return `Different location y: "${pA.location.items[1]}" "${pB.location.items[1]}"`;
}
if (!(pA.state === null && pB.state === null)) {
for (let k = 0; k < pA.state.length; k++) {
let sA = pA.state[k];
let sB = pB.state[k];
if (sA.active !== sB.active) {
return `Different active state: ${sA.dna.name} - ${sA.active}; ${sB.dna.name} - ${sB.active}`;
}
}
}
}
}
}
}
return false;
}