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 ***",
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); }
71 void test_counter(void)
89 T(0); /* should fail in 2.x */
96 v
= MPDM_S(L
"this is a phrase");
99 w
= mpdm_split_s(L
" ", v
);
104 void test_basic(void)
112 v
= mpdm_ref(MPDM_S(L
"65536"));
116 do_test("i == 65536", (i
== 65536));
117 do_test("v has MPDM_IVAL", (v
->flags
& MPDM_IVAL
));
120 do_test("r == 65536", (r
== 65536.0));
121 do_test("v has MPDM_RVAL", (v
->flags
& MPDM_RVAL
));
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
);
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
);
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
);
154 /* mbs / wcs tests */
155 v
= MPDM_MBS("This is (was) a multibyte string");
158 /* greek omega is 03a9 */
159 v
= MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
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]");
166 printf("(Previous value will only show on an Unicode terminal)\n");
168 v
= mpdm_ref(MPDM_R(3.1416));
170 do_test("rval 1", mpdm_rval(v
) == 3.1416);
171 do_test("ival 1", mpdm_ival(v
) == 3);
174 v
= MPDM_R(777777.0 / 2.0);
176 do_test("mpdm_rnew 1", mpdm_cmp(v
, MPDM_LS(L
"388888.5")) == 0);
178 v
= MPDM_R(388888.500);
180 do_test("mpdm_rnew 2", mpdm_cmp(v
, MPDM_LS(L
"388888.5")) == 0);
182 v
= MPDM_R(388888.412);
184 do_test("mpdm_rnew 3", mpdm_cmp(v
, MPDM_LS(L
"388888.412")) == 0);
186 v
= MPDM_R(388888.6543);
188 do_test("mpdm_rnew 4", mpdm_cmp(v
, MPDM_LS(L
"388888.6543")) == 0);
190 v
= MPDM_R(388888.0);
192 do_test("mpdm_rnew 5", mpdm_cmp(v
, MPDM_LS(L
"388888")) == 0);
194 v
= MPDM_R(0.050000);
196 do_test("mpdm_rnew 6", mpdm_cmp(v
, MPDM_LS(L
"0.05")) == 0);
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);
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");
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);
224 mpdm_t
sort_cb(mpdm_t args
)
228 /* sorts reversely */
229 d
= mpdm_cmp(mpdm_aget(args
, 1), mpdm_aget(args
, 0));
234 void test_array(void)
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"));
253 do_test("a->size == 7", (a
->size
== 7));
257 mpdm_aset(a
, NULL
, 3);
261 do_test("NULLs are sorted on top", (mpdm_aget(a
, 0) == NULL
));
267 do_test("v is referenced again", (v
!= NULL
&& v
->ref
> 0));
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);
285 mpdm_collapse(a
, 3, 1);
286 do_test("acollapse unrefs values", (v
->ref
< n
));
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);
310 v
= mpdm_queue(a
, MPDM_I(14), 5);
313 do_test("queue size should be 5", a
->size
== 5);
314 do_test("last taken value should be 8", mpdm_ival(v
) == 8);
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);
337 mpdm_push(v
, MPDM_I(100));
343 /* array comparisons with mpdm_cmp() */
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);
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",
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
));
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
));
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
));
408 do_test("hash: ival", (i
== n
* 10));
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);
427 /* use of non-strings as hashes */
432 mpdm_hset(h
, v
, MPDM_I(1234));
434 mpdm_hset(h
, v
, MPDM_I(12345));
436 mpdm_hset(h
, v
, MPDM_I(9876));
439 mpdm_hset(h
, v
, MPDM_I(6543));
440 i
= mpdm_ival(mpdm_hget(h
, v
));
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")));
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);
459 for (n
= 0; n
< 1000; n
++) {
460 if (mpdm_hget_s(h
, L
"ok") != NULL
)
463 printf("i: %d\n", i
);
464 do_test("hget_s 1.2", 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);
480 void test_splice(void)
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);
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);
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);
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);
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");
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));
551 void test_strcat(void)
556 w
= MPDM_LS(L
"something");
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);
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);
584 void test_split(void)
588 printf("mpdm_split test\n\n");
590 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"four.elems.in.string"));
592 do_test("4 elems: ", (w
->size
== 4));
594 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"unseparated string"));
596 do_test("1 elem: ", (w
->size
== 1));
598 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
".dot.at start"));
600 do_test("3 elems: ", (w
->size
== 3));
602 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"dot.at end."));
604 do_test("3 elems: ", (w
->size
== 3));
606 w
= mpdm_split(MPDM_S(L
"."),
607 MPDM_S(L
"three...dots (two empty elements)"));
609 do_test("4 elems: ", (w
->size
== 4));
611 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"."));
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);
628 printf("mpdm_join test\n\n");
631 s
= mpdm_ref(MPDM_LS(L
"--"));
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));
641 do_test("1 elem, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce")) == 0));
643 mpdm_push(w
, MPDM_LS(L
"n'est"));
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"));
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));
662 static mpdm_t
active(mpdm_t args
)
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"));
691 do_test("auto_indent == 16384", (i
== 16384));
699 mpdm_t eol
= MPDM_LS(L
"\n");
703 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"));
706 printf("Can't create test.txt; no further file tests possible.\n");
710 do_test("Create test.txt", f
!= NULL
);
712 mpdm_write(f
, MPDM_LS(L
"0"));
714 mpdm_write(f
, MPDM_LS(L
"1"));
717 /* write WITHOUT eol */
718 mpdm_write(f
, MPDM_LS(L
"2"));
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
);
734 mpdm_unlink(MPDM_LS(L
"test.txt"));
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");
742 /* v=mpdm_glob(MPDM_LS(L"*"));*/
744 v
= mpdm_glob(NULL
, NULL
);
751 void test_regex(void)
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");
773 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), w
, 0);
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
);
801 v
= mpdm_sregex(MPDM_LS(L
"/A/"), MPDM_LS(L
"change all A to A"),
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"),
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"),
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"),
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"),
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);
831 (L
"numbers, numbers, numbers, numbers all are numbers"))
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"),
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"),
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",
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",
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 */
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);
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);
908 v
= mpdm_regex(w
, MPDM_LS(L
" key = value"), 0);
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);
916 v
= mpdm_regex(w
, MPDM_LS(L
"\t\tkey\t=\tvalue "), 0);
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);
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 */");
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);
943 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
945 w
= MPDM_LS(L
"-\x03a9-");
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);
962 printf("Multibyte sregex test omitted; activate with -m\n");
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 */
981 static mpdm_t
sum(mpdm_t args
, mpdm_t ctxt
)
982 /* executable value: sum all args */
987 for (n
= t
= 0; n
< args
->size
; n
++)
988 t
+= mpdm_ival(mpdm_aget(args
, n
));
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 */
1006 /* to avoid destroying args */
1007 a
= mpdm_ref(mpdm_clone(args
));
1009 /* shift first argument */
1013 for (n
= 0; n
< mpdm_size(c
); n
++) {
1015 o
= mpdm_aget(c
, n
);
1017 /* gets next value */
1020 switch (*(wchar_t *) o
->data
) {
1043 void test_exec(void)
1049 printf("test_exec\n");
1053 /* a simple value */
1055 mpdm_exec(x
, NULL
, NULL
);
1056 mpdm_exec(x
, x
, NULL
);
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);
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
"+"));
1079 x
= mpdm_ref(MPDM_A(2));
1080 x
->flags
|= MPDM_EXEC
;
1082 mpdm_aset(x
, MPDM_X(calculator
), 0);
1085 do_test("exec 1", mpdm_ival(mpdm_exec(x
, w
, NULL
)) == -12);
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
"+"));
1097 do_test("exec 2", mpdm_ival(mpdm_exec(x
, w
, NULL
)) == 67);
1104 void test_encoding(void)
1111 v
= MPDM_MBS("?Espa?a!\n");
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");
1125 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
1128 do_test("Locale encoding", mpdm_cmp(w
, v
) == 0);
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"));
1139 for (ptr
= w
->data
; *ptr
!= L
'\0'; ptr
++)
1140 printf("%d", mpdm_wcwidth(*ptr
));
1143 if (mpdm_encoding(MPDM_LS(L
"UTF-8")) < 0) {
1145 ("No multiple encoding (iconv) support; no more tests possible.\n");
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"));
1157 /* new open file will use the specified encoding */
1158 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"));
1162 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
1165 do_test("iconv encoding", mpdm_cmp(w
, v
) == 0);
1168 mpdm_encoding(NULL
);
1173 void test_gettext(void)
1178 printf("\nTesting gettext...\n");
1180 mpdm_gettext_domain(MPDM_LS(L
"stress"), MPDM_LS(L
"./po"));
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"));
1187 printf("The same, but cached:\n");
1188 v
= mpdm_gettext(MPDM_LS(L
"This is a test string"));
1191 v
= mpdm_gettext(MPDM_LS(L
"This string is not translated"));
1194 printf("Ad-hoc translation hash:\n");
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"));
1207 void timer(int secs
)
1209 static clock_t clks
= 0;
1217 printf("%.2f seconds\n",
1218 (float) (clock() - clks
) / (float) CLOCKS_PER_SEC
);
1224 void bench_hash(int i
, mpdm_t l
, int buckets
)
1230 printf("Hash of %d buckets: \n", buckets
);
1231 h
= MPDM_H(buckets
);
1235 for (n
= 0; n
< i
; n
++) {
1236 v
= mpdm_aget(l
, n
);
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)
1259 if (!do_benchmarks
) {
1260 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1264 printf("BENCHMARKS\n");
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
);
1279 bench_hash(i
, l
, 0);
1280 bench_hash(i
, l
, 61);
1281 bench_hash(i
, l
, 89);
1282 bench_hash(i
, l
, 127);
1288 void test_conversion(void)
1290 wchar_t *wptr
= NULL
;
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)
1308 if ((f
= mpdm_popen(MPDM_LS(L
"date"), MPDM_LS(L
"r"))) != NULL
) {
1314 printf("Pipe from 'date':\n");
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)
1336 printf("sprintf tests\n");
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);
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);
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);
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);
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);
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);
1420 void test_scanf(void)
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"));
1543 printf("thread: start writing from thread\n");
1545 if ((f
= mpdm_open(fn
, MPDM_LS(L
"w"))) != NULL
) {
1548 for (n
= 0; n
< 1000; n
++) {
1549 mpdm_write(f
, MPDM_I(n
));
1550 mpdm_write(f
, MPDM_LS(L
"\n"));
1556 printf("thread: finished writing from thread\n");
1558 mpdm_mutex_lock(mutex
);
1560 mpdm_mutex_unlock(mutex
);
1562 printf("thread: t_finished set\n");
1568 void test_thread(void)
1570 mpdm_t fn
= mpdm_ref(MPDM_LS(L
"thread.txt"));
1574 printf("Testing threads and mutexes...\n");
1578 /* create the executable value */
1579 x
= mpdm_ref(MPDM_X(the_thread
));
1581 /* create a mutex */
1582 mutex
= mpdm_ref(mpdm_new_mutex());
1586 v
= mpdm_exec_thread(x
, NULL
, NULL
);
1588 printf("parent: waiting for the thread to finish...\n");
1595 mpdm_mutex_lock(mutex
);
1600 mpdm_mutex_unlock(mutex
);
1603 printf("parent: thread said it has finished.\n");
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");
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
);
1646 printf("parent: posting semaphore.\n");
1648 mpdm_semaphore_post(sem
);
1658 void (*func
) (void) = NULL
;
1660 int main(int argc
, char *argv
[])
1663 if (strcmp(argv
[1], "-b") == 0)
1665 if (strcmp(argv
[1], "-m") == 0)
1666 do_multibyte_sregex_tests
= 1;
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();*/
1703 printf("\n*** Total tests passed: %d/%d\n", oks
, tests
);
1706 printf("*** ALL TESTS PASSED\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
]);