3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2007 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;
49 void do_test(char * str
, int ok
)
53 sprintf(tmp
, "%s: %s\n", str
, ok
? "OK!" : "*** Failed ***");
61 failed_msgs
[i_failed_msgs
++] = strdup(tmp
);
78 do_test("i == 65536", (i
== 65536));
79 do_test("v has MPDM_IVAL", (v
->flags
& MPDM_IVAL
));
82 do_test("r == 65536", (r
== 65536.0));
83 do_test("v has MPDM_RVAL", (v
->flags
& MPDM_RVAL
));
85 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
86 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
88 /* partial copies of strings */
89 v
= MPDM_LS(L
"this is not America");
90 v
= MPDM_NS((wchar_t *)v
->data
+ 4, 4);
92 do_test("Partial string values", mpdm_cmp(v
, MPDM_LS(L
" is ")) == 0);
94 v
= MPDM_S(L
"MUAHAHAHA!");
95 do_test("Testing mpdm_clone semantics 1", mpdm_clone(v
) == v
);
98 mpdm_aset(v
, MPDM_S(L
"evil"), 0);
99 mpdm_aset(v
, MPDM_S(L
"dead"), 1);
102 do_test("Testing mpdm_clone semantics 2.1", w
!= v
);
105 do_test("Testing mpdm_clone semantics 2.2", v
->ref
> 1);
106 do_test("Testing mpdm_clone semantics 2.3", mpdm_aget(w
, 0) == v
);
108 /* mbs / wcs tests */
109 v
= MPDM_MBS("This is (was) a multibyte string");
112 /* greek omega is 03a9 */
113 v
= MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
115 printf("(Previous value will be NULL if locale doesn't match stress.c encoding)\n");
117 v
= MPDM_LS(L
"A capital greek omega between brackets [\x03a9]");
119 printf("(Previous value will only show on an Unicode terminal)\n");
123 do_test("rval 1", mpdm_rval(v
) == 3.1416);
124 do_test("ival 1", mpdm_ival(v
) == 3);
126 v
= MPDM_R(777777.0 / 2.0);
128 do_test("mpdm_rnew 1", mpdm_cmp(v
, MPDM_LS(L
"388888.5")) == 0);
130 v
= MPDM_R(388888.500);
132 do_test("mpdm_rnew 2", mpdm_cmp(v
, MPDM_LS(L
"388888.5")) == 0);
134 v
= MPDM_R(388888.412);
136 do_test("mpdm_rnew 3", mpdm_cmp(v
, MPDM_LS(L
"388888.412")) == 0);
138 v
= MPDM_R(388888.6543);
140 do_test("mpdm_rnew 4", mpdm_cmp(v
, MPDM_LS(L
"388888.6543")) == 0);
142 v
= MPDM_R(388888.0);
144 do_test("mpdm_rnew 5", mpdm_cmp(v
, MPDM_LS(L
"388888")) == 0);
146 v
= MPDM_R(0.050000);
148 do_test("mpdm_rnew 6", mpdm_cmp(v
, MPDM_LS(L
"0.05")) == 0);
152 do_test("mpdm_rnew 7", mpdm_cmp(v
, MPDM_LS(L
"0")) == 0);
154 v
= MPDM_LS(L
"0177");
155 do_test("mpdm_ival() for octal numbers", mpdm_ival(v
) == 0x7f);
157 v
= MPDM_LS(L
"0xFF");
158 do_test("mpdm_ival() for hexadecimal numbers", mpdm_ival(v
) == 255);
161 do_test("mpdm_rval() for octal numbers", mpdm_rval(v
) == 1.0);
163 v
= MPDM_LS(L
"0x7f");
164 do_test("mpdm_rval() for hexadecimal numbers", mpdm_ival(v
) == 127.0);
167 mpdm_t
sort_cb(mpdm_t args
)
171 /* sorts reversely */
172 d
= mpdm_cmp(mpdm_aget(args
, 1), mpdm_aget(args
, 0));
177 void test_array(void)
184 do_test("a->size == 0", (a
->size
== 0));
186 mpdm_push(a
, MPDM_LS(L
"sunday"));
187 mpdm_push(a
, MPDM_LS(L
"monday"));
188 mpdm_push(a
, MPDM_LS(L
"tuesday"));
189 mpdm_push(a
, MPDM_LS(L
"wednesday"));
190 mpdm_push(a
, MPDM_LS(L
"thursday"));
191 mpdm_push(a
, MPDM_LS(L
"friday"));
192 mpdm_push(a
, MPDM_LS(L
"saturday"));
194 do_test("a->size == 7", (a
->size
== 7));
196 v
= mpdm_aset(a
, NULL
, 3);
197 do_test("v->ref == 0", (v
->ref
== 0));
201 do_test("NULLs are sorted on top", (mpdm_aget(a
, 0) == NULL
));
205 do_test("v is referenced again", (v
!= NULL
&& v
->ref
> 0));
208 do_test("mpdm_asort() works (1)",
209 mpdm_cmp(mpdm_aget(a
,0), MPDM_LS(L
"friday")) == 0);
210 do_test("mpdm_asort() works (2)",
211 mpdm_cmp(mpdm_aget(a
,6), MPDM_LS(L
"wednesday")) == 0);
213 /* asort_cb sorts reversely */
214 a
= mpdm_sort_cb(a
, 1, MPDM_X(sort_cb
));
216 do_test("mpdm_asort_cb() works (1)",
217 mpdm_cmp(mpdm_aget(a
,6), MPDM_LS(L
"friday")) == 0);
218 do_test("mpdm_asort_cb() works (2)",
219 mpdm_cmp(mpdm_aget(a
,0), MPDM_LS(L
"wednesday")) == 0);
223 mpdm_collapse(a
, 3, 1);
224 do_test("acollapse unrefs values", (v
->ref
< n
));
229 /* add several values */
230 for(n
= 0;n
< 10;n
++)
231 v
= mpdm_queue(a
, MPDM_I(n
), 10);
233 do_test("queue should still output NULL", (v
== NULL
));
235 v
= mpdm_queue(a
, MPDM_I(11), 10);
236 do_test("queue should no longer output NULL", (v
!= NULL
));
238 v
= mpdm_queue(a
, MPDM_I(12), 10);
239 do_test("queue should return 1", mpdm_ival(v
) == 1);
240 v
= mpdm_queue(a
, MPDM_I(13), 10);
241 do_test("queue should return 2", mpdm_ival(v
) == 2);
242 do_test("queue size should be 10", a
->size
== 10);
245 v
= mpdm_queue(a
, MPDM_I(14), 5);
248 do_test("queue size should be 5", a
->size
== 5);
249 do_test("last taken value should be 8", mpdm_ival(v
) == 8);
252 mpdm_aset(a
, MPDM_I(666), 6000);
254 do_test("array should have been automatically expanded",
255 mpdm_size(a
) == 6001);
257 v
= mpdm_aget(a
, -1);
258 do_test("negative offsets in arrays 1", mpdm_ival(v
) == 666);
260 mpdm_aset(a
, MPDM_I(777), -2);
261 v
= mpdm_aget(a
, 5999);
262 do_test("negative offsets in arrays 2", mpdm_ival(v
) == 777);
264 mpdm_push(a
, MPDM_I(888));
265 v
= mpdm_aget(a
, -1);
266 do_test("negative offsets in arrays 3", mpdm_ival(v
) == 888);
269 mpdm_push(v
, MPDM_I(100));
272 /* array comparisons with mpdm_cmp() */
274 mpdm_aset(a
, MPDM_I(10), 0);
275 mpdm_aset(a
, MPDM_I(60), 1);
278 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a
, v
) == 0);
281 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a
, v
) > 0);
283 mpdm_push(v
, MPDM_I(80));
284 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a
, v
) < 0);
296 do_test("hsize 1", mpdm_hsize(h
) == 0);
298 mpdm_hset(h
, MPDM_S(L
"mp"), MPDM_I(6));
299 v
= mpdm_hget(h
, MPDM_S(L
"mp"));
301 do_test("hsize 2", mpdm_hsize(h
) == 1);
303 do_test("hash: v != NULL", (v
!= NULL
));
305 do_test("hash: v == 6", (i
== 6));
307 mpdm_hset(h
, MPDM_S(L
"mp2"), MPDM_I(66));
308 v
= mpdm_hget(h
, MPDM_S(L
"mp2"));
310 do_test("hsize 3", mpdm_hsize(h
) == 2);
312 do_test("hash: v != NULL", (v
!= NULL
));
314 do_test("hash: v == 66", (i
== 66));
316 /* fills 100 values */
317 for(n
= 0;n
< 50;n
++)
318 mpdm_hset(h
, MPDM_I(n
), MPDM_I(n
* 10));
319 for(n
= 100;n
>= 50;n
--)
320 mpdm_hset(h
, MPDM_I(n
), MPDM_I(n
* 10));
322 do_test("hsize 4", mpdm_hsize(h
) == 103);
324 /* tests 100 values */
325 for(n
= 0;n
< 100;n
++)
327 v
= mpdm_hget(h
, MPDM_I(n
));
333 do_test("hash: ival", (i
== n
* 10));
336 do_test("hash: hget", (v
!= NULL
));
339 printf("h's size: %d\n", mpdm_hsize(h
));
341 v
= mpdm_hdel(h
, MPDM_LS(L
"mp"));
342 do_test("hdel", mpdm_ival(v
) == 6);
343 do_test("hsize 5", mpdm_hsize(h
) == 102);
351 /* use of non-strings as hashes */
355 mpdm_hset(h
, v
, MPDM_I(1234));
357 mpdm_hset(h
, v
, MPDM_I(12345));
359 mpdm_hset(h
, v
, MPDM_I(9876));
361 mpdm_hset(h
, v
, MPDM_I(6543));
362 i
= mpdm_ival(mpdm_hget(h
, v
));
365 do_test("hash: using non-strings as hash keys", (i
== 6543));
367 mpdm_hset(h
, MPDM_LS(L
"ok"), MPDM_I(666));
369 do_test("exists 1", mpdm_exists(h
, MPDM_LS(L
"ok")));
370 do_test("exists 2", ! mpdm_exists(h
, MPDM_LS(L
"notok")));
372 v
= mpdm_hget_s(h
, L
"ok");
373 do_test("hget_s 1", mpdm_ival(v
) == 666);
375 mpdm_hset_s(h
, L
"ok", MPDM_I(777));
377 v
= mpdm_hget_s(h
, L
"ok");
378 do_test("hget_s + hset_s", mpdm_ival(v
) == 777);
382 void test_splice(void)
387 w
= mpdm_splice(MPDM_LS(L
"I'm agent Johnson"),
388 MPDM_LS(L
"special "), 4, 0);
389 do_test("splice insertion",
390 mpdm_cmp(mpdm_aget(w
, 0),
391 MPDM_LS(L
"I'm special agent Johnson")) == 0);
394 w
= mpdm_splice(MPDM_LS(L
"Life is a shit"), MPDM_LS(L
"cheat"), 10, 4);
395 do_test("splice insertion and deletion (1)",
396 mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"Life is a cheat")) == 0);
397 do_test("splice insertion and deletion (2)",
398 mpdm_cmp(mpdm_aget(w
, 1), MPDM_LS(L
"shit")) == 0);
401 w
= mpdm_splice(MPDM_LS(L
"I'm with dumb"), NULL
, 4, 4);
402 do_test("splice deletion (1)",
403 mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm dumb")) == 0);
404 do_test("splice deletion (2)",
405 mpdm_cmp(mpdm_aget(w
, 1), MPDM_LS(L
"with")) == 0);
408 v
= MPDM_LS(L
"It doesn't matter");
409 w
= mpdm_splice(v
, MPDM_LS(L
" two"), v
->size
, 0);
410 do_test("splice insertion at the end",
411 mpdm_cmp(mpdm_aget(w
, 0),
412 MPDM_LS(L
"It doesn't matter two")) == 0);
415 w
= mpdm_splice(NULL
, NULL
, 0, 0);
416 do_test("splice with two NULLS", (mpdm_aget(w
, 0) == NULL
));
418 w
= mpdm_splice(NULL
, MPDM_LS(L
"foo"), 0, 0);
419 do_test("splice with first value NULL",
420 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"foo")) == 0));
422 w
= mpdm_splice(MPDM_LS(L
"foo"), NULL
, 0, 0);
423 do_test("splice with second value NULL",
424 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"foo")) == 0));
426 v
= MPDM_LS(L
"I'm testing");
428 w
= mpdm_splice(v
, NULL
, 0, -1);
429 do_test("splice with negative del (1)",
430 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"")) == 0));
432 w
= mpdm_splice(v
, NULL
, 4, -1);
433 do_test("splice with negative del (2)",
434 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm ")) == 0));
436 w
= mpdm_splice(v
, NULL
, 4, -2);
437 do_test("splice with negative del (3)",
438 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm g")) == 0));
440 w
= mpdm_splice(v
, NULL
, 0, -4);
441 do_test("splice with negative del (4)",
442 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"ing")) == 0));
443 mpdm_dump(mpdm_aget(w
, 0));
445 w
= mpdm_splice(v
, NULL
, 4, -20);
446 do_test("splice with out-of-bounds negative del",
447 (mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I'm testing")) == 0));
451 void test_strcat(void)
456 w
= MPDM_LS(L
"something");
458 v
= mpdm_strcat(NULL
, NULL
);
459 do_test("mpdm_strcat(NULL, NULL) returns NULL", v
== NULL
);
461 v
= mpdm_strcat(NULL
, w
);
462 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v
, w
) == 0);
464 v
= mpdm_strcat(w
, NULL
);
465 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v
, w
) == 0);
468 v
= mpdm_strcat(NULL
, w
);
469 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v
, w
) == 0);
471 v
= mpdm_strcat(w
, NULL
);
472 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v
, w
) == 0);
474 v
= mpdm_strcat(w
, w
);
475 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v
, w
) == 0);
480 void test_split(void)
484 printf("mpdm_split test\n\n");
486 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"four.elems.in.string"));
488 do_test("4 elems: ", (w
->size
== 4));
490 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"unseparated string"));
492 do_test("1 elem: ", (w
->size
== 1));
494 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
".dot.at start"));
496 do_test("3 elems: ", (w
->size
== 3));
498 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"dot.at end."));
500 do_test("3 elems: ", (w
->size
== 3));
502 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"three...dots (two empty elements)"));
504 do_test("4 elems: ", (w
->size
== 4));
506 w
= mpdm_split(MPDM_S(L
"."), MPDM_S(L
"."));
508 do_test("2 elems: ", (w
->size
== 2));
510 w
= mpdm_split(NULL
, MPDM_S(L
"I am the man"));
511 do_test("NULL split 1: ", mpdm_size(w
) == 12);
512 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w
, 0), MPDM_LS(L
"I")) == 0);
522 printf("mpdm_join test\n\n");
528 mpdm_aset(w
, MPDM_S(L
"ce"), 0);
530 v
= mpdm_join(NULL
, w
);
531 do_test("1 elem, no separator", (mpdm_cmp(v
, MPDM_LS(L
"ce")) == 0));
534 do_test("1 elem, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce")) == 0));
536 mpdm_push(w
, MPDM_LS(L
"n'est"));
538 do_test("2 elems, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce--n'est")) == 0));
540 mpdm_push(w
, MPDM_LS(L
"pas"));
542 do_test("3 elems, '--' separator", (mpdm_cmp(v
, MPDM_LS(L
"ce--n'est--pas")) == 0));
544 v
= mpdm_join(NULL
, w
);
545 do_test("3 elems, no separator", (mpdm_cmp(v
, MPDM_LS(L
"cen'estpas")) == 0));
554 printf("mpdm_sset / mpdm_sget tests\n\n");
556 mpdm_sset(NULL
, MPDM_LS(L
"mp"), MPDM_H(7));
557 mpdm_sset(NULL
, MPDM_LS(L
"mp.config"), MPDM_H(7));
558 mpdm_sset(NULL
, MPDM_LS(L
"mp.config.auto_indent"), MPDM_I(16384));
559 mpdm_sset(NULL
, MPDM_LS(L
"mp.config.use_regex"), MPDM_I(1357));
560 mpdm_sset(NULL
, MPDM_LS(L
"mp.config.gtk_font_face"), MPDM_LS(L
"profontwindows"));
561 mpdm_sset(NULL
, MPDM_LS(L
"mp.lines"), MPDM_A(2));
562 mpdm_sset(NULL
, MPDM_LS(L
"mp.lines.0"), MPDM_LS(L
"First post!"));
563 mpdm_sset(NULL
, MPDM_LS(L
"mp.lines.1"), MPDM_LS(L
"Second post!"));
564 mpdm_dump(mpdm_root());
566 v
= mpdm_sget(NULL
, MPDM_LS(L
"mp.config.auto_indent"));
569 do_test("auto_indent == 16384", (i
== 16384));
579 mpdm_t eol
= MPDM_LS(L
"\n");
581 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"));
585 printf("Can't create test.txt; no further file tests possible.\n");
589 do_test("Create test.txt", f
!= NULL
);
591 mpdm_write(f
, MPDM_LS(L
"0"));
593 mpdm_write(f
, MPDM_LS(L
"1"));
596 /* write WITHOUT eol */
597 mpdm_write(f
, MPDM_LS(L
"2"));
601 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
603 do_test("test written file 0", mpdm_cmp(mpdm_read(f
), MPDM_LS(L
"0\n")) == 0);
604 do_test("test written file 1", mpdm_cmp(mpdm_read(f
), MPDM_LS(L
"1\n")) == 0);
605 do_test("test written file 2", mpdm_cmp(mpdm_read(f
), MPDM_LS(L
"2")) == 0);
606 do_test("test written file 3", mpdm_read(f
) == NULL
);
610 mpdm_unlink(MPDM_LS(L
"test.txt"));
611 do_test("unlink", mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r")) == NULL
);
613 v
= mpdm_stat(MPDM_LS(L
"stress.c"));
614 printf("Stat from stress.c:\n");
617 /* v=mpdm_glob(MPDM_LS(L"*"));*/
624 void test_regex(void)
629 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), MPDM_LS(L
"123456"), 0);
630 do_test("regex 0", v
!= NULL
);
632 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), MPDM_I(65536), 0);
633 do_test("regex 1", v
!= NULL
);
635 v
= mpdm_regex(MPDM_LS(L
"/^[0-9]+$/"), MPDM_LS(L
"12345678"), 0);
636 do_test("regex 2", v
!= NULL
);
638 v
= mpdm_regex(MPDM_LS(L
"/^[0-9]+$/"), MPDM_I(1), 0);
639 do_test("regex 3", v
!= NULL
);
641 v
= mpdm_regex(MPDM_LS(L
"/^[0-9]+$/"), MPDM_LS(L
"A12345-678"), 0);
642 do_test("regex 4", v
== NULL
);
644 w
= MPDM_LS(L
"Hell street, 666");
645 v
= mpdm_regex(MPDM_LS(L
"/[0-9]+/"), w
, 0);
646 do_test("regex 5", mpdm_cmp(v
, MPDM_I(666)) == 0);
650 v
= mpdm_regex(MPDM_LS(L
"/regex/"), MPDM_LS(L
"CASE-INSENSITIVE REGEX"), 0);
651 do_test("regex 6.1 (case sensitive)", v
== NULL
);
653 v
= mpdm_regex(MPDM_LS(L
"/regex/i"), MPDM_LS(L
"CASE-INSENSITIVE REGEX"), 0);
654 do_test("regex 6.2 (case insensitive)", v
!= NULL
);
656 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
657 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
659 v
= mpdm_regex(MPDM_LS(L
"/^\\s*/"), MPDM_LS(L
"123456"), 0);
660 do_test("regex 7", v
!= NULL
);
662 v
= mpdm_regex(MPDM_LS(L
"/^\\s+/"), MPDM_LS(L
"123456"), 0);
663 do_test("regex 8", v
== NULL
);
665 v
= mpdm_regex(MPDM_LS(L
"/^\\s+/"), NULL
, 0);
666 do_test("regex 9 (NULL string to match)", v
== NULL
);
670 v
= mpdm_sregex(MPDM_LS(L
"/A/"),MPDM_LS(L
"change all A to A"),
672 do_test("sregex 0", mpdm_cmp(v
, MPDM_LS(L
"change all E to A")) == 0);
674 v
= mpdm_sregex(MPDM_LS(L
"/A/g"),MPDM_LS(L
"change all A to A"),
676 do_test("sregex 1", mpdm_cmp(v
, MPDM_LS(L
"change all E to E")) == 0);
678 v
= mpdm_sregex(MPDM_LS(L
"/A+/g"),MPDM_LS(L
"change all AAAAAA to E"),
680 do_test("sregex 2", mpdm_cmp(v
, MPDM_LS(L
"change all E to E")) == 0);
682 v
= mpdm_sregex(MPDM_LS(L
"/A+/g"),MPDM_LS(L
"change all A A A A A A to E"),
684 do_test("sregex 3", mpdm_cmp(v
, MPDM_LS(L
"change all E E E E E E to E")) == 0);
686 v
= mpdm_sregex(MPDM_LS(L
"/A+/g"),MPDM_LS(L
"change all AAA A AA AAAAA A AAA to E"),
688 do_test("sregex 3.2", mpdm_cmp(v
, MPDM_LS(L
"change all E E E E E E to E")) == 0);
690 v
= mpdm_sregex(MPDM_LS(L
"/[0-9]+/g"),MPDM_LS(L
"1, 20, 333, 40 all are numbers"),
691 MPDM_LS(L
"numbers"),0);
692 do_test("sregex 4", mpdm_cmp(v
, MPDM_LS(L
"numbers, numbers, numbers, numbers all are numbers")) == 0);
694 v
= mpdm_sregex(MPDM_LS(L
"/[a-zA-Z_]+/g"),MPDM_LS(L
"regex, mpdm_regex, TexMex"),
696 do_test("sregex 5", mpdm_cmp(v
, MPDM_LS(L
"sex, sex, sex")) == 0);
698 v
= mpdm_sregex(MPDM_LS(L
"/[a-zA-Z]+/g"),MPDM_LS(L
"regex, mpdm_regex, TexMex"),
700 do_test("sregex 6", mpdm_cmp(v
, MPDM_LS(L
", _, ")) == 0);
702 v
= mpdm_sregex(MPDM_LS(L
"/\\\\/g"),MPDM_LS(L
"\\MSDOS\\style\\path"),
704 do_test("sregex 7", mpdm_cmp(v
, MPDM_LS(L
"/MSDOS/style/path")) == 0);
706 v
= mpdm_sregex(MPDM_LS(L
"/regex/gi"),MPDM_LS(L
"regex, Regex, REGEX"),
708 do_test("sregex 8", mpdm_cmp(v
, MPDM_LS(L
"sex, sex, sex")) == 0);
710 v
= mpdm_sregex(NULL
, NULL
, NULL
, 0);
711 do_test("Previous sregex substitutions must be 3", mpdm_ival(v
) == 3);
713 /* & in substitution tests */
714 v
= MPDM_LS(L
"this string has many words");
715 v
= mpdm_sregex(MPDM_LS(L
"/[a-z]+/g"), v
, MPDM_LS(L
"[&]"), 0);
716 do_test("& in sregex target", mpdm_cmp(v
, MPDM_LS(L
"[this] [string] [has] [many] [words]")) == 0);
718 v
= MPDM_LS(L
"this string has many words");
719 v
= mpdm_sregex(MPDM_LS(L
"/[a-z]+/g"), v
, MPDM_LS(L
"[\\&]"), 0);
720 do_test("escaped & in sregex target", mpdm_cmp(v
, MPDM_LS(L
"[&] [&] [&] [&] [&]")) == 0);
722 /* multiple regex tests */
725 mpdm_push(w
, MPDM_LS(L
"/^[ \t]*/"));
726 mpdm_push(w
, MPDM_LS(L
"/[^ \t=]+/"));
727 mpdm_push(w
, MPDM_LS(L
"/[ \t]*=[ \t]*/"));
728 mpdm_push(w
, MPDM_LS(L
"/[^ \t]+/"));
729 mpdm_push(w
, MPDM_LS(L
"/[ \t]*$/"));
731 v
= mpdm_regex(w
, MPDM_LS(L
"key=value"), 0);
732 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v
, 1),MPDM_LS(L
"key")) == 0);
733 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v
, 3),MPDM_LS(L
"value")) == 0);
735 v
= mpdm_regex(w
, MPDM_LS(L
" key = value"), 0);
736 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v
, 1),MPDM_LS(L
"key")) == 0);
737 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v
, 3),MPDM_LS(L
"value")) == 0);
739 v
= mpdm_regex(w
, MPDM_LS(L
"\t\tkey\t=\tvalue "), 0);
740 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v
, 1),MPDM_LS(L
"key")) == 0);
741 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v
, 3),MPDM_LS(L
"value")) == 0);
743 v
= mpdm_regex(w
, MPDM_LS(L
"key= "), 0);
744 do_test("multi-regex 4", v
== NULL
);
746 printf("Multiple line regexes\n");
747 w
= MPDM_LS(L
"/* this is\na C-like comment */");
748 v
= mpdm_regex(MPDM_LS(L
"|/\\*.+\\*/|"), w
, 0);
749 do_test("Multiline regex 1", mpdm_cmp(v
, w
) == 0);
751 v
= mpdm_regex(MPDM_LS(L
"/is$/"), w
, 0);
752 do_test("Multiline regex 2", v
== NULL
);
754 v
= mpdm_regex(MPDM_LS(L
"/is$/m"), w
, 0);
755 do_test("Multiline regex 3", mpdm_cmp(v
, MPDM_LS(L
"is")) == 0);
757 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
759 w
= MPDM_LS(L
"-\x03a9-");
761 v
= mpdm_regex(MPDM_LS(L
"/-$/"), w
, 0);
762 do_test("Multibyte environment regex 1",
763 mpdm_cmp(v
, MPDM_LS(L
"-")) == 0);
765 if(do_multibyte_sregex_tests
)
767 v
= mpdm_sregex(MPDM_LS(L
"/-$/"), w
, MPDM_LS(L
"~"), 0);
768 do_test("Multibyte environment sregex 1",
769 mpdm_cmp(v
, MPDM_LS(L
"-\x03a9~")) == 0);
771 v
= mpdm_sregex(MPDM_LS(L
"/-/g"), w
, MPDM_LS(L
"~"), 0);
772 do_test("Multibyte environment sregex 2",
773 mpdm_cmp(v
, MPDM_LS(L
"~\x03a9~")) == 0);
776 printf("Multibyte sregex test omitted; activate with -m\n");
778 /* 'last' flag tests */
779 v
= MPDM_LS(L
"this string has many words");
780 v
= mpdm_regex(MPDM_LS(L
"/[a-z]+/l"), v
, 0);
781 do_test("Flag l in mpdm_regex", mpdm_cmp(v
, MPDM_LS(L
"words")) == 0);
785 static mpdm_t
dumper(mpdm_t args
)
786 /* executable value */
793 static mpdm_t
sum(mpdm_t args
)
794 /* executable value: sum all args */
800 for(n
= t
= 0;n
< args
->size
;n
++)
801 t
+= mpdm_ival(mpdm_aget(args
, n
));
808 static mpdm_t
calculator(mpdm_t c
, mpdm_t args
)
809 /* 2 argument version: calculator. c contains a 'script' to
810 do things with the arguments */
817 /* to avoid destroying args */
818 a
= mpdm_clone(args
);
820 /* unshift first argument */
824 for(n
= 0;n
< mpdm_size(c
);n
++)
829 /* gets next value */
832 switch(*(wchar_t *)o
->data
)
834 case '+': t
+= mpdm_ival(v
); break;
835 case '-': t
-= mpdm_ival(v
); break;
836 case '*': t
*= mpdm_ival(v
); break;
837 case '/': t
/= mpdm_ival(v
); break;
851 printf("test_exec\n");
861 mpdm_aset(w
, MPDM_I(100), 0);
862 mpdm_aset(w
, MPDM_I(220), 1);
863 mpdm_aset(w
, MPDM_I(333), 2);
865 do_test("exec 0", mpdm_ival(mpdm_exec(x
, w
)) == 653);
867 mpdm_push(w
, MPDM_I(1));
869 /* multiple executable value: vm and compiler support */
871 /* calculator 'script' */
873 mpdm_push(p
, MPDM_LS(L
"+"));
874 mpdm_push(p
, MPDM_LS(L
"-"));
875 mpdm_push(p
, MPDM_LS(L
"+"));
879 x
->flags
|= MPDM_EXEC
;
881 mpdm_aset(x
, MPDM_X(calculator
), 0);
884 do_test("exec 1", mpdm_ival(mpdm_exec(x
, w
)) == -12);
886 /* another 'script', different operations with the same values */
888 mpdm_push(p
, MPDM_LS(L
"*"));
889 mpdm_push(p
, MPDM_LS(L
"/"));
890 mpdm_push(p
, MPDM_LS(L
"+"));
894 do_test("exec 2", mpdm_ival(mpdm_exec(x
, w
)) == 67);
898 void test_encoding(void)
905 v
= MPDM_MBS("?Espa?a!\n");
907 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
909 if((f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"))) == NULL
)
911 printf("Can't write test.txt; no further file test possible.\n");
918 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
921 do_test("Locale encoding", mpdm_cmp(w
, v
) == 0);
924 printf("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
926 f
= mpdm_open(MPDM_LS(L
"utf8.txt"), MPDM_LS(L
"r"));
931 for(ptr
= w
->data
;*ptr
!= L
'\0';ptr
++)
932 printf("%d", mpdm_wcwidth(*ptr
));
935 if(mpdm_encoding(MPDM_LS(L
"UTF-8")) < 0)
937 printf("No multiple encoding (iconv) support; no more tests possible.\n");
941 printf("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
943 f
= mpdm_open(MPDM_LS(L
"utf8.txt"), MPDM_LS(L
"r"));
948 /* new open file will use the specified encoding */
949 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"w"));
953 f
= mpdm_open(MPDM_LS(L
"test.txt"), MPDM_LS(L
"r"));
956 do_test("iconv encoding", mpdm_cmp(w
, v
) == 0);
963 void test_gettext(void)
968 printf("\nTesting gettext...\n");
970 mpdm_gettext_domain(MPDM_LS(L
"stress"), MPDM_LS(L
"./po"));
972 printf("Should follow a translated string of 'This is a test string':\n");
973 v
= mpdm_gettext(MPDM_LS(L
"This is a test string"));
976 printf("The same, but cached:\n");
977 v
= mpdm_gettext(MPDM_LS(L
"This is a test string"));
980 v
= mpdm_gettext(MPDM_LS(L
"This string is not translated"));
983 printf("Ad-hoc translation hash:\n");
985 mpdm_hset(h
, MPDM_LS(L
"test string"), MPDM_LS(L
"cadena de prueba"));
987 mpdm_gettext_domain(MPDM_LS(L
"stress"), h
);
988 v
= mpdm_gettext(MPDM_LS(L
"test string"));
995 static clock_t clks
= 0;
1004 printf("%.2f seconds\n",
1005 (float)(clock() - clks
)/(float)CLOCKS_PER_SEC
);
1011 void bench_hash(int i
, mpdm_t l
, int buckets
)
1017 printf("Hash of %d buckets: \n", buckets
);
1018 h
= MPDM_H(buckets
);
1021 for(n
= 0;n
< i
;n
++)
1023 v
= mpdm_aget(l
, n
);
1028 printf("Bucket usage:\n");
1029 for(n=0;n < mpdm_size(h);n++)
1030 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1036 void benchmark(void)
1046 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1050 printf("BENCHMARKS\n");
1054 printf("Creating %d values...\n", i
);
1056 l
= mpdm_ref(MPDM_A(i
));
1057 for(n
= 0;n
< i
;n
++)
1059 sprintf(tmp
,"%08x",n
);
1060 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1061 mpdm_aset(l
, MPDM_I(n
), n
);
1066 bench_hash(i
, l
, 0);
1067 bench_hash(i
, l
, 61);
1068 bench_hash(i
, l
, 89);
1069 bench_hash(i
, l
, 127);
1075 void test_conversion(void)
1077 wchar_t * wptr
= NULL
;
1081 ptr
= mpdm_wcstombs(L
"", &size
);
1082 do_test("mpdm_wcstombs converts an empty string", ptr
!= NULL
);
1084 wptr
= mpdm_mbstowcs("", &size
, 0);
1085 do_test("mpdm_mbstowcs converts an empty string", wptr
!= NULL
);
1089 void test_pipes(void)
1095 if((f
= mpdm_popen(MPDM_LS(L
"date"), MPDM_LS(L
"r"))) != NULL
)
1102 printf("Pipe from 'date':\n");
1106 printf("Can't pipe to 'date'\n");
1110 void test_misc(void)
1112 printf("Home dir:\n");
1113 mpdm_dump(mpdm_home_dir());
1114 printf("App dir:\n");
1115 mpdm_dump(mpdm_app_dir());
1119 void test_sprintf(void)
1124 printf("sprintf tests\n");
1127 mpdm_push(v
, MPDM_I(100));
1128 mpdm_push(v
, MPDM_LS(L
"beers"));
1130 w
= mpdm_sprintf(MPDM_LS(L
"%d %s for me"), v
);
1131 do_test("sprintf 1", mpdm_cmp(w
, MPDM_LS(L
"100 beers for me")) == 0);
1133 w
= mpdm_sprintf(MPDM_LS(L
"%d %s for me %d"), v
);
1134 do_test("sprintf 2", mpdm_cmp(w
, MPDM_LS(L
"100 beers for me %d")) == 0);
1136 w
= mpdm_sprintf(MPDM_LS(L
"%10d %s for me"), v
);
1137 do_test("sprintf 3", mpdm_cmp(w
, MPDM_LS(L
" 100 beers for me")) == 0);
1139 w
= mpdm_sprintf(MPDM_LS(L
"%010d %s for me"), v
);
1140 do_test("sprintf 4", mpdm_cmp(w
, MPDM_LS(L
"0000000100 beers for me")) == 0);
1143 mpdm_push(v
, MPDM_R(3.1416));
1145 w
= mpdm_sprintf(MPDM_LS(L
"Value for PI is %6.4f"), v
);
1146 do_test("sprintf 2.1", mpdm_cmp(w
, MPDM_LS(L
"Value for PI is 3.1416")) == 0);
1148 w
= mpdm_sprintf(MPDM_LS(L
"Value for PI is %08.2f"), v
);
1149 do_test("sprintf 2.1", mpdm_cmp(w
, MPDM_LS(L
"Value for PI is 00003.14")) == 0);
1152 mpdm_push(v
, MPDM_LS(L
"stress"));
1154 w
= mpdm_sprintf(MPDM_LS(L
"This is a |%10s| test"), v
);
1155 do_test("sprintf 3.1", mpdm_cmp(w
, MPDM_LS(L
"This is a | stress| test")) == 0);
1157 w
= mpdm_sprintf(MPDM_LS(L
"This is a |%-10s| test"), v
);
1158 do_test("sprintf 3.2", mpdm_cmp(w
, MPDM_LS(L
"This is a |stress | test")) == 0);
1161 mpdm_push(v
, MPDM_I(0x263a));
1163 w
= mpdm_sprintf(MPDM_LS(L
"%c"), v
);
1164 do_test("sprintf 3.3", mpdm_cmp(w
, MPDM_LS(L
"\x263a")) == 0);
1170 mpdm_t v
= MPDM_S(L
"string");
1171 mpdm_t w
= mpdm_ulc(v
, 1);
1173 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v
, 1), w
) == 0);
1174 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w
, 0), v
) == 0);
1178 int main(int argc
, char * argv
[])
1182 if(strcmp(argv
[1], "-b") == 0)
1184 if(strcmp(argv
[1], "-m") == 0)
1185 do_multibyte_sregex_tests
= 1;
1211 /* mpdm_dump_unref();*/
1213 printf("memory: %d\n", mpdm
->memory_usage
);
1223 printf("memory: %d\n", mpdm
->memory_usage
);
1225 /* mpdm_dump_unref();*/
1229 printf("\n*** Total tests passed: %d/%d\n", oks
, tests
);
1232 printf("*** ALL TESTS PASSED\n");
1237 printf("*** %d %s\n", tests
- oks
, "TESTS ---FAILED---");
1239 printf("\nFailed tests:\n\n");
1240 for(n
= 0;n
< i_failed_msgs
;n
++)
1241 printf("%s", failed_msgs
[n
]);