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
34 /* total number of tests and oks */
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;
48 void _do_test(char *str
, int ok
, int src_line
)
52 sprintf(tmp
, "%s: %s (line %d)\n", str
, ok
? "OK!" : "*** Failed ***", src_line
);
60 failed_msgs
[i_failed_msgs
++] = strdup(tmp
);
63 #define do_test(str, ok) _do_test(str, ok, __LINE__)
65 #define C { int CC = mpdm->count
66 #define T(i) do_test("v counter", CC + i == mpdm->count); }
70 void test_counter(void)
74 C
; v
= MPDM_S(L
"hi"); T(1);
76 C
; w
= MPDM_A(0); T(1);
78 C
; mpdm_push(w
, v
); T(0);
80 C
; mpdm_adel(w
, 0); T(0); /* should fail in 2.x */
82 C
; mpdm_queue(w
, v
, 10); T(0);
84 C
; v
= MPDM_S(L
"this is a phrase"); T(1);
85 C
; w
= mpdm_split_s(L
" ", v
); T(5);
97 v
= mpdm_ref(MPDM_S(L
"65536"));
101 do_test("i == 65536", (i
== 65536));
102 do_test("v has MPDM_IVAL", (v
->flags
& MPDM_IVAL
));
105 do_test("r == 65536", (r
== 65536.0));
106 do_test("v has MPDM_RVAL", (v
->flags
& MPDM_RVAL
));
110 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
111 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
113 /* partial copies of strings */
114 v
= MPDM_LS(L
"this is not America");
115 v
= MPDM_NS((wchar_t *) v
->data
+ 4, 4);
117 do_test("Partial string values", mpdm_cmp(v
, MPDM_LS(L
" is ")) == 0);
119 v
= mpdm_ref(MPDM_S(L
"MUAHAHAHA!"));
120 w
= mpdm_ref(mpdm_clone(v
));
121 do_test("Testing mpdm_clone semantics 1", w
== v
);
125 v
= mpdm_ref(MPDM_A(2));
126 mpdm_aset(v
, MPDM_S(L
"evil"), 0);
127 mpdm_aset(v
, MPDM_S(L
"dead"), 1);
128 w
= mpdm_ref(mpdm_clone(v
));
130 do_test("Testing mpdm_clone semantics 2.1", w
!= v
);
133 do_test("Testing mpdm_clone semantics 2.2", t
->ref
> 1);
134 do_test("Testing mpdm_clone semantics 2.3", mpdm_aget(w
, 0) == t
);
139 /* mbs / wcs tests */
140 v
= MPDM_MBS("This is (was) a multibyte string");
143 /* greek omega is 03a9 */
144 v
= MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
146 printf("(Previous value will be NULL if locale doesn't match stress.c encoding)\n");
148 v
= MPDM_LS(L
"A capital greek omega between brackets [\x03a9]");
150 printf("(Previous value will only show on an Unicode terminal)\n");
152 v
= mpdm_ref(MPDM_R(3.1416));
154 do_test("rval 1", mpdm_rval(v
) == 3.1416);
155 do_test("ival 1", mpdm_ival(v
) == 3);
158 v
= MPDM_R(777777.0 / 2.0);
160 do_test("mpdm_rnew 1", mpdm_cmp(v
, MPDM_LS(L
"388888.5")) == 0);
162 v
= MPDM_R(388888.500);
164 do_test("mpdm_rnew 2", mpdm_cmp(v
, MPDM_LS(L
"388888.5")) == 0);
166 v
= MPDM_R(388888.412);
168 do_test("mpdm_rnew 3", mpdm_cmp(v
, MPDM_LS(L
"388888.412")) == 0);
170 v
= MPDM_R(388888.6543);
172 do_test("mpdm_rnew 4", mpdm_cmp(v
, MPDM_LS(L
"388888.6543")) == 0);
174 v
= MPDM_R(388888.0);
176 do_test("mpdm_rnew 5", mpdm_cmp(v
, MPDM_LS(L
"388888")) == 0);
178 v
= MPDM_R(0.050000);
180 do_test("mpdm_rnew 6", mpdm_cmp(v
, MPDM_LS(L
"0.05")) == 0);
184 do_test("mpdm_rnew 7", mpdm_cmp(v
, MPDM_LS(L
"0")) == 0);
186 v
= MPDM_LS(L
"0177");
187 do_test("mpdm_ival() for octal numbers", mpdm_ival(v
) == 0x7f);
189 v
= MPDM_LS(L
"0xFF");
190 do_test("mpdm_ival() for hexadecimal numbers", mpdm_ival(v
) == 255);
193 do_test("mpdm_rval() for octal numbers", mpdm_rval(v
) == 1.0);
195 v
= MPDM_LS(L
"0x7f");
196 do_test("mpdm_rval() for hexadecimal numbers", mpdm_ival(v
) == 127.0);
198 do_test("Two NULLs are equal", mpdm_cmp(NULL
, NULL
) == 0);
200 v
= MPDM_LS(L
"hahaha");
202 do_test("mpdm_cmp_s 1", mpdm_cmp_s(v
, L
"hahaha") == 0);
203 do_test("mpdm_cmp_s 2", mpdm_cmp_s(v
, L
"aahaha") > 0);
204 do_test("mpdm_cmp_s 3", mpdm_cmp_s(v
, L
"zahaha") < 0);
208 mpdm_t
sort_cb(mpdm_t args
)
212 /* sorts reversely */
213 d
= mpdm_cmp(mpdm_aget(args
, 1), mpdm_aget(args
, 0));
218 void test_array(void)
227 do_test("a->size == 0", (a
->size
== 0));
229 mpdm_push(a
, MPDM_LS(L
"sunday"));
230 mpdm_push(a
, MPDM_LS(L
"monday"));
231 mpdm_push(a
, MPDM_LS(L
"tuesday"));
232 mpdm_push(a
, MPDM_LS(L
"wednesday"));
233 mpdm_push(a
, MPDM_LS(L
"thursday"));
234 mpdm_push(a
, MPDM_LS(L
"friday"));
235 mpdm_push(a
, MPDM_LS(L
"saturday"));
237 do_test("a->size == 7", (a
->size
== 7));
241 mpdm_aset(a
, NULL
, 3);
245 do_test("NULLs are sorted on top", (mpdm_aget(a
, 0) == NULL
));
251 do_test("v is referenced again", (v
!= NULL
&& v
->ref
> 0));
254 do_test("mpdm_asort() works (1)", mpdm_cmp(mpdm_aget(a
, 0), MPDM_LS(L
"friday")) == 0);
255 do_test("mpdm_asort() works (2)",
256 mpdm_cmp(mpdm_aget(a
, 6), MPDM_LS(L
"wednesday")) == 0);
258 /* asort_cb sorts reversely */
259 mpdm_sort_cb(a
, 1, MPDM_X(sort_cb
));
261 do_test("mpdm_asort_cb() works (1)",
262 mpdm_cmp(mpdm_aget(a
, 6), MPDM_LS(L
"friday")) == 0);
263 do_test("mpdm_asort_cb() works (2)",
264 mpdm_cmp(mpdm_aget(a
, 0), MPDM_LS(L
"wednesday")) == 0);
268 mpdm_collapse(a
, 3, 1);
269 do_test("acollapse unrefs values", (v
->ref
< n
));
277 /* add several values */
278 for (n
= 0; n
< 10; n
++)
279 v
= mpdm_queue(a
, MPDM_I(n
), 10);
281 do_test("queue should still output NULL", (v
== NULL
));
283 v
= mpdm_queue(a
, MPDM_I(11), 10);
284 do_test("queue should no longer output NULL", (v
!= NULL
));
286 v
= mpdm_queue(a
, MPDM_I(12), 10);
287 do_test("queue should return 1", mpdm_ival(v
) == 1);
288 v
= mpdm_queue(a
, MPDM_I(13), 10);
289 do_test("queue should return 2", mpdm_ival(v
) == 2);
290 do_test("queue size should be 10", a
->size
== 10);
293 v
= mpdm_queue(a
, MPDM_I(14), 5);
296 do_test("queue size should be 5", a
->size
== 5);
297 do_test("last taken value should be 8", mpdm_ival(v
) == 8);
302 mpdm_aset(a
, MPDM_I(666), 6000);
304 do_test("array should have been automatically expanded", mpdm_size(a
) == 6001);
306 v
= mpdm_aget(a
, -1);
307 do_test("negative offsets in arrays 1", mpdm_ival(v
) == 666);
309 mpdm_aset(a
, MPDM_I(777), -2);
310 v
= mpdm_aget(a
, 5999);
311 do_test("negative offsets in arrays 2", mpdm_ival(v
) == 777);
313 mpdm_push(a
, MPDM_I(888));
314 v
= mpdm_aget(a
, -1);
315 do_test("negative offsets in arrays 3", mpdm_ival(v
) == 888);
319 mpdm_push(v
, MPDM_I(100));
325 /* array comparisons with mpdm_cmp() */
328 mpdm_aset(a
, MPDM_I(10), 0);
329 mpdm_aset(a
, MPDM_I(60), 1);
331 v
= mpdm_ref(mpdm_clone(a
));
332 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a
, v
) == 0);
335 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a
, v
) > 0);
337 mpdm_push(v
, MPDM_I(80));
338 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a
, v
) < 0);
354 do_test("hsize 1", mpdm_hsize(h
) == 0);
356 mpdm_hset(h
, MPDM_S(L
"mp"), MPDM_I(6));
357 v
= mpdm_hget(h
, MPDM_S(L
"mp"));
359 do_test("hsize 2", mpdm_hsize(h
) == 1);
361 do_test("hash: v != NULL", (v
!= NULL
));
363 do_test("hash: v == 6", (i
== 6));
365 mpdm_hset(h
, MPDM_S(L
"mp2"), MPDM_I(66));
366 v
= mpdm_hget(h
, MPDM_S(L
"mp2"));
368 do_test("hsize 3", mpdm_hsize(h
) == 2);
370 do_test("hash: v != NULL", (v
!= NULL
));
372 do_test("hash: v == 66", (i
== 66));
374 /* fills 100 values */
375 for (n
= 0; n
< 50; n
++)
376 mpdm_hset(h
, MPDM_I(n
), MPDM_I(n
* 10));
377 for (n
= 100; n
>= 50; n
--)
378 mpdm_hset(h
, MPDM_I(n
), MPDM_I(n
* 10));
380 do_test("hsize 4", mpdm_hsize(h
) == 103);
382 /* tests 100 values */
383 for (n
= 0; n
< 100; n
++) {
384 v
= mpdm_hget(h
, MPDM_I(n
));
389 do_test("hash: ival", (i
== n
* 10));
392 do_test("hash: hget", (v
!= NULL
));
395 printf("h's size: %d\n", mpdm_hsize(h
));
397 mpdm_hdel(h
, MPDM_LS(L
"mp"));
398 do_test("hsize 5", mpdm_hsize(h
) == 102);
408 /* use of non-strings as hashes */
413 mpdm_hset(h
, v
, MPDM_I(1234));
415 mpdm_hset(h
, v
, MPDM_I(12345));
417 mpdm_hset(h
, v
, MPDM_I(9876));
420 mpdm_hset(h
, v
, MPDM_I(6543));
421 i
= mpdm_ival(mpdm_hget(h
, v
));
425 do_test("hash: using non-strings as hash keys", (i
== 6543));
427 mpdm_hset(h
, MPDM_LS(L
"ok"), MPDM_I(666));
429 do_test("exists 1", mpdm_exists(h
, MPDM_LS(L
"ok")));
430 do_test("exists 2", !mpdm_exists(h
, MPDM_LS(L
"notok")));
433 v
= mpdm_hget_s(h
, L
"ok");
434 printf("v %s\n", v
== NULL
? "is NULL" : "is NOT NULL");
435 do_test("hget_s 1", mpdm_ival(v
) == 666);
440 for (n
= 0; n
< 1000; n
++) {
441 if (mpdm_hget_s(h
, L
"ok") != NULL
)
444 printf("i: %d\n", i
);
445 do_test("hget_s 1.2", i
== 1000);
448 mpdm_hget_s(h
, L
"ok");
450 do_test("hget 1.2.1", mpdm_hget(h
, MPDM_LS(L
"ok")) != NULL
);
452 mpdm_hset_s(h
, L
"ok", MPDM_I(777));
454 v
= mpdm_hget_s(h
, L
"ok");
455 do_test("hget_s + hset_s", mpdm_ival(v
) == 777);
461 void test_splice(void)
466 w
= mpdm_splice(MPDM_LS(L
"I'm agent Johnson"), MPDM_LS(L
"special "), 4, 0);
467 do_test("splice insertion",
468 mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm special agent Johnson")) == 0);
471 w
= mpdm_splice(MPDM_LS(L
"Life is a shit"), MPDM_LS(L
"cheat"), 10, 4);
472 do_test("splice insertion and deletion (1)",
473 mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"Life is a cheat")) == 0);
474 do_test("splice insertion and deletion (2)",
475 mpdm_cmp(mpdm_aget(w
, 1), MPDM_LS(L
"shit")) == 0);
478 w
= mpdm_splice(MPDM_LS(L
"I'm with dumb"), NULL
, 4, 4);
479 do_test("splice deletion (1)", mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm dumb")) == 0);
480 do_test("splice deletion (2)", mpdm_cmp(mpdm_aget(w
, 1), MPDM_LS(L
"with")) == 0);
483 v
= MPDM_LS(L
"It doesn't matter");
484 w
= mpdm_splice(v
, MPDM_LS(L
" two"), v
->size
, 0);
485 do_test("splice insertion at the end",
486 mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"It doesn't matter two")) == 0);
489 w
= mpdm_splice(NULL
, NULL
, 0, 0);
490 do_test("splice with two NULLS", (mpdm_aget(w
, 0) == NULL
));
492 w
= mpdm_splice(NULL
, MPDM_LS(L
"foo"), 0, 0);
493 do_test("splice with first value NULL",
494 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"foo")) == 0));
496 w
= mpdm_splice(MPDM_LS(L
"foo"), NULL
, 0, 0);
497 do_test("splice with second value NULL",
498 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"foo")) == 0));
500 v
= MPDM_LS(L
"I'm testing");
503 w
= mpdm_splice(v
, NULL
, 0, -1);
504 do_test("splice with negative del (1)",
505 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"")) == 0));
507 w
= mpdm_splice(v
, NULL
, 4, -1);
508 do_test("splice with negative del (2)",
509 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm ")) == 0));
511 w
= mpdm_splice(v
, NULL
, 4, -2);
512 do_test("splice with negative del (3)",
513 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm g")) == 0));
515 w
= mpdm_splice(v
, NULL
, 0, -4);
516 do_test("splice with negative del (4)",
517 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"ing")) == 0));
518 mpdm_dump(mpdm_aget(w
, 0));
520 w
= mpdm_splice(v
, NULL
, 4, -20);
521 do_test("splice with out-of-bounds negative del",
522 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm testing")) == 0));
527 void test_strcat(void)
532 w
= MPDM_LS(L
"something");
535 v
= mpdm_strcat(NULL
, NULL
);
536 do_test("mpdm_strcat(NULL, NULL) returns NULL", v
== NULL
);
538 v
= mpdm_strcat(NULL
, w
);
539 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v
, w
) == 0);
541 v
= mpdm_strcat(w
, NULL
);
542 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v
, w
) == 0);
547 v
= mpdm_strcat(NULL
, w
);
548 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v
, w
) == 0);
550 v
= mpdm_strcat(w
, NULL
);
551 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v
, w
) == 0);
553 v
= mpdm_strcat(w
, w
);
554 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v
, w
) == 0);
560 void test_split(void)
564 printf("mpdm_split test\n\n");
566 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"four.elems.in.string"));
568 do_test("4 elems: ", (w
->size
== 4));
570 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"unseparated string"));
572 do_test("1 elem: ", (w
->size
== 1));
574 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
".dot.at start"));
576 do_test("3 elems: ", (w
->size
== 3));
578 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"dot.at end."));
580 do_test("3 elems: ", (w
->size
== 3));
582 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"three...dots (two empty elements)"));
584 do_test("4 elems: ", (w
->size
== 4));
586 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"."));
588 do_test("2 elems: ", (w
->size
== 2));
590 w
= mpdm_split(NULL
, MPDM_S(L
"I am the man"));
591 do_test("NULL split 1: ", mpdm_size(w
) == 12);
592 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I")) == 0);
602 printf("mpdm_join test\n\n");
605 s
= mpdm_ref(MPDM_LS(L
"--"));
609 mpdm_aset(w
, MPDM_S(L
"ce"), 0);
611 v
= mpdm_join(NULL
, w
);
612 do_test("1 elem, no separator", (mpdm_cmp(v
, MPDM_LS(L
"ce")) == 0));
615 do_test("1 elem, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce")) == 0));
617 mpdm_push(w
, MPDM_LS(L
"n'est"));
619 do_test("2 elems, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce--n'est")) == 0));
621 mpdm_push(w
, MPDM_LS(L
"pas"));
623 do_test("3 elems, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce--n'est--pas")) == 0));
625 v
= mpdm_join(NULL
, w
);
626 do_test("3 elems, no separator", (mpdm_cmp(v
, MPDM_LS(L
"cen'estpas")) == 0));
633 static mpdm_t
active(mpdm_t args
)
644 printf("mpdm_sset / mpdm_sget tests\n\n");
646 mpdm_sset(NULL
, MPDM_LS(L
"mp"), MPDM_H(7));
647 mpdm_sset(NULL
, MPDM_LS(L
"mp.config"), MPDM_H(7));
648 mpdm_sset(NULL
, MPDM_LS(L
"mp.config.auto_indent"), MPDM_I(16384));
649 mpdm_sset(NULL
, MPDM_LS(L
"mp.config.use_regex"), MPDM_I(1357));
650 mpdm_sset(NULL
, MPDM_LS(L
"mp.config.gtk_font_face"), MPDM_LS(L
"profontwindows"));
651 mpdm_sset(NULL
, MPDM_LS(L
"mp.lines"), MPDM_A(2));
652 mpdm_sset(NULL
, MPDM_LS(L
"mp.lines.0"), MPDM_LS(L
"First post!"));
653 mpdm_sset(NULL
, MPDM_LS(L
"mp.lines.1"), MPDM_LS(L
"Second post!"));
654 mpdm_sset(NULL
, MPDM_LS(L
"mp.active"), MPDM_X(active
));
655 mpdm_sset(NULL
, MPDM_LS(L
"mp.active.syntax"), NULL
);
656 mpdm_dump(mpdm_root());
658 v
= mpdm_sget(NULL
, MPDM_LS(L
"mp.config.auto_indent"));
661 do_test("auto_indent == 16384", (i
== 16384));
671 mpdm_t eol
= MPDM_LS(L
"\n");
675 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"));
678 printf("Can't create test.txt; no further file tests possible.\n");
682 do_test("Create test.txt", f
!= NULL
);
684 mpdm_write(f
, MPDM_LS(L
"0"));
686 mpdm_write(f
, MPDM_LS(L
"1"));
689 /* write WITHOUT eol */
690 mpdm_write(f
, MPDM_LS(L
"2"));
694 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
696 do_test("test written file 0", mpdm_cmp(mpdm_read(f
), MPDM_LS(L
"0\n")) == 0);
697 do_test("test written file 1", mpdm_cmp(mpdm_read(f
), MPDM_LS(L
"1\n")) == 0);
698 do_test("test written file 2", mpdm_cmp(mpdm_read(f
), MPDM_LS(L
"2")) == 0);
699 do_test("test written file 3", mpdm_read(f
) == NULL
);
703 mpdm_unlink(MPDM_LS(L
"test.txt"));
704 do_test("unlink", mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r")) == NULL
);
706 v
= mpdm_stat(MPDM_LS(L
"stress.c"));
707 printf("Stat from stress.c:\n");
710 /* v=mpdm_glob(MPDM_LS(L"*"));*/
712 v
= mpdm_glob(NULL
, NULL
);
719 void test_regex(void)
724 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), MPDM_LS(L
"123456"), 0);
725 do_test("regex 0", v
!= NULL
);
727 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), MPDM_I(65536), 0);
728 do_test("regex 1", v
!= NULL
);
730 v
= mpdm_regex(MPDM_LS(L
"/^[0-9]+$/"), MPDM_LS(L
"12345678"), 0);
731 do_test("regex 2", v
!= NULL
);
733 v
= mpdm_regex(MPDM_LS(L
"/^[0-9]+$/"), MPDM_I(1), 0);
734 do_test("regex 3", v
!= NULL
);
736 v
= mpdm_regex(MPDM_LS(L
"/^[0-9]+$/"), MPDM_LS(L
"A12345-678"), 0);
737 do_test("regex 4", v
== NULL
);
739 w
= MPDM_LS(L
"Hell street, 666");
741 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), w
, 0);
745 do_test("regex 5", mpdm_cmp(v
, MPDM_I(666)) == 0);
747 v
= mpdm_regex(MPDM_LS(L
"/regex/"), MPDM_LS(L
"CASE-INSENSITIVE REGEX"), 0);
748 do_test("regex 6.1 (case sensitive)", v
== NULL
);
750 v
= mpdm_regex(MPDM_LS(L
"/regex/i"), MPDM_LS(L
"CASE-INSENSITIVE REGEX"), 0);
751 do_test("regex 6.2 (case insensitive)", v
!= NULL
);
753 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
754 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
756 v
= mpdm_regex(MPDM_LS(L
"/^\\s*/"), MPDM_LS(L
"123456"), 0);
757 do_test("regex 7", v
!= NULL
);
759 v
= mpdm_regex(MPDM_LS(L
"/^\\s+/"), MPDM_LS(L
"123456"), 0);
760 do_test("regex 8", v
== NULL
);
762 v
= mpdm_regex(MPDM_LS(L
"/^\\s+/"), NULL
, 0);
763 do_test("regex 9 (NULL string to match)", v
== NULL
);
767 v
= mpdm_sregex(MPDM_LS(L
"/A/"), MPDM_LS(L
"change all A to A"), MPDM_LS(L
"E"), 0);
768 do_test("sregex 0", mpdm_cmp(v
, MPDM_LS(L
"change all E to A")) == 0);
770 v
= mpdm_sregex(MPDM_LS(L
"/A/g"), MPDM_LS(L
"change all A to A"), MPDM_LS(L
"E"), 0);
771 do_test("sregex 1", mpdm_cmp(v
, MPDM_LS(L
"change all E to E")) == 0);
773 v
= mpdm_sregex(MPDM_LS(L
"/A+/g"), MPDM_LS(L
"change all AAAAAA to E"),
775 do_test("sregex 2", mpdm_cmp(v
, MPDM_LS(L
"change all E to E")) == 0);
777 v
= mpdm_sregex(MPDM_LS(L
"/A+/g"), MPDM_LS(L
"change all A A A A A A to E"),
779 do_test("sregex 3", mpdm_cmp(v
, MPDM_LS(L
"change all E E E E E E to E")) == 0);
781 v
= mpdm_sregex(MPDM_LS(L
"/A+/g"), MPDM_LS(L
"change all AAA A AA AAAAA A AAA to E"),
783 do_test("sregex 3.2", mpdm_cmp(v
, MPDM_LS(L
"change all E E E E E E to E")) == 0);
785 v
= mpdm_sregex(MPDM_LS(L
"/[0-9]+/g"), MPDM_LS(L
"1, 20, 333, 40 all are numbers"),
786 MPDM_LS(L
"numbers"), 0);
789 MPDM_LS(L
"numbers, numbers, numbers, numbers all are numbers")) == 0);
791 v
= mpdm_sregex(MPDM_LS(L
"/[a-zA-Z_]+/g"), MPDM_LS(L
"regex, mpdm_regex, TexMex"),
793 do_test("sregex 5", mpdm_cmp(v
, MPDM_LS(L
"sex, sex, sex")) == 0);
795 v
= mpdm_sregex(MPDM_LS(L
"/[a-zA-Z]+/g"), MPDM_LS(L
"regex, mpdm_regex, TexMex"),
797 do_test("sregex 6", mpdm_cmp(v
, MPDM_LS(L
", _, ")) == 0);
799 v
= mpdm_sregex(MPDM_LS(L
"/\\\\/g"), MPDM_LS(L
"\\MSDOS\\style\\path"),
801 do_test("sregex 7", mpdm_cmp(v
, MPDM_LS(L
"/MSDOS/style/path")) == 0);
803 v
= mpdm_sregex(MPDM_LS(L
"/regex/gi"), MPDM_LS(L
"regex, Regex, REGEX"),
805 do_test("sregex 8", mpdm_cmp(v
, MPDM_LS(L
"sex, sex, sex")) == 0);
807 v
= mpdm_sregex(NULL
, NULL
, NULL
, 0);
808 do_test("Previous sregex substitutions must be 3", mpdm_ival(v
) == 3);
810 /* & in substitution tests */
811 v
= MPDM_LS(L
"this string has many words");
812 v
= mpdm_sregex(MPDM_LS(L
"/[a-z]+/g"), v
, MPDM_LS(L
"[&]"), 0);
813 do_test("& in sregex target",
814 mpdm_cmp(v
, MPDM_LS(L
"[this] [string] [has] [many] [words]")) == 0);
816 v
= MPDM_LS(L
"this string has many words");
817 v
= mpdm_sregex(MPDM_LS(L
"/[a-z]+/g"), v
, MPDM_LS(L
"[\\&]"), 0);
818 do_test("escaped & in sregex target",
819 mpdm_cmp(v
, MPDM_LS(L
"[&] [&] [&] [&] [&]")) == 0);
821 v
= MPDM_LS(L
"this string has many words");
822 v
= mpdm_sregex(MPDM_LS(L
"/[a-z]+/g"), v
, MPDM_LS(L
"\\\\&"), 0);
823 do_test("escaped \\ in sregex target",
824 mpdm_cmp(v
, MPDM_LS(L
"\\this \\string \\has \\many \\words")) == 0);
826 v
= MPDM_LS(L
"hola ");
827 v
= mpdm_sregex(MPDM_LS(L
"/[ \t]$/"), v
, MPDM_LS(L
""), 0);
828 do_test("sregex output size 1", v
->size
== 4);
830 v
= MPDM_LS(L
"hola ");
831 v
= mpdm_sregex(MPDM_LS(L
"/[ \t]$/"), v
, NULL
, 0);
832 do_test("sregex output size 2", v
->size
== 4);
834 v
= MPDM_LS(L
"hola ");
835 v
= mpdm_sregex(MPDM_LS(L
"/[ \t]$/"), v
, MPDM_LS(L
"!"), 0);
836 do_test("sregex output size 3", v
->size
== 5);
838 v
= MPDM_LS(L
"holo");
839 v
= mpdm_sregex(MPDM_LS(L
"/o/g"), v
, MPDM_LS(L
"!!"), 0);
840 do_test("sregex output size 4", v
->size
== 6);
842 /* multiple regex tests */
846 mpdm_push(w
, MPDM_LS(L
"/^[ \t]*/"));
847 mpdm_push(w
, MPDM_LS(L
"/[^ \t=]+/"));
848 mpdm_push(w
, MPDM_LS(L
"/[ \t]*=[ \t]*/"));
849 mpdm_push(w
, MPDM_LS(L
"/[^ \t]+/"));
850 mpdm_push(w
, MPDM_LS(L
"/[ \t]*$/"));
852 v
= mpdm_regex(w
, MPDM_LS(L
"key=value"), 0);
854 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"key")) == 0);
855 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v
, 3), MPDM_LS(L
"value")) == 0);
858 v
= mpdm_regex(w
, MPDM_LS(L
" key = value"), 0);
860 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"key")) == 0);
861 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v
, 3), MPDM_LS(L
"value")) == 0);
864 v
= mpdm_regex(w
, MPDM_LS(L
"\t\tkey\t=\tvalue "), 0);
866 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"key")) == 0);
867 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v
, 3), MPDM_LS(L
"value")) == 0);
872 /* v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
873 do_test("multi-regex 4", v == NULL);
875 printf("Multiple line regexes\n");
876 w
= MPDM_LS(L
"/* this is\na C-like comment */");
879 v
= mpdm_regex(MPDM_LS(L
"|/\\*.+\\*/|"), w
, 0);
880 do_test("Multiline regex 1", mpdm_cmp(v
, w
) == 0);
882 v
= mpdm_regex(MPDM_LS(L
"/is$/"), w
, 0);
883 do_test("Multiline regex 2", v
== NULL
);
885 v
= mpdm_regex(MPDM_LS(L
"/is$/m"), w
, 0);
886 do_test("Multiline regex 3", mpdm_cmp(v
, MPDM_LS(L
"is")) == 0);
889 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
891 w
= MPDM_LS(L
"-\x03a9-");
894 v
= mpdm_regex(MPDM_LS(L
"/-$/"), w
, 0);
895 do_test("Multibyte environment regex 1", mpdm_cmp(v
, MPDM_LS(L
"-")) == 0);
897 if (do_multibyte_sregex_tests
) {
898 v
= mpdm_sregex(MPDM_LS(L
"/-$/"), w
, MPDM_LS(L
"~"), 0);
899 do_test("Multibyte environment sregex 1",
900 mpdm_cmp(v
, MPDM_LS(L
"-\x03a9~")) == 0);
902 v
= mpdm_sregex(MPDM_LS(L
"/-/g"), w
, MPDM_LS(L
"~"), 0);
903 do_test("Multibyte environment sregex 2",
904 mpdm_cmp(v
, MPDM_LS(L
"~\x03a9~")) == 0);
907 printf("Multibyte sregex test omitted; activate with -m\n");
911 /* 'last' flag tests */
912 v
= MPDM_LS(L
"this string has many words");
913 v
= mpdm_regex(MPDM_LS(L
"/[a-z]+/l"), v
, 0);
914 do_test("Flag l in mpdm_regex", mpdm_cmp(v
, MPDM_LS(L
"words")) == 0);
918 static mpdm_t
dumper(mpdm_t args
, mpdm_t ctxt
)
919 /* executable value */
926 static mpdm_t
sum(mpdm_t args
, mpdm_t ctxt
)
927 /* executable value: sum all args */
932 for (n
= t
= 0; n
< args
->size
; n
++)
933 t
+= mpdm_ival(mpdm_aget(args
, n
));
940 static mpdm_t
calculator(mpdm_t c
, mpdm_t args
, mpdm_t ctxt
)
941 /* 3 argument version: calculator. c contains a 'script' to
942 do things with the arguments */
951 /* to avoid destroying args */
952 a
= mpdm_ref(mpdm_clone(args
));
954 /* shift first argument */
958 for (n
= 0; n
< mpdm_size(c
); n
++) {
962 /* gets next value */
965 switch (*(wchar_t *) o
->data
) {
994 printf("test_exec\n");
1000 mpdm_exec(x
, NULL
, NULL
);
1001 mpdm_exec(x
, x
, NULL
);
1004 x
= mpdm_ref(MPDM_X(sum
));
1005 w
= mpdm_ref(MPDM_A(3));
1006 mpdm_aset(w
, MPDM_I(100), 0);
1007 mpdm_aset(w
, MPDM_I(220), 1);
1008 mpdm_aset(w
, MPDM_I(333), 2);
1010 do_test("exec 0", mpdm_ival(mpdm_exec(x
, w
, NULL
)) == 653);
1013 mpdm_push(w
, MPDM_I(1));
1015 /* multiple executable value: vm and compiler support */
1017 /* calculator 'script' */
1018 p
= mpdm_ref(MPDM_A(0));
1019 mpdm_push(p
, MPDM_LS(L
"+"));
1020 mpdm_push(p
, MPDM_LS(L
"-"));
1021 mpdm_push(p
, MPDM_LS(L
"+"));
1024 x
= mpdm_ref(MPDM_A(2));
1025 x
->flags
|= MPDM_EXEC
;
1027 mpdm_aset(x
, MPDM_X(calculator
), 0);
1030 do_test("exec 1", mpdm_ival(mpdm_exec(x
, w
, NULL
)) == -12);
1034 /* another 'script', different operations with the same values */
1035 p
= mpdm_ref(MPDM_A(0));
1036 mpdm_push(p
, MPDM_LS(L
"*"));
1037 mpdm_push(p
, MPDM_LS(L
"/"));
1038 mpdm_push(p
, MPDM_LS(L
"+"));
1042 do_test("exec 2", mpdm_ival(mpdm_exec(x
, w
, NULL
)) == 67);
1049 void test_encoding(void)
1056 v
= MPDM_MBS("?Espa?a!\n");
1059 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
1061 if ((f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"))) == NULL
) {
1062 printf("Can't write test.txt; no further file test possible.\n");
1069 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
1072 do_test("Locale encoding", mpdm_cmp(w
, v
) == 0);
1076 ("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1078 f
= mpdm_open(MPDM_LS(L
"utf8.txt"), MPDM_LS(L
"r"));
1083 for (ptr
= w
->data
; *ptr
!= L
'\0'; ptr
++)
1084 printf("%d", mpdm_wcwidth(*ptr
));
1087 if (mpdm_encoding(MPDM_LS(L
"UTF-8")) < 0) {
1088 printf("No multiple encoding (iconv) support; no more tests possible.\n");
1093 ("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1095 f
= mpdm_open(MPDM_LS(L
"utf8.txt"), MPDM_LS(L
"r"));
1100 /* new open file will use the specified encoding */
1101 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"));
1105 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
1108 do_test("iconv encoding", mpdm_cmp(w
, v
) == 0);
1111 mpdm_encoding(NULL
);
1116 void test_gettext(void)
1121 printf("\nTesting gettext...\n");
1123 mpdm_gettext_domain(MPDM_LS(L
"stress"), MPDM_LS(L
"./po"));
1125 printf("Should follow a translated string of 'This is a test string':\n");
1126 v
= mpdm_gettext(MPDM_LS(L
"This is a test string"));
1129 printf("The same, but cached:\n");
1130 v
= mpdm_gettext(MPDM_LS(L
"This is a test string"));
1133 v
= mpdm_gettext(MPDM_LS(L
"This string is not translated"));
1136 printf("Ad-hoc translation hash:\n");
1140 mpdm_hset(h
, MPDM_LS(L
"test string"), MPDM_LS(L
"cadena de prueba"));
1142 mpdm_gettext_domain(MPDM_LS(L
"stress"), h
);
1143 v
= mpdm_gettext(MPDM_LS(L
"test string"));
1149 void timer(int secs
)
1151 static clock_t clks
= 0;
1159 printf("%.2f seconds\n", (float) (clock() - clks
) / (float) CLOCKS_PER_SEC
);
1165 void bench_hash(int i
, mpdm_t l
, int buckets
)
1171 printf("Hash of %d buckets: \n", buckets
);
1172 h
= MPDM_H(buckets
);
1176 for (n
= 0; n
< i
; n
++) {
1177 v
= mpdm_aget(l
, n
);
1185 printf("Bucket usage:\n");
1186 for(n=0;n < mpdm_size(h);n++)
1187 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1193 void benchmark(void)
1201 if (!do_benchmarks
) {
1202 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1206 printf("BENCHMARKS\n");
1210 printf("Creating %d values...\n", i
);
1212 l
= mpdm_ref(MPDM_A(i
));
1213 for (n
= 0; n
< i
; n
++) {
1214 sprintf(tmp
, "%08x", n
);
1215 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1216 mpdm_aset(l
, MPDM_I(n
), n
);
1221 bench_hash(i
, l
, 0);
1222 bench_hash(i
, l
, 61);
1223 bench_hash(i
, l
, 89);
1224 bench_hash(i
, l
, 127);
1230 void test_conversion(void)
1232 wchar_t *wptr
= NULL
;
1236 ptr
= mpdm_wcstombs(L
"", &size
);
1237 do_test("mpdm_wcstombs converts an empty string", ptr
!= NULL
);
1239 wptr
= mpdm_mbstowcs("", &size
, 0);
1240 do_test("mpdm_mbstowcs converts an empty string", wptr
!= NULL
);
1244 void test_pipes(void)
1250 if ((f
= mpdm_popen(MPDM_LS(L
"date"), MPDM_LS(L
"r"))) != NULL
) {
1256 printf("Pipe from 'date':\n");
1260 printf("Can't pipe to 'date'\n");
1264 void test_misc(void)
1266 printf("Home dir:\n");
1267 mpdm_dump(mpdm_home_dir());
1268 printf("App dir:\n");
1269 mpdm_dump(mpdm_app_dir());
1273 void test_sprintf(void)
1278 printf("sprintf tests\n");
1282 mpdm_push(v
, MPDM_I(100));
1283 mpdm_push(v
, MPDM_LS(L
"beers"));
1285 w
= mpdm_sprintf(MPDM_LS(L
"%d %s for me"), v
);
1286 do_test("sprintf 1", mpdm_cmp(w
, MPDM_LS(L
"100 beers for me")) == 0);
1288 w
= mpdm_sprintf(MPDM_LS(L
"%d %s for me %d"), v
);
1289 do_test("sprintf 2", mpdm_cmp(w
, MPDM_LS(L
"100 beers for me 0")) == 0);
1291 w
= mpdm_sprintf(MPDM_LS(L
"%10d %s for me"), v
);
1292 do_test("sprintf 3", mpdm_cmp(w
, MPDM_LS(L
" 100 beers for me")) == 0);
1294 w
= mpdm_sprintf(MPDM_LS(L
"%010d %s for me"), v
);
1295 do_test("sprintf 4", mpdm_cmp(w
, MPDM_LS(L
"0000000100 beers for me")) == 0);
1301 mpdm_push(v
, MPDM_R(3.1416));
1303 w
= mpdm_sprintf(MPDM_LS(L
"Value for PI is %6.4f"), v
);
1304 do_test("sprintf 2.1", mpdm_cmp(w
, MPDM_LS(L
"Value for PI is 3.1416")) == 0);
1306 /* w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1307 do_test("sprintf 2.2", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1313 mpdm_push(v
, MPDM_LS(L
"stress"));
1315 w
= mpdm_sprintf(MPDM_LS(L
"This is a |%10s| test"), v
);
1316 do_test("sprintf 3.1", mpdm_cmp(w
, MPDM_LS(L
"This is a | stress| test")) == 0);
1318 w
= mpdm_sprintf(MPDM_LS(L
"This is a |%-10s| test"), v
);
1319 do_test("sprintf 3.2", mpdm_cmp(w
, MPDM_LS(L
"This is a |stress | test")) == 0);
1325 mpdm_push(v
, MPDM_I(0x263a));
1327 w
= mpdm_sprintf(MPDM_LS(L
"%c"), v
);
1328 do_test("sprintf 3.3", mpdm_cmp(w
, MPDM_LS(L
"\x263a")) == 0);
1333 mpdm_push(v
, MPDM_I(75));
1335 w
= mpdm_sprintf(MPDM_LS(L
"%d%%"), v
);
1336 do_test("sprintf 4.1", mpdm_cmp(w
, MPDM_LS(L
"75%")) == 0);
1338 w
= mpdm_sprintf(MPDM_LS(L
"%b"), v
);
1339 do_test("sprintf 5.1", mpdm_cmp(w
, MPDM_LS(L
"1001011")) == 0);
1346 mpdm_t v
= mpdm_ref(MPDM_S(L
"string"));
1347 mpdm_t w
= mpdm_ref(mpdm_ulc(v
, 1));
1349 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v
, 1), w
) == 0);
1350 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w
, 0), v
) == 0);
1357 void test_scanf(void)
1361 v
= mpdm_sscanf(MPDM_LS(L
"%d %d"), MPDM_LS(L
"1234 5678"), 0);
1362 do_test("mpdm_sscanf_1.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"1234")) == 0);
1363 do_test("mpdm_sscanf_1.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"5678")) == 0);
1365 v
= mpdm_sscanf(MPDM_LS(L
"%s %f %d"), MPDM_LS(L
"this 12.34 5678"), 0);
1366 do_test("mpdm_sscanf_2.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"this")) == 0);
1367 do_test("mpdm_sscanf_2.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"12.34")) == 0);
1368 do_test("mpdm_sscanf_2.3", mpdm_cmp(mpdm_aget(v
, 2), MPDM_LS(L
"5678")) == 0);
1370 v
= mpdm_sscanf(MPDM_LS(L
"%s %*f %d"), MPDM_LS(L
"this 12.34 5678"), 0);
1371 do_test("mpdm_sscanf_3.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"this")) == 0);
1372 do_test("mpdm_sscanf_3.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"5678")) == 0);
1374 v
= mpdm_sscanf(MPDM_LS(L
"%4d%4d%2d%10d"), MPDM_LS(L
"12341234121234567890"), 0);
1375 do_test("mpdm_sscanf_4.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"1234")) == 0);
1376 do_test("mpdm_sscanf_4.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"1234")) == 0);
1377 do_test("mpdm_sscanf_4.3", mpdm_cmp(mpdm_aget(v
, 2), MPDM_LS(L
"12")) == 0);
1378 do_test("mpdm_sscanf_4.4", mpdm_cmp(mpdm_aget(v
, 3), MPDM_LS(L
"1234567890")) == 0);
1380 v
= mpdm_sscanf(MPDM_LS(L
"%[abc]%s"), MPDM_LS(L
"ccbaabcxaaae and more"), 0);
1381 do_test("mpdm_sscanf_5.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"ccbaabc")) == 0);
1382 do_test("mpdm_sscanf_5.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"xaaae")) == 0);
1384 v
= mpdm_sscanf(MPDM_LS(L
"%[a-d]%s"), MPDM_LS(L
"ccbaabcxaaae and more"), 0);
1385 do_test("mpdm_sscanf_6.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"ccbaabc")) == 0);
1386 do_test("mpdm_sscanf_6.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"xaaae")) == 0);
1388 v
= mpdm_sscanf(MPDM_LS(L
"%[^x]%s"), MPDM_LS(L
"ccbaabcxaaae and more"), 0);
1389 do_test("mpdm_sscanf_7.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"ccbaabc")) == 0);
1390 do_test("mpdm_sscanf_7.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"xaaae")) == 0);
1392 v
= mpdm_sscanf(MPDM_LS(L
"%[^:]: %s"), MPDM_LS(L
"key: value"), 0);
1393 do_test("mpdm_sscanf_8.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"key")) == 0);
1394 do_test("mpdm_sscanf_8.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"value")) == 0);
1396 v
= mpdm_sscanf(MPDM_LS(L
"%*[^/]/* %s */"), MPDM_LS(L
"this is code /* comment */ more code"), 0);
1397 do_test("mpdm_sscanf_9.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"comment")) == 0);
1399 v
= mpdm_sscanf(MPDM_LS(L
"%d%%%d"), MPDM_LS(L
"1234%5678"), 0);
1400 do_test("mpdm_sscanf_10.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"1234")) == 0);
1401 do_test("mpdm_sscanf_10.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"5678")) == 0);
1403 v
= mpdm_sscanf(MPDM_LS(L
"%*[abc]%n%*[^ ]%n"), MPDM_LS(L
"ccbaabcxaaae and more"), 0);
1404 do_test("mpdm_sscanf_11.1", mpdm_ival(mpdm_aget(v
, 0)) == 7);
1405 do_test("mpdm_sscanf_11.2", mpdm_ival(mpdm_aget(v
, 1)) == 12);
1407 v
= mpdm_sscanf(MPDM_LS(L
"/* %S */"), MPDM_LS(L
"/* inside the comment */"), 0);
1408 do_test("mpdm_sscanf_12.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"inside the comment")) == 0);
1410 v
= mpdm_sscanf(MPDM_LS(L
"/* %S */%s"), MPDM_LS(L
"/* inside the comment */outside"), 0);
1411 do_test("mpdm_sscanf_13.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"inside the comment")) == 0);
1412 do_test("mpdm_sscanf_13.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"outside")) == 0);
1414 v
= mpdm_sscanf(MPDM_LS(L
"%n"), MPDM_LS(L
""), 0);
1415 do_test("mpdm_sscanf_14.1", mpdm_size(v
) == 1 && mpdm_ival(mpdm_aget(v
, 0)) == 0);
1417 v
= mpdm_sscanf(MPDM_LS(L
"%[^%f]%f %[#%d@]"), MPDM_LS(L
"this 12.34 5678#12@34"), 0);
1418 do_test("mpdm_sscanf_15.1", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"this ")) == 0);
1419 do_test("mpdm_sscanf_15.2", mpdm_cmp(mpdm_aget(v
, 1), MPDM_LS(L
"12.34")) == 0);
1420 do_test("mpdm_sscanf_15.3", mpdm_cmp(mpdm_aget(v
, 2), MPDM_LS(L
"5678#12@34")) == 0);
1422 v
= mpdm_sscanf(MPDM_LS(L
"%*S\"%[^\n\"]\""), MPDM_LS(L
"a \"bbb\" c;"), 0);
1423 do_test("mpdm_sscanf_16", mpdm_cmp(mpdm_aget(v
, 0), MPDM_LS(L
"bbb")) == 0);
1425 do_test("mpdm_sscanf_17", mpdm_aget(v
, 0)->size
== 3);
1429 mpdm_t mutex
= NULL
;
1430 int t_finished
= -1;
1432 mpdm_t
the_thread(mpdm_t args
, mpdm_t ctxt
)
1433 /* running from a thread */
1435 mpdm_t fn
= mpdm_ref(MPDM_LS(L
"thread.txt"));
1438 printf("thread: start writing from thread\n");
1440 if ((f
= mpdm_open(fn
, MPDM_LS(L
"w"))) != NULL
) {
1443 for (n
= 0; n
< 1000; n
++) {
1444 mpdm_write(f
, MPDM_I(n
));
1445 mpdm_write(f
, MPDM_LS(L
"\n"));
1451 printf("thread: finished writing from thread\n");
1453 mpdm_mutex_lock(mutex
);
1455 mpdm_mutex_unlock(mutex
);
1457 printf("thread: t_finished set\n");
1463 void test_thread(void)
1465 mpdm_t fn
= mpdm_ref(MPDM_LS(L
"thread.txt"));
1469 printf("Testing threads and mutexes...\n");
1473 /* create the executable value */
1474 x
= mpdm_ref(MPDM_X(the_thread
));
1476 /* create a mutex */
1477 mutex
= mpdm_ref(mpdm_new_mutex());
1481 v
= mpdm_exec_thread(x
, NULL
, NULL
);
1483 printf("parent: waiting for the thread to finish...\n");
1490 mpdm_mutex_lock(mutex
);
1495 mpdm_mutex_unlock(mutex
);
1498 printf("parent: thread said it has finished.\n");
1510 mpdm_t
sem_thread(mpdm_t args
, mpdm_t ctxt
)
1512 printf("thread: waiting for semaphore...\n");
1514 mpdm_semaphore_wait(sem
);
1516 printf("thread: got semaphore.\n");
1526 printf("Testing threads and semaphores...\n");
1528 /* create the executable value */
1529 x
= mpdm_ref(MPDM_X(sem_thread
));
1531 /* creates the semaphore */
1532 sem
= mpdm_ref(mpdm_new_semaphore(0));
1534 printf("parent: launching thread.\n");
1536 v
= mpdm_exec_thread(x
, NULL
, NULL
);
1541 printf("parent: posting semaphore.\n");
1543 mpdm_semaphore_post(sem
);
1553 void (*func
)(void) = NULL
;
1555 int main(int argc
, char *argv
[])
1558 if (strcmp(argv
[1], "-b") == 0)
1560 if (strcmp(argv
[1], "-m") == 0)
1561 do_multibyte_sregex_tests
= 1;
1566 printf("sizeof(struct mpdm_val): %ld\n", (long) sizeof(struct mpdm_val
));
1567 printf("sizeof(void *): %d\n", sizeof(void *));
1568 printf("sizeof(void (*func)(void)): %d\n", sizeof(func
));
1570 /* test_counter();*/
1595 /* mpdm_dump_unref();*/
1607 /* mpdm_dump_unref();*/
1611 printf("\n*** Total tests passed: %d/%d\n", oks
, tests
);
1614 printf("*** ALL TESTS PASSED\n");
1618 printf("*** %d %s\n", tests
- oks
, "TESTS ---FAILED---");
1620 printf("\nFailed tests:\n\n");
1621 for (n
= 0; n
< i_failed_msgs
; n
++)
1622 printf("%s", failed_msgs
[n
]);