Never return the same string in mpdm_strcat().
[mpdm.git] / stress.c
blobb529922167880425e454f0a05245c97fe0e59177
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2009 Angel Ortega <angel@triptico.com>
6 stress.c - Stress tests for MPDM.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include <stdio.h>
27 #include <string.h>
28 #include <wchar.h>
30 #include <time.h>
32 #include "mpdm.h"
34 /* total number of tests and oks */
35 int tests = 0;
36 int oks = 0;
38 /* failed tests messages */
39 char *failed_msgs[5000];
40 int i_failed_msgs = 0;
42 int do_benchmarks = 0;
43 int do_multibyte_sregex_tests = 0;
45 /*******************
46 Code
47 ********************/
49 void do_test(char *str, int ok)
51 char tmp[1024];
53 sprintf(tmp, "%s: %s\n", str, ok ? "OK!" : "*** Failed ***");
54 printf(tmp);
56 tests++;
58 if (ok)
59 oks++;
60 else
61 failed_msgs[i_failed_msgs++] = strdup(tmp);
65 /* tests */
67 void test_basic(void)
69 int i;
70 double r;
71 mpdm_t v;
72 mpdm_t w;
74 v = MPDM_S(L"65536");
75 mpdm_dump(v);
76 i = mpdm_ival(v);
78 do_test("i == 65536", (i == 65536));
79 do_test("v has MPDM_IVAL", (v->flags & MPDM_IVAL));
81 r = mpdm_rval(v);
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);
97 v = MPDM_A(2);
98 mpdm_aset(v, MPDM_S(L"evil"), 0);
99 mpdm_aset(v, MPDM_S(L"dead"), 1);
100 w = mpdm_clone(v);
102 do_test("Testing mpdm_clone semantics 2.1", w != v);
104 v = mpdm_aget(v, 0);
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");
110 mpdm_dump(v);
112 /* greek omega is 03a9 */
113 v = MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
114 mpdm_dump(v);
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]");
118 mpdm_dump(v);
119 printf("(Previous value will only show on an Unicode terminal)\n");
121 v = MPDM_R(3.1416);
122 mpdm_dump(v);
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);
127 mpdm_dump(v);
128 do_test("mpdm_rnew 1", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
130 v = MPDM_R(388888.500);
131 mpdm_dump(v);
132 do_test("mpdm_rnew 2", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
134 v = MPDM_R(388888.412);
135 mpdm_dump(v);
136 do_test("mpdm_rnew 3", mpdm_cmp(v, MPDM_LS(L"388888.412")) == 0);
138 v = MPDM_R(388888.6543);
139 mpdm_dump(v);
140 do_test("mpdm_rnew 4", mpdm_cmp(v, MPDM_LS(L"388888.6543")) == 0);
142 v = MPDM_R(388888.0);
143 mpdm_dump(v);
144 do_test("mpdm_rnew 5", mpdm_cmp(v, MPDM_LS(L"388888")) == 0);
146 v = MPDM_R(0.050000);
147 mpdm_dump(v);
148 do_test("mpdm_rnew 6", mpdm_cmp(v, MPDM_LS(L"0.05")) == 0);
150 v = MPDM_R(0.000);
151 mpdm_dump(v);
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);
160 v = MPDM_LS(L"001");
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);
166 do_test("Two NULLs are equal", mpdm_cmp(NULL, NULL) == 0);
168 v = MPDM_LS(L"hahaha");
169 do_test("mpdm_cmp_s 1", mpdm_cmp_s(v, L"hahaha") == 0);
170 do_test("mpdm_cmp_s 2", mpdm_cmp_s(v, L"aahaha") > 0);
171 do_test("mpdm_cmp_s 3", mpdm_cmp_s(v, L"zahaha") < 0);
174 mpdm_t sort_cb(mpdm_t args)
176 int d;
178 /* sorts reversely */
179 d = mpdm_cmp(mpdm_aget(args, 1), mpdm_aget(args, 0));
180 return (MPDM_I(d));
184 void test_array(void)
186 int n;
187 mpdm_t a;
188 mpdm_t v;
190 a = MPDM_A(0);
191 do_test("a->size == 0", (a->size == 0));
193 mpdm_push(a, MPDM_LS(L"sunday"));
194 mpdm_push(a, MPDM_LS(L"monday"));
195 mpdm_push(a, MPDM_LS(L"tuesday"));
196 mpdm_push(a, MPDM_LS(L"wednesday"));
197 mpdm_push(a, MPDM_LS(L"thursday"));
198 mpdm_push(a, MPDM_LS(L"friday"));
199 mpdm_push(a, MPDM_LS(L"saturday"));
200 mpdm_dump(a);
201 do_test("a->size == 7", (a->size == 7));
203 v = mpdm_aset(a, NULL, 3);
204 do_test("v->ref == 0", (v->ref == 0));
205 mpdm_dump(a);
207 a = mpdm_sort(a, 1);
208 do_test("NULLs are sorted on top", (mpdm_aget(a, 0) == NULL));
210 mpdm_aset(a, v, 0);
211 v = mpdm_aget(a, 3);
212 do_test("v is referenced again", (v != NULL && v->ref > 0));
214 a = mpdm_sort(a, 1);
215 do_test("mpdm_asort() works (1)", mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"friday")) == 0);
216 do_test("mpdm_asort() works (2)",
217 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"wednesday")) == 0);
219 /* asort_cb sorts reversely */
220 a = mpdm_sort_cb(a, 1, MPDM_X(sort_cb));
222 do_test("mpdm_asort_cb() works (1)",
223 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"friday")) == 0);
224 do_test("mpdm_asort_cb() works (2)",
225 mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"wednesday")) == 0);
227 n = v->ref;
228 v = mpdm_aget(a, 3);
229 mpdm_collapse(a, 3, 1);
230 do_test("acollapse unrefs values", (v->ref < n));
232 /* test queues */
233 a = MPDM_A(0);
235 /* add several values */
236 for (n = 0; n < 10; n++)
237 v = mpdm_queue(a, MPDM_I(n), 10);
239 do_test("queue should still output NULL", (v == NULL));
241 v = mpdm_queue(a, MPDM_I(11), 10);
242 do_test("queue should no longer output NULL", (v != NULL));
244 v = mpdm_queue(a, MPDM_I(12), 10);
245 do_test("queue should return 1", mpdm_ival(v) == 1);
246 v = mpdm_queue(a, MPDM_I(13), 10);
247 do_test("queue should return 2", mpdm_ival(v) == 2);
248 do_test("queue size should be 10", a->size == 10);
250 mpdm_dump(a);
251 v = mpdm_queue(a, MPDM_I(14), 5);
252 mpdm_dump(a);
254 do_test("queue size should be 5", a->size == 5);
255 do_test("last taken value should be 8", mpdm_ival(v) == 8);
257 a = MPDM_A(4);
258 mpdm_aset(a, MPDM_I(666), 6000);
260 do_test("array should have been automatically expanded", mpdm_size(a) == 6001);
262 v = mpdm_aget(a, -1);
263 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
265 mpdm_aset(a, MPDM_I(777), -2);
266 v = mpdm_aget(a, 5999);
267 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
269 mpdm_push(a, MPDM_I(888));
270 v = mpdm_aget(a, -1);
271 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
273 v = MPDM_A(0);
274 mpdm_push(v, MPDM_I(100));
275 mpdm_pop(v);
277 /* array comparisons with mpdm_cmp() */
278 a = MPDM_A(2);
279 mpdm_aset(a, MPDM_I(10), 0);
280 mpdm_aset(a, MPDM_I(60), 1);
282 v = mpdm_clone(a);
283 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
285 mpdm_adel(v, -1);
286 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
288 mpdm_push(v, MPDM_I(80));
289 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a, v) < 0);
293 void test_hash(void)
295 mpdm_t h;
296 mpdm_t v;
297 int i, n;
299 h = MPDM_H(0);
301 do_test("hsize 1", mpdm_hsize(h) == 0);
303 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
304 v = mpdm_hget(h, MPDM_S(L"mp"));
306 do_test("hsize 2", mpdm_hsize(h) == 1);
308 do_test("hash: v != NULL", (v != NULL));
309 i = mpdm_ival(v);
310 do_test("hash: v == 6", (i == 6));
312 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
313 v = mpdm_hget(h, MPDM_S(L"mp2"));
315 do_test("hsize 3", mpdm_hsize(h) == 2);
317 do_test("hash: v != NULL", (v != NULL));
318 i = mpdm_ival(v);
319 do_test("hash: v == 66", (i == 66));
321 /* fills 100 values */
322 for (n = 0; n < 50; n++)
323 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
324 for (n = 100; n >= 50; n--)
325 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
327 do_test("hsize 4", mpdm_hsize(h) == 103);
329 /* tests 100 values */
330 for (n = 0; n < 100; n++) {
331 v = mpdm_hget(h, MPDM_I(n));
333 if (v != NULL) {
334 i = mpdm_ival(v);
335 if (!(i == n * 10))
336 do_test("hash: ival", (i == n * 10));
338 else
339 do_test("hash: hget", (v != NULL));
342 printf("h's size: %d\n", mpdm_hsize(h));
344 v = mpdm_hdel(h, MPDM_LS(L"mp"));
345 do_test("hdel", mpdm_ival(v) == 6);
346 do_test("hsize 5", mpdm_hsize(h) == 102);
349 mpdm_dump(h);
351 v=mpdm_hkeys(h);
352 mpdm_dump(v);
354 /* use of non-strings as hashes */
355 h = MPDM_H(0);
357 v = MPDM_A(0);
358 mpdm_hset(h, v, MPDM_I(1234));
359 v = MPDM_H(0);
360 mpdm_hset(h, v, MPDM_I(12345));
361 v = MPDM_H(0);
362 mpdm_hset(h, v, MPDM_I(9876));
363 v = MPDM_A(0);
364 mpdm_hset(h, v, MPDM_I(6543));
365 i = mpdm_ival(mpdm_hget(h, v));
367 mpdm_dump(h);
368 do_test("hash: using non-strings as hash keys", (i == 6543));
370 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
372 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
373 do_test("exists 2", !mpdm_exists(h, MPDM_LS(L"notok")));
375 v = mpdm_hget_s(h, L"ok");
376 do_test("hget_s 1", mpdm_ival(v) == 666);
378 mpdm_hset_s(h, L"ok", MPDM_I(777));
380 v = mpdm_hget_s(h, L"ok");
381 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
385 void test_splice(void)
387 mpdm_t w;
388 mpdm_t v;
390 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"), MPDM_LS(L"special "), 4, 0);
391 do_test("splice insertion",
392 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm special agent Johnson")) == 0);
393 mpdm_dump(w);
395 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
396 do_test("splice insertion and deletion (1)",
397 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
398 do_test("splice insertion and deletion (2)",
399 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
400 mpdm_dump(w);
402 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
403 do_test("splice deletion (1)", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
404 do_test("splice deletion (2)", mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
405 mpdm_dump(w);
407 v = MPDM_LS(L"It doesn't matter");
408 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
409 do_test("splice insertion at the end",
410 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"It doesn't matter two")) == 0);
411 mpdm_dump(w);
413 w = mpdm_splice(NULL, NULL, 0, 0);
414 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
416 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
417 do_test("splice with first value NULL",
418 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
420 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
421 do_test("splice with second value NULL",
422 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
424 v = MPDM_LS(L"I'm testing");
426 w = mpdm_splice(v, NULL, 0, -1);
427 do_test("splice with negative del (1)",
428 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
430 w = mpdm_splice(v, NULL, 4, -1);
431 do_test("splice with negative del (2)",
432 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
434 w = mpdm_splice(v, NULL, 4, -2);
435 do_test("splice with negative del (3)",
436 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
438 w = mpdm_splice(v, NULL, 0, -4);
439 do_test("splice with negative del (4)",
440 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
441 mpdm_dump(mpdm_aget(w, 0));
443 w = mpdm_splice(v, NULL, 4, -20);
444 do_test("splice with out-of-bounds negative del",
445 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
449 void test_strcat(void)
451 mpdm_t v;
452 mpdm_t w;
454 w = MPDM_LS(L"something");
456 v = mpdm_strcat(NULL, NULL);
457 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
459 v = mpdm_strcat(NULL, w);
460 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
462 v = mpdm_strcat(w, NULL);
463 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
465 w = MPDM_LS(L"");
466 v = mpdm_strcat(NULL, w);
467 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
469 v = mpdm_strcat(w, NULL);
470 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
472 v = mpdm_strcat(w, w);
473 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
478 void test_split(void)
480 mpdm_t w;
482 printf("mpdm_split test\n\n");
484 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"four.elems.in.string"));
485 mpdm_dump(w);
486 do_test("4 elems: ", (w->size == 4));
488 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"unseparated string"));
489 mpdm_dump(w);
490 do_test("1 elem: ", (w->size == 1));
492 w = mpdm_split(MPDM_S(L"."), MPDM_S(L".dot.at start"));
493 mpdm_dump(w);
494 do_test("3 elems: ", (w->size == 3));
496 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"dot.at end."));
497 mpdm_dump(w);
498 do_test("3 elems: ", (w->size == 3));
500 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"three...dots (two empty elements)"));
501 mpdm_dump(w);
502 do_test("4 elems: ", (w->size == 4));
504 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
505 mpdm_dump(w);
506 do_test("2 elems: ", (w->size == 2));
508 w = mpdm_split(NULL, MPDM_S(L"I am the man"));
509 do_test("NULL split 1: ", mpdm_size(w) == 12);
510 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
514 void test_join(void)
516 mpdm_t v;
517 mpdm_t s;
518 mpdm_t w;
520 printf("mpdm_join test\n\n");
522 /* separator */
523 s = MPDM_LS(L"--");
525 w = MPDM_A(1);
526 mpdm_aset(w, MPDM_S(L"ce"), 0);
528 v = mpdm_join(NULL, w);
529 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
531 v = mpdm_join(s, w);
532 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
534 mpdm_push(w, MPDM_LS(L"n'est"));
535 v = mpdm_join(s, w);
536 do_test("2 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
538 mpdm_push(w, MPDM_LS(L"pas"));
539 v = mpdm_join(s, w);
540 do_test("3 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
542 v = mpdm_join(NULL, w);
543 do_test("3 elems, no separator", (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
547 static mpdm_t active(mpdm_t args)
549 return MPDM_H(0);
553 void test_sym(void)
555 mpdm_t v;
556 int i;
558 printf("mpdm_sset / mpdm_sget tests\n\n");
560 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
561 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
562 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
563 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
564 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"), MPDM_LS(L"profontwindows"));
565 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
566 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
567 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
568 mpdm_sset(NULL, MPDM_LS(L"mp.active"), MPDM_X(active));
569 mpdm_sset(NULL, MPDM_LS(L"mp.active.syntax"), NULL);
570 mpdm_dump(mpdm_root());
572 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
573 i = mpdm_ival(v);
575 do_test("auto_indent == 16384", (i == 16384));
577 mpdm_sweep(-1);
581 void test_file(void)
583 mpdm_t f;
584 mpdm_t v;
585 mpdm_t eol = MPDM_LS(L"\n");
587 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
589 if (f == NULL) {
590 printf("Can't create test.txt; no further file tests possible.\n");
591 return;
594 do_test("Create test.txt", f != NULL);
596 mpdm_write(f, MPDM_LS(L"0"));
597 mpdm_write(f, eol);
598 mpdm_write(f, MPDM_LS(L"1"));
599 mpdm_write(f, eol);
601 /* write WITHOUT eol */
602 mpdm_write(f, MPDM_LS(L"2"));
604 mpdm_close(f);
606 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
608 do_test("test written file 0", mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
609 do_test("test written file 1", mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
610 do_test("test written file 2", mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
611 do_test("test written file 3", mpdm_read(f) == NULL);
613 mpdm_close(f);
615 mpdm_unlink(MPDM_LS(L"test.txt"));
616 do_test("unlink", mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
618 v = mpdm_stat(MPDM_LS(L"stress.c"));
619 printf("Stat from stress.c:\n");
620 mpdm_dump(v);
622 /* v=mpdm_glob(MPDM_LS(L"*"));*/
623 printf("Glob:\n");
624 v = mpdm_glob(NULL, NULL);
625 mpdm_dump(v);
629 void test_regex(void)
631 mpdm_t v;
632 mpdm_t w;
634 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_LS(L"123456"), 0);
635 do_test("regex 0", v != NULL);
637 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_I(65536), 0);
638 do_test("regex 1", v != NULL);
640 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"12345678"), 0);
641 do_test("regex 2", v != NULL);
643 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_I(1), 0);
644 do_test("regex 3", v != NULL);
646 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"A12345-678"), 0);
647 do_test("regex 4", v == NULL);
649 w = MPDM_LS(L"Hell street, 666");
650 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), w, 0);
651 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
653 mpdm_dump(v);
655 v = mpdm_regex(MPDM_LS(L"/regex/"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
656 do_test("regex 6.1 (case sensitive)", v == NULL);
658 v = mpdm_regex(MPDM_LS(L"/regex/i"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
659 do_test("regex 6.2 (case insensitive)", v != NULL);
661 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
662 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
664 v = mpdm_regex(MPDM_LS(L"/^\\s*/"), MPDM_LS(L"123456"), 0);
665 do_test("regex 7", v != NULL);
667 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), MPDM_LS(L"123456"), 0);
668 do_test("regex 8", v == NULL);
670 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), NULL, 0);
671 do_test("regex 9 (NULL string to match)", v == NULL);
673 /* sregex */
675 v = mpdm_sregex(MPDM_LS(L"/A/"), MPDM_LS(L"change all A to A"), MPDM_LS(L"E"), 0);
676 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
678 v = mpdm_sregex(MPDM_LS(L"/A/g"), MPDM_LS(L"change all A to A"), MPDM_LS(L"E"), 0);
679 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
681 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAAAAA to E"),
682 MPDM_LS(L"E"), 0);
683 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
685 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all A A A A A A to E"),
686 MPDM_LS(L"E"), 0);
687 do_test("sregex 3", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
689 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
690 MPDM_LS(L"E"), 0);
691 do_test("sregex 3.2", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
693 v = mpdm_sregex(MPDM_LS(L"/[0-9]+/g"), MPDM_LS(L"1, 20, 333, 40 all are numbers"),
694 MPDM_LS(L"numbers"), 0);
695 do_test("sregex 4",
696 mpdm_cmp(v,
697 MPDM_LS(L"numbers, numbers, numbers, numbers all are numbers")) == 0);
699 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z_]+/g"), MPDM_LS(L"regex, mpdm_regex, TexMex"),
700 MPDM_LS(L"sex"), 0);
701 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
703 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z]+/g"), MPDM_LS(L"regex, mpdm_regex, TexMex"),
704 NULL, 0);
705 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
707 v = mpdm_sregex(MPDM_LS(L"/\\\\/g"), MPDM_LS(L"\\MSDOS\\style\\path"),
708 MPDM_LS(L"/"), 0);
709 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
711 v = mpdm_sregex(MPDM_LS(L"/regex/gi"), MPDM_LS(L"regex, Regex, REGEX"),
712 MPDM_LS(L"sex"), 0);
713 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
715 v = mpdm_sregex(NULL, NULL, NULL, 0);
716 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
718 /* & in substitution tests */
719 v = MPDM_LS(L"this string has many words");
720 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[&]"), 0);
721 do_test("& in sregex target",
722 mpdm_cmp(v, MPDM_LS(L"[this] [string] [has] [many] [words]")) == 0);
724 v = MPDM_LS(L"this string has many words");
725 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[\\&]"), 0);
726 do_test("escaped & in sregex target",
727 mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 0);
729 v = MPDM_LS(L"this string has many words");
730 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"\\\\&"), 0);
731 do_test("escaped \\ in sregex target",
732 mpdm_cmp(v, MPDM_LS(L"\\this \\string \\has \\many \\words")) == 0);
734 /* multiple regex tests */
735 w = MPDM_A(0);
737 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
738 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
739 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
740 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
741 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
743 v = mpdm_regex(w, MPDM_LS(L"key=value"), 0);
744 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
745 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
747 v = mpdm_regex(w, MPDM_LS(L" key = value"), 0);
748 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
749 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
751 v = mpdm_regex(w, MPDM_LS(L"\t\tkey\t=\tvalue "), 0);
752 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
753 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
755 v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
756 do_test("multi-regex 4", v == NULL);
758 printf("Multiple line regexes\n");
759 w = MPDM_LS(L"/* this is\na C-like comment */");
760 v = mpdm_regex(MPDM_LS(L"|/\\*.+\\*/|"), w, 0);
761 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
763 v = mpdm_regex(MPDM_LS(L"/is$/"), w, 0);
764 do_test("Multiline regex 2", v == NULL);
766 v = mpdm_regex(MPDM_LS(L"/is$/m"), w, 0);
767 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
769 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
771 w = MPDM_LS(L"-\x03a9-");
773 v = mpdm_regex(MPDM_LS(L"/-$/"), w, 0);
774 do_test("Multibyte environment regex 1", mpdm_cmp(v, MPDM_LS(L"-")) == 0);
776 if (do_multibyte_sregex_tests) {
777 v = mpdm_sregex(MPDM_LS(L"/-$/"), w, MPDM_LS(L"~"), 0);
778 do_test("Multibyte environment sregex 1",
779 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
781 v = mpdm_sregex(MPDM_LS(L"/-/g"), w, MPDM_LS(L"~"), 0);
782 do_test("Multibyte environment sregex 2",
783 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
785 else
786 printf("Multibyte sregex test omitted; activate with -m\n");
788 /* 'last' flag tests */
789 v = MPDM_LS(L"this string has many words");
790 v = mpdm_regex(MPDM_LS(L"/[a-z]+/l"), v, 0);
791 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
795 static mpdm_t dumper(mpdm_t args)
796 /* executable value */
798 mpdm_dump(args);
799 return (NULL);
803 static mpdm_t sum(mpdm_t args)
804 /* executable value: sum all args */
806 int n, t = 0;
808 if (args != NULL) {
809 for (n = t = 0; n < args->size; n++)
810 t += mpdm_ival(mpdm_aget(args, n));
813 return (MPDM_I(t));
817 static mpdm_t calculator(mpdm_t c, mpdm_t args)
818 /* 2 argument version: calculator. c contains a 'script' to
819 do things with the arguments */
821 int n, t;
822 mpdm_t v;
823 mpdm_t a;
824 mpdm_t o;
826 /* to avoid destroying args */
827 a = mpdm_clone(args);
829 /* unshift first argument */
830 v = mpdm_adel(a, 0);
831 t = mpdm_ival(v);
833 for (n = 0; n < mpdm_size(c); n++) {
834 /* gets operator */
835 o = mpdm_aget(c, n);
837 /* gets next value */
838 v = mpdm_adel(a, 0);
840 switch (*(wchar_t *) o->data) {
841 case '+':
842 t += mpdm_ival(v);
843 break;
844 case '-':
845 t -= mpdm_ival(v);
846 break;
847 case '*':
848 t *= mpdm_ival(v);
849 break;
850 case '/':
851 t /= mpdm_ival(v);
852 break;
856 return (MPDM_I(t));
860 void test_exec(void)
862 mpdm_t x;
863 mpdm_t w;
864 mpdm_t p;
866 printf("test_exec\n");
868 x = MPDM_X(dumper);
870 /* a simple value */
871 mpdm_exec(x, NULL);
872 mpdm_exec(x, x);
874 x = MPDM_X(sum);
875 w = MPDM_A(3);
876 mpdm_aset(w, MPDM_I(100), 0);
877 mpdm_aset(w, MPDM_I(220), 1);
878 mpdm_aset(w, MPDM_I(333), 2);
880 do_test("exec 0", mpdm_ival(mpdm_exec(x, w)) == 653);
882 mpdm_push(w, MPDM_I(1));
884 /* multiple executable value: vm and compiler support */
886 /* calculator 'script' */
887 p = MPDM_A(0);
888 mpdm_push(p, MPDM_LS(L"+"));
889 mpdm_push(p, MPDM_LS(L"-"));
890 mpdm_push(p, MPDM_LS(L"+"));
892 /* the value */
893 x = MPDM_A(2);
894 x->flags |= MPDM_EXEC;
896 mpdm_aset(x, MPDM_X(calculator), 0);
897 mpdm_aset(x, p, 1);
899 do_test("exec 1", mpdm_ival(mpdm_exec(x, w)) == -12);
901 /* another 'script', different operations with the same values */
902 p = MPDM_A(0);
903 mpdm_push(p, MPDM_LS(L"*"));
904 mpdm_push(p, MPDM_LS(L"/"));
905 mpdm_push(p, MPDM_LS(L"+"));
907 mpdm_aset(x, p, 1);
909 do_test("exec 2", mpdm_ival(mpdm_exec(x, w)) == 67);
913 void test_encoding(void)
915 mpdm_t f;
916 mpdm_t v;
917 mpdm_t w;
918 const wchar_t *ptr;
920 v = MPDM_MBS("?Espa?a!\n");
922 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
924 if ((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL) {
925 printf("Can't write test.txt; no further file test possible.\n");
926 return;
929 mpdm_write(f, v);
930 mpdm_close(f);
932 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
933 w = mpdm_read(f);
934 mpdm_dump(w);
935 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
936 mpdm_close(f);
938 printf
939 ("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
941 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
942 w = mpdm_read(f);
943 mpdm_dump(w);
944 mpdm_close(f);
946 for (ptr = w->data; *ptr != L'\0'; ptr++)
947 printf("%d", mpdm_wcwidth(*ptr));
948 printf("\n");
950 if (mpdm_encoding(MPDM_LS(L"UTF-8")) < 0) {
951 printf("No multiple encoding (iconv) support; no more tests possible.\n");
952 return;
955 printf
956 ("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
958 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
959 w = mpdm_read(f);
960 mpdm_dump(w);
961 mpdm_close(f);
963 /* new open file will use the specified encoding */
964 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
965 mpdm_write(f, v);
966 mpdm_close(f);
968 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
969 w = mpdm_read(f);
970 mpdm_dump(w);
971 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
972 mpdm_close(f);
974 mpdm_encoding(NULL);
978 void test_gettext(void)
980 mpdm_t v;
981 mpdm_t h;
983 printf("\nTesting gettext...\n");
985 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
987 printf("Should follow a translated string of 'This is a test string':\n");
988 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
989 mpdm_dump(v);
991 printf("The same, but cached:\n");
992 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
993 mpdm_dump(v);
995 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
996 mpdm_dump(v);
998 printf("Ad-hoc translation hash:\n");
999 h = MPDM_H(0);
1000 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
1002 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
1003 v = mpdm_gettext(MPDM_LS(L"test string"));
1004 mpdm_dump(v);
1008 void timer(int secs)
1010 static clock_t clks = 0;
1012 switch (secs) {
1013 case 0:
1014 clks = clock();
1015 break;
1017 case -1:
1018 printf("%.2f seconds\n", (float) (clock() - clks) / (float) CLOCKS_PER_SEC);
1019 break;
1024 void bench_hash(int i, mpdm_t l, int buckets)
1026 mpdm_t h;
1027 mpdm_t v;
1028 int n;
1030 printf("Hash of %d buckets: \n", buckets);
1031 h = MPDM_H(buckets);
1033 timer(0);
1034 for (n = 0; n < i; n++) {
1035 v = mpdm_aget(l, n);
1036 mpdm_hset(h, v, v);
1038 timer(-1);
1040 printf("Bucket usage:\n");
1041 for(n=0;n < mpdm_size(h);n++)
1042 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1044 mpdm_sweep(-1);
1048 void benchmark(void)
1050 mpdm_t l;
1051 int i, n;
1052 char tmp[64];
1054 printf("\n");
1056 if (!do_benchmarks) {
1057 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1058 return;
1061 printf("BENCHMARKS\n");
1063 i = 500000;
1065 printf("Creating %d values...\n", i);
1067 l = mpdm_ref(MPDM_A(i));
1068 for (n = 0; n < i; n++) {
1069 sprintf(tmp, "%08x", n);
1070 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1071 mpdm_aset(l, MPDM_I(n), n);
1074 printf("OK\n");
1076 bench_hash(i, l, 0);
1077 bench_hash(i, l, 61);
1078 bench_hash(i, l, 89);
1079 bench_hash(i, l, 127);
1081 mpdm_unref(l);
1085 void test_conversion(void)
1087 wchar_t *wptr = NULL;
1088 char *ptr = NULL;
1089 int size = 0;
1091 ptr = mpdm_wcstombs(L"", &size);
1092 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1094 wptr = mpdm_mbstowcs("", &size, 0);
1095 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1099 void test_pipes(void)
1101 mpdm_t f;
1103 printf("\n");
1105 if ((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL) {
1106 mpdm_t v;
1108 v = mpdm_read(f);
1109 mpdm_pclose(f);
1111 printf("Pipe from 'date':\n");
1112 mpdm_dump(v);
1114 else
1115 printf("Can't pipe to 'date'\n");
1119 void test_misc(void)
1121 printf("Home dir:\n");
1122 mpdm_dump(mpdm_home_dir());
1123 printf("App dir:\n");
1124 mpdm_dump(mpdm_app_dir());
1128 void test_sprintf(void)
1130 mpdm_t v;
1131 mpdm_t w;
1133 printf("sprintf tests\n");
1135 v = MPDM_A(0);
1136 mpdm_push(v, MPDM_I(100));
1137 mpdm_push(v, MPDM_LS(L"beers"));
1139 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1140 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1142 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1143 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me 0")) == 0);
1145 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1146 do_test("sprintf 3", mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1148 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1149 do_test("sprintf 4", mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1151 v = MPDM_A(0);
1152 mpdm_push(v, MPDM_R(3.1416));
1154 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1155 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1157 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1158 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1160 v = MPDM_A(0);
1161 mpdm_push(v, MPDM_LS(L"stress"));
1163 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1164 do_test("sprintf 3.1", mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1166 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1167 do_test("sprintf 3.2", mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1169 v = MPDM_A(0);
1170 mpdm_push(v, MPDM_I(0x263a));
1172 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1173 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1175 v = MPDM_A(0);
1176 mpdm_push(v, MPDM_I(75));
1178 w = mpdm_sprintf(MPDM_LS(L"%d%%"), v);
1179 do_test("sprintf 4.1", mpdm_cmp(w, MPDM_LS(L"75%")) == 0);
1181 w = mpdm_sprintf(MPDM_LS(L"%b"), v);
1182 do_test("sprintf 5.1", mpdm_cmp(w, MPDM_LS(L"1001011")) == 0);
1186 void test_ulc(void)
1188 mpdm_t v = MPDM_S(L"string");
1189 mpdm_t w = mpdm_ulc(v, 1);
1191 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1192 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1196 void test_scanf(void)
1198 mpdm_t v;
1200 v = mpdm_sscanf(MPDM_LS(L"%d %d"), MPDM_LS(L"1234 5678"), 0);
1201 do_test("mpdm_sscanf_1.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1202 do_test("mpdm_sscanf_1.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1204 v = mpdm_sscanf(MPDM_LS(L"%s %f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1205 do_test("mpdm_sscanf_2.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1206 do_test("mpdm_sscanf_2.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1207 do_test("mpdm_sscanf_2.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678")) == 0);
1209 v = mpdm_sscanf(MPDM_LS(L"%s %*f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1210 do_test("mpdm_sscanf_3.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1211 do_test("mpdm_sscanf_3.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1213 v = mpdm_sscanf(MPDM_LS(L"%4d%4d%2d%10d"), MPDM_LS(L"12341234121234567890"), 0);
1214 do_test("mpdm_sscanf_4.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1215 do_test("mpdm_sscanf_4.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"1234")) == 0);
1216 do_test("mpdm_sscanf_4.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"12")) == 0);
1217 do_test("mpdm_sscanf_4.4", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"1234567890")) == 0);
1219 v = mpdm_sscanf(MPDM_LS(L"%[abc]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1220 do_test("mpdm_sscanf_5.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1221 do_test("mpdm_sscanf_5.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1223 v = mpdm_sscanf(MPDM_LS(L"%[a-d]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1224 do_test("mpdm_sscanf_6.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1225 do_test("mpdm_sscanf_6.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1227 v = mpdm_sscanf(MPDM_LS(L"%[^x]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1228 do_test("mpdm_sscanf_7.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1229 do_test("mpdm_sscanf_7.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1231 v = mpdm_sscanf(MPDM_LS(L"%[^:]: %s"), MPDM_LS(L"key: value"), 0);
1232 do_test("mpdm_sscanf_8.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"key")) == 0);
1233 do_test("mpdm_sscanf_8.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"value")) == 0);
1235 v = mpdm_sscanf(MPDM_LS(L"%*[^/]/* %s */"), MPDM_LS(L"this is code /* comment */ more code"), 0);
1236 do_test("mpdm_sscanf_9.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"comment")) == 0);
1238 v = mpdm_sscanf(MPDM_LS(L"%d%%%d"), MPDM_LS(L"1234%5678"), 0);
1239 do_test("mpdm_sscanf_10.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1240 do_test("mpdm_sscanf_10.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1242 v = mpdm_sscanf(MPDM_LS(L"%*[abc]%n%*[^ ]%n"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1243 do_test("mpdm_sscanf_11.1", mpdm_ival(mpdm_aget(v, 0)) == 7);
1244 do_test("mpdm_sscanf_11.2", mpdm_ival(mpdm_aget(v, 1)) == 12);
1246 v = mpdm_sscanf(MPDM_LS(L"/* %S */"), MPDM_LS(L"/* inside the comment */"), 0);
1247 do_test("mpdm_sscanf_12.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"inside the comment")) == 0);
1249 v = mpdm_sscanf(MPDM_LS(L"/* %S */%s"), MPDM_LS(L"/* inside the comment */outside"), 0);
1250 do_test("mpdm_sscanf_13.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"inside the comment")) == 0);
1251 do_test("mpdm_sscanf_13.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"outside")) == 0);
1253 v = mpdm_sscanf(MPDM_LS(L"%n"), MPDM_LS(L""), 0);
1254 do_test("mpdm_sscanf_14.1", mpdm_size(v) == 1 && mpdm_ival(mpdm_aget(v, 0)) == 0);
1256 v = mpdm_sscanf(MPDM_LS(L"%[^%f]%f %[#%d@]"), MPDM_LS(L"this 12.34 5678#12@34"), 0);
1257 do_test("mpdm_sscanf_15.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this ")) == 0);
1258 do_test("mpdm_sscanf_15.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1259 do_test("mpdm_sscanf_15.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678#12@34")) == 0);
1261 v = mpdm_sscanf(MPDM_LS(L"%*S\"%[^\n\"]\""), MPDM_LS(L"a \"bbb\" c;"), 0);
1262 do_test("mpdm_sscanf_16", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"bbb")) == 0);
1266 int main(int argc, char *argv[])
1268 if (argc > 1) {
1269 if (strcmp(argv[1], "-b") == 0)
1270 do_benchmarks = 1;
1271 if (strcmp(argv[1], "-m") == 0)
1272 do_multibyte_sregex_tests = 1;
1275 mpdm_startup();
1277 printf("sizeof(struct mpdm_val): %ld\n", (long) sizeof(struct mpdm_val));
1279 test_basic();
1280 test_array();
1281 test_hash();
1282 test_splice();
1283 test_strcat();
1284 test_split();
1285 test_join();
1286 test_sym();
1287 test_file();
1288 test_regex();
1289 test_exec();
1290 test_encoding();
1291 test_gettext();
1292 test_conversion();
1293 test_pipes();
1294 test_misc();
1295 test_sprintf();
1296 test_ulc();
1297 test_scanf();
1299 benchmark();
1301 /* mpdm_dump_unref();*/
1303 printf("memory: %d\n", mpdm->memory_usage);
1304 mpdm_sweep(-1);
1305 mpdm_sweep(-1);
1306 mpdm_sweep(-1);
1307 mpdm_sweep(-1);
1308 mpdm_sweep(-1);
1309 mpdm_sweep(-1);
1310 mpdm_sweep(-1);
1311 mpdm_sweep(-1);
1312 mpdm_sweep(-1);
1313 printf("memory: %d\n", mpdm->memory_usage);
1315 /* mpdm_dump_unref();*/
1317 mpdm_shutdown();
1319 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1321 if (oks == tests)
1322 printf("*** ALL TESTS PASSED\n");
1323 else {
1324 int n;
1326 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1328 printf("\nFailed tests:\n\n");
1329 for (n = 0; n < i_failed_msgs; n++)
1330 printf("%s", failed_msgs[n]);
1333 return (0);