UNPKG

37.9 kBtext/coffeescriptView Raw
1test_dependencies = ->
2 do_clearall()
3
4 testResult = findDependenciesInScript('1')
5 if testResult[0] == "All local dependencies: . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: " and
6 testResult[1] == "1" and
7 testResult[2] == ""
8 console.log "ok dependency test"
9 else
10 console.log "fail dependency test. expected: " + testResult
11
12 do_clearall()
13
14 # check that floats in code are expressed with maximum precision -------------------
15 testResult = findDependenciesInScript('a = float(1/3)')
16 if testResult[0] == "All local dependencies: variable a depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: ; " and
17 testResult[1] == "" and
18 testResult[2] == "a = 0.3333333333333333;"
19 console.log "ok dependency test"
20 else
21 console.log "fail dependency test. expected: " + testResult
22
23 do_clearall()
24
25 testResult = findDependenciesInScript('a = float(10^50)')
26 if testResult[0] == "All local dependencies: variable a depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: ; " and
27 testResult[1] == "" and
28 testResult[2] == "a = 1e+50;"
29 console.log "ok dependency test"
30 else
31 console.log "fail dependency test. expected: " + testResult
32
33 do_clearall()
34
35 testResult = findDependenciesInScript('f = x+1\n g = f\n h = g\n f = g')
36 if testResult[0] == "All local dependencies: variable f depends on: x, g, ; variable g depends on: f, ; variable h depends on: g, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: x, ; f --> g --> ... then f again, variable g depends on: x, ; g --> f --> ... then g again, variable h depends on: x, ; h --> g --> f --> ... then g again, " and
37 testResult[1] == "" and
38 testResult[2] == "// f is part of a cyclic dependency, no code generated.\n// g is part of a cyclic dependency, no code generated.\n// h is part of a cyclic dependency, no code generated."
39 else
40 console.log "fail dependency test. expected: " + testResult
41
42 do_clearall()
43
44 if findDependenciesInScript('f = x+1\n g = f + y\n h = g')[0] == "All local dependencies: variable f depends on: x, ; variable g depends on: f, y, ; variable h depends on: g, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: x, ; variable g depends on: x, y, ; variable h depends on: x, y, ; "
45 console.log "ok dependency test"
46 else
47 console.log "fail dependency test"
48
49 do_clearall()
50
51 if findDependenciesInScript('g = h(x,y)')[0] == "All local dependencies: variable g depends on: h, x, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable g depends on: h, x, y, ; "
52 console.log "ok dependency test"
53 else
54 console.log "fail dependency test"
55
56 do_clearall()
57
58 if findDependenciesInScript('f(x,y) = k')[0] == "All local dependencies: variable f depends on: 'x, 'y, k, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: 'x, 'y, k, ; "
59 console.log "ok dependency test"
60 else
61 console.log "fail dependency test"
62
63 do_clearall()
64
65 if findDependenciesInScript('x = z\n f(x,y) = k')[0] == "All local dependencies: variable x depends on: z, ; variable f depends on: 'x, 'y, k, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: z, ; variable f depends on: 'x, 'y, k, ; "
66 console.log "ok dependency test"
67 else
68 console.log "fail dependency test"
69
70 do_clearall()
71
72 if findDependenciesInScript('x = z\n g = f(x,y)')[0] == "All local dependencies: variable x depends on: z, ; variable g depends on: f, x, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: z, ; variable g depends on: f, z, y, ; "
73 console.log "ok dependency test"
74 else
75 console.log "fail dependency test"
76
77 do_clearall()
78
79 if findDependenciesInScript('x = 1\n x = y\n x = z')[0] == "All local dependencies: variable x depends on: y, z, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: y, z, ; "
80 console.log "ok dependency test"
81 else
82 console.log "fail dependency test"
83
84 do_clearall()
85
86 testResult = findDependenciesInScript('x = y*y')
87 if testResult[0] == "All local dependencies: variable x depends on: y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: y, ; " and
88 testResult[1] == "" and
89 testResult[2] == "x = function (y) { return ( Math.pow(y, 2) ); }"
90 console.log "ok dependency test"
91 else
92 console.log "fail dependency test. expected: " + testResult
93
94 do_clearall()
95
96 testResult = findDependenciesInScript('x = e*e')
97 if testResult[0] == "All local dependencies: variable x depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: ; " and
98 testResult[1] == "" and
99 testResult[2] == "x = Math.exp(2);"
100 console.log "ok dependency test"
101 else
102 console.log "fail dependency test. expected: " + testResult
103
104 testResult = findDependenciesInScript('x = e')
105 if testResult[0] == "All local dependencies: variable x depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: ; " and
106 testResult[1] == "" and
107 testResult[2] == "x = Math.E;"
108 console.log "ok dependency test"
109 else
110 console.log "fail dependency test. expected: " + testResult
111
112 testResult = findDependenciesInScript('x = -sqrt(2)/2')
113 if testResult[0] == "All local dependencies: variable x depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: ; " and
114 testResult[1] == "" and
115 testResult[2] == "x = -1/2*Math.SQRT2;"
116 console.log "ok dependency test"
117 else
118 console.log "fail dependency test. expected: " + testResult
119
120 do_clearall()
121
122 testResult = findDependenciesInScript('x = 2^(1/2-a)*2^a/10')
123 if testResult[0] == "All local dependencies: variable x depends on: a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: a, ; " and
124 testResult[1] == "" and
125 testResult[2] == "x = 1/10*Math.SQRT2;"
126 console.log "ok dependency test"
127 else
128 console.log "fail dependency test. expected: " + testResult
129
130 do_clearall()
131
132 testResult = findDependenciesInScript('x = rationalize(t*y/(t+y)+2*t^2*y*(2*t+y)^(-2))')
133 if testResult[0] == "All local dependencies: variable x depends on: t, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: t, y, ; " and
134 testResult[1] == "" and
135 testResult[2] == "x = function (t, y) { return ( t*y*(6*Math.pow(t, 2) + Math.pow(y, 2) + 6*t*y) / ((t + y)*Math.pow((2*t + y), 2)) ); }"
136 console.log "ok dependency test"
137 else
138 console.log "fail dependency test. expected: " + testResult
139
140 do_clearall()
141
142 testResult = findDependenciesInScript('x = abs((a+i*b)/(c+i*d))')
143 if testResult[0] == "All local dependencies: variable x depends on: a, b, c, d, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: a, b, c, d, ; " and
144 testResult[1] == "" and
145 testResult[2] == "x = function (a, b, c, d) { return ( Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2)) / (Math.sqrt(Math.pow(c, 2) + Math.pow(d, 2))) ); }"
146 console.log "ok dependency test"
147 else
148 console.log "fail dependency test. expected: " + testResult[2] + " obtained: " + testResult
149
150 do_clearall()
151
152 testResult = findDependenciesInScript('x = sin(1/10)^2 + cos(1/10)^2 + y')
153 if testResult[0] == "All local dependencies: variable x depends on: y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: y, ; " and
154 testResult[1] == "" and
155 testResult[2] == "x = function (y) { return ( 1 + y ); }"
156 console.log "ok dependency test"
157 else
158 console.log "fail dependency test. expected: " + testResult
159
160 do_clearall()
161
162 testResult = findDependenciesInScript('x = y + cos(1) + sin(1)')
163 if testResult[0] == "All local dependencies: variable x depends on: y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: y, ; " and
164 testResult[1] == "" and
165 testResult[2] == "x = function (y) { return ( y + Math.cos(1) + Math.sin(1) ); }"
166 console.log "ok dependency test"
167 else
168 console.log "fail dependency test. expected: " + testResult
169
170 do_clearall()
171
172 testResult = findDependenciesInScript('x = a + arccos(b) + arcsin(c)')
173 if testResult[0] == "All local dependencies: variable x depends on: a, arccos, b, arcsin, c, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: a, arccos, b, arcsin, c, ; " and
174 testResult[1] == "" and
175 testResult[2] == "x = function (a, b, c) { return ( a + Math.acos(b) + Math.asin(c) ); }"
176 console.log "ok dependency test"
177 else
178 console.log "fail dependency test. expected: " + testResult
179
180 do_clearall()
181
182 testResult = findDependenciesInScript('x = sin(1/10)^2 + cos(1/10)^2')
183 if testResult[0] == "All local dependencies: variable x depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable x depends on: ; " and
184 testResult[1] == "" and
185 testResult[2] == "x = 1;"
186 console.log "ok dependency test"
187 else
188 console.log "fail dependency test. expected: " + testResult
189
190 do_clearall()
191
192 testResult = findDependenciesInScript('a = unit(b) + c')
193 if testResult[0] == "All local dependencies: variable a depends on: b, c, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: b, c, ; " and
194 testResult[1] == "" and
195 testResult[2] == "a = function (c, b) { return ( c + identity(b) ); }"
196 console.log "ok dependency test"
197 else
198 console.log "fail dependency test. expected: " + testResult
199
200 do_clearall()
201
202 testResult = findDependenciesInScript('f(x) = x * x')
203 if testResult[0] == "All local dependencies: variable f depends on: 'x, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: 'x, ; " and
204 testResult[1] == "" and
205 testResult[2] == "f = function (x) { return ( x*x ); }"
206 console.log "ok dependency test"
207 else
208 console.log "fail dependency test. expected: " + testResult
209
210 do_clearall()
211
212 testResult = findDependenciesInScript('f(x) = x * x + g(y)')
213 if testResult[0] == "All local dependencies: variable f depends on: 'x, g, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: 'x, g, y, ; " and
214 testResult[1] == "" and
215 testResult[2] == "f = function (g, y, x) { return ( g(y) + Math.pow(x, 2) ); }"
216 console.log "ok dependency test"
217 else
218 console.log "fail dependency test. expected: " + testResult
219
220 do_clearall()
221
222 testResult = findDependenciesInScript('y = 2\nf(x) = x * x + g(y)')
223 if testResult[0] == "All local dependencies: variable y depends on: ; variable f depends on: 'x, g, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable y depends on: ; variable f depends on: 'x, g, ; " and
224 testResult[1] == "" and
225 testResult[2] == "y = 2;\nf = function (g, x) { return ( g(2) + Math.pow(x, 2) ); }"
226 console.log "ok dependency test"
227 else
228 console.log "fail dependency test. expected: " + testResult
229
230 do_clearall()
231
232 testResult = findDependenciesInScript('g(x) = x + 2\ny = 2\nf(x) = x * x + g(y)')
233 if testResult[0] == "All local dependencies: variable g depends on: 'x, ; variable y depends on: ; variable f depends on: 'x, g, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable g depends on: 'x, ; variable y depends on: ; variable f depends on: 'x, ; " and
234 testResult[1] == "" and
235 testResult[2] == "g = function (x) { return ( 2 + x ); }\ny = 2;\nf = function (x) { return ( 4 + Math.pow(x, 2) ); }"
236 console.log "ok dependency test"
237 else
238 console.log "fail dependency test. expected: " + testResult
239
240 do_clearall()
241
242 testResult = findDependenciesInScript('g(x) = x + 2\nf(x) = x * x + g(y)')
243 if testResult[0] == "All local dependencies: variable g depends on: 'x, ; variable f depends on: 'x, g, y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable g depends on: 'x, ; variable f depends on: 'x, y, ; " and
244 testResult[1] == "" and
245 testResult[2] == "g = function (x) { return ( 2 + x ); }\nf = function (y, x) { return ( 2 + y + Math.pow(x, 2) ); }"
246 console.log "ok dependency test"
247 else
248 console.log "fail dependency test. expected: " + testResult
249
250 do_clearall()
251
252 ###
253 testResult = findDependenciesInScript('g(x) = f(x)\nf(x)=g(x)')
254 if testResult[0] == "All local dependencies: variable g depends on: 'x, f, x, ; variable f depends on: 'x, g, x, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable g depends on: 'x, ; g --> f --> ... then g again, variable f depends on: 'x, x, ; f --> g --> ... then f again, " and
255 testResult[1] == "" and
256 testResult[2] == "// g is part of a cyclic dependency, no code generated.\n// f is part of a cyclic dependency, no code generated."
257 console.log "ok dependency test"
258 else
259 console.log "fail dependency test. expected: " + testResult
260
261 do_clearall()
262 ###
263
264 testResult = findDependenciesInScript('piApprox = sum((-1)^k * (1/(2*k + 1)),k,0,iterations)*4')
265 if testResult[0] == "All local dependencies: variable piApprox depends on: iterations, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable piApprox depends on: iterations, ; " and
266 testResult[1] == "" and
267 testResult[2] == "piApprox = function (iterations) { return ( 4*(function(){ var k; var holderSum = 0; var lowerlimit = 0; var upperlimit = iterations; for (k = lowerlimit; k < upperlimit; k++) { holderSum += Math.pow((-1), k) / (2*k + 1); } return holderSum;})() ); }" and
268 testResult[3] == "piApprox(iterations) = 4\\sum_{k=0}^{iterations}{\\frac{(-1)^k}{(2k+1)}}"
269 console.log "ok dependency test"
270 else
271 console.log "fail dependency test. expected: " + testResult
272
273 do_clearall()
274
275 testResult = findDependenciesInScript('piApprox = 2*product(4*k^2/(4*k^2-1),k,1,iterations)')
276 if testResult[0] == "All local dependencies: variable piApprox depends on: iterations, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable piApprox depends on: iterations, ; " and
277 testResult[1] == "" and
278 testResult[2] == "piApprox = function (iterations) { return ( 2*(function(){ var k; var holderProduct = 1; var lowerlimit = 1; var upperlimit = iterations; for (k = lowerlimit; k < upperlimit; k++) { holderProduct *= 4*Math.pow(k, 2) / (4*Math.pow(k, 2) - 1); } return holderProduct;})() ); }" and
279 testResult[3] == "piApprox(iterations) = 2\\prod_{k=1}^{iterations}{\\frac{4k^2}{(4k^2-1)}}"
280 console.log "ok dependency test"
281 else
282 console.log "fail dependency test. expected: " + testResult
283
284 do_clearall()
285
286 testResult = findDependenciesInScript('f = roots(a*x^2 + b*x + c, x)')
287 if testResult[0] == "All local dependencies: variable f depends on: a, b, c, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: a, b, c, ; " and
288 testResult[1] == "" and
289 testResult[2] == "f = function (a, b, c) { return ( [-1/2*(Math.sqrt(Math.pow(b, 2) / (Math.pow(a, 2)) - 4*c / a) + b / a),1/2*(Math.sqrt(Math.pow(b, 2) / (Math.pow(a, 2)) - 4*c / a) - b / a)] ); }"
290 console.log "ok dependency test"
291 else
292 console.log "fail dependency test. expected: " + testResult[2] + " obtained: " + testResult
293
294 do_clearall()
295
296 testResult = findDependenciesInScript('f = roots(a*x^2 + b*x + c)')
297 if testResult[0] == "All local dependencies: variable f depends on: a, b, c, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: a, b, c, ; " and
298 testResult[1] == "" and
299 testResult[2] == "f = function (a, b, c) { return ( [-1/2*(Math.sqrt(Math.pow(b, 2) / (Math.pow(a, 2)) - 4*c / a) + b / a),1/2*(Math.sqrt(Math.pow(b, 2) / (Math.pow(a, 2)) - 4*c / a) - b / a)] ); }"
300 console.log "ok dependency test"
301 else
302 console.log "fail dependency test. expected: " + testResult[2] + " obtained: " + testResult
303
304 do_clearall()
305
306 testResult = findDependenciesInScript('f = roots(integral(a*x + b))')
307 if testResult[0] == "All local dependencies: variable f depends on: a, b, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: a, b, ; " and
308 testResult[1] == "" and
309 testResult[2] == "f = function (a, b) { return ( [0,-2*b / a] ); }"
310 console.log "ok dependency test"
311 else
312 console.log "fail dependency test. expected: " + testResult
313
314 do_clearall()
315
316 testResult = findDependenciesInScript('f = roots(defint(a*x + y,y,0,1))')
317 if testResult[0] == "All local dependencies: variable f depends on: a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: a, ; " and
318 testResult[1] == "" and
319 testResult[2] == "f = function (a) { return ( -1 / (2*a) ); }"
320 console.log "ok dependency test"
321 else
322 console.log "fail dependency test. expected: " + testResult
323
324 do_clearall()
325
326 testResult = findDependenciesInScript('f = roots(defint(a*x + y + z,y,0,1, z, 0, 1))')
327 if testResult[0] == "All local dependencies: variable f depends on: a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: a, ; " and
328 testResult[1] == "" and
329 testResult[2] == "f = function (a) { return ( -1 / a ); }"
330 console.log "ok dependency test"
331 else
332 console.log "fail dependency test. expected: " + testResult
333
334 do_clearall()
335
336 testResult = findDependenciesInScript('f = defint(2*x - 3*y,x,0,2*y)')
337 if testResult[0] == "All local dependencies: variable f depends on: y, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: y, ; " and
338 testResult[1] == "" and
339 testResult[2] == "f = function (y) { return ( -2*Math.pow(y, 2) ); }"
340 console.log "ok dependency test"
341 else
342 console.log "fail dependency test. expected: " + testResult
343
344 do_clearall()
345
346 testResult = findDependenciesInScript('f = defint(12 - x^2 - (y^2)/2,x,0,2,y,0,3)')
347 if testResult[0] == "All local dependencies: variable f depends on: ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable f depends on: ; " and
348 testResult[1] == "" and
349 testResult[2] == "f = 55;"
350 console.log "ok dependency test"
351 else
352 console.log "fail dependency test. expected: " + testResult
353
354 do_clearall()
355
356 # this example checks that functions are not meddled with,
357 # in particular that in the function body, the variables
358 # bound by the parameters remain "separate" from previous
359 # variables with the same name.
360 testResult = findDependenciesInScript('a = 2\nf(a) = a+1+b')
361 if testResult[0] == "All local dependencies: variable a depends on: ; variable f depends on: 'a, b, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: ; variable f depends on: 'a, b, ; " and
362 testResult[1] == "" and
363 testResult[2] == "a = 2;\nf = function (a, b) { return ( 1 + a + b ); }"
364 console.log "ok dependency test"
365 else
366 console.log "fail dependency test. expected: " + testResult
367
368 do_clearall()
369
370 # similar as test above but this time we are not
371 # defining a function, so things are a bit different.
372 testResult = findDependenciesInScript('a = 2\nf = a+1')
373 if testResult[0] == "All local dependencies: variable a depends on: ; variable f depends on: a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: ; variable f depends on: ; " and
374 testResult[1] == "" and
375 testResult[2] == "a = 2;\nf = 3;"
376 console.log "ok dependency test"
377 else
378 console.log "fail dependency test. expected: " + testResult
379
380 do_clearall()
381
382 # similar as test above but this time we do a
383 # trick with the quote to see whether we
384 # get confused with the indirection
385 testResult = findDependenciesInScript('a := b\nf = a+1')
386 if testResult[0] == "All local dependencies: variable a depends on: b, ; variable f depends on: a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: b, ; variable f depends on: b, ; " and
387 testResult[1] == "" and
388 testResult[2] == "a = function (b) { return ( b ); }\nf = function (b) { return ( 1 + b ); }"
389 console.log "ok dependency test"
390 else
391 console.log "fail dependency test. expected: " + testResult
392
393 do_clearall()
394
395 # another tricky case of indirection through quote
396 testResult = findDependenciesInScript('a := b\nf(a) = a+1')
397 if testResult[0] == "All local dependencies: variable a depends on: b, ; variable f depends on: 'a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: b, ; variable f depends on: 'a, ; " and
398 testResult[1] == "" and
399 testResult[2] == "a = function (b) { return ( b ); }\nf = function (a) { return ( 1 + a ); }"
400 console.log "ok dependency test"
401 else
402 console.log "fail dependency test. expected: " + testResult
403
404 do_clearall()
405
406 # reassignment
407 testResult = findDependenciesInScript('b = 1\nb=a+b+c')
408 if testResult[0] == "All local dependencies: variable b depends on: a, c, ; . Symbols with reassignments: b, . Symbols in expressions without assignments: . All dependencies recursively: variable b depends on: a, c, ; " and
409 testResult[1] == "" and
410 testResult[2] == "b = function (a, c) { return ( 1 + a + c ); }"
411 console.log "ok dependency test"
412 else
413 console.log "fail dependency test. expected: " + testResult
414
415 do_clearall()
416
417 # reassignment
418 testResult = findDependenciesInScript('a = a+1')
419 if testResult[0] == "All local dependencies: variable a depends on: ; . Symbols with reassignments: a, . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: ; " and
420 testResult[1] == "" and
421 testResult[2] == "" and
422 testResult[5] == "Error: Stop: recursive evaluation of symbols: a -> a"
423 console.log "ok dependency test"
424 else
425 console.log "fail dependency test. expected: " + testResult
426
427 do_clearall()
428
429 # reassignment
430 testResult = computeDependenciesFromAlgebra('pattern(a,b)\nc= d\na=a+1')
431 if testResult.affectsVariables.length is 3 and
432 testResult.affectsVariables.indexOf("c") != -1 and
433 testResult.affectsVariables.indexOf("a") != -1 and
434 testResult.affectsVariables.indexOf("PATTERN_DEPENDENCY") != -1 and
435 testResult.affectedBy.length is 3 and
436 testResult.affectedBy.indexOf("d") != -1 and
437 testResult.affectedBy.indexOf("a") != -1 and
438 testResult.affectedBy.indexOf("PATTERN_DEPENDENCY") != -1
439 console.log "ok dependency test"
440 else
441 console.log "fail dependency test. expected: " + testResult
442
443 do_clearall()
444
445 testResult = computeDependenciesFromAlgebra('PCA(M) = eig(Mᵀ⋅M)')
446 if testResult.affectsVariables.length is 1 and
447 testResult.affectsVariables.indexOf("PCA") != -1 and
448 testResult.affectsVariables.indexOf("PATTERN_DEPENDENCY") == -1 and
449 testResult.affectedBy.length is 1 and
450 testResult.affectedBy.indexOf("PATTERN_DEPENDENCY") != -1
451 console.log "ok dependency test"
452 else
453 console.log "fail dependency test. expected: " + testResult
454
455 do_clearall()
456
457 testResult = computeDependenciesFromAlgebra('pattern(a_ᵀ⋅a_, cov(a_))')
458 if testResult.affectsVariables.length is 1 and
459 testResult.affectsVariables.indexOf("PATTERN_DEPENDENCY") != -1 and
460 testResult.affectedBy.length is 1 and
461 testResult.affectedBy.indexOf("PATTERN_DEPENDENCY") != -1
462 console.log "ok dependency test"
463 else
464 console.log "fail dependency test. expected: " + testResult
465
466 do_clearall()
467
468 testResult = findDependenciesInScript('a = b\nf = a+1')
469 if testResult[0] == "All local dependencies: variable a depends on: b, ; variable f depends on: a, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable a depends on: b, ; variable f depends on: b, ; " and
470 testResult[1] == "" and
471 testResult[2] == "a = function (b) { return ( b ); }\nf = function (b) { return ( 1 + b ); }"
472 console.log "ok dependency test"
473 else
474 console.log "fail dependency test. expected: " + testResult
475
476 do_clearall()
477
478 testResult = findDependenciesInScript('PCA(M) = eig(cov(M))')
479 if testResult[0] == "All local dependencies: variable PCA depends on: 'M, ; . Symbols with reassignments: . Symbols in expressions without assignments: . All dependencies recursively: variable PCA depends on: 'M, ; " and
480 testResult[1] == "" and
481 testResult[2] == "PCA = function (M) { return ( eig(cov(M)) ); }"
482 console.log "ok dependency test"
483 else
484 console.log "fail dependency test. expected: " + testResult
485
486 do_clearall()
487
488 computeResultsAndJavaScriptFromAlgebra('PCA(M) = eig(Mᵀ⋅M)')
489 testResult = run('symbolsinfo')
490 if testResult.indexOf('AVOID_BINDING_TO_EXTERNAL_SCOPE_VALUE') != -1
491 console.log "fail dependency tests. found AVOID_BINDING_TO_EXTERNAL_SCOPE_VALUE"
492 else
493 console.log "ok dependency test"
494
495 do_clearall()
496
497 # this checks error handling in case of pattern and syntax error
498 # picked up during scanning.
499 computeResultsAndJavaScriptFromAlgebra('pattern(a_ᵀ⋅a_, cov(a_))')
500 computeResultsAndJavaScriptFromAlgebra('simplify(')
501 testResult = computeResultsAndJavaScriptFromAlgebra('PCA = Mᵀ·M')
502
503 if testResult.code == "PCA = function (M) { return ( cov(M) ); }" and
504 testResult.latexResult == "$$PCA(M) = cov(M)$$" and
505 testResult.result == "$$PCA(M) = cov(M)$$" and
506 testResult.dependencyInfo.affectedBy[0] == "M" and
507 testResult.dependencyInfo.affectedBy[1] == "PATTERN_DEPENDENCY" and
508 testResult.dependencyInfo.affectsVariables.length == 1 and
509 testResult.dependencyInfo.affectsVariables[0] == "PCA"
510 console.log "ok dependency test"
511 else
512 console.log "fail dependency tests. Error handling 1"
513 console.log testResult
514 return
515
516 do_clearall()
517
518
519 if ENABLE_CACHING
520 console.log "checking hit/miss patterns ======================="
521 resetCache()
522 original_CACHE_HITSMISS_DEBUGS = CACHE_HITSMISS_DEBUGS
523 CACHE_HITSMISS_DEBUGS = true
524
525 # first two should miss because caches are completely empty
526 # note that each call produces two misses because one is from
527 # "findDependenciesInScript" itself and one is from "run"
528 # after these two, all the others should hit a cache
529 computeResultsAndJavaScriptFromAlgebra('pattern(a_ᵀ⋅a_, cov(a_))')
530 computeResultsAndJavaScriptFromAlgebra('PCA = Mᵀ·M')
531
532 if totalAllCachesHits() != 0
533 console.log "test checking hit/miss patterns fail, got: " + totalAllCachesHits() + " instead of 0"
534
535 clearAlgebraEnvironment()
536 console.log "\nclearAlgebraEnvironment()"
537 currentStateHash = getStateHash()
538 console.log "state hash after nclearAlgebraEnvironment: " + currentStateHash
539 console.log "\n"
540
541 computeResultsAndJavaScriptFromAlgebra('pattern(a_ᵀ⋅a_, cov(a_))')
542 computeResultsAndJavaScriptFromAlgebra('PCA = Mᵀ·M')
543
544 if totalAllCachesHits() != 2
545 console.log "test checking hit/miss patterns fail, got: " + totalAllCachesHits() + " instead of 2"
546
547 clearAlgebraEnvironment()
548 console.log "\nclearAlgebraEnvironment()"
549 currentStateHash = getStateHash()
550 console.log "state hash after nclearAlgebraEnvironment: " + currentStateHash
551 console.log "\n"
552
553 computeResultsAndJavaScriptFromAlgebra('pattern(a_ᵀ⋅a_, cov(a_))')
554 computeResultsAndJavaScriptFromAlgebra('PCA = Mᵀ·M')
555 CACHE_HITSMISS_DEBUGS = original_CACHE_HITSMISS_DEBUGS
556
557 if totalAllCachesHits() != 4
558 console.log "test checking hit/miss patterns fail, got: " + totalAllCachesHits() + " instead of 4"
559
560 clearAlgebraEnvironment()
561 console.log "\nclearAlgebraEnvironment()"
562 currentStateHash = getStateHash()
563 console.log "state hash after nclearAlgebraEnvironment: " + currentStateHash
564 console.log "\n"
565
566 computeResultsAndJavaScriptFromAlgebra('pattern(a_ᵀ⋅a_, cov(a_))')
567 # note that in case of syntax error, "last" is not updated.
568 # so if new symbols have been scanned yet, then they are not created
569 # and the cache should hit
570 # TODO ideally a scan resulting in an error should produce no new
571 # symbols in the symbol table at all
572 computeResultsAndJavaScriptFromAlgebra('simplify(')
573
574 currentStateHash = getStateHash()
575 console.log "state hash after syntax error: " + currentStateHash
576
577 computeResultsAndJavaScriptFromAlgebra('PCA = Mᵀ·M')
578 CACHE_HITSMISS_DEBUGS = original_CACHE_HITSMISS_DEBUGS
579
580 if totalAllCachesHits() != 6
581 console.log "test checking hit/miss patterns fail, got: " + totalAllCachesHits() + " instead of 6"
582
583 if totalAllCachesMisses() != 5
584 console.log "test checking hit/miss patterns fail, got: " + totalAllCachesMisses() + " instead of 5"
585
586
587 resetCache()
588 do_clearall()
589 console.log "end of checking hit/miss patterns ======================="
590
591 testResult = computeResultsAndJavaScriptFromAlgebra('x + x + x')
592
593 if testResult.code == "" and
594 testResult.latexResult == "$$3x$$" and
595 testResult.result == "$$3x$$" and
596 testResult.dependencyInfo.affectedBy.length == 2 and
597 testResult.dependencyInfo.affectedBy[0] == "x" and
598 testResult.dependencyInfo.affectedBy[1] == "PATTERN_DEPENDENCY" and
599 testResult.dependencyInfo.affectsVariables.length == 0
600 console.log "ok dependency test"
601 else
602 console.log "fail dependency tests"
603
604 do_clearall()
605
606 computeResultsAndJavaScriptFromAlgebra('x = y + 2')
607 testResult = computeResultsAndJavaScriptFromAlgebra('x + x + x')
608
609 if testResult.code == "" and
610 testResult.latexResult == "$$3y+6$$" and
611 testResult.result == "$$3y+6$$" and
612 testResult.dependencyInfo.affectedBy.length == 2 and
613 testResult.dependencyInfo.affectedBy[0] == "x" and
614 testResult.dependencyInfo.affectedBy[1] == "PATTERN_DEPENDENCY" and
615 testResult.dependencyInfo.affectsVariables.length == 0
616 console.log "ok dependency test"
617 else
618 console.log "fail dependency tests"
619
620 do_clearall()
621
622 testResult = computeResultsAndJavaScriptFromAlgebra('[[0,1],[1,0]]')
623
624 console.log "testResult.latexResult " + testResult.latexResult
625 console.log "testResult.result " + testResult.result
626 if testResult.latexResult == "$$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$$" and
627 testResult.result == "$$\\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$$" and
628 testResult.dependencyInfo.affectsVariables.length == 0
629 console.log "ok dependency test"
630 else
631 console.log "fail dependency tests"
632
633 do_clearall()
634
635 testResult = computeResultsAndJavaScriptFromAlgebra('x = [[0,1],[1,0]]')
636
637 console.log "testResult.latexResult " + testResult.latexResult
638 console.log "testResult.result " + testResult.result
639 if testResult.code == "x = [[0,1],[1,0]];" and
640 testResult.latexResult == "$$x = \\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$$" and
641 testResult.result == "$$x = \\begin{bmatrix} 0 & 1 \\\\ 1 & 0 \\end{bmatrix}$$" and
642 testResult.dependencyInfo.affectedBy.length == 1 and
643 testResult.dependencyInfo.affectedBy[0] == "PATTERN_DEPENDENCY" and
644 testResult.dependencyInfo.affectsVariables.length == 1 and
645 testResult.dependencyInfo.affectsVariables[0] == "x"
646 console.log "ok dependency test"
647 else
648 console.log "fail dependency tests"
649
650 do_clearall()
651
652 # a simple array lookup like this is turned into
653 # a function, which is slighly silly but
654 # it's orthogonal, this works also if instead of
655 # b we have an arbitrary non-trivial function
656 # on b, maybe even symbolic e.g. the round
657 # of the root of by = 6, i.e. round(root(by-6,y))
658 testResult = computeResultsAndJavaScriptFromAlgebra('x = a[b]')
659
660 console.log "testResult.latexResult " + testResult.latexResult
661 console.log "testResult.result " + testResult.result
662 console.log "testResult.code " + testResult.code
663 console.log "testResult.dependencyInfo.affectedBy " + testResult.dependencyInfo.affectedBy
664 if testResult.code == "x = function (a, b) { return ( a[b] ); }" and
665 testResult.latexResult == "$$x(a, b) = a[b]$$" and
666 testResult.result == "$$x(a, b) = a[b]$$" and
667 testResult.dependencyInfo.affectedBy.length == 3 and
668 testResult.dependencyInfo.affectedBy[0] == "a" and
669 testResult.dependencyInfo.affectedBy[1] == "b" and
670 testResult.dependencyInfo.affectedBy[2] == "PATTERN_DEPENDENCY" and
671 testResult.dependencyInfo.affectsVariables.length == 1 and
672 testResult.dependencyInfo.affectsVariables[0] == "x"
673 console.log "ok dependency test"
674 else
675 console.log "fail dependency tests "
676
677 do_clearall()
678
679 testResult = computeResultsAndJavaScriptFromAlgebra('x = a ⋅ b')
680
681 if testResult.code == "x = function (a, b) { return ( dot(a, b) ); }" and
682 testResult.latexResult == "$$x(a, b) = a \\cdot b$$" and
683 testResult.result == "$$x(a, b) = a \\cdot b$$" and
684 testResult.dependencyInfo.affectedBy.length == 3 and
685 testResult.dependencyInfo.affectedBy[0] == "a" and
686 testResult.dependencyInfo.affectedBy[1] == "b" and
687 testResult.dependencyInfo.affectedBy[2] == "PATTERN_DEPENDENCY" and
688 testResult.dependencyInfo.affectsVariables.length == 1 and
689 testResult.dependencyInfo.affectsVariables[0] == "x"
690 console.log "ok dependency test"
691 else
692 console.log "fail dependency tests"
693
694 do_clearall()
695
696 # Here we test an actual sequence of invokations form the
697 # notebook.
698
699 code1 = 'pattern(a_ᵀ⋅a_, cov(a_))'
700 code2 = 'PCA = Mᵀ·M'
701
702 # invokation sequence, we check that the
703 # full evaluation of the last piece of code
704 # gives the correct result.
705 computeDependenciesFromAlgebra code1
706 computeDependenciesFromAlgebra code2
707 computeResultsAndJavaScriptFromAlgebra code1
708 computeDependenciesFromAlgebra code1
709 computeDependenciesFromAlgebra code2
710 res = computeResultsAndJavaScriptFromAlgebra code2
711
712 if res.code == 'PCA = function (M) { return ( cov(M) ); }' and
713 res.latexResult == '$$PCA(M) = cov(M)$$' and
714 res.dependencyInfo.affectsVariables.length == 1 and
715 res.dependencyInfo.affectsVariables[0] == 'PCA' and
716 res.dependencyInfo.affectedBy.length == 2 and
717 res.dependencyInfo.affectedBy[0] == 'M' and
718 res.dependencyInfo.affectedBy[1] == 'PATTERN_DEPENDENCY'
719 console.log "ok dependency test"
720 else
721 console.log "fail dependency tests"
722
723
724
725 do_clearall()
726
727 # overwriting a pattern, as seen from the notebook
728
729 code1 = 'pattern(a_ + a_, 42 * a_)'
730 code2 = 'pattern(a_ + a_, 21 * a_)'
731 code3 = 'f = x + x'
732
733 computeResultsAndJavaScriptFromAlgebra code1
734 computeResultsAndJavaScriptFromAlgebra code2
735 res = computeResultsAndJavaScriptFromAlgebra code3
736
737 if res.code == 'f = function (x) { return ( 21*x ); }' and
738 res.latexResult == '$$f(x) = 21x$$' and
739 res.dependencyInfo.affectsVariables.length == 1 and
740 res.dependencyInfo.affectsVariables[0] == 'f' and
741 res.dependencyInfo.affectedBy.length == 2 and
742 res.dependencyInfo.affectedBy[0] == 'x' and
743 res.dependencyInfo.affectedBy[1] == 'PATTERN_DEPENDENCY'
744 console.log "ok dependency test"
745 else
746 console.log "fail dependency tests"
747
748
749
750 do_clearall()
751
752 # tests
753
754 res = computeResultsAndJavaScriptFromAlgebra "f=test(x<1,-x-4,3<=x,x*x+7,120/x+5)"
755
756 if res.code == 'f = function (x) { return ( (function(){if (((x) < (1))){return (-x - 4);} else if (((3) <= (x))){return (x*x + 7);}else {return (120 / x + 5);}})() ); }' and
757 res.latexResult == '$$f(x) = \\left\\{ \\begin{array}{ll}{-x-4} & if & {x} < {1} \\\\\\\\{xx+7} & if & {3} \\leq {x} \\\\\\\\{\\frac{120}{x}+5} & otherwise \\end{array} \\right.$$' and
758 res.dependencyInfo.affectsVariables.length == 1 and
759 res.dependencyInfo.affectsVariables[0] == 'f' and
760 res.dependencyInfo.affectedBy.length == 2 and
761 res.dependencyInfo.affectedBy[0] == 'x' and
762 res.dependencyInfo.affectedBy[1] == 'PATTERN_DEPENDENCY'
763 console.log "ok dependency test"
764 else
765 console.log "fail dependency tests"
766
767
768 do_clearall()
769
770 # tests
771
772 res = computeResultsAndJavaScriptFromAlgebra "f=floor(x) + ceiling(x) + round(x)"
773
774 if res.code == 'f = function (x) { return ( ceiling(x) + floor(x) + round(x) ); }' and
775 res.latexResult == '$$f(x) = \\lceil {x} \\rceil + \\lfloor {x} \\rfloor +round(x)$$' and
776 res.dependencyInfo.affectsVariables.length == 1 and
777 res.dependencyInfo.affectsVariables[0] == 'f' and
778 res.dependencyInfo.affectedBy.length == 2 and
779 res.dependencyInfo.affectedBy[0] == 'x' and
780 res.dependencyInfo.affectedBy[1] == 'PATTERN_DEPENDENCY'
781 console.log "ok dependency test"
782 else
783 console.log "fail dependency tests"
784
785
786 do_clearall()
787
788 console.log "-- done dependency tests"