-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshadama-tests.js
389 lines (335 loc) · 10.6 KB
/
shadama-tests.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
"use strict";
function assert(result, expected, str) {
var stringify = (obj) => {
var type = Object.prototype.toString.call(obj);
if (type === "[object Object]") {
var pairs = [];
for (var k in obj) {
if (!obj.hasOwnProperty(k)) continue;
pairs.push([k, stringify(obj[k])]);
}
pairs.sort((a, b) => a[0] < b[0] ? -1 : 1);
pairs = pairs.map(v => '"' + v[0] + '":' + v[1]);
return "{" + pairs + "}";
}
if (type === "[object Array]") {
return "[" + obj.map(v => stringify(v)) + "]";
}
return JSON.stringify(obj);
};
var a = stringify(result);
var b = stringify(expected);
if (a != b) {
console.log("Expected: " + b + " got: " + a + " called from ");
console.log(new Error("assertion failed").stack);
}
return a != b;
}
function setTestParams() {
// TEXTURE_SIZE = 16;
// FIELD_WIDTH = 32;
// FIELD_HEIGHT = 32;
// T = TEXTURE_SIZE;
// FW = FIELD_WIDTH;
// FH = FIELD_HEIGHT;
}
function setUp() {
var ary = ["x", "y", "dx", "dy", "r", "g", "b", "a"];
update(Breed, "Breed1", ary);
update(Patch, "Patch1", ary);
env["Breed1"].setCount(10);
}
// function test() {
// setUp();
// test1();
// test2();
// test3();
// test4();
// test6();
// }
function test() {
directTest();
}
function directTest() {
setUp();
var r = env["Breed1"];
r.fillSpace("x", "y", FW, FH);
var count = 0;
var f = function() {
for (var i = 0; i < 10; i++) {
console.log("debugFrame()");
r.debugFrame();
}
r.draw();
if (count < 100) {
requestAnimationFrame(f);
count++;
}
}
f();
}
function test1() {
var obj = env["Breed1"];
for (var k in obj.own) {
var elem = obj.own[k];
assert(obj[elem].constructor, WebGLTexture);
assert(obj["new"+elem].constructor, WebGLTexture);
}
}
function caller(scriptName, thisName, bindings, inParams) {
(function() {
var data = scripts[scriptName];
var func = data[0];
var ins = data[1][0]; // [[name, <fieldName>]]
var formals = data[1][1];
var outs = data[1][2]; //[[object, <fieldName>]]
var objects = {};
objects.this = env[thisName];
objects = addAsSet(objects, bindings);
var params = inParams || {};
func(objects, outs, ins, params);
})();
}
function test2() {
var source = `
def set() {
this.x = 1.0;
this.y = 0.0;
this.dx = 1.0;
this.dy = 1.0;
this.r = 1.0;
this.g = 0.0;
this.b = 1.0;
this.a = 1.0;
}
def setColor() {
this.r = 1.0;
this.g = 0.0;
this.b = 1.0;
this.a = 1.0;
}
def clear(patch) {
patch.x = 1.0;
patch.y = 1.0;
patch.dx = 1.0;
patch.dy = 1.0;
}
def move() {
var dx = this.dx;
var dy = this.dy - 0.01;
this.x = this.x + dy;
this.y = this.y + dy;
}
`;
loadShadama(null, source);
caller("set", "Breed1", {});
debugDisplay("Breed1", "x");
assert(debugArray1[0], 1);
assert(debugArray1[10], 0);
}
function test3() {
env["Breed1"].fillSpace("x", "y", FW, FH);
debugDisplay("Breed1", "x");
assert(debugArray1[0], 0);
assert(debugArray1[9], 9);
assert(debugArray1[10], 10);
assert(debugArray1[FW-1], FW-1);
assert(debugArray1[FW], 0);
debugDisplay("Breed1", "y");
assert(debugArray1[0], 0);
assert(debugArray1[9], 0);
assert(debugArray1[10], 0);
assert(debugArray1[FW-1], 0);
assert(debugArray1[FW], 1);
}
function test4() {
var obj = env["Breed1"];
env["Breed1"].fillSpace("x", "y", FW, FH);
debugDisplay("Breed1", "newx");
var old = debugArray1.slice();
textureCopy(obj, obj["x"], obj["new"+"x"]);
debugDisplay("Breed1", "newx");
var now = debugArray1.slice();
assert(old[10], 10); // because updateOwnVariable resets both new and old
assert(now[10], 10);
}
function test5() {
var b = env["Patch1"];
var r = env["Breed1"];
var count = 0;
var f = function () {
for (var i = 0; i < 10; i++) {
console.log("test5()");
textureCopy(b, b["x"], b["new"+"x"]);
textureCopy(r, r["x"], r["new"+"x"]);
textureCopy(b, b["y"], b["new"+"y"]);
textureCopy(r, r["y"], r["new"+"y"]);
textureCopy(b, b["dx"], b["new"+"dx"]);
textureCopy(r, r["dx"], r["new"+"dx"]);
textureCopy(b, b["dy"], b["new"+"dy"]);
textureCopy(r, r["dy"], r["new"+"dy"]);
}
if (count < 100) {
requestAnimationFrame(f);
count++;
}
}
f();
}
function test6() {
var b = env["Patch1"];
var r = env["Breed1"];
env["Breed1"].fillSpace("x", "y", FW, FH);
var count = 0;
var f = function() {
for (var i = 0; i < 10; i++) {
console.log("test6()");
caller("setColor", "Breed1", {});
}
r.draw();
if (count < 100) {
requestAnimationFrame(f);
count++;
}
}
f();
}
function grammarTest(aString, rule, ctor) {
var match = parse(aString, rule);
if (!match.succeeded()) {
console.log(aString);
console.log("did not parse: " + aString);
}
if (!ctor) {
ctor = rule;
}
if (match._cst.ctorName != ctor) {
console.log(str);
console.log("did not get " + ctor + " from " + aString);
}
}
function symTableTest(str, prod, sem, expected) {
var stringify = (obj) => {
var type = Object.prototype.toString.call(obj);
if (type === "[object Object]") {
var pairs = [];
for (var k in obj) {
if (!obj.hasOwnProperty(k)) continue;
pairs.push([k, stringify(obj[k])]);
}
pairs.sort((a, b) => a[0] < b[0] ? -1 : 1);
pairs = pairs.map(v => '"' + v[0] + '":' + v[1]);
return "{" + pairs + "}";
}
if (type === "[object Array]") {
return "[" + obj.map(v => stringify(v)) + "]";
}
return JSON.stringify(obj);
};
var match = parse(str, prod);
if (!match.succeeded()) {
console.log(str);
console.log("did not parse: " + str);
};
var rawTable = function(table) {
var t;
if (table.constructor === SymTable) {
t = table;
} else {
t = table[Object.keys(table)[0]];
}
return t.rawTable();
}
var n = sem(match);
var table = new SymTable();
var ret = n.symTable(table);
var result = rawTable(ret);
var a = stringify(result);
var b = stringify(expected);
if (a != b) {
console.log(str);
console.log("Expected: " + b + " got: " + a);
}
}
function grammarUnitTests() {
grammarTest("abc", "ident");
grammarTest("if", "if");
grammarTest("breed", "breed");
grammarTest("patch", "patch");
grammarTest("else", "else");
grammarTest("def", "def");
grammarTest("3.4", "number");
grammarTest("abc", "PrimExpression");
grammarTest("3.5", "PrimExpression");
grammarTest("(3.5 + abc)", "PrimExpression");
grammarTest("3.5 + abc", "AddExpression");
grammarTest("abc - 3", "AddExpression_minus");
grammarTest("abc * 3", "AddExpression");
grammarTest("abc * 3", "MulExpression");
grammarTest("abc * 3 * 3.0", "Expression");
grammarTest("var c = 3;", "VariableStatement");
grammarTest("this.x", "LeftHandSideExpression");
grammarTest("patch.x", "LeftHandSideExpression");
grammarTest("forward(this.x)", "PrimitiveCall");
grammarTest("forward(this.x + 3)", "PrimitiveCall");
grammarTest("turn(this.x + 3, x)", "PrimitiveCall");
grammarTest("mod(this.x , 2)", "PrimitiveCall");
grammarTest("forward(this.x + 3);", "Statement");
grammarTest("a == b", "EqualityExpression");
grammarTest("a > 3", "RelationalExpression");
grammarTest("a > 3 + 4", "RelationalExpression");
grammarTest("this.x = 3 + 4;", "AssignmentStatement");
grammarTest("if (this.x > 3) {this.x = 3;} else {this.x = 4;}", "IfStatement");
grammarTest("if (this.x > 3) {this.x = 3;}", "IfStatement");
grammarTest("this.x + 3;", "ExpressionStatement");
grammarTest("var x = 3;", "VariableStatement");
grammarTest("{var x = 3; x = x + 3;}", "Block");
grammarTest("breed Turtle (x, y)", "Breed");
grammarTest("patch Patch (x, y)", "Patch");
grammarTest("def foo(x, y) {var x = 3; x = x + 2.1;}", "Script");
}
function symTableUnitTests() {
symTableTest("this.x = 3;", "Statement", s,{"propOut.this.x": ["propOut", "this","x"]});
symTableTest("{this.x = 3; other.y = 4;}", "Statement", s,{"propOut.this.x": ["propOut", "this", "x"], "propOut.other.y": ["propOut", "other", "y"]});
symTableTest("{this.x = 3; this.x = 4;}", "Statement", s, {"propOut.this.x": ["propOut", "this", "x"]});
symTableTest("{var x = 3; x = x + 3;}", "Block", s, {"var.null.x": ["var", null, "x"]});
symTableTest(`
if (other.x > 0) {
this.x = 3;
other.a = 4;
}
`, "Statement", s, {"propOut.this.x": ["propOut", "this", "x"], "propOut.other.a": ["propOut", "other", "a"], "propIn.other.x": ["propIn", "other", "x"]});
symTableTest(`
if (other.x > 0) {
this.x = 3;
other.a = 4;
} else {
this.y = 3;
other.a = 4;
}
`, "Statement", s, {"propOut.this.x": ["propOut", "this", "x"], "propOut.other.a": ["propOut", "other", "a"], "propOut.this.y": ["propOut", "this", "y"], "propIn.other.x": ["propIn", "other", "x"]});
symTableTest("{this.x = this.y; other.z = this.x;}", "Statement", s, {
"propIn.this.y": ["propIn", "this", "y"],
"propOut.this.x": ["propOut" ,"this", "x"],
"propOut.other.z": ["propOut" ,"other", "z"],
"propIn.this.x": ["propIn" ,"this", "x"]});
symTableTest("{this.x = 3; this.y = other.x;}", "Statement", s, {
"propOut.this.x": ["propOut" ,"this", "x"],
"propIn.other.x": ["propIn", "other", "x"],
"propOut.this.y": ["propOut" ,"this", "y"]});
symTableTest("def foo(a, b, c) {this.x = 3; this.y = other.x;}", "Script", s, {
"propIn.this.x": ["propIn", "this", "x"],
"propIn.this.y": ["propIn", "this", "y"],
"propIn.other.x": ["propIn", "other", "x"],
"propOut.this.x": ["propOut" ,"this", "x"],
"propOut.this.y": ["propOut" ,"this", "y"],
"param.null.a": ["param" , null, "a"],
"param.null.b": ["param" , null, "b"],
"param.null.c": ["param" , null, "c"],
});
}
function translateTests() {
console.log(translate("static foo() {Turtle.forward();}", "TopLevel"));
console.log(translate("static bar(x) {if(x){ Turtle.forward();}}", "TopLevel"));
console.log(translate("static bar(x) {if(x){ Turtle.forward();} else {Turtle.turn(x);}}", "TopLevel"));
}