-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbabelsberg-test.js
317 lines (288 loc) · 8.5 KB
/
babelsberg-test.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import bbb from './babelsberg.js'
import ConstraintInterpreter from './constraintinterpreter.js'
import * as acorn from './jsinterpreter/acorn.js'
import Interpreter from './jsinterpreter/interpreter.js'
import Relax from './bbb-relax.js'
import ClSimplexSolver from './bbb-dwarfcassowary.js';
import {assert} from '../lively4-core/node_modules/chai/chai.js'
function pt(x, y) {
return new Point(x, y)
}
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
addPt(other) {
return pt(this.x + other.x, this.y + other.y);
}
equals(other) {
return this.x == other.x && this.y == other.y;
}
leqPt(other) {
return this.x <= other.x && this.y <= other.y;
}
}
describe("bbb", function() {
it("should interpret", function() {
var predicate = function () {
return 23;
}
var i = new Interpreter(`var returnValue = (${predicate.toString()})();`);
i.run();
assert.equal(23, i.stateStack[0].scope.properties.returnValue)
})
it("should interpret constrained", function() {
var predicate = function () {
return 23;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal(23, r)
})
it("should interpret constrained a unary expression", function() {
var predicate = function () {
return -23;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal(-23, r)
})
it("should interpret constrained a binary expression", function() {
var predicate = function () {
return 2-23;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal(-21, r)
})
it("should interpret constrained a logical expression", function() {
var predicate = function () {
return true && false;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal(false, r)
})
it("should interpret constrained two logical expressions", function() {
var predicate = function () {
return true && false || true;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal(true, r)
})
xit("should interpret constrained a property access", function() {
var predicate = function () {
return window.nonexistingthing;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal(undefined, r)
})
xit("should interpret constrained a property access", function() {
var foo = {x: 23};
var predicate = function () {
return foo.x;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString(), {foo: foo})
assert.equal(foo.x, r)
console.log(foo)
})
xit("should allow constrained access to important globals", function() {
var predicate = function () {
return [jQuery, $, _, lively];
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString())
assert.equal([jQuery, $, _, lively], r)
})
xit("should do simple things in constrainted mode", function() {
var obj = {a: 2, b: 3};
var predicate = function () {
return obj.a + obj.b;
}
var r = ConstraintInterpreter.runAndReturn(predicate.toString(), {obj: obj})
assert.equal(obj.a + obj.b, r)
})
it("should solve a simple constraint", function() {
var obj = {a: 2, b: 3};
bbb.always({
solver: new Relax(),
ctx: {
obj: obj
}
}, function() {
return obj.a + obj.b == 3;
});
assert(Math.round(obj.a + obj.b) == 3, "Solver failed: " + obj.a + ", " + obj.b)
})
it("should solve a simple constraint and keep it satisfied", function() {
var obj = {a: 2, b: 3};
bbb.always({
solver: new Relax(),
ctx: {
obj: obj
}
}, function() {
return obj.a + obj.b == 3;
});
assert(Math.round(obj.a + obj.b) == 3, "Solver failed: " + obj.a + ", " + obj.b);
obj.a = 10;
assert(Math.round(obj.a + obj.b) == 3, "Solver failed: " + obj.a + ", " + obj.b);
})
it('should Simple', function () {
var obj = {a: 2, b: 3};
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
obj: obj
}
}, function() {
return obj.a + obj.b == 3;
});
assert(obj.a + obj.b == 3, "Solver failed: " + obj.a + ", " + obj.b)
})
it('should Inequality', function() {
var obj = {a: 8};
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
obj: obj
}
}, function() {
return obj.a >= 100;
});
assert(obj.a == 100);
obj.a = 110;
assert(obj.a == 110);
})
it('should DisableConstraint', function() {
var obj = {a: 8};
var error = false;
var c = bbb.always({
solver: new ClSimplexSolver(),
ctx: {
obj: obj
}
}, function() {
return obj.a >= 100;
});
assert(obj.a == 100);
obj.a = 110;
assert(obj.a == 110);
try {
obj.a = 90;
} catch(e) {
error = true
}
assert(error);
c.disable();
obj.a = 90;
assert(obj.a == 90);
c.enable();
assert(obj.a == 100);
error = false;
try {
obj.a = 90;
} catch(e) {
error = true
}
assert(error);
})
it('should SimplePath', function () {
ClSimplexSolver.resetInstance();
var pointA = {x:1, y:2},
pointB = {x:2, y:3},
o = {a: pointA, b: pointB};
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
o: o
}
}, function() {
return o.a.x + 100 <= o.b.x;
});
assert(pointA.x + 100 <= pointB.x, "Solver failed")
})
it('should AssignStay', function() {
var obj = {a: 2, b: 3};
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
obj: obj
}
}, function() {
return obj.a + obj.b == 3;
});
assert(obj.a + obj.b == 3, "Solver failed");
obj.a = -5;
assert(obj.a + obj.b == 3, "Constraint violated after assignment");
assert(obj.a == -5, "Assignment without effect");
})
it('should EqualityComplexObject', function() {
var solver = new ClSimplexSolver(),
assignmentFailed = false;
point = pt(0,0);
bbb.always({
solver: solver,
ctx: {
solver: solver,
point: point,
pt: pt,
_$_self: this.doitContext || this
}
}, function() {
return point.equals(pt(10, 10).addPt(pt(11, 11)));;
});
assert(point.equals(pt(21, 21)), "changed invisible point!");
try {
point.x = 100;
} catch(e) {
assignmentFailed = true;
}
assert(point.equals(pt(21, 21)) && assignmentFailed, "changed x!");
assert(point.equals(pt(21, 21)), "changed x!");
})
it('should PointEquals', function() {
var pt1 = pt(10, 10),
pt2 = pt(20, 20);
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
pt1: pt1,
pt2: pt2
}
}, function() {
return pt1.equals(pt2);
});
assert(pt1.equals(pt2));
})
it('should PointAddition', function() {
var pt1 = pt(10, 10),
pt2 = pt(20, 20),
pt3 = pt(0, 0);
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
pt1: pt1,
pt2: pt2,
pt3: pt3
}
}, function() {
return pt1.addPt(pt2).equals(pt3);
});
assert(pt1.addPt(pt2).equals(pt3));
})
it('should PointAssignment', function() {
var obj = {p: pt(10, 10)};
bbb.always({
solver: new ClSimplexSolver(),
ctx: {
obj: obj
}
}, function() {
return obj.p.x >= 100 && obj.p.y >= 100;
});
assert(pt(100, 100).leqPt(obj.p));
obj.p.x = 150;
assert(pt(100, 100).leqPt(obj.p));
assert(obj.p.x === 150);
obj.p = pt(150, 100);
assert(pt(100, 100).leqPt(obj.p));
assert(obj.p.x === 150, "point assignment failed to keep the new point intact");
})
})