Use mpdm_void() wherever possible.
[mpsl.git] / stress.c
blobeab1884d7abf547319d0030cc4b01a7ddeb0a304
1 /*
3 MPSL - Minimum Profit Scripting Language
4 Copyright (C) 2003/2009 Angel Ortega <angel@triptico.com>
6 stress.c - Stress tests for MPSL.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include <stdio.h>
27 #include <string.h>
28 #include <wchar.h>
30 #include "mpdm.h"
31 #include "mpsl.h"
33 /* total number of tests and oks */
34 int tests = 0;
35 int oks = 0;
37 /* failed tests messages */
38 char *failed_msgs[5000];
39 int i_failed_msgs = 0;
41 /*******************
42 Code
43 ********************/
45 #define do_test(s, o) _do_test(s, o ,__LINE__)
47 void _do_test(char *str, int ok, int line)
49 char tmp[1024];
51 sprintf(tmp, "%s (line %d): %s\n", str, line, ok ? "OK!" : "*** Failed ***");
52 printf(tmp);
54 tests++;
56 if (ok)
57 oks++;
58 else
59 failed_msgs[i_failed_msgs++] = strdup(tmp);
63 /* tests */
65 void do_set(mpdm_t * v1, mpdm_t v2)
67 mpdm_ref(v2);
68 mpdm_unref(*v1);
69 *v1 = v2;
73 #define do_test_mpsl(c) _do_test_mpsl(c, __LINE__)
75 mpdm_t _do_test_mpsl(char *code, int line)
77 static mpdm_t v = NULL;
79 do_set(&v, mpsl_compile(MPDM_MBS(code)));
81 printf("Compile: ");
82 _do_test(code, v != NULL, line);
83 return (v);
87 mpdm_t do_test_mpsl_file(char *file, mpdm_t inc)
89 static mpdm_t v = NULL;
91 do_set(&v, mpsl_compile_file(MPDM_MBS(file), inc));
93 printf("Compile file: ");
94 do_test(file, v != NULL);
95 return (v);
99 mpdm_t do_test_exec(mpdm_t x, mpdm_t a)
101 static mpdm_t v = NULL;
103 do_set(&v, mpdm_exec(x, a, NULL));
105 return (v);
109 void test_mpsl(void)
111 mpdm_t v;
113 printf("MPSL (Minimum Profit Scripting Language)\n\n");
115 v = mpsl_compile(MPDM_LS(L"a=1;"));
116 do_test_exec(v, NULL);
118 printf("MPSL compilation tests-------------\n");
120 do_test_mpsl("a=1;");
121 do_test_mpsl("a.b.c=1;");
122 do_test_mpsl("a.b.c=d;");
123 do_test_mpsl("a.b.c=d.e.f;");
124 do_test_mpsl("a[\"b\"]=1;");
125 do_test_mpsl("a[\"b\"].c=1;");
126 do_test_mpsl("a[\"b\"][\"c\"]=1;");
127 do_test_mpsl("/* empty hash */ days={};");
128 do_test_mpsl("days.lunes=\"monday\";");
129 do_test_mpsl("days[\"martes\"]=\"tuesday\";");
131 do_test_mpsl("1 + ((3 - 5) * 8);");
132 do_test_mpsl("1.5 + ((3.1 - 5.8) * 8.0);");
133 do_test_mpsl("a=1 + ((3 - 5) * 8);");
134 do_test_mpsl("2 + 3 * 4;");
135 do_test_mpsl("2 * 3 + 4;");
137 do_test_mpsl("/* hash */ y={ 'enero' => 'january', 'febrero' => 'february' };");
138 do_test_mpsl("/* array */ a=[\"this\", \"one\", \"is\", 666, \"cool\"];");
140 do_test_mpsl
141 ("/* greatest common divisor (Euclid's algorithm) */ sub gcd(m, n) { while (m > 0) { if(n > m) { local t = m; m = n; n = t; } m -= n; } n; }");
143 do_test_mpsl("/* range assign */ a = [ 1 .. 1000 ];");
145 do_test_mpsl("a.b.c ++;");
146 do_test_mpsl("a.b.c --;");
147 do_test_mpsl("a.b.c += 100;");
149 do_test_mpsl("foreach (a, [ 1 .. 1000 ]) { print(a); }");
151 do_test_mpsl("local a;");
152 do_test_mpsl("local a, b, c;");
153 do_test_mpsl("local a = 666;");
154 do_test_mpsl("local a; a = 666;");
156 do_test_mpsl("a > b - 1;");
157 do_test_mpsl("a > b - 1 && a < c + 1;");
159 do_test_mpsl("a = NULL;");
160 do_test_mpsl("a = 100; b = 200;c = 300;");
161 do_test_mpsl("sub test {a = 100; b = 200;c = 300;}");
162 do_test_mpsl("sub test (d, e) {a = 100; b = 200;c = b;}");
163 do_test_mpsl("a();");
164 do_test_mpsl("! 1 > 2;");
165 do_test_mpsl("! (1 > 2);");
166 do_test_mpsl("1 != 2;");
167 do_test_mpsl("\"hello\" ne \"goodbye\";");
169 mpdm_dump(do_test_mpsl("sub test(a, b) { c=1; }"));
170 mpdm_dump(do_test_mpsl("sub test(a, b) { c=1; d=2; }"));
171 mpdm_dump(do_test_mpsl("sub test(a, b) { c=1; d=2; e=3; }"));
175 void test_mpsl2(void)
177 mpdm_t v;
178 mpdm_t w;
180 /* execution tests */
181 v = do_test_mpsl("666;");
182 mpdm_dump(v);
183 v = do_test_exec(v, NULL);
184 do_test("literal number", mpdm_ival(v) == 666);
186 v = do_test_mpsl("\"goodbye\";");
187 v = do_test_exec(v, NULL);
188 do_test("literal string", mpdm_cmp(v, MPDM_LS(L"goodbye")) == 0);
190 v = do_test_mpsl("1 + 3 + 5;");
191 v = do_test_exec(v, NULL);
192 do_test("mpsl calculator 1", mpdm_rval(v) == 9.0);
194 v = do_test_mpsl("1 + ((3 - 5) * 8);");
195 v = do_test_exec(v, NULL);
196 do_test("mpsl calculator 2", mpdm_rval(v) == -15.0);
198 /* next value cannot be tested as an exact equality,
199 as rounding errors will manifest */
200 v = do_test_mpsl("1.5 + ((3.1 - 5.8) * 8.0);");
201 v = do_test_exec(v, NULL);
202 do_test("mpsl calculator 3", mpdm_rval(v) < -20.0 && mpdm_rval(v) > -21.0);
204 v = do_test_mpsl("2 + 3 * 4;");
205 v = do_test_exec(v, NULL);
206 do_test("mpsl calculator 4", mpdm_rval(v) == 14.0);
208 v = do_test_mpsl("2 * 3 + 4;");
209 v = do_test_exec(v, NULL);
210 do_test("mpsl calculator 5", mpdm_rval(v) == 10.0);
212 v = do_test_exec(do_test_mpsl("2 + 3 * 4;"), NULL);
213 mpdm_ref(v);
214 w = do_test_exec(do_test_mpsl("2 + (3 * 4);"), NULL);
215 do_test("mpsl calculator 6 (operator precedence)", mpdm_rval(v) == mpdm_rval(w));
216 mpdm_unref(v);
218 v = do_test_exec(do_test_mpsl("2 + 3 * 4;"), NULL);
219 mpdm_ref(v);
220 w = do_test_exec(do_test_mpsl("(2 + 3) * 4;"), NULL);
221 do_test("mpsl calculator 7 (operator precedence)", mpdm_rval(v) != mpdm_rval(w));
222 mpdm_unref(v);
224 v = do_test_mpsl("/* array */ [\"this\", \"one\", \"is\", 666, \"cool\"];");
225 v = do_test_exec(v, NULL);
226 mpdm_dump(v);
227 do_test("mpsl array", mpdm_ival(mpdm_aget(v, 3)) == 666);
229 v = do_test_mpsl
230 ("/* hash */ { \"enero\" => \"january\", \"febrero\" => \"february\" };");
231 v = do_test_exec(v, NULL);
232 mpdm_dump(v);
233 do_test("mpsl hash", mpdm_cmp(mpdm_hget(v,
234 MPDM_LS(L"febrero")),
235 MPDM_LS(L"february")) == 0);
237 v = do_test_mpsl("! 1;");
238 v = do_test_exec(v, NULL);
239 do_test("boolean not 1", v == NULL);
240 v = do_test_mpsl("! 0;");
241 v = do_test_exec(v, NULL);
242 do_test("boolean not 2", v != NULL);
244 v = do_test_mpsl("1 && 3;");
245 v = do_test_exec(v, NULL);
246 do_test("boolean and 1", mpdm_ival(v) == 3);
247 v = do_test_mpsl("1 && 0;");
248 v = do_test_exec(v, NULL);
249 do_test("boolean and 2", !mpsl_is_true(v));
250 v = do_test_mpsl("0 && 1;");
251 v = do_test_exec(v, NULL);
252 do_test("boolean and 3", !mpsl_is_true(v));
254 v = do_test_mpsl("1 || 3;");
255 v = do_test_exec(v, NULL);
256 do_test("boolean or 1", mpdm_ival(v) == 1);
257 v = do_test_mpsl("2 || 0;");
258 v = do_test_exec(v, NULL);
259 do_test("boolean or 2", mpdm_ival(v) == 2);
260 v = do_test_mpsl("0 || 3;");
261 v = do_test_exec(v, NULL);
262 do_test("boolean or 3", mpdm_ival(v) == 3);
264 v = do_test_mpsl("6 == 6;");
265 v = do_test_exec(v, NULL);
266 do_test("numeric == 1", v != NULL);
267 v = do_test_mpsl("8.0 == 8.0;");
268 v = do_test_exec(v, NULL);
269 do_test("numeric == 2", v != NULL);
270 v = do_test_mpsl("6 == 8;");
271 v = do_test_exec(v, NULL);
272 do_test("numeric == 3", v == NULL);
274 v = do_test_mpsl("6 != 6;");
275 v = do_test_exec(v, NULL);
276 do_test("numeric != 1", v == NULL);
277 v = do_test_mpsl("8.0 != 8.0;");
278 v = do_test_exec(v, NULL);
279 do_test("numeric != 2", v == NULL);
280 v = do_test_mpsl("6 != 8;");
281 v = do_test_exec(v, NULL);
282 do_test("numeric != 3", v != NULL);
284 v = do_test_mpsl("6 < 6;");
285 v = do_test_exec(v, NULL);
286 do_test("numeric < 1", v == NULL);
287 v = do_test_mpsl("8 < 6;");
288 v = do_test_exec(v, NULL);
289 do_test("numeric < 2", v == NULL);
290 v = do_test_mpsl("5 < 6;");
291 v = do_test_exec(v, NULL);
292 do_test("numeric < 3", v != NULL);
294 v = do_test_mpsl("6 > 6;");
295 v = do_test_exec(v, NULL);
296 do_test("numeric > 1", v == NULL);
297 v = do_test_mpsl("8 > 6;");
298 v = do_test_exec(v, NULL);
299 do_test("numeric > 2", v != NULL);
300 v = do_test_mpsl("5 > 6;");
301 v = do_test_exec(v, NULL);
302 do_test("numeric > 3", v == NULL);
304 v = do_test_mpsl("6 <= 6;");
305 v = do_test_exec(v, NULL);
306 do_test("numeric <= 1", v != NULL);
307 v = do_test_mpsl("8 <= 6;");
308 v = do_test_exec(v, NULL);
309 do_test("numeric <= 2", v == NULL);
310 v = do_test_mpsl("5 <= 6;");
311 v = do_test_exec(v, NULL);
312 do_test("numeric <= 3", v != NULL);
314 v = do_test_mpsl("6 >= 6;");
315 v = do_test_exec(v, NULL);
316 do_test("numeric >= 1", v != NULL);
317 v = do_test_mpsl("8 >= 6;");
318 v = do_test_exec(v, NULL);
319 do_test("numeric >= 2", v != NULL);
320 v = do_test_mpsl("5 >= 6;");
321 v = do_test_exec(v, NULL);
322 do_test("numeric >= 3", v == NULL);
324 v = do_test_mpsl("11 % 6;");
325 v = do_test_exec(v, NULL);
326 do_test("modulo", mpdm_ival(v) == 5);
328 v = do_test_mpsl("variable=16384;");
329 mpdm_dump(v);
330 v = do_test_exec(v, NULL);
331 do_test("assign 1", mpdm_ival(v) == 16384);
333 v = do_test_mpsl("array=[10, 20, 30, 40];");
334 v = do_test_exec(v, NULL);
335 do_test("assign 2", mpdm_ival(mpdm_aget(v, 2)) == 30);
337 v = do_test_mpsl("a=1; b=2; c=3;");
338 mpdm_dump(v);
339 v = do_test_exec(v, NULL);
341 v = do_test_mpsl("CACHE={}; CACHE.regex=[]; CACHE.regex[0]=12345;");
342 v = do_test_exec(v, NULL);
344 v = do_test_mpsl("variable;");
345 v = do_test_exec(v, NULL);
346 do_test("symval 1", mpdm_ival(v) == 16384);
348 v = do_test_mpsl("variable2=1 + ((3 - 5) * 8); variable2;");
349 mpdm_dump(v);
350 v = do_test_exec(v, NULL);
351 do_test("symval 2", mpdm_rval(v) == -15);
353 v = do_test_mpsl("variable3=variable2 * 2;");
354 v = do_test_exec(v, NULL);
355 do_test("symval 3", mpdm_ival(v) == -30);
357 v = do_test_mpsl("sub mysum(a, b) { a + b; }");
358 mpdm_dump(v);
359 v = do_test_exec(v, NULL);
360 do_test("sub 1", v != NULL);
362 v = do_test_mpsl("sub pi() { 3.1416; }");
363 mpdm_dump(v);
364 v = do_test_exec(v, NULL);
365 do_test("sub 2", v != NULL);
367 v = do_test_mpsl("var10=pi();");
368 v = do_test_exec(v, NULL);
369 do_test("exec 1", mpdm_rval(v) == 3.1416);
371 v = do_test_mpsl("var11=pi() * 10000; var11;");
372 v = do_test_exec(v, NULL);
373 do_test("exec 2", mpdm_rval(v) == 31416);
375 v = do_test_mpsl("mysum(100, 20);");
376 v = do_test_exec(v, NULL);
377 do_test("exec 3", mpdm_rval(v) == 120.0);
379 v = do_test_mpsl("a = NULL;");
380 v = do_test_exec(v, NULL);
381 do_test("NULL 1", v == NULL);
383 v = do_test_mpsl("a == NULL;");
384 v = do_test_exec(v, NULL);
385 do_test("NULL 2", mpdm_ival(v) == 1);
387 v = do_test_mpsl("local a, b; a = 1; b = 2;");
388 v = do_test_exec(v, NULL);
390 v = do_test_mpsl("a == NULL;");
391 v = do_test_exec(v, NULL);
392 do_test("local 1", mpdm_ival(v) == 1);
394 v = do_test_mpsl("66 * -1;");
395 v = do_test_exec(v, NULL);
396 do_test("uminus", mpdm_ival(v) == -66);
398 v = do_test_mpsl("\"test\" eq \"test\";");
399 v = do_test_exec(v, NULL);
400 do_test("streq 1", mpsl_is_true(v));
402 v = do_test_mpsl("\"test\" eq \"prueba\";");
403 v = do_test_exec(v, NULL);
404 do_test("streq 1", !mpsl_is_true(v));
406 v = do_test_mpsl("a = 6; ++ a;");
407 v = do_test_exec(v, NULL);
408 do_test("pinc", mpdm_ival(v) == 7);
410 v = do_test_mpsl("a++;");
411 v = do_test_exec(v, NULL);
412 do_test("sinc", mpdm_ival(v) == 7);
414 v = do_test_mpsl("a += 10;");
415 v = do_test_exec(v, NULL);
416 do_test("iadd", mpdm_ival(v) == 18);
418 v = do_test_mpsl("local a, b, c; a=1; b=2; c=3; if(a == b) c=1000; c;");
419 v = do_test_exec(v, NULL);
420 do_test("if 1", mpdm_ival(v) == 3);
422 v = do_test_mpsl("local a, b, c; a=1; b=2; c=3; if(a <= b) c=1000; c;");
423 v = do_test_exec(v, NULL);
424 do_test("if 2", mpdm_ival(v) == 1000);
426 v = do_test_mpsl("local a, b, c; a=1; b=2; if(a == b) c=1000; else c=2000; c;");
427 v = do_test_exec(v, NULL);
428 do_test("ifelse", mpdm_ival(v) == 2000);
430 v = do_test_mpsl("local a; a = 0; while(a < 100) { a++; } a;");
431 v = do_test_exec(v, NULL);
432 do_test("ifelse", mpdm_ival(v) == 100);
434 v = do_test_mpsl("a=mysum(100, 50); a;");
435 v = do_test_exec(v, NULL);
436 do_test("mysum 1", mpdm_ival(v) == 150);
438 v = do_test_mpsl("a=mysum(2000, 500); a;");
439 v = do_test_exec(v, NULL);
440 do_test("mysum 2", mpdm_ival(v) == 2500);
442 w = mpdm_ref(MPDM_A(2));
443 mpdm_aset(w, MPDM_I(100), 0);
444 mpdm_aset(w, MPDM_I(50), 1);
446 /* asks for the value of the mysum symbol (the code) */
447 v = do_test_mpsl("mysum;");
448 /* executes, so mysum() itself is being returned */
449 v = do_test_exec(v, NULL);
450 mpdm_dump(v);
451 do_test("mysum 3", mpdm_ival(do_test_exec(v, w)) == 150);
453 mpdm_aset(w, MPDM_I(75), 1);
454 do_test("mysum 4", mpdm_ival(do_test_exec(v, w)) == 175);
456 /* compiles (and executes) the definition of gcd() */
457 v = do_test_mpsl
458 ("/* greatest common divisor (Euclid's algorithm) */ sub gcd(m, n) { while (m > 0) { if(n > m) { local t = m; m = n; n = t; } m -= n; } n; }");
459 do_test_exec(v, NULL);
461 /* gets a pointer to gcd() */
462 v = do_test_exec(do_test_mpsl("gcd;"), NULL);
463 mpdm_dump(v);
465 /* executes gcd(100, 50); */
466 mpdm_aset(w, MPDM_I(50), 1);
467 do_test("gcd() 1", mpdm_ival(do_test_exec(v, w)) == 50);
469 /* executes gcd(100, 75); */
470 mpdm_aset(w, MPDM_I(75), 1);
471 do_test("gcd() 2", mpdm_ival(do_test_exec(v, w)) == 25);
473 mpdm_unref(w);
475 /* string concatenation */
476 w = mpdm_ref(MPDM_LS(L"big lebowski"));
478 v = do_test_mpsl("\"big\" ~ \" lebowski\";");
479 do_test("~ (strcat 1)", mpdm_cmp(do_test_exec(v, NULL), w) == 0);
481 v = do_test_mpsl("\"big\" ~ \" \" ~ \"lebowski\";");
482 do_test("~ (strcat 2)", mpdm_cmp(do_test_exec(v, NULL), w) == 0);
484 mpdm_unref(w);
488 void test_mpsl3(void)
490 mpdm_t v;
492 v = do_test_mpsl("v=[10,20]; w=v[0]; w;");
493 mpdm_dump(v);
494 v = do_test_exec(v, NULL);
495 mpdm_dump(v);
497 /* library functions tests */
498 v = do_test_mpsl("dump( [1, 2, 3, 4, 5] );");
499 do_test_exec(v, NULL);
501 v = do_test_mpsl("if(size([2, 3, 4]) == 3) { dump(\"YES\"); } else { dump(\"NO\"); }");
502 do_test_exec(v, NULL);
504 v = do_test_mpsl("is_array(1);");
505 do_test("is_array 1", do_test_exec(v, NULL) == NULL);
506 v = do_test_mpsl("is_array([]);");
507 do_test("is_array 2", do_test_exec(v, NULL) != NULL);
508 v = do_test_mpsl("is_array({});");
509 do_test("is_array 3", do_test_exec(v, NULL) != NULL);
511 v = do_test_mpsl("is_hash(1);");
512 do_test("is_hash 1", do_test_exec(v, NULL) == NULL);
513 v = do_test_mpsl("is_hash([]);");
514 do_test("is_hash 2", do_test_exec(v, NULL) == NULL);
515 v = do_test_mpsl("is_hash({});");
516 do_test("is_hash 3", do_test_exec(v, NULL) != NULL);
518 v = do_test_mpsl("v=splice(\"inventions of life\", NULL, 0, 10); v[1];");
519 v = do_test_exec(v, NULL);
520 do_test("splice 1", mpdm_cmp(v, MPDM_LS(L"inventions")) == 0);
522 v = do_test_mpsl("v[0];");
523 v = do_test_exec(v, NULL);
524 do_test("splice 2", mpdm_cmp(v, MPDM_LS(L" of life")) == 0);
526 v = do_test_mpsl("sub func() { if(1 == 1) { return(6); 24; } 12; }");
527 v = do_test_exec(v, NULL);
528 v = do_test_mpsl("a=func();");
529 v = do_test_exec(v, NULL);
530 do_test("return 1", mpdm_ival(v) == 6);
532 v = do_test_mpsl("a=func(); 500;");
533 v = do_test_exec(v, NULL);
534 do_test("return 2", mpdm_ival(v) == 500);
536 v = do_test_mpsl("sub func() { while(1 == 1) { return(6); 24; } 12; }");
537 v = do_test_exec(v, NULL);
538 v = do_test_mpsl("a=func();");
539 v = do_test_exec(v, NULL);
540 do_test("return 3", mpdm_ival(v) == 6);
542 v = do_test_mpsl("sub func() { foreach(a, [1 .. 10]) { return(6); 24; } 12; }");
543 v = do_test_exec(v, NULL);
544 v = do_test_mpsl("a=func();");
545 v = do_test_exec(v, NULL);
546 do_test("return 4", mpdm_ival(v) == 6);
548 v = do_test_mpsl("a=1; while(a < 10) { a++; } a;");
549 v = do_test_exec(v, NULL);
550 do_test("while", mpdm_ival(v) == 10);
552 v = do_test_mpsl("a=1; while(a < 10) { if(a == 5) break; a++; } a;");
553 v = do_test_exec(v, NULL);
554 do_test("break", mpdm_ival(v) == 5);
556 v = do_test_mpsl("b=0; foreach(a, [1, 2, 3, 4]) { dump(a); b += a; } return(b);");
557 v = do_test_exec(v, NULL);
558 do_test("foreach", mpdm_ival(v) == 10);
560 v = do_test_mpsl("b=0; foreach(a, [1 .. 20]) { dump(a); b += a; } return(b);");
561 v = do_test_exec(v, NULL);
562 do_test("foreach+range 1", mpdm_ival(v) == 210);
564 v = do_test_mpsl("b=0; foreach(a, [20 .. 1]) { dump(a); b += a; } return(b);");
565 v = do_test_exec(v, NULL);
566 do_test("foreach+range 2", mpdm_ival(v) == 210);
568 v = do_test_mpsl("print(\"print: \", 1, 2, [1, 2, 3], func(), 4);");
569 v = do_test_exec(v, NULL);
571 v = do_test_mpsl("'This is\\na string.';");
572 v = do_test_exec(v, NULL);
573 mpdm_dump(v);
575 v = do_test_mpsl("\"This is\\na string.\";");
576 v = do_test_exec(v, NULL);
577 mpdm_dump(v);
579 v = do_test_mpsl("sub t(h) { dump(h); dump(h.x); h.x; } H={}; H.x=5; t(H);");
580 v = do_test_exec(v, NULL);
581 do_test("Accesing a hash's component passed as argument", mpdm_ival(v) == 5);
583 /* mpdm_dump(mpdm_root());*/
587 void test_mpsl_file(void)
589 mpdm_t v;
590 mpdm_t inc;
592 mpsl_trap(NULL);
594 inc = mpdm_ref(MPDM_A(0));
595 mpdm_push(inc, MPDM_LS(L"."));
597 printf("Compiling from file:\n");
598 v = do_test_mpsl_file("test.mpsl", inc);
599 v = do_test_exec(v, NULL);
601 mpdm_unref(inc);
605 void test_abort_and_eval(void)
607 mpdm_t v;
608 mpdm_t e;
610 mpsl_abort = 0;
611 v = mpdm_ref(mpsl_compile(MPDM_LS(L"1000;")));
612 do_test("Abort 1", mpdm_ival(mpdm_exec(v, NULL, NULL)) == 1000);
614 /* set global abort function */
615 mpsl_abort = 1;
616 do_test("Abort 2", mpdm_exec(v, NULL, NULL) == NULL);
618 mpsl_abort = 0;
619 do_test("Abort 3", mpdm_ival(mpdm_exec(v, NULL, NULL)) == 1000);
620 mpdm_unref(v);
622 mpsl_error(NULL);
624 v = mpsl_eval(MPDM_LS(L"invalid_code()"), NULL, NULL);
625 e = mpdm_hget_s(mpdm_root(), L"ERROR");
626 printf("The following error is OK:\n");
627 mpdm_dump(e);
628 do_test("eval 1", v == NULL && e != NULL);
630 v = mpsl_eval(MPDM_LS(L"undef_func();"), NULL, NULL);
631 e = mpdm_hget_s(mpdm_root(), L"ERROR");
632 printf("The following error is also OK:\n");
633 mpdm_dump(e);
634 do_test("eval 2", v == NULL && e != NULL);
636 v = mpsl_eval(MPDM_LS(L"load('unexistent_file.mpsl');"), NULL, NULL);
637 e = mpdm_hget_s(mpdm_root(), L"ERROR");
638 printf("The following error is also OK:\n");
639 mpdm_dump(e);
640 do_test("eval 3", v == NULL && e != NULL);
642 v = mpsl_eval(MPDM_LS(L"2000;"), NULL, NULL);
643 e = mpdm_hget_s(mpdm_root(), L"ERROR");
644 do_test("eval 4", mpdm_ival(v) == 2000 && e == NULL);
648 int main(void)
650 mpsl_startup();
652 test_mpsl();
653 test_mpsl2();
654 test_mpsl3();
655 test_mpsl_file();
656 test_abort_and_eval();
658 mpsl_shutdown();
660 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
662 if (oks == tests)
663 printf("*** ALL TESTS PASSED\n");
664 else {
665 int n;
667 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
669 printf("\nFailed tests:\n\n");
670 for (n = 0; n < i_failed_msgs; n++)
671 printf("%s", failed_msgs[n]);
674 return (0);