Ref/unref all args in mpdm_sprintf().
[mpdm.git] / stress.c
blobf08d4a8fdbaeae576194d966bc420b92ae262f51
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2010 Angel Ortega <angel@triptico.com>
6 stress.c - Stress tests for MPDM.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
26 #include <stdio.h>
27 #include <string.h>
28 #include <wchar.h>
30 #include <time.h>
32 #include "mpdm.h"
34 /* total number of tests and oks */
35 int tests = 0;
36 int oks = 0;
38 /* failed tests messages */
39 char *failed_msgs[5000];
40 int i_failed_msgs = 0;
42 int do_benchmarks = 0;
43 int do_multibyte_sregex_tests = 0;
46 /** code **/
48 void _do_test(char *str, int ok, int src_line)
50 char tmp[1024];
52 sprintf(tmp, "%s: %s (line %d)\n", str, ok ? "OK!" : "*** Failed ***", src_line);
53 printf(tmp);
55 tests++;
57 if (ok)
58 oks++;
59 else
60 failed_msgs[i_failed_msgs++] = strdup(tmp);
63 #define do_test(str, ok) _do_test(str, ok, __LINE__)
65 #define C { int CC = mpdm->count
66 #define T(i) do_test("v counter", CC + i == mpdm->count); }
68 /** tests **/
70 void test_counter(void)
72 mpdm_t v, w;
74 C; v = MPDM_S(L"hi"); T(1);
76 C; w = MPDM_A(0); T(1);
78 C; mpdm_push(w, v); T(0);
80 C; mpdm_adel(w, 0); T(0); /* should fail in 2.x */
82 C; mpdm_queue(w, v, 10); T(0);
84 C; v = MPDM_S(L"this is a phrase"); T(1);
85 C; w = mpdm_split_s(L" ", v); T(5);
89 void test_basic(void)
91 int i;
92 double r;
93 mpdm_t v;
94 mpdm_t w;
95 mpdm_t t;
97 v = mpdm_ref(MPDM_S(L"65536"));
98 mpdm_dump(v);
99 i = mpdm_ival(v);
101 do_test("i == 65536", (i == 65536));
102 do_test("v has MPDM_IVAL", (v->flags & MPDM_IVAL));
104 r = mpdm_rval(v);
105 do_test("r == 65536", (r == 65536.0));
106 do_test("v has MPDM_RVAL", (v->flags & MPDM_RVAL));
108 mpdm_unref(v);
110 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
111 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
113 /* partial copies of strings */
114 v = MPDM_LS(L"this is not America");
115 v = MPDM_NS((wchar_t *) v->data + 4, 4);
117 do_test("Partial string values", mpdm_cmp(v, MPDM_LS(L" is ")) == 0);
119 v = mpdm_ref(MPDM_S(L"MUAHAHAHA!"));
120 w = mpdm_ref(mpdm_clone(v));
121 do_test("Testing mpdm_clone semantics 1", w == v);
122 mpdm_unref(v);
123 mpdm_unref(w);
125 v = mpdm_ref(MPDM_A(2));
126 mpdm_aset(v, MPDM_S(L"evil"), 0);
127 mpdm_aset(v, MPDM_S(L"dead"), 1);
128 w = mpdm_ref(mpdm_clone(v));
130 do_test("Testing mpdm_clone semantics 2.1", w != v);
132 t = mpdm_aget(v, 0);
133 do_test("Testing mpdm_clone semantics 2.2", t->ref > 1);
134 do_test("Testing mpdm_clone semantics 2.3", mpdm_aget(w, 0) == t);
136 mpdm_unref(w);
137 mpdm_unref(v);
139 /* mbs / wcs tests */
140 v = MPDM_MBS("This is (was) a multibyte string");
141 mpdm_dump(v);
143 /* greek omega is 03a9 */
144 v = MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
145 mpdm_dump(v);
146 printf("(Previous value will be NULL if locale doesn't match stress.c encoding)\n");
148 v = MPDM_LS(L"A capital greek omega between brackets [\x03a9]");
149 mpdm_dump(v);
150 printf("(Previous value will only show on an Unicode terminal)\n");
152 v = mpdm_ref(MPDM_R(3.1416));
153 mpdm_dump(v);
154 do_test("rval 1", mpdm_rval(v) == 3.1416);
155 do_test("ival 1", mpdm_ival(v) == 3);
156 mpdm_unref(v);
158 v = MPDM_R(777777.0 / 2.0);
159 mpdm_dump(v);
160 do_test("mpdm_rnew 1", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
162 v = MPDM_R(388888.500);
163 mpdm_dump(v);
164 do_test("mpdm_rnew 2", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
166 v = MPDM_R(388888.412);
167 mpdm_dump(v);
168 do_test("mpdm_rnew 3", mpdm_cmp(v, MPDM_LS(L"388888.412")) == 0);
170 v = MPDM_R(388888.6543);
171 mpdm_dump(v);
172 do_test("mpdm_rnew 4", mpdm_cmp(v, MPDM_LS(L"388888.6543")) == 0);
174 v = MPDM_R(388888.0);
175 mpdm_dump(v);
176 do_test("mpdm_rnew 5", mpdm_cmp(v, MPDM_LS(L"388888")) == 0);
178 v = MPDM_R(0.050000);
179 mpdm_dump(v);
180 do_test("mpdm_rnew 6", mpdm_cmp(v, MPDM_LS(L"0.05")) == 0);
182 v = MPDM_R(0.000);
183 mpdm_dump(v);
184 do_test("mpdm_rnew 7", mpdm_cmp(v, MPDM_LS(L"0")) == 0);
186 v = MPDM_LS(L"0177");
187 do_test("mpdm_ival() for octal numbers", mpdm_ival(v) == 0x7f);
189 v = MPDM_LS(L"0xFF");
190 do_test("mpdm_ival() for hexadecimal numbers", mpdm_ival(v) == 255);
192 v = MPDM_LS(L"001");
193 do_test("mpdm_rval() for octal numbers", mpdm_rval(v) == 1.0);
195 v = MPDM_LS(L"0x7f");
196 do_test("mpdm_rval() for hexadecimal numbers", mpdm_ival(v) == 127.0);
198 do_test("Two NULLs are equal", mpdm_cmp(NULL, NULL) == 0);
200 v = MPDM_LS(L"hahaha");
201 do_test("mpdm_cmp_s 1", mpdm_cmp_s(v, L"hahaha") == 0);
202 do_test("mpdm_cmp_s 2", mpdm_cmp_s(v, L"aahaha") > 0);
203 do_test("mpdm_cmp_s 3", mpdm_cmp_s(v, L"zahaha") < 0);
206 mpdm_t sort_cb(mpdm_t args)
208 int d;
210 /* sorts reversely */
211 d = mpdm_cmp(mpdm_aget(args, 1), mpdm_aget(args, 0));
212 return (MPDM_I(d));
216 void test_array(void)
218 int n;
219 mpdm_t a;
220 mpdm_t v;
222 a = MPDM_A(0);
223 do_test("a->size == 0", (a->size == 0));
225 mpdm_push(a, MPDM_LS(L"sunday"));
226 mpdm_push(a, MPDM_LS(L"monday"));
227 mpdm_push(a, MPDM_LS(L"tuesday"));
228 mpdm_push(a, MPDM_LS(L"wednesday"));
229 mpdm_push(a, MPDM_LS(L"thursday"));
230 mpdm_push(a, MPDM_LS(L"friday"));
231 mpdm_push(a, MPDM_LS(L"saturday"));
232 mpdm_dump(a);
233 do_test("a->size == 7", (a->size == 7));
235 v = mpdm_aget(a, 3);
236 mpdm_aset(a, NULL, 3);
237 mpdm_dump(a);
239 mpdm_sort(a, 1);
240 do_test("NULLs are sorted on top", (mpdm_aget(a, 0) == NULL));
242 mpdm_aset(a, v, 0);
243 v = mpdm_aget(a, 3);
244 do_test("v is referenced again", (v != NULL && v->ref > 0));
246 mpdm_sort(a, 1);
247 do_test("mpdm_asort() works (1)", mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"friday")) == 0);
248 do_test("mpdm_asort() works (2)",
249 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"wednesday")) == 0);
251 /* asort_cb sorts reversely */
252 mpdm_sort_cb(a, 1, MPDM_X(sort_cb));
254 do_test("mpdm_asort_cb() works (1)",
255 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"friday")) == 0);
256 do_test("mpdm_asort_cb() works (2)",
257 mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"wednesday")) == 0);
259 n = v->ref;
260 v = mpdm_aget(a, 3);
261 mpdm_collapse(a, 3, 1);
262 do_test("acollapse unrefs values", (v->ref < n));
264 /* test queues */
265 a = MPDM_A(0);
267 /* add several values */
268 for (n = 0; n < 10; n++)
269 v = mpdm_queue(a, MPDM_I(n), 10);
271 do_test("queue should still output NULL", (v == NULL));
273 v = mpdm_queue(a, MPDM_I(11), 10);
274 do_test("queue should no longer output NULL", (v != NULL));
276 v = mpdm_queue(a, MPDM_I(12), 10);
277 do_test("queue should return 1", mpdm_ival(v) == 1);
278 v = mpdm_queue(a, MPDM_I(13), 10);
279 do_test("queue should return 2", mpdm_ival(v) == 2);
280 do_test("queue size should be 10", a->size == 10);
282 mpdm_dump(a);
283 v = mpdm_queue(a, MPDM_I(14), 5);
284 mpdm_dump(a);
286 do_test("queue size should be 5", a->size == 5);
287 do_test("last taken value should be 8", mpdm_ival(v) == 8);
289 a = MPDM_A(4);
290 mpdm_aset(a, MPDM_I(666), 6000);
292 do_test("array should have been automatically expanded", mpdm_size(a) == 6001);
294 v = mpdm_aget(a, -1);
295 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
297 mpdm_aset(a, MPDM_I(777), -2);
298 v = mpdm_aget(a, 5999);
299 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
301 mpdm_push(a, MPDM_I(888));
302 v = mpdm_aget(a, -1);
303 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
305 v = MPDM_A(0);
306 mpdm_push(v, MPDM_I(100));
307 mpdm_pop(v);
309 /* array comparisons with mpdm_cmp() */
310 a = MPDM_A(2);
311 mpdm_aset(a, MPDM_I(10), 0);
312 mpdm_aset(a, MPDM_I(60), 1);
314 mpdm_ref(a);
315 v = mpdm_ref(mpdm_clone(a));
316 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
318 mpdm_adel(v, -1);
319 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
321 mpdm_push(v, MPDM_I(80));
322 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a, v) < 0);
324 mpdm_unref(v);
325 mpdm_unref(a);
329 void test_hash(void)
331 mpdm_t h;
332 mpdm_t v;
333 int i, n;
335 h = MPDM_H(0);
337 do_test("hsize 1", mpdm_hsize(h) == 0);
339 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
340 v = mpdm_hget(h, MPDM_S(L"mp"));
342 do_test("hsize 2", mpdm_hsize(h) == 1);
344 do_test("hash: v != NULL", (v != NULL));
345 i = mpdm_ival(v);
346 do_test("hash: v == 6", (i == 6));
348 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
349 v = mpdm_hget(h, MPDM_S(L"mp2"));
351 do_test("hsize 3", mpdm_hsize(h) == 2);
353 do_test("hash: v != NULL", (v != NULL));
354 i = mpdm_ival(v);
355 do_test("hash: v == 66", (i == 66));
357 /* fills 100 values */
358 for (n = 0; n < 50; n++)
359 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
360 for (n = 100; n >= 50; n--)
361 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
363 do_test("hsize 4", mpdm_hsize(h) == 103);
365 /* tests 100 values */
366 for (n = 0; n < 100; n++) {
367 v = mpdm_hget(h, MPDM_I(n));
369 if (v != NULL) {
370 i = mpdm_ival(v);
371 if (!(i == n * 10))
372 do_test("hash: ival", (i == n * 10));
374 else
375 do_test("hash: hget", (v != NULL));
378 printf("h's size: %d\n", mpdm_hsize(h));
380 mpdm_hdel(h, MPDM_LS(L"mp"));
381 do_test("hsize 5", mpdm_hsize(h) == 102);
384 mpdm_dump(h);
386 v=mpdm_hkeys(h);
387 mpdm_dump(v);
389 /* use of non-strings as hashes */
390 h = MPDM_H(0);
392 v = MPDM_A(0);
393 mpdm_hset(h, v, MPDM_I(1234));
394 v = MPDM_H(0);
395 mpdm_hset(h, v, MPDM_I(12345));
396 v = MPDM_H(0);
397 mpdm_hset(h, v, MPDM_I(9876));
398 v = MPDM_A(0);
399 mpdm_hset(h, v, MPDM_I(6543));
400 i = mpdm_ival(mpdm_hget(h, v));
402 mpdm_dump(h);
403 do_test("hash: using non-strings as hash keys", (i == 6543));
405 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
407 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
408 do_test("exists 2", !mpdm_exists(h, MPDM_LS(L"notok")));
410 mpdm_dump(h);
411 v = mpdm_hget_s(h, L"ok");
412 printf("v %s\n", v == NULL ? "is NULL" : "is NOT NULL");
413 do_test("hget_s 1", mpdm_ival(v) == 666);
414 mpdm_dump(v);
415 mpdm_dump(h);
417 i = 0;
418 for (n = 0; n < 1000; n++) {
419 if (mpdm_hget_s(h, L"ok") != NULL)
420 i++;
422 printf("i: %d\n", i);
423 do_test("hget_s 1.2", i == 1000);
425 if (i != 1000)
426 mpdm_hget_s(h, L"ok");
428 do_test("hget 1.2.1", mpdm_hget(h, MPDM_LS(L"ok")) != NULL);
430 mpdm_hset_s(h, L"ok", MPDM_I(777));
432 v = mpdm_hget_s(h, L"ok");
433 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
437 void test_splice(void)
439 mpdm_t w;
440 mpdm_t v;
442 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"), MPDM_LS(L"special "), 4, 0);
443 do_test("splice insertion",
444 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm special agent Johnson")) == 0);
445 mpdm_dump(w);
447 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
448 do_test("splice insertion and deletion (1)",
449 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
450 do_test("splice insertion and deletion (2)",
451 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
452 mpdm_dump(w);
454 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
455 do_test("splice deletion (1)", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
456 do_test("splice deletion (2)", mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
457 mpdm_dump(w);
459 v = MPDM_LS(L"It doesn't matter");
460 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
461 do_test("splice insertion at the end",
462 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"It doesn't matter two")) == 0);
463 mpdm_dump(w);
465 w = mpdm_splice(NULL, NULL, 0, 0);
466 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
468 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
469 do_test("splice with first value NULL",
470 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
472 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
473 do_test("splice with second value NULL",
474 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
476 v = MPDM_LS(L"I'm testing");
477 mpdm_ref(v);
479 w = mpdm_splice(v, NULL, 0, -1);
480 do_test("splice with negative del (1)",
481 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
483 w = mpdm_splice(v, NULL, 4, -1);
484 do_test("splice with negative del (2)",
485 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
487 w = mpdm_splice(v, NULL, 4, -2);
488 do_test("splice with negative del (3)",
489 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
491 w = mpdm_splice(v, NULL, 0, -4);
492 do_test("splice with negative del (4)",
493 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
494 mpdm_dump(mpdm_aget(w, 0));
496 w = mpdm_splice(v, NULL, 4, -20);
497 do_test("splice with out-of-bounds negative del",
498 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
499 mpdm_unref(v);
503 void test_strcat(void)
505 mpdm_t v;
506 mpdm_t w;
508 w = MPDM_LS(L"something");
510 v = mpdm_strcat(NULL, NULL);
511 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
513 v = mpdm_strcat(NULL, w);
514 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
516 v = mpdm_strcat(w, NULL);
517 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
519 w = MPDM_LS(L"");
520 v = mpdm_strcat(NULL, w);
521 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
523 v = mpdm_strcat(w, NULL);
524 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
526 v = mpdm_strcat(w, w);
527 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
532 void test_split(void)
534 mpdm_t w;
536 printf("mpdm_split test\n\n");
538 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"four.elems.in.string"));
539 mpdm_dump(w);
540 do_test("4 elems: ", (w->size == 4));
542 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"unseparated string"));
543 mpdm_dump(w);
544 do_test("1 elem: ", (w->size == 1));
546 w = mpdm_split(MPDM_S(L"."), MPDM_S(L".dot.at start"));
547 mpdm_dump(w);
548 do_test("3 elems: ", (w->size == 3));
550 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"dot.at end."));
551 mpdm_dump(w);
552 do_test("3 elems: ", (w->size == 3));
554 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"three...dots (two empty elements)"));
555 mpdm_dump(w);
556 do_test("4 elems: ", (w->size == 4));
558 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
559 mpdm_dump(w);
560 do_test("2 elems: ", (w->size == 2));
562 w = mpdm_split(NULL, MPDM_S(L"I am the man"));
563 do_test("NULL split 1: ", mpdm_size(w) == 12);
564 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
568 void test_join(void)
570 mpdm_t v;
571 mpdm_t s;
572 mpdm_t w;
574 printf("mpdm_join test\n\n");
576 /* separator */
577 s = mpdm_ref(MPDM_LS(L"--"));
579 w = MPDM_A(1);
580 mpdm_aset(w, MPDM_S(L"ce"), 0);
582 mpdm_ref(w);
584 v = mpdm_join(NULL, w);
585 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
587 v = mpdm_join(s, w);
588 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
590 mpdm_push(w, MPDM_LS(L"n'est"));
591 v = mpdm_join(s, w);
592 do_test("2 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
594 mpdm_push(w, MPDM_LS(L"pas"));
595 v = mpdm_join(s, w);
596 do_test("3 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
598 v = mpdm_join(NULL, w);
599 do_test("3 elems, no separator", (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
601 mpdm_unref(w);
602 mpdm_unref(s);
606 static mpdm_t active(mpdm_t args)
608 return MPDM_H(0);
612 void test_sym(void)
614 mpdm_t v;
615 int i;
617 printf("mpdm_sset / mpdm_sget tests\n\n");
619 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
620 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
621 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
622 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
623 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"), MPDM_LS(L"profontwindows"));
624 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
625 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
626 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
627 mpdm_sset(NULL, MPDM_LS(L"mp.active"), MPDM_X(active));
628 mpdm_sset(NULL, MPDM_LS(L"mp.active.syntax"), NULL);
629 mpdm_dump(mpdm_root());
631 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
632 i = mpdm_ival(v);
634 do_test("auto_indent == 16384", (i == 16384));
636 mpdm_sweep(-1);
640 void test_file(void)
642 mpdm_t f;
643 mpdm_t v;
644 mpdm_t eol = MPDM_LS(L"\n");
646 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
648 if (f == NULL) {
649 printf("Can't create test.txt; no further file tests possible.\n");
650 return;
653 do_test("Create test.txt", f != NULL);
655 mpdm_write(f, MPDM_LS(L"0"));
656 mpdm_write(f, eol);
657 mpdm_write(f, MPDM_LS(L"1"));
658 mpdm_write(f, eol);
660 /* write WITHOUT eol */
661 mpdm_write(f, MPDM_LS(L"2"));
663 mpdm_close(f);
665 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
667 do_test("test written file 0", mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
668 do_test("test written file 1", mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
669 do_test("test written file 2", mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
670 do_test("test written file 3", mpdm_read(f) == NULL);
672 mpdm_close(f);
674 mpdm_unlink(MPDM_LS(L"test.txt"));
675 do_test("unlink", mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
677 v = mpdm_stat(MPDM_LS(L"stress.c"));
678 printf("Stat from stress.c:\n");
679 mpdm_dump(v);
681 /* v=mpdm_glob(MPDM_LS(L"*"));*/
682 printf("Glob:\n");
683 v = mpdm_glob(NULL, NULL);
684 mpdm_dump(v);
688 void test_regex(void)
690 mpdm_t v;
691 mpdm_t w;
693 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_LS(L"123456"), 0);
694 do_test("regex 0", v != NULL);
696 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_I(65536), 0);
697 do_test("regex 1", v != NULL);
699 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"12345678"), 0);
700 do_test("regex 2", v != NULL);
702 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_I(1), 0);
703 do_test("regex 3", v != NULL);
705 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"A12345-678"), 0);
706 do_test("regex 4", v == NULL);
708 w = MPDM_LS(L"Hell street, 666");
709 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), w, 0);
710 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
712 mpdm_dump(v);
714 v = mpdm_regex(MPDM_LS(L"/regex/"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
715 do_test("regex 6.1 (case sensitive)", v == NULL);
717 v = mpdm_regex(MPDM_LS(L"/regex/i"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
718 do_test("regex 6.2 (case insensitive)", v != NULL);
720 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
721 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
723 v = mpdm_regex(MPDM_LS(L"/^\\s*/"), MPDM_LS(L"123456"), 0);
724 do_test("regex 7", v != NULL);
726 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), MPDM_LS(L"123456"), 0);
727 do_test("regex 8", v == NULL);
729 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), NULL, 0);
730 do_test("regex 9 (NULL string to match)", v == NULL);
732 /* sregex */
734 v = mpdm_sregex(MPDM_LS(L"/A/"), MPDM_LS(L"change all A to A"), MPDM_LS(L"E"), 0);
735 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
737 v = mpdm_sregex(MPDM_LS(L"/A/g"), MPDM_LS(L"change all A to A"), MPDM_LS(L"E"), 0);
738 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
740 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAAAAA to E"),
741 MPDM_LS(L"E"), 0);
742 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
744 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all A A A A A A to E"),
745 MPDM_LS(L"E"), 0);
746 do_test("sregex 3", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
748 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
749 MPDM_LS(L"E"), 0);
750 do_test("sregex 3.2", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
752 v = mpdm_sregex(MPDM_LS(L"/[0-9]+/g"), MPDM_LS(L"1, 20, 333, 40 all are numbers"),
753 MPDM_LS(L"numbers"), 0);
754 do_test("sregex 4",
755 mpdm_cmp(v,
756 MPDM_LS(L"numbers, numbers, numbers, numbers all are numbers")) == 0);
758 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z_]+/g"), MPDM_LS(L"regex, mpdm_regex, TexMex"),
759 MPDM_LS(L"sex"), 0);
760 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
762 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z]+/g"), MPDM_LS(L"regex, mpdm_regex, TexMex"),
763 NULL, 0);
764 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
766 v = mpdm_sregex(MPDM_LS(L"/\\\\/g"), MPDM_LS(L"\\MSDOS\\style\\path"),
767 MPDM_LS(L"/"), 0);
768 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
770 v = mpdm_sregex(MPDM_LS(L"/regex/gi"), MPDM_LS(L"regex, Regex, REGEX"),
771 MPDM_LS(L"sex"), 0);
772 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
774 v = mpdm_sregex(NULL, NULL, NULL, 0);
775 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
777 /* & in substitution tests */
778 v = MPDM_LS(L"this string has many words");
779 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[&]"), 0);
780 do_test("& in sregex target",
781 mpdm_cmp(v, MPDM_LS(L"[this] [string] [has] [many] [words]")) == 0);
783 v = MPDM_LS(L"this string has many words");
784 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[\\&]"), 0);
785 do_test("escaped & in sregex target",
786 mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 0);
788 v = MPDM_LS(L"this string has many words");
789 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"\\\\&"), 0);
790 do_test("escaped \\ in sregex target",
791 mpdm_cmp(v, MPDM_LS(L"\\this \\string \\has \\many \\words")) == 0);
793 v = MPDM_LS(L"hola ");
794 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, MPDM_LS(L""), 0);
795 do_test("sregex output size 1", v->size == 4);
797 v = MPDM_LS(L"hola ");
798 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, NULL, 0);
799 do_test("sregex output size 2", v->size == 4);
801 v = MPDM_LS(L"hola ");
802 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, MPDM_LS(L"!"), 0);
803 do_test("sregex output size 3", v->size == 5);
805 v = MPDM_LS(L"holo");
806 v = mpdm_sregex(MPDM_LS(L"/o/g"), v, MPDM_LS(L"!!"), 0);
807 do_test("sregex output size 4", v->size == 6);
809 /* multiple regex tests */
810 w = MPDM_A(0);
812 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
813 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
814 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
815 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
816 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
818 v = mpdm_regex(w, MPDM_LS(L"key=value"), 0);
819 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
820 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
822 v = mpdm_regex(w, MPDM_LS(L" key = value"), 0);
823 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
824 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
826 v = mpdm_regex(w, MPDM_LS(L"\t\tkey\t=\tvalue "), 0);
827 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
828 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
830 /* v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
831 do_test("multi-regex 4", v == NULL);
833 printf("Multiple line regexes\n");
834 w = MPDM_LS(L"/* this is\na C-like comment */");
835 v = mpdm_regex(MPDM_LS(L"|/\\*.+\\*/|"), w, 0);
836 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
838 v = mpdm_regex(MPDM_LS(L"/is$/"), w, 0);
839 do_test("Multiline regex 2", v == NULL);
841 v = mpdm_regex(MPDM_LS(L"/is$/m"), w, 0);
842 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
844 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
846 w = MPDM_LS(L"-\x03a9-");
848 v = mpdm_regex(MPDM_LS(L"/-$/"), w, 0);
849 do_test("Multibyte environment regex 1", mpdm_cmp(v, MPDM_LS(L"-")) == 0);
851 if (do_multibyte_sregex_tests) {
852 v = mpdm_sregex(MPDM_LS(L"/-$/"), w, MPDM_LS(L"~"), 0);
853 do_test("Multibyte environment sregex 1",
854 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
856 v = mpdm_sregex(MPDM_LS(L"/-/g"), w, MPDM_LS(L"~"), 0);
857 do_test("Multibyte environment sregex 2",
858 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
860 else
861 printf("Multibyte sregex test omitted; activate with -m\n");
863 /* 'last' flag tests */
864 v = MPDM_LS(L"this string has many words");
865 v = mpdm_regex(MPDM_LS(L"/[a-z]+/l"), v, 0);
866 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
870 static mpdm_t dumper(mpdm_t args)
871 /* executable value */
873 mpdm_dump(args);
874 return (NULL);
878 static mpdm_t sum(mpdm_t args)
879 /* executable value: sum all args */
881 int n, t = 0;
883 if (args != NULL) {
884 for (n = t = 0; n < args->size; n++)
885 t += mpdm_ival(mpdm_aget(args, n));
888 return (MPDM_I(t));
892 static mpdm_t calculator(mpdm_t c, mpdm_t args)
893 /* 2 argument version: calculator. c contains a 'script' to
894 do things with the arguments */
896 int n, t;
897 mpdm_t v;
898 mpdm_t a;
899 mpdm_t o;
901 mpdm_ref(args);
903 /* to avoid destroying args */
904 a = mpdm_clone(args);
906 /* shift first argument */
907 v = mpdm_shift(a);
908 t = mpdm_ival(v);
910 for (n = 0; n < mpdm_size(c); n++) {
911 /* gets operator */
912 o = mpdm_aget(c, n);
914 /* gets next value */
915 v = mpdm_shift(a);
917 switch (*(wchar_t *) o->data) {
918 case '+':
919 t += mpdm_ival(v);
920 break;
921 case '-':
922 t -= mpdm_ival(v);
923 break;
924 case '*':
925 t *= mpdm_ival(v);
926 break;
927 case '/':
928 t /= mpdm_ival(v);
929 break;
933 mpdm_unref(args);
935 return (MPDM_I(t));
939 void test_exec(void)
941 mpdm_t x;
942 mpdm_t w;
943 mpdm_t p;
945 printf("test_exec\n");
947 x = MPDM_X(dumper);
949 /* a simple value */
950 mpdm_ref(x);
951 mpdm_exec(x, NULL);
952 mpdm_exec(x, x);
953 mpdm_unref(x);
955 x = mpdm_ref(MPDM_X(sum));
956 w = mpdm_ref(MPDM_A(3));
957 mpdm_aset(w, MPDM_I(100), 0);
958 mpdm_aset(w, MPDM_I(220), 1);
959 mpdm_aset(w, MPDM_I(333), 2);
961 do_test("exec 0", mpdm_ival(mpdm_exec(x, w)) == 653);
962 x = mpdm_unref(x);
964 mpdm_push(w, MPDM_I(1));
966 /* multiple executable value: vm and compiler support */
968 /* calculator 'script' */
969 p = mpdm_ref(MPDM_A(0));
970 mpdm_push(p, MPDM_LS(L"+"));
971 mpdm_push(p, MPDM_LS(L"-"));
972 mpdm_push(p, MPDM_LS(L"+"));
974 /* the value */
975 x = mpdm_ref(MPDM_A(2));
976 x->flags |= MPDM_EXEC;
978 mpdm_aset(x, MPDM_X(calculator), 0);
979 mpdm_aset(x, p, 1);
981 do_test("exec 1", mpdm_ival(mpdm_exec(x, w)) == -12);
983 mpdm_unref(p);
985 /* another 'script', different operations with the same values */
986 p = mpdm_ref(MPDM_A(0));
987 mpdm_push(p, MPDM_LS(L"*"));
988 mpdm_push(p, MPDM_LS(L"/"));
989 mpdm_push(p, MPDM_LS(L"+"));
991 mpdm_aset(x, p, 1);
993 do_test("exec 2", mpdm_ival(mpdm_exec(x, w)) == 67);
994 x = mpdm_unref(x);
995 p = mpdm_unref(p);
996 w = mpdm_unref(w);
1000 void test_encoding(void)
1002 mpdm_t f;
1003 mpdm_t v;
1004 mpdm_t w;
1005 const wchar_t *ptr;
1007 v = MPDM_MBS("?Espa?a!\n");
1009 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
1011 if ((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL) {
1012 printf("Can't write test.txt; no further file test possible.\n");
1013 return;
1016 mpdm_write(f, v);
1017 mpdm_close(f);
1019 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1020 w = mpdm_read(f);
1021 mpdm_dump(w);
1022 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
1023 mpdm_close(f);
1025 printf
1026 ("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1028 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1029 w = mpdm_read(f);
1030 mpdm_dump(w);
1031 mpdm_close(f);
1033 for (ptr = w->data; *ptr != L'\0'; ptr++)
1034 printf("%d", mpdm_wcwidth(*ptr));
1035 printf("\n");
1037 if (mpdm_encoding(MPDM_LS(L"UTF-8")) < 0) {
1038 printf("No multiple encoding (iconv) support; no more tests possible.\n");
1039 return;
1042 printf
1043 ("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1045 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1046 w = mpdm_read(f);
1047 mpdm_dump(w);
1048 mpdm_close(f);
1050 /* new open file will use the specified encoding */
1051 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
1052 mpdm_write(f, v);
1053 mpdm_close(f);
1055 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1056 w = mpdm_read(f);
1057 mpdm_dump(w);
1058 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
1059 mpdm_close(f);
1061 mpdm_encoding(NULL);
1065 void test_gettext(void)
1067 mpdm_t v;
1068 mpdm_t h;
1070 printf("\nTesting gettext...\n");
1072 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
1074 printf("Should follow a translated string of 'This is a test string':\n");
1075 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1076 mpdm_dump(v);
1078 printf("The same, but cached:\n");
1079 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1080 mpdm_dump(v);
1082 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
1083 mpdm_dump(v);
1085 printf("Ad-hoc translation hash:\n");
1086 h = MPDM_H(0);
1087 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
1089 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
1090 v = mpdm_gettext(MPDM_LS(L"test string"));
1091 mpdm_dump(v);
1095 void timer(int secs)
1097 static clock_t clks = 0;
1099 switch (secs) {
1100 case 0:
1101 clks = clock();
1102 break;
1104 case -1:
1105 printf("%.2f seconds\n", (float) (clock() - clks) / (float) CLOCKS_PER_SEC);
1106 break;
1111 void bench_hash(int i, mpdm_t l, int buckets)
1113 mpdm_t h;
1114 mpdm_t v;
1115 int n;
1117 printf("Hash of %d buckets: \n", buckets);
1118 h = MPDM_H(buckets);
1120 timer(0);
1121 for (n = 0; n < i; n++) {
1122 v = mpdm_aget(l, n);
1123 mpdm_hset(h, v, v);
1125 timer(-1);
1127 printf("Bucket usage:\n");
1128 for(n=0;n < mpdm_size(h);n++)
1129 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1131 mpdm_sweep(-1);
1135 void benchmark(void)
1137 mpdm_t l;
1138 int i, n;
1139 char tmp[64];
1141 printf("\n");
1143 if (!do_benchmarks) {
1144 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1145 return;
1148 printf("BENCHMARKS\n");
1150 i = 500000;
1152 printf("Creating %d values...\n", i);
1154 l = mpdm_ref(MPDM_A(i));
1155 for (n = 0; n < i; n++) {
1156 sprintf(tmp, "%08x", n);
1157 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1158 mpdm_aset(l, MPDM_I(n), n);
1161 printf("OK\n");
1163 bench_hash(i, l, 0);
1164 bench_hash(i, l, 61);
1165 bench_hash(i, l, 89);
1166 bench_hash(i, l, 127);
1168 mpdm_unref(l);
1172 void test_conversion(void)
1174 wchar_t *wptr = NULL;
1175 char *ptr = NULL;
1176 int size = 0;
1178 ptr = mpdm_wcstombs(L"", &size);
1179 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1181 wptr = mpdm_mbstowcs("", &size, 0);
1182 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1186 void test_pipes(void)
1188 mpdm_t f;
1190 printf("\n");
1192 if ((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL) {
1193 mpdm_t v;
1195 v = mpdm_read(f);
1196 mpdm_pclose(f);
1198 printf("Pipe from 'date':\n");
1199 mpdm_dump(v);
1201 else
1202 printf("Can't pipe to 'date'\n");
1206 void test_misc(void)
1208 printf("Home dir:\n");
1209 mpdm_dump(mpdm_home_dir());
1210 printf("App dir:\n");
1211 mpdm_dump(mpdm_app_dir());
1215 void test_sprintf(void)
1217 mpdm_t v;
1218 mpdm_t w;
1220 printf("sprintf tests\n");
1222 v = MPDM_A(0);
1223 mpdm_push(v, MPDM_I(100));
1224 mpdm_push(v, MPDM_LS(L"beers"));
1225 mpdm_ref(v);
1227 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1228 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1230 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1231 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me 0")) == 0);
1233 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1234 do_test("sprintf 3", mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1236 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1237 do_test("sprintf 4", mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1239 mpdm_unref(v);
1241 v = MPDM_A(0);
1242 mpdm_push(v, MPDM_R(3.1416));
1243 mpdm_ref(v);
1245 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1246 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1248 /* w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1249 do_test("sprintf 2.2", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1251 mpdm_unref(v);
1253 v = MPDM_A(0);
1254 mpdm_push(v, MPDM_LS(L"stress"));
1255 mpdm_ref(v);
1257 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1258 do_test("sprintf 3.1", mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1260 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1261 do_test("sprintf 3.2", mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1263 mpdm_unref(v);
1265 v = MPDM_A(0);
1266 mpdm_push(v, MPDM_I(0x263a));
1267 mpdm_ref(v);
1269 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1270 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1271 mpdm_unref(v);
1273 v = MPDM_A(0);
1274 mpdm_push(v, MPDM_I(75));
1275 mpdm_ref(v);
1277 w = mpdm_sprintf(MPDM_LS(L"%d%%"), v);
1278 do_test("sprintf 4.1", mpdm_cmp(w, MPDM_LS(L"75%")) == 0);
1280 w = mpdm_sprintf(MPDM_LS(L"%b"), v);
1281 do_test("sprintf 5.1", mpdm_cmp(w, MPDM_LS(L"1001011")) == 0);
1282 mpdm_unref(v);
1286 void test_ulc(void)
1288 mpdm_t v = MPDM_S(L"string");
1289 mpdm_t w = mpdm_ulc(v, 1);
1291 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1292 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1296 void test_scanf(void)
1298 mpdm_t v;
1300 v = mpdm_sscanf(MPDM_LS(L"%d %d"), MPDM_LS(L"1234 5678"), 0);
1301 do_test("mpdm_sscanf_1.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1302 do_test("mpdm_sscanf_1.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1304 v = mpdm_sscanf(MPDM_LS(L"%s %f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1305 do_test("mpdm_sscanf_2.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1306 do_test("mpdm_sscanf_2.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1307 do_test("mpdm_sscanf_2.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678")) == 0);
1309 v = mpdm_sscanf(MPDM_LS(L"%s %*f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1310 do_test("mpdm_sscanf_3.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1311 do_test("mpdm_sscanf_3.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1313 v = mpdm_sscanf(MPDM_LS(L"%4d%4d%2d%10d"), MPDM_LS(L"12341234121234567890"), 0);
1314 do_test("mpdm_sscanf_4.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1315 do_test("mpdm_sscanf_4.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"1234")) == 0);
1316 do_test("mpdm_sscanf_4.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"12")) == 0);
1317 do_test("mpdm_sscanf_4.4", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"1234567890")) == 0);
1319 v = mpdm_sscanf(MPDM_LS(L"%[abc]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1320 do_test("mpdm_sscanf_5.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1321 do_test("mpdm_sscanf_5.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1323 v = mpdm_sscanf(MPDM_LS(L"%[a-d]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1324 do_test("mpdm_sscanf_6.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1325 do_test("mpdm_sscanf_6.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1327 v = mpdm_sscanf(MPDM_LS(L"%[^x]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1328 do_test("mpdm_sscanf_7.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1329 do_test("mpdm_sscanf_7.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1331 v = mpdm_sscanf(MPDM_LS(L"%[^:]: %s"), MPDM_LS(L"key: value"), 0);
1332 do_test("mpdm_sscanf_8.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"key")) == 0);
1333 do_test("mpdm_sscanf_8.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"value")) == 0);
1335 v = mpdm_sscanf(MPDM_LS(L"%*[^/]/* %s */"), MPDM_LS(L"this is code /* comment */ more code"), 0);
1336 do_test("mpdm_sscanf_9.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"comment")) == 0);
1338 v = mpdm_sscanf(MPDM_LS(L"%d%%%d"), MPDM_LS(L"1234%5678"), 0);
1339 do_test("mpdm_sscanf_10.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1340 do_test("mpdm_sscanf_10.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1342 v = mpdm_sscanf(MPDM_LS(L"%*[abc]%n%*[^ ]%n"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1343 do_test("mpdm_sscanf_11.1", mpdm_ival(mpdm_aget(v, 0)) == 7);
1344 do_test("mpdm_sscanf_11.2", mpdm_ival(mpdm_aget(v, 1)) == 12);
1346 v = mpdm_sscanf(MPDM_LS(L"/* %S */"), MPDM_LS(L"/* inside the comment */"), 0);
1347 do_test("mpdm_sscanf_12.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"inside the comment")) == 0);
1349 v = mpdm_sscanf(MPDM_LS(L"/* %S */%s"), MPDM_LS(L"/* inside the comment */outside"), 0);
1350 do_test("mpdm_sscanf_13.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"inside the comment")) == 0);
1351 do_test("mpdm_sscanf_13.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"outside")) == 0);
1353 v = mpdm_sscanf(MPDM_LS(L"%n"), MPDM_LS(L""), 0);
1354 do_test("mpdm_sscanf_14.1", mpdm_size(v) == 1 && mpdm_ival(mpdm_aget(v, 0)) == 0);
1356 v = mpdm_sscanf(MPDM_LS(L"%[^%f]%f %[#%d@]"), MPDM_LS(L"this 12.34 5678#12@34"), 0);
1357 do_test("mpdm_sscanf_15.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this ")) == 0);
1358 do_test("mpdm_sscanf_15.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1359 do_test("mpdm_sscanf_15.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678#12@34")) == 0);
1361 v = mpdm_sscanf(MPDM_LS(L"%*S\"%[^\n\"]\""), MPDM_LS(L"a \"bbb\" c;"), 0);
1362 do_test("mpdm_sscanf_16", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"bbb")) == 0);
1364 do_test("mpdm_sscanf_17", mpdm_aget(v, 0)->size == 3);
1368 void (*func)(void) = NULL;
1370 int main(int argc, char *argv[])
1372 if (argc > 1) {
1373 if (strcmp(argv[1], "-b") == 0)
1374 do_benchmarks = 1;
1375 if (strcmp(argv[1], "-m") == 0)
1376 do_multibyte_sregex_tests = 1;
1379 mpdm_startup();
1381 printf("sizeof(struct mpdm_val): %ld\n", (long) sizeof(struct mpdm_val));
1382 printf("sizeof(void *): %d\n", sizeof(void *));
1383 printf("sizeof(void (*func)(void)): %d\n", sizeof(func));
1385 test_counter();
1386 test_basic();
1387 test_array();
1388 test_hash();
1389 test_splice();
1390 test_strcat();
1391 test_split();
1392 test_join();
1393 test_sym();
1394 test_file();
1395 test_regex();
1396 test_exec();
1397 test_encoding();
1398 test_gettext();
1399 test_conversion();
1400 test_pipes();
1401 test_misc();
1402 test_sprintf();
1403 test_ulc();
1404 test_scanf();
1406 benchmark();
1408 /* mpdm_dump_unref();*/
1410 printf("memory: %d\n", mpdm->memory_usage);
1411 mpdm_sweep(-1);
1412 mpdm_sweep(-1);
1413 mpdm_sweep(-1);
1414 mpdm_sweep(-1);
1415 mpdm_sweep(-1);
1416 mpdm_sweep(-1);
1417 mpdm_sweep(-1);
1418 mpdm_sweep(-1);
1419 mpdm_sweep(-1);
1420 printf("memory: %d\n", mpdm->memory_usage);
1422 /* mpdm_dump_unref();*/
1424 mpdm_shutdown();
1426 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1428 if (oks == tests)
1429 printf("*** ALL TESTS PASSED\n");
1430 else {
1431 int n;
1433 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1435 printf("\nFailed tests:\n\n");
1436 for (n = 0; n < i_failed_msgs; n++)
1437 printf("%s", failed_msgs[n]);
1440 return (0);