Use mpdm_string() instead of v->data in regex1().
[mpdm.git] / stress.c
blob2709e895a21666f9a416484d6d3400e9d151b3b2
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 stress.c - Stress tests for MPDM.
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 <time.h>
32 #include "mpdm.h"
34 /* total number of tests and oks */
35 int tests = 0;
36 int oks = 0;
38 /* failed tests messages */
39 char *failed_msgs[5000];
40 int i_failed_msgs = 0;
42 int do_benchmarks = 0;
43 int do_multibyte_sregex_tests = 0;
46 /** code **/
48 void _do_test(char *str, int ok, int src_line)
50 char tmp[1024];
52 sprintf(tmp, "%s: %s (line %d)\n", str, ok ? "OK!" : "*** Failed ***",
53 src_line);
54 printf(tmp);
56 tests++;
58 if (ok)
59 oks++;
60 else
61 failed_msgs[i_failed_msgs++] = strdup(tmp);
64 #define do_test(str, ok) _do_test(str, ok, __LINE__)
66 #define C { int CC = mpdm->count
67 #define T(i) do_test("v counter", CC + i == mpdm->count); }
69 /** tests **/
71 void test_counter(void)
73 mpdm_t v, w;
76 v = MPDM_S(L"hi");
77 T(1);
80 w = MPDM_A(0);
81 T(1);
84 mpdm_push(w, v);
85 T(0);
88 mpdm_adel(w, 0);
89 T(0); /* should fail in 2.x */
92 mpdm_queue(w, v, 10);
93 T(0);
96 v = MPDM_S(L"this is a phrase");
97 T(1);
99 w = mpdm_split_s(v, L" ");
100 T(5);
104 void test_basic(void)
106 int i;
107 double r;
108 mpdm_t v;
109 mpdm_t w;
110 mpdm_t t;
112 v = mpdm_ref(MPDM_S(L"65536"));
113 mpdm_dump(v);
114 i = mpdm_ival(v);
116 do_test("i == 65536", (i == 65536));
117 do_test("v has MPDM_IVAL", (v->flags & MPDM_IVAL));
119 r = mpdm_rval(v);
120 do_test("r == 65536", (r == 65536.0));
121 do_test("v has MPDM_RVAL", (v->flags & MPDM_RVAL));
123 mpdm_unref(v);
125 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
126 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
128 /* partial copies of strings */
129 v = MPDM_LS(L"this is not America");
130 v = MPDM_NS((wchar_t *) v->data + 4, 4);
132 do_test("Partial string values", mpdm_cmp(v, MPDM_LS(L" is ")) == 0);
134 v = mpdm_ref(MPDM_S(L"MUAHAHAHA!"));
135 w = mpdm_ref(mpdm_clone(v));
136 do_test("Testing mpdm_clone semantics 1", w == v);
137 mpdm_unref(v);
138 mpdm_unref(w);
140 v = mpdm_ref(MPDM_A(2));
141 mpdm_aset(v, MPDM_S(L"evil"), 0);
142 mpdm_aset(v, MPDM_S(L"dead"), 1);
143 w = mpdm_ref(mpdm_clone(v));
145 do_test("Testing mpdm_clone semantics 2.1", w != v);
147 t = mpdm_aget(v, 0);
148 do_test("Testing mpdm_clone semantics 2.2", t->ref > 1);
149 do_test("Testing mpdm_clone semantics 2.3", mpdm_aget(w, 0) == t);
151 mpdm_unref(w);
152 mpdm_unref(v);
154 /* mbs / wcs tests */
155 v = MPDM_MBS("This is (was) a multibyte string");
156 mpdm_dump(v);
158 /* greek omega is 03a9 */
159 v = MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
160 mpdm_dump(v);
161 printf
162 ("(Previous value will be NULL if locale doesn't match stress.c encoding)\n");
164 v = MPDM_LS(L"A capital greek omega between brackets [\x03a9]");
165 mpdm_dump(v);
166 printf("(Previous value will only show on an Unicode terminal)\n");
168 v = mpdm_ref(MPDM_R(3.1416));
169 mpdm_dump(v);
170 do_test("rval 1", mpdm_rval(v) == 3.1416);
171 do_test("ival 1", mpdm_ival(v) == 3);
172 mpdm_unref(v);
174 v = MPDM_R(777777.0 / 2.0);
175 mpdm_dump(v);
176 do_test("mpdm_rnew 1", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
178 v = MPDM_R(388888.500);
179 mpdm_dump(v);
180 do_test("mpdm_rnew 2", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
182 v = MPDM_R(388888.412);
183 mpdm_dump(v);
184 do_test("mpdm_rnew 3", mpdm_cmp(v, MPDM_LS(L"388888.412")) == 0);
186 v = MPDM_R(388888.6543);
187 mpdm_dump(v);
188 do_test("mpdm_rnew 4", mpdm_cmp(v, MPDM_LS(L"388888.6543")) == 0);
190 v = MPDM_R(388888.0);
191 mpdm_dump(v);
192 do_test("mpdm_rnew 5", mpdm_cmp(v, MPDM_LS(L"388888")) == 0);
194 v = MPDM_R(0.050000);
195 mpdm_dump(v);
196 do_test("mpdm_rnew 6", mpdm_cmp(v, MPDM_LS(L"0.05")) == 0);
198 v = MPDM_R(0.000);
199 mpdm_dump(v);
200 do_test("mpdm_rnew 7", mpdm_cmp(v, MPDM_LS(L"0")) == 0);
202 v = MPDM_LS(L"0177");
203 do_test("mpdm_ival() for octal numbers", mpdm_ival(v) == 0x7f);
205 v = MPDM_LS(L"0xFF");
206 do_test("mpdm_ival() for hexadecimal numbers", mpdm_ival(v) == 255);
208 v = MPDM_LS(L"001");
209 do_test("mpdm_rval() for octal numbers", mpdm_rval(v) == 1.0);
211 v = MPDM_LS(L"0x7f");
212 do_test("mpdm_rval() for hexadecimal numbers", mpdm_ival(v) == 127.0);
214 do_test("Two NULLs are equal", mpdm_cmp(NULL, NULL) == 0);
216 v = MPDM_LS(L"hahaha");
217 mpdm_ref(v);
218 do_test("mpdm_cmp_s 1", mpdm_cmp_s(v, L"hahaha") == 0);
219 do_test("mpdm_cmp_s 2", mpdm_cmp_s(v, L"aahaha") > 0);
220 do_test("mpdm_cmp_s 3", mpdm_cmp_s(v, L"zahaha") < 0);
221 mpdm_unref(v);
224 mpdm_t sort_cb(mpdm_t args)
226 int d;
228 /* sorts reversely */
229 d = mpdm_cmp(mpdm_aget(args, 1), mpdm_aget(args, 0));
230 return (MPDM_I(d));
234 void test_array(void)
236 int n;
237 mpdm_t a;
238 mpdm_t v;
240 a = MPDM_A(0);
241 mpdm_ref(a);
243 do_test("a->size == 0", (a->size == 0));
245 mpdm_push(a, MPDM_LS(L"sunday"));
246 mpdm_push(a, MPDM_LS(L"monday"));
247 mpdm_push(a, MPDM_LS(L"tuesday"));
248 mpdm_push(a, MPDM_LS(L"wednesday"));
249 mpdm_push(a, MPDM_LS(L"thursday"));
250 mpdm_push(a, MPDM_LS(L"friday"));
251 mpdm_push(a, MPDM_LS(L"saturday"));
252 mpdm_dump(a);
253 do_test("a->size == 7", (a->size == 7));
255 v = mpdm_aget(a, 3);
256 mpdm_ref(v);
257 mpdm_aset(a, NULL, 3);
258 mpdm_dump(a);
260 mpdm_sort(a, 1);
261 do_test("NULLs are sorted on top", (mpdm_aget(a, 0) == NULL));
263 mpdm_aset(a, v, 0);
264 mpdm_unref(v);
266 v = mpdm_aget(a, 3);
267 do_test("v is referenced again", (v != NULL && v->ref > 0));
269 mpdm_sort(a, 1);
270 do_test("mpdm_asort() works (1)",
271 mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"friday")) == 0);
272 do_test("mpdm_asort() works (2)",
273 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"wednesday")) == 0);
275 /* asort_cb sorts reversely */
276 mpdm_sort_cb(a, 1, MPDM_X(sort_cb));
278 do_test("mpdm_asort_cb() works (1)",
279 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"friday")) == 0);
280 do_test("mpdm_asort_cb() works (2)",
281 mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"wednesday")) == 0);
283 v = mpdm_aget(a, 3);
284 mpdm_ref(v);
285 n = v->ref;
286 mpdm_collapse(a, 3, 1);
287 do_test("acollapse unrefs values", (v->ref < n));
288 mpdm_unref(v);
290 mpdm_unref(a);
292 /* test queues */
293 a = MPDM_A(0);
294 mpdm_ref(a);
296 /* add several values */
297 for (n = 0; n < 10; n++)
298 v = mpdm_queue(a, MPDM_I(n), 10);
300 do_test("queue should still output NULL", (v == NULL));
302 v = mpdm_queue(a, MPDM_I(11), 10);
303 do_test("queue should no longer output NULL", (v != NULL));
305 v = mpdm_queue(a, MPDM_I(12), 10);
306 do_test("queue should return 1", mpdm_ival(v) == 1);
307 v = mpdm_queue(a, MPDM_I(13), 10);
308 do_test("queue should return 2", mpdm_ival(v) == 2);
309 do_test("queue size should be 10", a->size == 10);
311 mpdm_dump(a);
312 v = mpdm_queue(a, MPDM_I(14), 5);
313 mpdm_dump(a);
315 do_test("queue size should be 5", a->size == 5);
316 do_test("last taken value should be 8", mpdm_ival(v) == 8);
317 mpdm_unref(a);
319 a = MPDM_A(4);
320 mpdm_ref(a);
321 mpdm_aset(a, MPDM_I(666), 6000);
323 do_test("array should have been automatically expanded",
324 mpdm_size(a) == 6001);
326 v = mpdm_aget(a, -1);
327 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
329 mpdm_aset(a, MPDM_I(777), -2);
330 v = mpdm_aget(a, 5999);
331 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
333 mpdm_push(a, MPDM_I(888));
334 v = mpdm_aget(a, -1);
335 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
337 v = MPDM_A(0);
338 mpdm_ref(v);
339 mpdm_push(v, MPDM_I(100));
340 mpdm_pop(v);
341 mpdm_unref(v);
343 mpdm_unref(a);
345 /* array comparisons with mpdm_cmp() */
346 a = MPDM_A(2);
347 mpdm_ref(a);
348 mpdm_aset(a, MPDM_I(10), 0);
349 mpdm_aset(a, MPDM_I(60), 1);
351 v = mpdm_ref(mpdm_clone(a));
352 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
354 mpdm_adel(v, -1);
355 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
357 mpdm_push(v, MPDM_I(80));
358 do_test("mpdm_cmp: 2# element is bigger, so array is bigger",
359 mpdm_cmp(a, v) < 0);
361 mpdm_unref(v);
362 mpdm_unref(a);
366 void test_hash(void)
368 mpdm_t h;
369 mpdm_t v;
370 int i, n;
372 h = MPDM_H(0);
373 mpdm_ref(h);
375 do_test("hsize 1", mpdm_hsize(h) == 0);
377 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
378 v = mpdm_hget(h, MPDM_S(L"mp"));
380 do_test("hsize 2", mpdm_hsize(h) == 1);
382 do_test("hash: v != NULL", (v != NULL));
383 i = mpdm_ival(v);
384 do_test("hash: v == 6", (i == 6));
386 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
387 v = mpdm_hget(h, MPDM_S(L"mp2"));
389 do_test("hsize 3", mpdm_hsize(h) == 2);
391 do_test("hash: v != NULL", (v != NULL));
392 i = mpdm_ival(v);
393 do_test("hash: v == 66", (i == 66));
395 /* fills 100 values */
396 for (n = 0; n < 50; n++)
397 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
398 for (n = 100; n >= 50; n--)
399 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
401 do_test("hsize 4", mpdm_hsize(h) == 103);
403 /* tests 100 values */
404 for (n = 0; n < 100; n++) {
405 v = mpdm_hget(h, MPDM_I(n));
407 if (v != NULL) {
408 i = mpdm_ival(v);
409 if (!(i == n * 10))
410 do_test("hash: ival", (i == n * 10));
412 else
413 do_test("hash: hget", (v != NULL));
416 printf("h's size: %d\n", mpdm_hsize(h));
418 mpdm_hdel(h, MPDM_LS(L"mp"));
419 do_test("hsize 5", mpdm_hsize(h) == 102);
421 mpdm_unref(h);
424 mpdm_dump(h);
426 v=mpdm_hkeys(h);
427 mpdm_dump(v);
429 /* use of non-strings as hashes */
430 h = MPDM_H(0);
431 mpdm_ref(h);
433 v = MPDM_A(0);
434 mpdm_hset(h, v, MPDM_I(1234));
435 v = MPDM_H(0);
436 mpdm_hset(h, v, MPDM_I(12345));
437 v = MPDM_H(0);
438 mpdm_hset(h, v, MPDM_I(9876));
439 v = MPDM_A(0);
440 mpdm_ref(v);
441 mpdm_hset(h, v, MPDM_I(6543));
442 i = mpdm_ival(mpdm_hget(h, v));
443 mpdm_unref(v);
445 mpdm_dump(h);
446 do_test("hash: using non-strings as hash keys", (i == 6543));
448 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
450 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
451 do_test("exists 2", !mpdm_exists(h, MPDM_LS(L"notok")));
453 mpdm_dump(h);
454 v = mpdm_hget_s(h, L"ok");
455 printf("v %s\n", v == NULL ? "is NULL" : "is NOT NULL");
456 do_test("hget_s 1", mpdm_ival(v) == 666);
457 mpdm_dump(v);
458 mpdm_dump(h);
460 i = 0;
461 for (n = 0; n < 1000; n++) {
462 if (mpdm_hget_s(h, L"ok") != NULL)
463 i++;
465 printf("i: %d\n", i);
466 do_test("hget_s 1.2", i == 1000);
468 if (i != 1000)
469 mpdm_hget_s(h, L"ok");
471 do_test("hget 1.2.1", mpdm_hget(h, MPDM_LS(L"ok")) != NULL);
473 mpdm_hset_s(h, L"ok", MPDM_I(777));
475 v = mpdm_hget_s(h, L"ok");
476 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
478 mpdm_unref(h);
482 void test_splice(void)
484 mpdm_t w;
485 mpdm_t v;
487 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"), MPDM_LS(L"special "), 4,
489 do_test("splice insertion",
490 mpdm_cmp(mpdm_aget(w, 0),
491 MPDM_LS(L"I'm special agent Johnson")) == 0);
492 mpdm_dump(w);
494 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
495 do_test("splice insertion and deletion (1)",
496 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
497 do_test("splice insertion and deletion (2)",
498 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
499 mpdm_dump(w);
501 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
502 do_test("splice deletion (1)",
503 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
504 do_test("splice deletion (2)",
505 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
506 mpdm_dump(w);
508 v = MPDM_LS(L"It doesn't matter");
509 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
510 do_test("splice insertion at the end",
511 mpdm_cmp(mpdm_aget(w, 0),
512 MPDM_LS(L"It doesn't matter two")) == 0);
513 mpdm_dump(w);
515 w = mpdm_splice(NULL, NULL, 0, 0);
516 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
518 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
519 do_test("splice with first value NULL",
520 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
522 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
523 do_test("splice with second value NULL",
524 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
526 v = MPDM_LS(L"I'm testing");
527 mpdm_ref(v);
529 w = mpdm_splice(v, NULL, 0, -1);
530 do_test("splice with negative del (1)",
531 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
533 w = mpdm_splice(v, NULL, 4, -1);
534 do_test("splice with negative del (2)",
535 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
537 w = mpdm_splice(v, NULL, 4, -2);
538 do_test("splice with negative del (3)",
539 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
541 w = mpdm_splice(v, NULL, 0, -4);
542 do_test("splice with negative del (4)",
543 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
544 mpdm_dump(mpdm_aget(w, 0));
546 w = mpdm_splice(v, NULL, 4, -20);
547 do_test("splice with out-of-bounds negative del",
548 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
549 mpdm_unref(v);
553 void test_strcat(void)
555 mpdm_t v;
556 mpdm_t w;
558 w = MPDM_LS(L"something");
559 mpdm_ref(w);
561 v = mpdm_strcat(NULL, NULL);
562 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
564 v = mpdm_strcat(NULL, w);
565 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
567 v = mpdm_strcat(w, NULL);
568 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
569 mpdm_unref(w);
571 w = MPDM_LS(L"");
572 mpdm_ref(w);
573 v = mpdm_strcat(NULL, w);
574 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
576 v = mpdm_strcat(w, NULL);
577 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
579 v = mpdm_strcat(w, w);
580 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
582 mpdm_unref(w);
586 void test_split(void)
588 mpdm_t w;
590 printf("mpdm_split test\n\n");
592 w = mpdm_split(MPDM_S(L"four.elems.in.string"), MPDM_S(L"."));
593 mpdm_dump(w);
594 do_test("4 elems: ", (w->size == 4));
596 w = mpdm_split(MPDM_S(L"unseparated string"), MPDM_S(L"."));
597 mpdm_dump(w);
598 do_test("1 elem: ", (w->size == 1));
600 w = mpdm_split(MPDM_S(L".dot.at start"), MPDM_S(L"."));
601 mpdm_dump(w);
602 do_test("3 elems: ", (w->size == 3));
604 w = mpdm_split(MPDM_S(L"dot.at end."), MPDM_S(L"."));
605 mpdm_dump(w);
606 do_test("3 elems: ", (w->size == 3));
608 w = mpdm_split(MPDM_S(L"three...dots (two empty elements)"), MPDM_S(L"."));
609 mpdm_dump(w);
610 do_test("4 elems: ", (w->size == 4));
612 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
613 mpdm_dump(w);
614 do_test("2 elems: ", (w->size == 2));
616 w = mpdm_split(MPDM_S(L"I am the man"), NULL);
617 do_test("NULL split 1: ", mpdm_size(w) == 12);
618 do_test("NULL split 2: ",
619 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
623 void test_join(void)
625 mpdm_t v;
626 mpdm_t s;
627 mpdm_t w;
629 printf("mpdm_join test\n\n");
631 /* separator */
632 s = mpdm_ref(MPDM_LS(L"--"));
634 w = MPDM_A(1);
635 mpdm_ref(w);
636 mpdm_aset(w, MPDM_S(L"ce"), 0);
638 v = mpdm_join(w, NULL);
639 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
641 v = mpdm_join(w, s);
642 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
644 mpdm_push(w, MPDM_LS(L"n'est"));
645 v = mpdm_join(w, s);
646 do_test("2 elems, '--' separator",
647 (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
649 mpdm_push(w, MPDM_LS(L"pas"));
650 v = mpdm_join(w, s);
651 do_test("3 elems, '--' separator",
652 (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
654 v = mpdm_join(w, NULL);
655 do_test("3 elems, no separator",
656 (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
658 mpdm_unref(w);
659 mpdm_unref(s);
663 static mpdm_t active(mpdm_t args)
665 return MPDM_H(0);
669 void test_sym(void)
671 mpdm_t v;
672 int i;
674 printf("mpdm_sset / mpdm_sget tests\n\n");
676 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
677 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
678 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
679 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
680 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"),
681 MPDM_LS(L"profontwindows"));
682 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
683 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
684 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
685 mpdm_sset(NULL, MPDM_LS(L"mp.active"), MPDM_X(active));
686 mpdm_sset(NULL, MPDM_LS(L"mp.active.syntax"), NULL);
687 mpdm_dump(mpdm_root());
689 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
690 i = mpdm_ival(v);
692 do_test("auto_indent == 16384", (i == 16384));
696 void test_file(void)
698 mpdm_t f;
699 mpdm_t v;
700 mpdm_t eol = MPDM_LS(L"\n");
702 mpdm_ref(eol);
704 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
706 if (f == NULL) {
707 printf("Can't create test.txt; no further file tests possible.\n");
708 return;
711 do_test("Create test.txt", f != NULL);
713 mpdm_write(f, MPDM_LS(L"0"));
714 mpdm_write(f, eol);
715 mpdm_write(f, MPDM_LS(L"1"));
716 mpdm_write(f, eol);
718 /* write WITHOUT eol */
719 mpdm_write(f, MPDM_LS(L"2"));
721 mpdm_close(f);
723 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
725 do_test("test written file 0",
726 mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
727 do_test("test written file 1",
728 mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
729 do_test("test written file 2",
730 mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
731 do_test("test written file 3", mpdm_read(f) == NULL);
733 mpdm_close(f);
735 mpdm_unlink(MPDM_LS(L"test.txt"));
736 do_test("unlink",
737 mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
739 v = mpdm_stat(MPDM_LS(L"stress.c"));
740 printf("Stat from stress.c:\n");
741 mpdm_dump(v);
743 /* v=mpdm_glob(MPDM_LS(L"*"));*/
744 printf("Glob:\n");
745 v = mpdm_glob(NULL, NULL);
746 mpdm_dump(v);
748 mpdm_unref(eol);
752 void test_regex(void)
754 mpdm_t v;
755 mpdm_t w;
757 v = mpdm_regex(MPDM_LS(L"123456"), MPDM_LS(L"/[0-9]+/"), 0);
758 do_test("regex 0", v != NULL);
760 v = mpdm_regex(MPDM_I(65536), MPDM_LS(L"/[0-9]+/"), 0);
761 do_test("regex 1", v != NULL);
763 v = mpdm_regex(MPDM_LS(L"12345678"), MPDM_LS(L"/^[0-9]+$/"), 0);
764 do_test("regex 2", v != NULL);
766 v = mpdm_regex(MPDM_I(1), MPDM_LS(L"/^[0-9]+$/"), 0);
767 do_test("regex 3", v != NULL);
769 v = mpdm_regex(MPDM_LS(L"A12345-678"), MPDM_LS(L"/^[0-9]+$/"), 0);
770 do_test("regex 4", v == NULL);
772 w = MPDM_LS(L"Hell street, 666");
773 mpdm_ref(w);
774 v = mpdm_regex(w, MPDM_LS(L"/[0-9]+/"), 0);
776 mpdm_dump(v);
778 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
780 v = mpdm_regex(MPDM_LS(L"CASE-INSENSITIVE REGEX"), MPDM_LS(L"/regex/"),
782 do_test("regex 6.1 (case sensitive)", v == NULL);
784 v = mpdm_regex(MPDM_LS(L"CASE-INSENSITIVE REGEX"),
785 MPDM_LS(L"/regex/i"),
787 do_test("regex 6.2 (case insensitive)", v != NULL);
789 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
790 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
792 v = mpdm_regex(MPDM_LS(L"123456"), MPDM_LS(L"/^\\s*/"), 0);
793 do_test("regex 7", v != NULL);
795 v = mpdm_regex(MPDM_LS(L"123456"), MPDM_LS(L"/^\\s+/"), 0);
796 do_test("regex 8", v == NULL);
798 v = mpdm_regex(NULL, MPDM_LS(L"/^\\s+/"), 0);
799 do_test("regex 9 (NULL string to match)", v == NULL);
801 /* sregex */
803 v = mpdm_sregex(MPDM_LS(L"change all A to A"),
804 MPDM_LS(L"/A/"),
805 MPDM_LS(L"E"), 0);
806 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
808 v = mpdm_sregex(MPDM_LS(L"change all A to A"),
809 MPDM_LS(L"/A/g"),
810 MPDM_LS(L"E"), 0);
811 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
813 v = mpdm_sregex(MPDM_LS(L"change all AAAAAA to E"),
814 MPDM_LS(L"/A+/g"),
815 MPDM_LS(L"E"), 0);
816 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
818 v = mpdm_sregex(MPDM_LS(L"change all A A A A A A to E"),
819 MPDM_LS(L"/A+/g"),
820 MPDM_LS(L"E"),
822 do_test("sregex 3",
823 mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
825 v = mpdm_sregex(MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
826 MPDM_LS(L"/A+/g"),
827 MPDM_LS(L"E"), 0);
828 do_test("sregex 3.2",
829 mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
831 v = mpdm_sregex(MPDM_LS(L"1, 20, 333, 40 all are numbers"),
832 MPDM_LS(L"/[0-9]+/g"),
833 MPDM_LS(L"numbers"), 0);
834 do_test("sregex 4",
835 mpdm_cmp(v,
836 MPDM_LS
837 (L"numbers, numbers, numbers, numbers all are numbers"))
838 == 0);
840 v = mpdm_sregex(MPDM_LS(L"regex, mpdm_regex, TexMex"),
841 MPDM_LS(L"/[a-zA-Z_]+/g"),
842 MPDM_LS(L"sex"),
844 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
846 v = mpdm_sregex(MPDM_LS(L"regex, mpdm_regex, TexMex"),
847 MPDM_LS(L"/[a-zA-Z]+/g"),
848 NULL, 0);
849 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
851 v = mpdm_sregex(MPDM_LS(L"\\MSDOS\\style\\path"),
852 MPDM_LS(L"/\\\\/g"),
853 MPDM_LS(L"/"), 0);
854 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
856 v = mpdm_sregex(MPDM_LS(L"regex, Regex, REGEX"),
857 MPDM_LS(L"/regex/gi"),
858 MPDM_LS(L"sex"), 0);
859 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
861 v = mpdm_sregex(NULL, NULL, NULL, 0);
862 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
864 /* & in substitution tests */
865 v = MPDM_LS(L"this string has many words");
866 v = mpdm_sregex(v, MPDM_LS(L"/[a-z]+/g"), MPDM_LS(L"[&]"), 0);
867 do_test("& in sregex target",
868 mpdm_cmp(v,
869 MPDM_LS(L"[this] [string] [has] [many] [words]")) ==
872 v = MPDM_LS(L"this string has many words");
873 v = mpdm_sregex(v, MPDM_LS(L"/[a-z]+/g"), MPDM_LS(L"[\\&]"), 0);
874 do_test("escaped & in sregex target",
875 mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 0);
877 v = MPDM_LS(L"this string has many words");
878 v = mpdm_sregex(v, MPDM_LS(L"/[a-z]+/g"), MPDM_LS(L"\\\\&"), 0);
879 do_test("escaped \\ in sregex target",
880 mpdm_cmp(v,
881 MPDM_LS(L"\\this \\string \\has \\many \\words")) ==
884 v = MPDM_LS(L"hola ");
885 v = mpdm_sregex(v, MPDM_LS(L"/[ \t]$/"), MPDM_LS(L""), 0);
886 do_test("sregex output size 1", v->size == 4);
888 v = MPDM_LS(L"hola ");
889 v = mpdm_sregex(v, MPDM_LS(L"/[ \t]$/"), NULL, 0);
890 do_test("sregex output size 2", v->size == 4);
892 v = MPDM_LS(L"hola ");
893 v = mpdm_sregex(v, MPDM_LS(L"/[ \t]$/"), MPDM_LS(L"!"), 0);
894 do_test("sregex output size 3", v->size == 5);
896 v = MPDM_LS(L"holo");
897 v = mpdm_sregex(v, MPDM_LS(L"/o/g"), MPDM_LS(L"!!"), 0);
898 do_test("sregex output size 4", v->size == 6);
900 /* multiple regex tests */
901 w = MPDM_A(0);
902 mpdm_ref(w);
904 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
905 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
906 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
907 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
908 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
910 v = mpdm_regex(MPDM_LS(L"key=value"), w, 0);
911 mpdm_ref(v);
912 do_test("multi-regex 1.1",
913 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
914 do_test("multi-regex 1.2",
915 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
916 mpdm_unref(v);
918 v = mpdm_regex(MPDM_LS(L" key = value"), w, 0);
919 mpdm_ref(v);
920 do_test("multi-regex 2.1",
921 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
922 do_test("multi-regex 2.2",
923 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
924 mpdm_unref(v);
926 v = mpdm_regex(MPDM_LS(L"\t\tkey\t=\tvalue "), w, 0);
927 mpdm_ref(v);
928 do_test("multi-regex 3.1",
929 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
930 do_test("multi-regex 3.2",
931 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
932 mpdm_unref(v);
934 mpdm_unref(w);
936 /* v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
937 do_test("multi-regex 4", v == NULL);
939 printf("Multiple line regexes\n");
940 w = MPDM_LS(L"/* this is\na C-like comment */");
941 mpdm_ref(w);
943 v = mpdm_regex(w, MPDM_LS(L"|/\\*.+\\*/|"), 0);
944 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
946 v = mpdm_regex(w, MPDM_LS(L"/is$/"), 0);
947 do_test("Multiline regex 2", v == NULL);
949 v = mpdm_regex(w, MPDM_LS(L"/is$/m"), 0);
950 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
951 mpdm_unref(w);
953 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
955 w = MPDM_LS(L"-\x03a9-");
956 mpdm_ref(w);
958 v = mpdm_regex(w, MPDM_LS(L"/-$/"), 0);
959 do_test("Multibyte environment regex 1",
960 mpdm_cmp(v, MPDM_LS(L"-")) == 0);
962 if (do_multibyte_sregex_tests) {
963 v = mpdm_sregex(w, MPDM_LS(L"/-$/"), MPDM_LS(L"~"), 0);
964 do_test("Multibyte environment sregex 1",
965 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
967 v = mpdm_sregex(w, MPDM_LS(L"/-/g"), MPDM_LS(L"~"), 0);
968 do_test("Multibyte environment sregex 2",
969 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
971 else
972 printf("Multibyte sregex test omitted; activate with -m\n");
974 mpdm_unref(w);
976 /* 'last' flag tests */
977 v = MPDM_LS(L"this string has many words");
978 v = mpdm_regex(v, MPDM_LS(L"/[a-z]+/l"), 0);
979 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
983 static mpdm_t dumper(mpdm_t args, mpdm_t ctxt)
984 /* executable value */
986 mpdm_dump(args);
987 return (NULL);
991 static mpdm_t sum(mpdm_t args, mpdm_t ctxt)
992 /* executable value: sum all args */
994 int n, t = 0;
996 if (args != NULL) {
997 for (n = t = 0; n < args->size; n++)
998 t += mpdm_ival(mpdm_aget(args, n));
1001 return (MPDM_I(t));
1005 static mpdm_t calculator(mpdm_t c, mpdm_t args, mpdm_t ctxt)
1006 /* 3 argument version: calculator. c contains a 'script' to
1007 do things with the arguments */
1009 int n, t;
1010 mpdm_t v;
1011 mpdm_t a;
1012 mpdm_t o;
1014 mpdm_ref(args);
1016 /* to avoid destroying args */
1017 a = mpdm_ref(mpdm_clone(args));
1019 /* shift first argument */
1020 v = mpdm_shift(a);
1021 t = mpdm_ival(v);
1023 for (n = 0; n < mpdm_size(c); n++) {
1024 /* gets operator */
1025 o = mpdm_aget(c, n);
1027 /* gets next value */
1028 v = mpdm_shift(a);
1030 switch (*(wchar_t *) o->data) {
1031 case '+':
1032 t += mpdm_ival(v);
1033 break;
1034 case '-':
1035 t -= mpdm_ival(v);
1036 break;
1037 case '*':
1038 t *= mpdm_ival(v);
1039 break;
1040 case '/':
1041 t /= mpdm_ival(v);
1042 break;
1046 mpdm_unref(a);
1047 mpdm_unref(args);
1049 return (MPDM_I(t));
1053 void test_exec(void)
1055 mpdm_t x;
1056 mpdm_t w;
1057 mpdm_t p;
1059 printf("test_exec\n");
1061 x = MPDM_X(dumper);
1063 /* a simple value */
1064 mpdm_ref(x);
1065 mpdm_exec(x, NULL, NULL);
1066 mpdm_exec(x, x, NULL);
1067 mpdm_unref(x);
1069 x = mpdm_ref(MPDM_X(sum));
1070 w = mpdm_ref(MPDM_A(3));
1071 mpdm_aset(w, MPDM_I(100), 0);
1072 mpdm_aset(w, MPDM_I(220), 1);
1073 mpdm_aset(w, MPDM_I(333), 2);
1075 do_test("exec 0", mpdm_ival(mpdm_exec(x, w, NULL)) == 653);
1076 x = mpdm_unref(x);
1078 mpdm_push(w, MPDM_I(1));
1080 /* multiple executable value: vm and compiler support */
1082 /* calculator 'script' */
1083 p = mpdm_ref(MPDM_A(0));
1084 mpdm_push(p, MPDM_LS(L"+"));
1085 mpdm_push(p, MPDM_LS(L"-"));
1086 mpdm_push(p, MPDM_LS(L"+"));
1088 /* the value */
1089 x = mpdm_ref(MPDM_A(2));
1090 x->flags |= MPDM_EXEC;
1092 mpdm_aset(x, MPDM_X(calculator), 0);
1093 mpdm_aset(x, p, 1);
1095 do_test("exec 1", mpdm_ival(mpdm_exec(x, w, NULL)) == -12);
1097 mpdm_unref(p);
1099 /* another 'script', different operations with the same values */
1100 p = mpdm_ref(MPDM_A(0));
1101 mpdm_push(p, MPDM_LS(L"*"));
1102 mpdm_push(p, MPDM_LS(L"/"));
1103 mpdm_push(p, MPDM_LS(L"+"));
1105 mpdm_aset(x, p, 1);
1107 do_test("exec 2", mpdm_ival(mpdm_exec(x, w, NULL)) == 67);
1108 x = mpdm_unref(x);
1109 p = mpdm_unref(p);
1110 w = mpdm_unref(w);
1114 void test_encoding(void)
1116 mpdm_t f;
1117 mpdm_t v;
1118 mpdm_t w;
1119 const wchar_t *ptr;
1121 v = MPDM_MBS("?Espa?a!\n");
1122 mpdm_ref(v);
1124 printf
1125 ("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
1127 if ((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL) {
1128 printf("Can't write test.txt; no further file test possible.\n");
1129 return;
1132 mpdm_write(f, v);
1133 mpdm_close(f);
1135 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1136 w = mpdm_read(f);
1137 mpdm_dump(w);
1138 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
1139 mpdm_close(f);
1141 printf
1142 ("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1144 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1145 w = mpdm_read(f);
1146 mpdm_dump(w);
1147 mpdm_close(f);
1149 for (ptr = w->data; *ptr != L'\0'; ptr++)
1150 printf("%d", mpdm_wcwidth(*ptr));
1151 printf("\n");
1153 if (mpdm_encoding(MPDM_LS(L"UTF-8")) < 0) {
1154 printf
1155 ("No multiple encoding (iconv) support; no more tests possible.\n");
1156 return;
1159 printf
1160 ("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1162 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1163 w = mpdm_read(f);
1164 mpdm_dump(w);
1165 mpdm_close(f);
1167 /* new open file will use the specified encoding */
1168 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
1169 mpdm_write(f, v);
1170 mpdm_close(f);
1172 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1173 w = mpdm_read(f);
1174 mpdm_dump(w);
1175 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
1176 mpdm_close(f);
1178 mpdm_encoding(NULL);
1179 mpdm_unref(v);
1183 void test_gettext(void)
1185 mpdm_t v;
1186 mpdm_t h;
1188 printf("\nTesting gettext...\n");
1190 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
1192 printf
1193 ("Should follow a translated string of 'This is a test string':\n");
1194 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1195 mpdm_dump(v);
1197 printf("The same, but cached:\n");
1198 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1199 mpdm_dump(v);
1201 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
1202 mpdm_dump(v);
1204 printf("Ad-hoc translation hash:\n");
1206 h = MPDM_H(0);
1207 mpdm_ref(h);
1208 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
1210 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
1211 v = mpdm_gettext(MPDM_LS(L"test string"));
1212 mpdm_dump(v);
1213 mpdm_unref(h);
1217 void timer(int secs)
1219 static clock_t clks = 0;
1221 switch (secs) {
1222 case 0:
1223 clks = clock();
1224 break;
1226 case -1:
1227 printf("%.2f seconds\n",
1228 (float) (clock() - clks) / (float) CLOCKS_PER_SEC);
1229 break;
1234 void bench_hash(int i, mpdm_t l, int buckets)
1236 mpdm_t h;
1237 mpdm_t v;
1238 int n;
1240 printf("Hash of %d buckets: \n", buckets);
1241 h = MPDM_H(buckets);
1242 mpdm_ref(h);
1244 timer(0);
1245 for (n = 0; n < i; n++) {
1246 v = mpdm_aget(l, n);
1247 mpdm_hset(h, v, v);
1249 timer(-1);
1251 mpdm_unref(h);
1254 printf("Bucket usage:\n");
1255 for(n=0;n < mpdm_size(h);n++)
1256 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1261 void benchmark(void)
1263 mpdm_t l;
1264 int i, n;
1265 char tmp[64];
1267 printf("\n");
1269 if (!do_benchmarks) {
1270 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1271 return;
1274 printf("BENCHMARKS\n");
1276 i = 500000;
1278 printf("Creating %d values...\n", i);
1280 l = mpdm_ref(MPDM_A(i));
1281 for (n = 0; n < i; n++) {
1282 sprintf(tmp, "%08x", n);
1283 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1284 mpdm_aset(l, MPDM_I(n), n);
1287 printf("OK\n");
1289 bench_hash(i, l, 0);
1290 bench_hash(i, l, 61);
1291 bench_hash(i, l, 89);
1292 bench_hash(i, l, 127);
1294 mpdm_unref(l);
1298 void test_conversion(void)
1300 wchar_t *wptr = NULL;
1301 char *ptr = NULL;
1302 int size = 0;
1304 ptr = mpdm_wcstombs(L"", &size);
1305 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1307 wptr = mpdm_mbstowcs("", &size, 0);
1308 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1312 void test_pipes(void)
1314 mpdm_t f;
1316 printf("\n");
1318 if ((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL) {
1319 mpdm_t v;
1321 v = mpdm_read(f);
1322 mpdm_pclose(f);
1324 printf("Pipe from 'date':\n");
1325 mpdm_dump(v);
1327 else
1328 printf("Can't pipe to 'date'\n");
1332 void test_misc(void)
1334 printf("Home dir:\n");
1335 mpdm_dump(mpdm_home_dir());
1336 printf("App dir:\n");
1337 mpdm_dump(mpdm_app_dir());
1341 void test_sprintf(void)
1343 mpdm_t v;
1344 mpdm_t w;
1346 printf("sprintf tests\n");
1348 v = MPDM_A(0);
1349 mpdm_ref(v);
1350 mpdm_push(v, MPDM_I(100));
1351 mpdm_push(v, MPDM_LS(L"beers"));
1353 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1354 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1356 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1357 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me 0")) == 0);
1359 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1360 do_test("sprintf 3",
1361 mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1363 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1364 do_test("sprintf 4",
1365 mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1367 mpdm_unref(v);
1369 v = MPDM_A(0);
1370 mpdm_ref(v);
1371 mpdm_push(v, MPDM_R(3.1416));
1373 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1374 do_test("sprintf 2.1",
1375 mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1377 /* w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1378 do_test("sprintf 2.2", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1380 mpdm_unref(v);
1382 v = MPDM_A(0);
1383 mpdm_ref(v);
1384 mpdm_push(v, MPDM_LS(L"stress"));
1386 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1387 do_test("sprintf 3.1",
1388 mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1390 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1391 do_test("sprintf 3.2",
1392 mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1394 mpdm_unref(v);
1396 v = MPDM_A(0);
1397 mpdm_ref(v);
1398 mpdm_push(v, MPDM_I(0x263a));
1400 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1401 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1402 mpdm_unref(v);
1404 v = MPDM_A(0);
1405 mpdm_ref(v);
1406 mpdm_push(v, MPDM_I(75));
1408 w = mpdm_sprintf(MPDM_LS(L"%d%%"), v);
1409 do_test("sprintf 4.1", mpdm_cmp(w, MPDM_LS(L"75%")) == 0);
1411 w = mpdm_sprintf(MPDM_LS(L"%b"), v);
1412 do_test("sprintf 5.1", mpdm_cmp(w, MPDM_LS(L"1001011")) == 0);
1413 mpdm_unref(v);
1417 void test_ulc(void)
1419 mpdm_t v = mpdm_ref(MPDM_S(L"string"));
1420 mpdm_t w = mpdm_ref(mpdm_ulc(v, 1));
1422 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1423 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1425 mpdm_unref(w);
1426 mpdm_unref(v);
1430 void test_scanf(void)
1432 mpdm_t v;
1434 v = mpdm_sscanf(MPDM_LS(L"1234 5678"), MPDM_LS(L"%d %d"), 0);
1435 do_test("mpdm_sscanf_1.1",
1436 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1437 do_test("mpdm_sscanf_1.2",
1438 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1440 v = mpdm_sscanf(MPDM_LS(L"this 12.34 5678"), MPDM_LS(L"%s %f %d"), 0);
1441 do_test("mpdm_sscanf_2.1",
1442 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1443 do_test("mpdm_sscanf_2.2",
1444 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1445 do_test("mpdm_sscanf_2.3",
1446 mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678")) == 0);
1448 v = mpdm_sscanf(MPDM_LS(L"this 12.34 5678"), MPDM_LS(L"%s %*f %d"), 0);
1449 do_test("mpdm_sscanf_3.1",
1450 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1451 do_test("mpdm_sscanf_3.2",
1452 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1454 v = mpdm_sscanf(MPDM_LS(L"12341234121234567890"),
1455 MPDM_LS(L"%4d%4d%2d%10d"),
1457 do_test("mpdm_sscanf_4.1",
1458 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1459 do_test("mpdm_sscanf_4.2",
1460 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"1234")) == 0);
1461 do_test("mpdm_sscanf_4.3",
1462 mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"12")) == 0);
1463 do_test("mpdm_sscanf_4.4",
1464 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"1234567890")) == 0);
1466 v = mpdm_sscanf(MPDM_LS(L"ccbaabcxaaae and more"),
1467 MPDM_LS(L"%[abc]%s"),
1469 do_test("mpdm_sscanf_5.1",
1470 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1471 do_test("mpdm_sscanf_5.2",
1472 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1474 v = mpdm_sscanf(MPDM_LS(L"ccbaabcxaaae and more"),
1475 MPDM_LS(L"%[a-d]%s"),
1477 do_test("mpdm_sscanf_6.1",
1478 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1479 do_test("mpdm_sscanf_6.2",
1480 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1482 v = mpdm_sscanf(MPDM_LS(L"ccbaabcxaaae and more"),
1483 MPDM_LS(L"%[^x]%s"),
1485 do_test("mpdm_sscanf_7.1",
1486 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1487 do_test("mpdm_sscanf_7.2",
1488 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1490 v = mpdm_sscanf(MPDM_LS(L"key: value"),
1491 MPDM_LS(L"%[^:]: %s"),
1493 do_test("mpdm_sscanf_8.1",
1494 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"key")) == 0);
1495 do_test("mpdm_sscanf_8.2",
1496 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"value")) == 0);
1498 v = mpdm_sscanf(MPDM_LS(L"this is code /* comment */ more code"),
1499 MPDM_LS(L"%*[^/]/* %s */"),
1501 do_test("mpdm_sscanf_9.1",
1502 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"comment")) == 0);
1504 v = mpdm_sscanf(MPDM_LS(L"1234%5678"), MPDM_LS(L"%d%%%d"), 0);
1505 do_test("mpdm_sscanf_10.1",
1506 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1507 do_test("mpdm_sscanf_10.2",
1508 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1510 v = mpdm_sscanf(MPDM_LS(L"ccbaabcxaaae and more"),
1511 MPDM_LS(L"%*[abc]%n%*[^ ]%n"),
1513 do_test("mpdm_sscanf_11.1", mpdm_ival(mpdm_aget(v, 0)) == 7);
1514 do_test("mpdm_sscanf_11.2", mpdm_ival(mpdm_aget(v, 1)) == 12);
1516 v = mpdm_sscanf(MPDM_LS(L"/* inside the comment */"),
1517 MPDM_LS(L"/* %S */"),
1519 do_test("mpdm_sscanf_12.1",
1520 mpdm_cmp(mpdm_aget(v, 0),
1521 MPDM_LS(L"inside the comment")) == 0);
1523 v = mpdm_sscanf(MPDM_LS(L"/* inside the comment */outside"),
1524 MPDM_LS(L"/* %S */%s"),
1526 do_test("mpdm_sscanf_13.1",
1527 mpdm_cmp(mpdm_aget(v, 0),
1528 MPDM_LS(L"inside the comment")) == 0);
1529 do_test("mpdm_sscanf_13.2",
1530 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"outside")) == 0);
1532 v = mpdm_sscanf(MPDM_LS(L""), MPDM_LS(L"%n"), 0);
1533 do_test("mpdm_sscanf_14.1", mpdm_size(v) == 1
1534 && mpdm_ival(mpdm_aget(v, 0)) == 0);
1536 v = mpdm_sscanf(MPDM_LS(L"this 12.34 5678#12@34"),
1537 MPDM_LS(L"%[^%f]%f %[#%d@]"),
1539 do_test("mpdm_sscanf_15.1",
1540 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this ")) == 0);
1541 do_test("mpdm_sscanf_15.2",
1542 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1543 do_test("mpdm_sscanf_15.3",
1544 mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678#12@34")) == 0);
1546 v = mpdm_sscanf( MPDM_LS(L"a \"bbb\" c;"),
1547 MPDM_LS(L"%*S\"%[^\n\"]\""),
1549 do_test("mpdm_sscanf_16",
1550 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"bbb")) == 0);
1552 do_test("mpdm_sscanf_17", mpdm_aget(v, 0)->size == 3);
1556 mpdm_t mutex = NULL;
1557 int t_finished = -1;
1559 mpdm_t the_thread(mpdm_t args, mpdm_t ctxt)
1560 /* running from a thread */
1562 mpdm_t fn = mpdm_ref(MPDM_LS(L"thread.txt"));
1563 mpdm_t f;
1565 printf("thread: start writing from thread\n");
1567 if ((f = mpdm_open(fn, MPDM_LS(L"w"))) != NULL) {
1568 int n;
1570 for (n = 0; n < 1000; n++) {
1571 mpdm_write(f, MPDM_I(n));
1572 mpdm_write(f, MPDM_LS(L"\n"));
1575 mpdm_close(f);
1578 printf("thread: finished writing from thread\n");
1580 mpdm_mutex_lock(mutex);
1581 t_finished = 1;
1582 mpdm_mutex_unlock(mutex);
1584 printf("thread: t_finished set\n");
1586 return NULL;
1590 void test_thread(void)
1592 mpdm_t fn = mpdm_ref(MPDM_LS(L"thread.txt"));
1593 mpdm_t x, v;
1594 int done;
1596 printf("Testing threads and mutexes...\n");
1598 mpdm_unlink(fn);
1600 /* create the executable value */
1601 x = mpdm_ref(MPDM_X(the_thread));
1603 /* create a mutex */
1604 mutex = mpdm_ref(mpdm_new_mutex());
1606 t_finished = 0;
1608 v = mpdm_exec_thread(x, NULL, NULL);
1610 printf("parent: waiting for the thread to finish...\n");
1611 mpdm_ref(v);
1613 done = 0;
1614 while (!done) {
1615 mpdm_sleep(10);
1617 mpdm_mutex_lock(mutex);
1619 if (t_finished > 0)
1620 done = 1;
1622 mpdm_mutex_unlock(mutex);
1625 printf("parent: thread said it has finished.\n");
1627 mpdm_unref(v);
1629 mpdm_unref(mutex);
1630 mpdm_unref(x);
1631 mpdm_unref(fn);
1635 mpdm_t sem = NULL;
1637 mpdm_t sem_thread(mpdm_t args, mpdm_t ctxt)
1639 printf("thread: waiting for semaphore...\n");
1641 mpdm_semaphore_wait(sem);
1643 printf("thread: got semaphore.\n");
1645 return NULL;
1649 void test_sem(void)
1651 mpdm_t x, v;
1653 printf("Testing threads and semaphores...\n");
1655 /* create the executable value */
1656 x = mpdm_ref(MPDM_X(sem_thread));
1658 /* creates the semaphore */
1659 sem = mpdm_ref(mpdm_new_semaphore(0));
1661 printf("parent: launching thread.\n");
1663 v = mpdm_exec_thread(x, NULL, NULL);
1664 mpdm_ref(v);
1666 mpdm_sleep(10);
1668 printf("parent: posting semaphore.\n");
1670 mpdm_semaphore_post(sem);
1672 mpdm_sleep(10);
1674 mpdm_unref(v);
1675 mpdm_unref(sem);
1676 mpdm_unref(x);
1680 void (*func) (void) = NULL;
1682 int main(int argc, char *argv[])
1684 if (argc > 1) {
1685 if (strcmp(argv[1], "-b") == 0)
1686 do_benchmarks = 1;
1687 if (strcmp(argv[1], "-m") == 0)
1688 do_multibyte_sregex_tests = 1;
1691 mpdm_startup();
1693 printf("sizeof(struct mpdm_val): %ld\n",
1694 (long) sizeof(struct mpdm_val));
1695 printf("sizeof(void *): %d\n", sizeof(void *));
1696 printf("sizeof(void (*func)(void)): %d\n", sizeof(func));
1698 /* test_counter();*/
1699 test_basic();
1700 test_array();
1701 test_hash();
1702 test_splice();
1703 test_strcat();
1704 test_split();
1705 test_join();
1706 test_sym();
1707 test_file();
1708 test_regex();
1709 test_exec();
1710 test_encoding();
1711 test_gettext();
1712 test_conversion();
1713 test_pipes();
1714 test_misc();
1715 test_sprintf();
1716 test_ulc();
1717 test_scanf();
1718 test_thread();
1719 test_sem();
1721 benchmark();
1723 mpdm_shutdown();
1725 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1727 if (oks == tests)
1728 printf("*** ALL TESTS PASSED\n");
1729 else {
1730 int n;
1732 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1734 printf("\nFailed tests:\n\n");
1735 for (n = 0; n < i_failed_msgs; n++)
1736 printf("%s", failed_msgs[n]);
1739 return oks == tests ? 0 : 1;