New function mpdm_string2().
[mpdm.git] / stress.c
blobf01a76b38353882e5b85b6caadd7143582f71c03
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(L" ", v);
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 n = v->ref;
284 v = mpdm_aget(a, 3);
285 mpdm_collapse(a, 3, 1);
286 do_test("acollapse unrefs values", (v->ref < n));
288 mpdm_unref(a);
290 /* test queues */
291 a = MPDM_A(0);
292 mpdm_ref(a);
294 /* add several values */
295 for (n = 0; n < 10; n++)
296 v = mpdm_queue(a, MPDM_I(n), 10);
298 do_test("queue should still output NULL", (v == NULL));
300 v = mpdm_queue(a, MPDM_I(11), 10);
301 do_test("queue should no longer output NULL", (v != NULL));
303 v = mpdm_queue(a, MPDM_I(12), 10);
304 do_test("queue should return 1", mpdm_ival(v) == 1);
305 v = mpdm_queue(a, MPDM_I(13), 10);
306 do_test("queue should return 2", mpdm_ival(v) == 2);
307 do_test("queue size should be 10", a->size == 10);
309 mpdm_dump(a);
310 v = mpdm_queue(a, MPDM_I(14), 5);
311 mpdm_dump(a);
313 do_test("queue size should be 5", a->size == 5);
314 do_test("last taken value should be 8", mpdm_ival(v) == 8);
315 mpdm_unref(a);
317 a = MPDM_A(4);
318 mpdm_ref(a);
319 mpdm_aset(a, MPDM_I(666), 6000);
321 do_test("array should have been automatically expanded",
322 mpdm_size(a) == 6001);
324 v = mpdm_aget(a, -1);
325 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
327 mpdm_aset(a, MPDM_I(777), -2);
328 v = mpdm_aget(a, 5999);
329 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
331 mpdm_push(a, MPDM_I(888));
332 v = mpdm_aget(a, -1);
333 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
335 v = MPDM_A(0);
336 mpdm_ref(v);
337 mpdm_push(v, MPDM_I(100));
338 mpdm_pop(v);
339 mpdm_unref(v);
341 mpdm_unref(a);
343 /* array comparisons with mpdm_cmp() */
344 a = MPDM_A(2);
345 mpdm_ref(a);
346 mpdm_aset(a, MPDM_I(10), 0);
347 mpdm_aset(a, MPDM_I(60), 1);
349 v = mpdm_ref(mpdm_clone(a));
350 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
352 mpdm_adel(v, -1);
353 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
355 mpdm_push(v, MPDM_I(80));
356 do_test("mpdm_cmp: 2# element is bigger, so array is bigger",
357 mpdm_cmp(a, v) < 0);
359 mpdm_unref(v);
360 mpdm_unref(a);
364 void test_hash(void)
366 mpdm_t h;
367 mpdm_t v;
368 int i, n;
370 h = MPDM_H(0);
371 mpdm_ref(h);
373 do_test("hsize 1", mpdm_hsize(h) == 0);
375 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
376 v = mpdm_hget(h, MPDM_S(L"mp"));
378 do_test("hsize 2", mpdm_hsize(h) == 1);
380 do_test("hash: v != NULL", (v != NULL));
381 i = mpdm_ival(v);
382 do_test("hash: v == 6", (i == 6));
384 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
385 v = mpdm_hget(h, MPDM_S(L"mp2"));
387 do_test("hsize 3", mpdm_hsize(h) == 2);
389 do_test("hash: v != NULL", (v != NULL));
390 i = mpdm_ival(v);
391 do_test("hash: v == 66", (i == 66));
393 /* fills 100 values */
394 for (n = 0; n < 50; n++)
395 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
396 for (n = 100; n >= 50; n--)
397 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
399 do_test("hsize 4", mpdm_hsize(h) == 103);
401 /* tests 100 values */
402 for (n = 0; n < 100; n++) {
403 v = mpdm_hget(h, MPDM_I(n));
405 if (v != NULL) {
406 i = mpdm_ival(v);
407 if (!(i == n * 10))
408 do_test("hash: ival", (i == n * 10));
410 else
411 do_test("hash: hget", (v != NULL));
414 printf("h's size: %d\n", mpdm_hsize(h));
416 mpdm_hdel(h, MPDM_LS(L"mp"));
417 do_test("hsize 5", mpdm_hsize(h) == 102);
419 mpdm_unref(h);
422 mpdm_dump(h);
424 v=mpdm_hkeys(h);
425 mpdm_dump(v);
427 /* use of non-strings as hashes */
428 h = MPDM_H(0);
429 mpdm_ref(h);
431 v = MPDM_A(0);
432 mpdm_hset(h, v, MPDM_I(1234));
433 v = MPDM_H(0);
434 mpdm_hset(h, v, MPDM_I(12345));
435 v = MPDM_H(0);
436 mpdm_hset(h, v, MPDM_I(9876));
437 v = MPDM_A(0);
438 mpdm_ref(v);
439 mpdm_hset(h, v, MPDM_I(6543));
440 i = mpdm_ival(mpdm_hget(h, v));
441 mpdm_unref(v);
443 mpdm_dump(h);
444 do_test("hash: using non-strings as hash keys", (i == 6543));
446 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
448 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
449 do_test("exists 2", !mpdm_exists(h, MPDM_LS(L"notok")));
451 mpdm_dump(h);
452 v = mpdm_hget_s(h, L"ok");
453 printf("v %s\n", v == NULL ? "is NULL" : "is NOT NULL");
454 do_test("hget_s 1", mpdm_ival(v) == 666);
455 mpdm_dump(v);
456 mpdm_dump(h);
458 i = 0;
459 for (n = 0; n < 1000; n++) {
460 if (mpdm_hget_s(h, L"ok") != NULL)
461 i++;
463 printf("i: %d\n", i);
464 do_test("hget_s 1.2", i == 1000);
466 if (i != 1000)
467 mpdm_hget_s(h, L"ok");
469 do_test("hget 1.2.1", mpdm_hget(h, MPDM_LS(L"ok")) != NULL);
471 mpdm_hset_s(h, L"ok", MPDM_I(777));
473 v = mpdm_hget_s(h, L"ok");
474 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
476 mpdm_unref(h);
480 void test_splice(void)
482 mpdm_t w;
483 mpdm_t v;
485 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"), MPDM_LS(L"special "), 4,
487 do_test("splice insertion",
488 mpdm_cmp(mpdm_aget(w, 0),
489 MPDM_LS(L"I'm special agent Johnson")) == 0);
490 mpdm_dump(w);
492 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
493 do_test("splice insertion and deletion (1)",
494 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
495 do_test("splice insertion and deletion (2)",
496 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
497 mpdm_dump(w);
499 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
500 do_test("splice deletion (1)",
501 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
502 do_test("splice deletion (2)",
503 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
504 mpdm_dump(w);
506 v = MPDM_LS(L"It doesn't matter");
507 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
508 do_test("splice insertion at the end",
509 mpdm_cmp(mpdm_aget(w, 0),
510 MPDM_LS(L"It doesn't matter two")) == 0);
511 mpdm_dump(w);
513 w = mpdm_splice(NULL, NULL, 0, 0);
514 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
516 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
517 do_test("splice with first value NULL",
518 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
520 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
521 do_test("splice with second value NULL",
522 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
524 v = MPDM_LS(L"I'm testing");
525 mpdm_ref(v);
527 w = mpdm_splice(v, NULL, 0, -1);
528 do_test("splice with negative del (1)",
529 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
531 w = mpdm_splice(v, NULL, 4, -1);
532 do_test("splice with negative del (2)",
533 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
535 w = mpdm_splice(v, NULL, 4, -2);
536 do_test("splice with negative del (3)",
537 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
539 w = mpdm_splice(v, NULL, 0, -4);
540 do_test("splice with negative del (4)",
541 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
542 mpdm_dump(mpdm_aget(w, 0));
544 w = mpdm_splice(v, NULL, 4, -20);
545 do_test("splice with out-of-bounds negative del",
546 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
547 mpdm_unref(v);
551 void test_strcat(void)
553 mpdm_t v;
554 mpdm_t w;
556 w = MPDM_LS(L"something");
557 mpdm_ref(w);
559 v = mpdm_strcat(NULL, NULL);
560 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
562 v = mpdm_strcat(NULL, w);
563 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
565 v = mpdm_strcat(w, NULL);
566 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
567 mpdm_unref(w);
569 w = MPDM_LS(L"");
570 mpdm_ref(w);
571 v = mpdm_strcat(NULL, w);
572 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
574 v = mpdm_strcat(w, NULL);
575 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
577 v = mpdm_strcat(w, w);
578 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
580 mpdm_unref(w);
584 void test_split(void)
586 mpdm_t w;
588 printf("mpdm_split test\n\n");
590 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"four.elems.in.string"));
591 mpdm_dump(w);
592 do_test("4 elems: ", (w->size == 4));
594 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"unseparated string"));
595 mpdm_dump(w);
596 do_test("1 elem: ", (w->size == 1));
598 w = mpdm_split(MPDM_S(L"."), MPDM_S(L".dot.at start"));
599 mpdm_dump(w);
600 do_test("3 elems: ", (w->size == 3));
602 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"dot.at end."));
603 mpdm_dump(w);
604 do_test("3 elems: ", (w->size == 3));
606 w = mpdm_split(MPDM_S(L"."),
607 MPDM_S(L"three...dots (two empty elements)"));
608 mpdm_dump(w);
609 do_test("4 elems: ", (w->size == 4));
611 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
612 mpdm_dump(w);
613 do_test("2 elems: ", (w->size == 2));
615 w = mpdm_split(NULL, MPDM_S(L"I am the man"));
616 do_test("NULL split 1: ", mpdm_size(w) == 12);
617 do_test("NULL split 2: ",
618 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
622 void test_join(void)
624 mpdm_t v;
625 mpdm_t s;
626 mpdm_t w;
628 printf("mpdm_join test\n\n");
630 /* separator */
631 s = mpdm_ref(MPDM_LS(L"--"));
633 w = MPDM_A(1);
634 mpdm_ref(w);
635 mpdm_aset(w, MPDM_S(L"ce"), 0);
637 v = mpdm_join(NULL, w);
638 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
640 v = mpdm_join(s, w);
641 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
643 mpdm_push(w, MPDM_LS(L"n'est"));
644 v = mpdm_join(s, w);
645 do_test("2 elems, '--' separator",
646 (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
648 mpdm_push(w, MPDM_LS(L"pas"));
649 v = mpdm_join(s, w);
650 do_test("3 elems, '--' separator",
651 (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
653 v = mpdm_join(NULL, w);
654 do_test("3 elems, no separator",
655 (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
657 mpdm_unref(w);
658 mpdm_unref(s);
662 static mpdm_t active(mpdm_t args)
664 return MPDM_H(0);
668 void test_sym(void)
670 mpdm_t v;
671 int i;
673 printf("mpdm_sset / mpdm_sget tests\n\n");
675 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
676 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
677 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
678 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
679 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"),
680 MPDM_LS(L"profontwindows"));
681 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
682 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
683 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
684 mpdm_sset(NULL, MPDM_LS(L"mp.active"), MPDM_X(active));
685 mpdm_sset(NULL, MPDM_LS(L"mp.active.syntax"), NULL);
686 mpdm_dump(mpdm_root());
688 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
689 i = mpdm_ival(v);
691 do_test("auto_indent == 16384", (i == 16384));
695 void test_file(void)
697 mpdm_t f;
698 mpdm_t v;
699 mpdm_t eol = MPDM_LS(L"\n");
701 mpdm_ref(eol);
703 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
705 if (f == NULL) {
706 printf("Can't create test.txt; no further file tests possible.\n");
707 return;
710 do_test("Create test.txt", f != NULL);
712 mpdm_write(f, MPDM_LS(L"0"));
713 mpdm_write(f, eol);
714 mpdm_write(f, MPDM_LS(L"1"));
715 mpdm_write(f, eol);
717 /* write WITHOUT eol */
718 mpdm_write(f, MPDM_LS(L"2"));
720 mpdm_close(f);
722 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
724 do_test("test written file 0",
725 mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
726 do_test("test written file 1",
727 mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
728 do_test("test written file 2",
729 mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
730 do_test("test written file 3", mpdm_read(f) == NULL);
732 mpdm_close(f);
734 mpdm_unlink(MPDM_LS(L"test.txt"));
735 do_test("unlink",
736 mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
738 v = mpdm_stat(MPDM_LS(L"stress.c"));
739 printf("Stat from stress.c:\n");
740 mpdm_dump(v);
742 /* v=mpdm_glob(MPDM_LS(L"*"));*/
743 printf("Glob:\n");
744 v = mpdm_glob(NULL, NULL);
745 mpdm_dump(v);
747 mpdm_unref(eol);
751 void test_regex(void)
753 mpdm_t v;
754 mpdm_t w;
756 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_LS(L"123456"), 0);
757 do_test("regex 0", v != NULL);
759 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_I(65536), 0);
760 do_test("regex 1", v != NULL);
762 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"12345678"), 0);
763 do_test("regex 2", v != NULL);
765 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_I(1), 0);
766 do_test("regex 3", v != NULL);
768 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"A12345-678"), 0);
769 do_test("regex 4", v == NULL);
771 w = MPDM_LS(L"Hell street, 666");
772 mpdm_ref(w);
773 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), w, 0);
775 mpdm_dump(v);
777 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
779 v = mpdm_regex(MPDM_LS(L"/regex/"), MPDM_LS(L"CASE-INSENSITIVE REGEX"),
781 do_test("regex 6.1 (case sensitive)", v == NULL);
783 v = mpdm_regex(MPDM_LS(L"/regex/i"),
784 MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
785 do_test("regex 6.2 (case insensitive)", v != NULL);
787 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
788 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
790 v = mpdm_regex(MPDM_LS(L"/^\\s*/"), MPDM_LS(L"123456"), 0);
791 do_test("regex 7", v != NULL);
793 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), MPDM_LS(L"123456"), 0);
794 do_test("regex 8", v == NULL);
796 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), NULL, 0);
797 do_test("regex 9 (NULL string to match)", v == NULL);
799 /* sregex */
801 v = mpdm_sregex(MPDM_LS(L"/A/"), MPDM_LS(L"change all A to A"),
802 MPDM_LS(L"E"), 0);
803 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
805 v = mpdm_sregex(MPDM_LS(L"/A/g"), MPDM_LS(L"change all A to A"),
806 MPDM_LS(L"E"), 0);
807 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
809 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAAAAA to E"),
810 MPDM_LS(L"E"), 0);
811 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
813 v = mpdm_sregex(MPDM_LS(L"/A+/g"),
814 MPDM_LS(L"change all A A A A A A to E"), MPDM_LS(L"E"),
816 do_test("sregex 3",
817 mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
819 v = mpdm_sregex(MPDM_LS(L"/A+/g"),
820 MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
821 MPDM_LS(L"E"), 0);
822 do_test("sregex 3.2",
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"/[0-9]+/g"),
826 MPDM_LS(L"1, 20, 333, 40 all are numbers"),
827 MPDM_LS(L"numbers"), 0);
828 do_test("sregex 4",
829 mpdm_cmp(v,
830 MPDM_LS
831 (L"numbers, numbers, numbers, numbers all are numbers"))
832 == 0);
834 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z_]+/g"),
835 MPDM_LS(L"regex, mpdm_regex, TexMex"), MPDM_LS(L"sex"),
837 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
839 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z]+/g"),
840 MPDM_LS(L"regex, mpdm_regex, TexMex"), NULL, 0);
841 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
843 v = mpdm_sregex(MPDM_LS(L"/\\\\/g"), MPDM_LS(L"\\MSDOS\\style\\path"),
844 MPDM_LS(L"/"), 0);
845 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
847 v = mpdm_sregex(MPDM_LS(L"/regex/gi"), MPDM_LS(L"regex, Regex, REGEX"),
848 MPDM_LS(L"sex"), 0);
849 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
851 v = mpdm_sregex(NULL, NULL, NULL, 0);
852 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
854 /* & in substitution tests */
855 v = MPDM_LS(L"this string has many words");
856 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[&]"), 0);
857 do_test("& in sregex target",
858 mpdm_cmp(v,
859 MPDM_LS(L"[this] [string] [has] [many] [words]")) ==
862 v = MPDM_LS(L"this string has many words");
863 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[\\&]"), 0);
864 do_test("escaped & in sregex target",
865 mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 0);
867 v = MPDM_LS(L"this string has many words");
868 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"\\\\&"), 0);
869 do_test("escaped \\ in sregex target",
870 mpdm_cmp(v,
871 MPDM_LS(L"\\this \\string \\has \\many \\words")) ==
874 v = MPDM_LS(L"hola ");
875 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, MPDM_LS(L""), 0);
876 do_test("sregex output size 1", v->size == 4);
878 v = MPDM_LS(L"hola ");
879 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, NULL, 0);
880 do_test("sregex output size 2", v->size == 4);
882 v = MPDM_LS(L"hola ");
883 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, MPDM_LS(L"!"), 0);
884 do_test("sregex output size 3", v->size == 5);
886 v = MPDM_LS(L"holo");
887 v = mpdm_sregex(MPDM_LS(L"/o/g"), v, MPDM_LS(L"!!"), 0);
888 do_test("sregex output size 4", v->size == 6);
890 /* multiple regex tests */
891 w = MPDM_A(0);
892 mpdm_ref(w);
894 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
895 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
896 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
897 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
898 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
900 v = mpdm_regex(w, MPDM_LS(L"key=value"), 0);
901 mpdm_ref(v);
902 do_test("multi-regex 1.1",
903 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
904 do_test("multi-regex 1.2",
905 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
906 mpdm_unref(v);
908 v = mpdm_regex(w, MPDM_LS(L" key = value"), 0);
909 mpdm_ref(v);
910 do_test("multi-regex 2.1",
911 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
912 do_test("multi-regex 2.2",
913 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
914 mpdm_unref(v);
916 v = mpdm_regex(w, MPDM_LS(L"\t\tkey\t=\tvalue "), 0);
917 mpdm_ref(v);
918 do_test("multi-regex 3.1",
919 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
920 do_test("multi-regex 3.2",
921 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
922 mpdm_unref(v);
924 mpdm_unref(w);
926 /* v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
927 do_test("multi-regex 4", v == NULL);
929 printf("Multiple line regexes\n");
930 w = MPDM_LS(L"/* this is\na C-like comment */");
931 mpdm_ref(w);
933 v = mpdm_regex(MPDM_LS(L"|/\\*.+\\*/|"), w, 0);
934 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
936 v = mpdm_regex(MPDM_LS(L"/is$/"), w, 0);
937 do_test("Multiline regex 2", v == NULL);
939 v = mpdm_regex(MPDM_LS(L"/is$/m"), w, 0);
940 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
941 mpdm_unref(w);
943 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
945 w = MPDM_LS(L"-\x03a9-");
946 mpdm_ref(w);
948 v = mpdm_regex(MPDM_LS(L"/-$/"), w, 0);
949 do_test("Multibyte environment regex 1",
950 mpdm_cmp(v, MPDM_LS(L"-")) == 0);
952 if (do_multibyte_sregex_tests) {
953 v = mpdm_sregex(MPDM_LS(L"/-$/"), w, MPDM_LS(L"~"), 0);
954 do_test("Multibyte environment sregex 1",
955 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
957 v = mpdm_sregex(MPDM_LS(L"/-/g"), w, MPDM_LS(L"~"), 0);
958 do_test("Multibyte environment sregex 2",
959 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
961 else
962 printf("Multibyte sregex test omitted; activate with -m\n");
964 mpdm_unref(w);
966 /* 'last' flag tests */
967 v = MPDM_LS(L"this string has many words");
968 v = mpdm_regex(MPDM_LS(L"/[a-z]+/l"), v, 0);
969 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
973 static mpdm_t dumper(mpdm_t args, mpdm_t ctxt)
974 /* executable value */
976 mpdm_dump(args);
977 return (NULL);
981 static mpdm_t sum(mpdm_t args, mpdm_t ctxt)
982 /* executable value: sum all args */
984 int n, t = 0;
986 if (args != NULL) {
987 for (n = t = 0; n < args->size; n++)
988 t += mpdm_ival(mpdm_aget(args, n));
991 return (MPDM_I(t));
995 static mpdm_t calculator(mpdm_t c, mpdm_t args, mpdm_t ctxt)
996 /* 3 argument version: calculator. c contains a 'script' to
997 do things with the arguments */
999 int n, t;
1000 mpdm_t v;
1001 mpdm_t a;
1002 mpdm_t o;
1004 mpdm_ref(args);
1006 /* to avoid destroying args */
1007 a = mpdm_ref(mpdm_clone(args));
1009 /* shift first argument */
1010 v = mpdm_shift(a);
1011 t = mpdm_ival(v);
1013 for (n = 0; n < mpdm_size(c); n++) {
1014 /* gets operator */
1015 o = mpdm_aget(c, n);
1017 /* gets next value */
1018 v = mpdm_shift(a);
1020 switch (*(wchar_t *) o->data) {
1021 case '+':
1022 t += mpdm_ival(v);
1023 break;
1024 case '-':
1025 t -= mpdm_ival(v);
1026 break;
1027 case '*':
1028 t *= mpdm_ival(v);
1029 break;
1030 case '/':
1031 t /= mpdm_ival(v);
1032 break;
1036 mpdm_unref(a);
1037 mpdm_unref(args);
1039 return (MPDM_I(t));
1043 void test_exec(void)
1045 mpdm_t x;
1046 mpdm_t w;
1047 mpdm_t p;
1049 printf("test_exec\n");
1051 x = MPDM_X(dumper);
1053 /* a simple value */
1054 mpdm_ref(x);
1055 mpdm_exec(x, NULL, NULL);
1056 mpdm_exec(x, x, NULL);
1057 mpdm_unref(x);
1059 x = mpdm_ref(MPDM_X(sum));
1060 w = mpdm_ref(MPDM_A(3));
1061 mpdm_aset(w, MPDM_I(100), 0);
1062 mpdm_aset(w, MPDM_I(220), 1);
1063 mpdm_aset(w, MPDM_I(333), 2);
1065 do_test("exec 0", mpdm_ival(mpdm_exec(x, w, NULL)) == 653);
1066 x = mpdm_unref(x);
1068 mpdm_push(w, MPDM_I(1));
1070 /* multiple executable value: vm and compiler support */
1072 /* calculator 'script' */
1073 p = mpdm_ref(MPDM_A(0));
1074 mpdm_push(p, MPDM_LS(L"+"));
1075 mpdm_push(p, MPDM_LS(L"-"));
1076 mpdm_push(p, MPDM_LS(L"+"));
1078 /* the value */
1079 x = mpdm_ref(MPDM_A(2));
1080 x->flags |= MPDM_EXEC;
1082 mpdm_aset(x, MPDM_X(calculator), 0);
1083 mpdm_aset(x, p, 1);
1085 do_test("exec 1", mpdm_ival(mpdm_exec(x, w, NULL)) == -12);
1087 mpdm_unref(p);
1089 /* another 'script', different operations with the same values */
1090 p = mpdm_ref(MPDM_A(0));
1091 mpdm_push(p, MPDM_LS(L"*"));
1092 mpdm_push(p, MPDM_LS(L"/"));
1093 mpdm_push(p, MPDM_LS(L"+"));
1095 mpdm_aset(x, p, 1);
1097 do_test("exec 2", mpdm_ival(mpdm_exec(x, w, NULL)) == 67);
1098 x = mpdm_unref(x);
1099 p = mpdm_unref(p);
1100 w = mpdm_unref(w);
1104 void test_encoding(void)
1106 mpdm_t f;
1107 mpdm_t v;
1108 mpdm_t w;
1109 const wchar_t *ptr;
1111 v = MPDM_MBS("?Espa?a!\n");
1112 mpdm_ref(v);
1114 printf
1115 ("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
1117 if ((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL) {
1118 printf("Can't write test.txt; no further file test possible.\n");
1119 return;
1122 mpdm_write(f, v);
1123 mpdm_close(f);
1125 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1126 w = mpdm_read(f);
1127 mpdm_dump(w);
1128 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
1129 mpdm_close(f);
1131 printf
1132 ("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1134 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1135 w = mpdm_read(f);
1136 mpdm_dump(w);
1137 mpdm_close(f);
1139 for (ptr = w->data; *ptr != L'\0'; ptr++)
1140 printf("%d", mpdm_wcwidth(*ptr));
1141 printf("\n");
1143 if (mpdm_encoding(MPDM_LS(L"UTF-8")) < 0) {
1144 printf
1145 ("No multiple encoding (iconv) support; no more tests possible.\n");
1146 return;
1149 printf
1150 ("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1152 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1153 w = mpdm_read(f);
1154 mpdm_dump(w);
1155 mpdm_close(f);
1157 /* new open file will use the specified encoding */
1158 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
1159 mpdm_write(f, v);
1160 mpdm_close(f);
1162 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1163 w = mpdm_read(f);
1164 mpdm_dump(w);
1165 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
1166 mpdm_close(f);
1168 mpdm_encoding(NULL);
1169 mpdm_unref(v);
1173 void test_gettext(void)
1175 mpdm_t v;
1176 mpdm_t h;
1178 printf("\nTesting gettext...\n");
1180 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
1182 printf
1183 ("Should follow a translated string of 'This is a test string':\n");
1184 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1185 mpdm_dump(v);
1187 printf("The same, but cached:\n");
1188 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1189 mpdm_dump(v);
1191 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
1192 mpdm_dump(v);
1194 printf("Ad-hoc translation hash:\n");
1196 h = MPDM_H(0);
1197 mpdm_ref(h);
1198 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
1200 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
1201 v = mpdm_gettext(MPDM_LS(L"test string"));
1202 mpdm_dump(v);
1203 mpdm_unref(h);
1207 void timer(int secs)
1209 static clock_t clks = 0;
1211 switch (secs) {
1212 case 0:
1213 clks = clock();
1214 break;
1216 case -1:
1217 printf("%.2f seconds\n",
1218 (float) (clock() - clks) / (float) CLOCKS_PER_SEC);
1219 break;
1224 void bench_hash(int i, mpdm_t l, int buckets)
1226 mpdm_t h;
1227 mpdm_t v;
1228 int n;
1230 printf("Hash of %d buckets: \n", buckets);
1231 h = MPDM_H(buckets);
1232 mpdm_ref(h);
1234 timer(0);
1235 for (n = 0; n < i; n++) {
1236 v = mpdm_aget(l, n);
1237 mpdm_hset(h, v, v);
1239 timer(-1);
1241 mpdm_unref(h);
1244 printf("Bucket usage:\n");
1245 for(n=0;n < mpdm_size(h);n++)
1246 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1251 void benchmark(void)
1253 mpdm_t l;
1254 int i, n;
1255 char tmp[64];
1257 printf("\n");
1259 if (!do_benchmarks) {
1260 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1261 return;
1264 printf("BENCHMARKS\n");
1266 i = 500000;
1268 printf("Creating %d values...\n", i);
1270 l = mpdm_ref(MPDM_A(i));
1271 for (n = 0; n < i; n++) {
1272 sprintf(tmp, "%08x", n);
1273 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1274 mpdm_aset(l, MPDM_I(n), n);
1277 printf("OK\n");
1279 bench_hash(i, l, 0);
1280 bench_hash(i, l, 61);
1281 bench_hash(i, l, 89);
1282 bench_hash(i, l, 127);
1284 mpdm_unref(l);
1288 void test_conversion(void)
1290 wchar_t *wptr = NULL;
1291 char *ptr = NULL;
1292 int size = 0;
1294 ptr = mpdm_wcstombs(L"", &size);
1295 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1297 wptr = mpdm_mbstowcs("", &size, 0);
1298 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1302 void test_pipes(void)
1304 mpdm_t f;
1306 printf("\n");
1308 if ((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL) {
1309 mpdm_t v;
1311 v = mpdm_read(f);
1312 mpdm_pclose(f);
1314 printf("Pipe from 'date':\n");
1315 mpdm_dump(v);
1317 else
1318 printf("Can't pipe to 'date'\n");
1322 void test_misc(void)
1324 printf("Home dir:\n");
1325 mpdm_dump(mpdm_home_dir());
1326 printf("App dir:\n");
1327 mpdm_dump(mpdm_app_dir());
1331 void test_sprintf(void)
1333 mpdm_t v;
1334 mpdm_t w;
1336 printf("sprintf tests\n");
1338 v = MPDM_A(0);
1339 mpdm_ref(v);
1340 mpdm_push(v, MPDM_I(100));
1341 mpdm_push(v, MPDM_LS(L"beers"));
1343 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1344 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1346 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1347 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me 0")) == 0);
1349 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1350 do_test("sprintf 3",
1351 mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1353 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1354 do_test("sprintf 4",
1355 mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1357 mpdm_unref(v);
1359 v = MPDM_A(0);
1360 mpdm_ref(v);
1361 mpdm_push(v, MPDM_R(3.1416));
1363 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1364 do_test("sprintf 2.1",
1365 mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1367 /* w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1368 do_test("sprintf 2.2", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1370 mpdm_unref(v);
1372 v = MPDM_A(0);
1373 mpdm_ref(v);
1374 mpdm_push(v, MPDM_LS(L"stress"));
1376 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1377 do_test("sprintf 3.1",
1378 mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1380 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1381 do_test("sprintf 3.2",
1382 mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1384 mpdm_unref(v);
1386 v = MPDM_A(0);
1387 mpdm_ref(v);
1388 mpdm_push(v, MPDM_I(0x263a));
1390 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1391 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1392 mpdm_unref(v);
1394 v = MPDM_A(0);
1395 mpdm_ref(v);
1396 mpdm_push(v, MPDM_I(75));
1398 w = mpdm_sprintf(MPDM_LS(L"%d%%"), v);
1399 do_test("sprintf 4.1", mpdm_cmp(w, MPDM_LS(L"75%")) == 0);
1401 w = mpdm_sprintf(MPDM_LS(L"%b"), v);
1402 do_test("sprintf 5.1", mpdm_cmp(w, MPDM_LS(L"1001011")) == 0);
1403 mpdm_unref(v);
1407 void test_ulc(void)
1409 mpdm_t v = mpdm_ref(MPDM_S(L"string"));
1410 mpdm_t w = mpdm_ref(mpdm_ulc(v, 1));
1412 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1413 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1415 mpdm_unref(w);
1416 mpdm_unref(v);
1420 void test_scanf(void)
1422 mpdm_t v;
1424 v = mpdm_sscanf(MPDM_LS(L"%d %d"), MPDM_LS(L"1234 5678"), 0);
1425 do_test("mpdm_sscanf_1.1",
1426 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1427 do_test("mpdm_sscanf_1.2",
1428 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1430 v = mpdm_sscanf(MPDM_LS(L"%s %f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1431 do_test("mpdm_sscanf_2.1",
1432 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1433 do_test("mpdm_sscanf_2.2",
1434 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1435 do_test("mpdm_sscanf_2.3",
1436 mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678")) == 0);
1438 v = mpdm_sscanf(MPDM_LS(L"%s %*f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1439 do_test("mpdm_sscanf_3.1",
1440 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1441 do_test("mpdm_sscanf_3.2",
1442 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1444 v = mpdm_sscanf(MPDM_LS(L"%4d%4d%2d%10d"),
1445 MPDM_LS(L"12341234121234567890"), 0);
1446 do_test("mpdm_sscanf_4.1",
1447 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1448 do_test("mpdm_sscanf_4.2",
1449 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"1234")) == 0);
1450 do_test("mpdm_sscanf_4.3",
1451 mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"12")) == 0);
1452 do_test("mpdm_sscanf_4.4",
1453 mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"1234567890")) == 0);
1455 v = mpdm_sscanf(MPDM_LS(L"%[abc]%s"),
1456 MPDM_LS(L"ccbaabcxaaae and more"), 0);
1457 do_test("mpdm_sscanf_5.1",
1458 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1459 do_test("mpdm_sscanf_5.2",
1460 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1462 v = mpdm_sscanf(MPDM_LS(L"%[a-d]%s"),
1463 MPDM_LS(L"ccbaabcxaaae and more"), 0);
1464 do_test("mpdm_sscanf_6.1",
1465 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1466 do_test("mpdm_sscanf_6.2",
1467 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1469 v = mpdm_sscanf(MPDM_LS(L"%[^x]%s"), MPDM_LS(L"ccbaabcxaaae and more"),
1471 do_test("mpdm_sscanf_7.1",
1472 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1473 do_test("mpdm_sscanf_7.2",
1474 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1476 v = mpdm_sscanf(MPDM_LS(L"%[^:]: %s"), MPDM_LS(L"key: value"), 0);
1477 do_test("mpdm_sscanf_8.1",
1478 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"key")) == 0);
1479 do_test("mpdm_sscanf_8.2",
1480 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"value")) == 0);
1482 v = mpdm_sscanf(MPDM_LS(L"%*[^/]/* %s */"),
1483 MPDM_LS(L"this is code /* comment */ more code"), 0);
1484 do_test("mpdm_sscanf_9.1",
1485 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"comment")) == 0);
1487 v = mpdm_sscanf(MPDM_LS(L"%d%%%d"), MPDM_LS(L"1234%5678"), 0);
1488 do_test("mpdm_sscanf_10.1",
1489 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1490 do_test("mpdm_sscanf_10.2",
1491 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1493 v = mpdm_sscanf(MPDM_LS(L"%*[abc]%n%*[^ ]%n"),
1494 MPDM_LS(L"ccbaabcxaaae and more"), 0);
1495 do_test("mpdm_sscanf_11.1", mpdm_ival(mpdm_aget(v, 0)) == 7);
1496 do_test("mpdm_sscanf_11.2", mpdm_ival(mpdm_aget(v, 1)) == 12);
1498 v = mpdm_sscanf(MPDM_LS(L"/* %S */"),
1499 MPDM_LS(L"/* inside the comment */"), 0);
1500 do_test("mpdm_sscanf_12.1",
1501 mpdm_cmp(mpdm_aget(v, 0),
1502 MPDM_LS(L"inside the comment")) == 0);
1504 v = mpdm_sscanf(MPDM_LS(L"/* %S */%s"),
1505 MPDM_LS(L"/* inside the comment */outside"), 0);
1506 do_test("mpdm_sscanf_13.1",
1507 mpdm_cmp(mpdm_aget(v, 0),
1508 MPDM_LS(L"inside the comment")) == 0);
1509 do_test("mpdm_sscanf_13.2",
1510 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"outside")) == 0);
1512 v = mpdm_sscanf(MPDM_LS(L"%n"), MPDM_LS(L""), 0);
1513 do_test("mpdm_sscanf_14.1", mpdm_size(v) == 1
1514 && mpdm_ival(mpdm_aget(v, 0)) == 0);
1516 v = mpdm_sscanf(MPDM_LS(L"%[^%f]%f %[#%d@]"),
1517 MPDM_LS(L"this 12.34 5678#12@34"), 0);
1518 do_test("mpdm_sscanf_15.1",
1519 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this ")) == 0);
1520 do_test("mpdm_sscanf_15.2",
1521 mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1522 do_test("mpdm_sscanf_15.3",
1523 mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678#12@34")) == 0);
1525 v = mpdm_sscanf(MPDM_LS(L"%*S\"%[^\n\"]\""), MPDM_LS(L"a \"bbb\" c;"),
1527 do_test("mpdm_sscanf_16",
1528 mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"bbb")) == 0);
1530 do_test("mpdm_sscanf_17", mpdm_aget(v, 0)->size == 3);
1534 mpdm_t mutex = NULL;
1535 int t_finished = -1;
1537 mpdm_t the_thread(mpdm_t args, mpdm_t ctxt)
1538 /* running from a thread */
1540 mpdm_t fn = mpdm_ref(MPDM_LS(L"thread.txt"));
1541 mpdm_t f;
1543 printf("thread: start writing from thread\n");
1545 if ((f = mpdm_open(fn, MPDM_LS(L"w"))) != NULL) {
1546 int n;
1548 for (n = 0; n < 1000; n++) {
1549 mpdm_write(f, MPDM_I(n));
1550 mpdm_write(f, MPDM_LS(L"\n"));
1553 mpdm_close(f);
1556 printf("thread: finished writing from thread\n");
1558 mpdm_mutex_lock(mutex);
1559 t_finished = 1;
1560 mpdm_mutex_unlock(mutex);
1562 printf("thread: t_finished set\n");
1564 return NULL;
1568 void test_thread(void)
1570 mpdm_t fn = mpdm_ref(MPDM_LS(L"thread.txt"));
1571 mpdm_t x, v;
1572 int done;
1574 printf("Testing threads and mutexes...\n");
1576 mpdm_unlink(fn);
1578 /* create the executable value */
1579 x = mpdm_ref(MPDM_X(the_thread));
1581 /* create a mutex */
1582 mutex = mpdm_ref(mpdm_new_mutex());
1584 t_finished = 0;
1586 v = mpdm_exec_thread(x, NULL, NULL);
1588 printf("parent: waiting for the thread to finish...\n");
1589 mpdm_ref(v);
1591 done = 0;
1592 while (!done) {
1593 mpdm_sleep(10);
1595 mpdm_mutex_lock(mutex);
1597 if (t_finished > 0)
1598 done = 1;
1600 mpdm_mutex_unlock(mutex);
1603 printf("parent: thread said it has finished.\n");
1605 mpdm_unref(v);
1607 mpdm_unref(mutex);
1608 mpdm_unref(x);
1609 mpdm_unref(fn);
1613 mpdm_t sem = NULL;
1615 mpdm_t sem_thread(mpdm_t args, mpdm_t ctxt)
1617 printf("thread: waiting for semaphore...\n");
1619 mpdm_semaphore_wait(sem);
1621 printf("thread: got semaphore.\n");
1623 return NULL;
1627 void test_sem(void)
1629 mpdm_t x, v;
1631 printf("Testing threads and semaphores...\n");
1633 /* create the executable value */
1634 x = mpdm_ref(MPDM_X(sem_thread));
1636 /* creates the semaphore */
1637 sem = mpdm_ref(mpdm_new_semaphore(0));
1639 printf("parent: launching thread.\n");
1641 v = mpdm_exec_thread(x, NULL, NULL);
1642 mpdm_ref(v);
1644 mpdm_sleep(10);
1646 printf("parent: posting semaphore.\n");
1648 mpdm_semaphore_post(sem);
1650 mpdm_sleep(10);
1652 mpdm_unref(v);
1653 mpdm_unref(sem);
1654 mpdm_unref(x);
1658 void (*func) (void) = NULL;
1660 int main(int argc, char *argv[])
1662 if (argc > 1) {
1663 if (strcmp(argv[1], "-b") == 0)
1664 do_benchmarks = 1;
1665 if (strcmp(argv[1], "-m") == 0)
1666 do_multibyte_sregex_tests = 1;
1669 mpdm_startup();
1671 printf("sizeof(struct mpdm_val): %ld\n",
1672 (long) sizeof(struct mpdm_val));
1673 printf("sizeof(void *): %d\n", sizeof(void *));
1674 printf("sizeof(void (*func)(void)): %d\n", sizeof(func));
1676 /* test_counter();*/
1677 test_basic();
1678 test_array();
1679 test_hash();
1680 test_splice();
1681 test_strcat();
1682 test_split();
1683 test_join();
1684 test_sym();
1685 test_file();
1686 test_regex();
1687 test_exec();
1688 test_encoding();
1689 test_gettext();
1690 test_conversion();
1691 test_pipes();
1692 test_misc();
1693 test_sprintf();
1694 test_ulc();
1695 test_scanf();
1696 test_thread();
1697 test_sem();
1699 benchmark();
1701 mpdm_shutdown();
1703 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1705 if (oks == tests)
1706 printf("*** ALL TESTS PASSED\n");
1707 else {
1708 int n;
1710 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1712 printf("\nFailed tests:\n\n");
1713 for (n = 0; n < i_failed_msgs; n++)
1714 printf("%s", failed_msgs[n]);
1717 return oks == tests ? 0 : 1;