Ref/unref all args in mpdm_join_s().
[mpdm.git] / stress.c
blob2bfcab37b912d1ccd2a475c9fdfd39a59195b65e
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_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 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
109 printf("mpdm_string: %ls\n", mpdm_string(MPDM_H(0)));
111 /* partial copies of strings */
112 v = MPDM_LS(L"this is not America");
113 v = MPDM_NS((wchar_t *) v->data + 4, 4);
115 do_test("Partial string values", mpdm_cmp(v, MPDM_LS(L" is ")) == 0);
117 v = mpdm_ref(MPDM_S(L"MUAHAHAHA!"));
118 w = mpdm_ref(mpdm_clone(v));
119 do_test("Testing mpdm_clone semantics 1", w == v);
120 mpdm_unref(v);
121 mpdm_unref(w);
123 v = mpdm_ref(MPDM_A(2));
124 mpdm_aset(v, MPDM_S(L"evil"), 0);
125 mpdm_aset(v, MPDM_S(L"dead"), 1);
126 w = mpdm_ref(mpdm_clone(v));
128 do_test("Testing mpdm_clone semantics 2.1", w != v);
130 t = mpdm_aget(v, 0);
131 do_test("Testing mpdm_clone semantics 2.2", t->ref > 1);
132 do_test("Testing mpdm_clone semantics 2.3", mpdm_aget(w, 0) == t);
134 mpdm_unref(w);
135 mpdm_unref(v);
137 /* mbs / wcs tests */
138 v = MPDM_MBS("This is (was) a multibyte string");
139 mpdm_dump(v);
141 /* greek omega is 03a9 */
142 v = MPDM_MBS("?Espa?a! (non-ASCII string, as ISO-8859-1 char *)");
143 mpdm_dump(v);
144 printf("(Previous value will be NULL if locale doesn't match stress.c encoding)\n");
146 v = MPDM_LS(L"A capital greek omega between brackets [\x03a9]");
147 mpdm_dump(v);
148 printf("(Previous value will only show on an Unicode terminal)\n");
150 v = MPDM_R(3.1416);
151 mpdm_dump(v);
152 do_test("rval 1", mpdm_rval(v) == 3.1416);
153 do_test("ival 1", mpdm_ival(v) == 3);
155 v = MPDM_R(777777.0 / 2.0);
156 mpdm_dump(v);
157 do_test("mpdm_rnew 1", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
159 v = MPDM_R(388888.500);
160 mpdm_dump(v);
161 do_test("mpdm_rnew 2", mpdm_cmp(v, MPDM_LS(L"388888.5")) == 0);
163 v = MPDM_R(388888.412);
164 mpdm_dump(v);
165 do_test("mpdm_rnew 3", mpdm_cmp(v, MPDM_LS(L"388888.412")) == 0);
167 v = MPDM_R(388888.6543);
168 mpdm_dump(v);
169 do_test("mpdm_rnew 4", mpdm_cmp(v, MPDM_LS(L"388888.6543")) == 0);
171 v = MPDM_R(388888.0);
172 mpdm_dump(v);
173 do_test("mpdm_rnew 5", mpdm_cmp(v, MPDM_LS(L"388888")) == 0);
175 v = MPDM_R(0.050000);
176 mpdm_dump(v);
177 do_test("mpdm_rnew 6", mpdm_cmp(v, MPDM_LS(L"0.05")) == 0);
179 v = MPDM_R(0.000);
180 mpdm_dump(v);
181 do_test("mpdm_rnew 7", mpdm_cmp(v, MPDM_LS(L"0")) == 0);
183 v = MPDM_LS(L"0177");
184 do_test("mpdm_ival() for octal numbers", mpdm_ival(v) == 0x7f);
186 v = MPDM_LS(L"0xFF");
187 do_test("mpdm_ival() for hexadecimal numbers", mpdm_ival(v) == 255);
189 v = MPDM_LS(L"001");
190 do_test("mpdm_rval() for octal numbers", mpdm_rval(v) == 1.0);
192 v = MPDM_LS(L"0x7f");
193 do_test("mpdm_rval() for hexadecimal numbers", mpdm_ival(v) == 127.0);
195 do_test("Two NULLs are equal", mpdm_cmp(NULL, NULL) == 0);
197 v = MPDM_LS(L"hahaha");
198 do_test("mpdm_cmp_s 1", mpdm_cmp_s(v, L"hahaha") == 0);
199 do_test("mpdm_cmp_s 2", mpdm_cmp_s(v, L"aahaha") > 0);
200 do_test("mpdm_cmp_s 3", mpdm_cmp_s(v, L"zahaha") < 0);
203 mpdm_t sort_cb(mpdm_t args)
205 int d;
207 /* sorts reversely */
208 d = mpdm_cmp(mpdm_aget(args, 1), mpdm_aget(args, 0));
209 return (MPDM_I(d));
213 void test_array(void)
215 int n;
216 mpdm_t a;
217 mpdm_t v;
219 a = MPDM_A(0);
220 do_test("a->size == 0", (a->size == 0));
222 mpdm_push(a, MPDM_LS(L"sunday"));
223 mpdm_push(a, MPDM_LS(L"monday"));
224 mpdm_push(a, MPDM_LS(L"tuesday"));
225 mpdm_push(a, MPDM_LS(L"wednesday"));
226 mpdm_push(a, MPDM_LS(L"thursday"));
227 mpdm_push(a, MPDM_LS(L"friday"));
228 mpdm_push(a, MPDM_LS(L"saturday"));
229 mpdm_dump(a);
230 do_test("a->size == 7", (a->size == 7));
232 v = mpdm_aget(a, 3);
233 mpdm_aset(a, NULL, 3);
234 mpdm_dump(a);
236 mpdm_sort(a, 1);
237 do_test("NULLs are sorted on top", (mpdm_aget(a, 0) == NULL));
239 mpdm_aset(a, v, 0);
240 v = mpdm_aget(a, 3);
241 do_test("v is referenced again", (v != NULL && v->ref > 0));
243 mpdm_sort(a, 1);
244 do_test("mpdm_asort() works (1)", mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"friday")) == 0);
245 do_test("mpdm_asort() works (2)",
246 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"wednesday")) == 0);
248 /* asort_cb sorts reversely */
249 mpdm_sort_cb(a, 1, MPDM_X(sort_cb));
251 do_test("mpdm_asort_cb() works (1)",
252 mpdm_cmp(mpdm_aget(a, 6), MPDM_LS(L"friday")) == 0);
253 do_test("mpdm_asort_cb() works (2)",
254 mpdm_cmp(mpdm_aget(a, 0), MPDM_LS(L"wednesday")) == 0);
256 n = v->ref;
257 v = mpdm_aget(a, 3);
258 mpdm_collapse(a, 3, 1);
259 do_test("acollapse unrefs values", (v->ref < n));
261 /* test queues */
262 a = MPDM_A(0);
264 /* add several values */
265 for (n = 0; n < 10; n++)
266 v = mpdm_queue(a, MPDM_I(n), 10);
268 do_test("queue should still output NULL", (v == NULL));
270 v = mpdm_queue(a, MPDM_I(11), 10);
271 do_test("queue should no longer output NULL", (v != NULL));
273 v = mpdm_queue(a, MPDM_I(12), 10);
274 do_test("queue should return 1", mpdm_ival(v) == 1);
275 v = mpdm_queue(a, MPDM_I(13), 10);
276 do_test("queue should return 2", mpdm_ival(v) == 2);
277 do_test("queue size should be 10", a->size == 10);
279 mpdm_dump(a);
280 v = mpdm_queue(a, MPDM_I(14), 5);
281 mpdm_dump(a);
283 do_test("queue size should be 5", a->size == 5);
284 do_test("last taken value should be 8", mpdm_ival(v) == 8);
286 a = MPDM_A(4);
287 mpdm_aset(a, MPDM_I(666), 6000);
289 do_test("array should have been automatically expanded", mpdm_size(a) == 6001);
291 v = mpdm_aget(a, -1);
292 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
294 mpdm_aset(a, MPDM_I(777), -2);
295 v = mpdm_aget(a, 5999);
296 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
298 mpdm_push(a, MPDM_I(888));
299 v = mpdm_aget(a, -1);
300 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
302 v = MPDM_A(0);
303 mpdm_push(v, MPDM_I(100));
304 mpdm_pop(v);
306 /* array comparisons with mpdm_cmp() */
307 a = MPDM_A(2);
308 mpdm_aset(a, MPDM_I(10), 0);
309 mpdm_aset(a, MPDM_I(60), 1);
311 mpdm_ref(a);
312 v = mpdm_ref(mpdm_clone(a));
313 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
315 mpdm_adel(v, -1);
316 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
318 mpdm_push(v, MPDM_I(80));
319 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a, v) < 0);
321 mpdm_unref(v);
322 mpdm_unref(a);
326 void test_hash(void)
328 mpdm_t h;
329 mpdm_t v;
330 int i, n;
332 h = MPDM_H(0);
334 do_test("hsize 1", mpdm_hsize(h) == 0);
336 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
337 v = mpdm_hget(h, MPDM_S(L"mp"));
339 do_test("hsize 2", mpdm_hsize(h) == 1);
341 do_test("hash: v != NULL", (v != NULL));
342 i = mpdm_ival(v);
343 do_test("hash: v == 6", (i == 6));
345 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
346 v = mpdm_hget(h, MPDM_S(L"mp2"));
348 do_test("hsize 3", mpdm_hsize(h) == 2);
350 do_test("hash: v != NULL", (v != NULL));
351 i = mpdm_ival(v);
352 do_test("hash: v == 66", (i == 66));
354 /* fills 100 values */
355 for (n = 0; n < 50; n++)
356 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
357 for (n = 100; n >= 50; n--)
358 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
360 do_test("hsize 4", mpdm_hsize(h) == 103);
362 /* tests 100 values */
363 for (n = 0; n < 100; n++) {
364 v = mpdm_hget(h, MPDM_I(n));
366 if (v != NULL) {
367 i = mpdm_ival(v);
368 if (!(i == n * 10))
369 do_test("hash: ival", (i == n * 10));
371 else
372 do_test("hash: hget", (v != NULL));
375 printf("h's size: %d\n", mpdm_hsize(h));
377 mpdm_hdel(h, MPDM_LS(L"mp"));
378 do_test("hsize 5", mpdm_hsize(h) == 102);
381 mpdm_dump(h);
383 v=mpdm_hkeys(h);
384 mpdm_dump(v);
386 /* use of non-strings as hashes */
387 h = MPDM_H(0);
389 v = MPDM_A(0);
390 mpdm_hset(h, v, MPDM_I(1234));
391 v = MPDM_H(0);
392 mpdm_hset(h, v, MPDM_I(12345));
393 v = MPDM_H(0);
394 mpdm_hset(h, v, MPDM_I(9876));
395 v = MPDM_A(0);
396 mpdm_hset(h, v, MPDM_I(6543));
397 i = mpdm_ival(mpdm_hget(h, v));
399 mpdm_dump(h);
400 do_test("hash: using non-strings as hash keys", (i == 6543));
402 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
404 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
405 do_test("exists 2", !mpdm_exists(h, MPDM_LS(L"notok")));
407 mpdm_dump(h);
408 v = mpdm_hget_s(h, L"ok");
409 printf("v %s\n", v == NULL ? "is NULL" : "is NOT NULL");
410 do_test("hget_s 1", mpdm_ival(v) == 666);
411 mpdm_dump(v);
412 mpdm_dump(h);
414 i = 0;
415 for (n = 0; n < 1000; n++) {
416 if (mpdm_hget_s(h, L"ok") != NULL)
417 i++;
419 printf("i: %d\n", i);
420 do_test("hget_s 1.2", i == 1000);
422 if (i != 1000)
423 mpdm_hget_s(h, L"ok");
425 do_test("hget 1.2.1", mpdm_hget(h, MPDM_LS(L"ok")) != NULL);
427 mpdm_hset_s(h, L"ok", MPDM_I(777));
429 v = mpdm_hget_s(h, L"ok");
430 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
434 void test_splice(void)
436 mpdm_t w;
437 mpdm_t v;
439 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"), MPDM_LS(L"special "), 4, 0);
440 do_test("splice insertion",
441 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm special agent Johnson")) == 0);
442 mpdm_dump(w);
444 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
445 do_test("splice insertion and deletion (1)",
446 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
447 do_test("splice insertion and deletion (2)",
448 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
449 mpdm_dump(w);
451 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
452 do_test("splice deletion (1)", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
453 do_test("splice deletion (2)", mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
454 mpdm_dump(w);
456 v = MPDM_LS(L"It doesn't matter");
457 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
458 do_test("splice insertion at the end",
459 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"It doesn't matter two")) == 0);
460 mpdm_dump(w);
462 w = mpdm_splice(NULL, NULL, 0, 0);
463 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
465 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
466 do_test("splice with first value NULL",
467 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
469 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
470 do_test("splice with second value NULL",
471 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
473 v = MPDM_LS(L"I'm testing");
475 w = mpdm_splice(v, NULL, 0, -1);
476 do_test("splice with negative del (1)",
477 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
479 w = mpdm_splice(v, NULL, 4, -1);
480 do_test("splice with negative del (2)",
481 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
483 w = mpdm_splice(v, NULL, 4, -2);
484 do_test("splice with negative del (3)",
485 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
487 w = mpdm_splice(v, NULL, 0, -4);
488 do_test("splice with negative del (4)",
489 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
490 mpdm_dump(mpdm_aget(w, 0));
492 w = mpdm_splice(v, NULL, 4, -20);
493 do_test("splice with out-of-bounds negative del",
494 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
498 void test_strcat(void)
500 mpdm_t v;
501 mpdm_t w;
503 w = MPDM_LS(L"something");
505 v = mpdm_strcat(NULL, NULL);
506 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
508 v = mpdm_strcat(NULL, w);
509 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
511 v = mpdm_strcat(w, NULL);
512 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
514 w = MPDM_LS(L"");
515 v = mpdm_strcat(NULL, w);
516 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
518 v = mpdm_strcat(w, NULL);
519 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
521 v = mpdm_strcat(w, w);
522 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
527 void test_split(void)
529 mpdm_t w;
531 printf("mpdm_split test\n\n");
533 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"four.elems.in.string"));
534 mpdm_dump(w);
535 do_test("4 elems: ", (w->size == 4));
537 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"unseparated string"));
538 mpdm_dump(w);
539 do_test("1 elem: ", (w->size == 1));
541 w = mpdm_split(MPDM_S(L"."), MPDM_S(L".dot.at start"));
542 mpdm_dump(w);
543 do_test("3 elems: ", (w->size == 3));
545 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"dot.at end."));
546 mpdm_dump(w);
547 do_test("3 elems: ", (w->size == 3));
549 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"three...dots (two empty elements)"));
550 mpdm_dump(w);
551 do_test("4 elems: ", (w->size == 4));
553 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
554 mpdm_dump(w);
555 do_test("2 elems: ", (w->size == 2));
557 w = mpdm_split(NULL, MPDM_S(L"I am the man"));
558 do_test("NULL split 1: ", mpdm_size(w) == 12);
559 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
563 void test_join(void)
565 mpdm_t v;
566 mpdm_t s;
567 mpdm_t w;
569 printf("mpdm_join test\n\n");
571 /* separator */
572 s = mpdm_ref(MPDM_LS(L"--"));
574 w = MPDM_A(1);
575 mpdm_aset(w, MPDM_S(L"ce"), 0);
577 mpdm_ref(w);
579 v = mpdm_join(NULL, w);
580 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
582 v = mpdm_join(s, w);
583 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
585 mpdm_push(w, MPDM_LS(L"n'est"));
586 v = mpdm_join(s, w);
587 do_test("2 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
589 mpdm_push(w, MPDM_LS(L"pas"));
590 v = mpdm_join(s, w);
591 do_test("3 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
593 v = mpdm_join(NULL, w);
594 do_test("3 elems, no separator", (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
596 mpdm_unref(w);
597 mpdm_unref(s);
601 static mpdm_t active(mpdm_t args)
603 return MPDM_H(0);
607 void test_sym(void)
609 mpdm_t v;
610 int i;
612 printf("mpdm_sset / mpdm_sget tests\n\n");
614 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
615 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
616 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
617 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
618 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"), MPDM_LS(L"profontwindows"));
619 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
620 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
621 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
622 mpdm_sset(NULL, MPDM_LS(L"mp.active"), MPDM_X(active));
623 mpdm_sset(NULL, MPDM_LS(L"mp.active.syntax"), NULL);
624 mpdm_dump(mpdm_root());
626 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
627 i = mpdm_ival(v);
629 do_test("auto_indent == 16384", (i == 16384));
631 mpdm_sweep(-1);
635 void test_file(void)
637 mpdm_t f;
638 mpdm_t v;
639 mpdm_t eol = MPDM_LS(L"\n");
641 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
643 if (f == NULL) {
644 printf("Can't create test.txt; no further file tests possible.\n");
645 return;
648 do_test("Create test.txt", f != NULL);
650 mpdm_write(f, MPDM_LS(L"0"));
651 mpdm_write(f, eol);
652 mpdm_write(f, MPDM_LS(L"1"));
653 mpdm_write(f, eol);
655 /* write WITHOUT eol */
656 mpdm_write(f, MPDM_LS(L"2"));
658 mpdm_close(f);
660 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
662 do_test("test written file 0", mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
663 do_test("test written file 1", mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
664 do_test("test written file 2", mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
665 do_test("test written file 3", mpdm_read(f) == NULL);
667 mpdm_close(f);
669 mpdm_unlink(MPDM_LS(L"test.txt"));
670 do_test("unlink", mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
672 v = mpdm_stat(MPDM_LS(L"stress.c"));
673 printf("Stat from stress.c:\n");
674 mpdm_dump(v);
676 /* v=mpdm_glob(MPDM_LS(L"*"));*/
677 printf("Glob:\n");
678 v = mpdm_glob(NULL, NULL);
679 mpdm_dump(v);
683 void test_regex(void)
685 mpdm_t v;
686 mpdm_t w;
688 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_LS(L"123456"), 0);
689 do_test("regex 0", v != NULL);
691 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_I(65536), 0);
692 do_test("regex 1", v != NULL);
694 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"12345678"), 0);
695 do_test("regex 2", v != NULL);
697 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_I(1), 0);
698 do_test("regex 3", v != NULL);
700 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"A12345-678"), 0);
701 do_test("regex 4", v == NULL);
703 w = MPDM_LS(L"Hell street, 666");
704 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), w, 0);
705 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
707 mpdm_dump(v);
709 v = mpdm_regex(MPDM_LS(L"/regex/"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
710 do_test("regex 6.1 (case sensitive)", v == NULL);
712 v = mpdm_regex(MPDM_LS(L"/regex/i"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
713 do_test("regex 6.2 (case insensitive)", v != NULL);
715 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
716 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
718 v = mpdm_regex(MPDM_LS(L"/^\\s*/"), MPDM_LS(L"123456"), 0);
719 do_test("regex 7", v != NULL);
721 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), MPDM_LS(L"123456"), 0);
722 do_test("regex 8", v == NULL);
724 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), NULL, 0);
725 do_test("regex 9 (NULL string to match)", v == NULL);
727 /* sregex */
729 v = mpdm_sregex(MPDM_LS(L"/A/"), MPDM_LS(L"change all A to A"), MPDM_LS(L"E"), 0);
730 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
732 v = mpdm_sregex(MPDM_LS(L"/A/g"), MPDM_LS(L"change all A to A"), MPDM_LS(L"E"), 0);
733 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
735 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAAAAA to E"),
736 MPDM_LS(L"E"), 0);
737 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
739 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all A A A A A A to E"),
740 MPDM_LS(L"E"), 0);
741 do_test("sregex 3", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
743 v = mpdm_sregex(MPDM_LS(L"/A+/g"), MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
744 MPDM_LS(L"E"), 0);
745 do_test("sregex 3.2", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
747 v = mpdm_sregex(MPDM_LS(L"/[0-9]+/g"), MPDM_LS(L"1, 20, 333, 40 all are numbers"),
748 MPDM_LS(L"numbers"), 0);
749 do_test("sregex 4",
750 mpdm_cmp(v,
751 MPDM_LS(L"numbers, numbers, numbers, numbers all are numbers")) == 0);
753 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z_]+/g"), MPDM_LS(L"regex, mpdm_regex, TexMex"),
754 MPDM_LS(L"sex"), 0);
755 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
757 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z]+/g"), MPDM_LS(L"regex, mpdm_regex, TexMex"),
758 NULL, 0);
759 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
761 v = mpdm_sregex(MPDM_LS(L"/\\\\/g"), MPDM_LS(L"\\MSDOS\\style\\path"),
762 MPDM_LS(L"/"), 0);
763 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
765 v = mpdm_sregex(MPDM_LS(L"/regex/gi"), MPDM_LS(L"regex, Regex, REGEX"),
766 MPDM_LS(L"sex"), 0);
767 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
769 v = mpdm_sregex(NULL, NULL, NULL, 0);
770 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
772 /* & in substitution tests */
773 v = MPDM_LS(L"this string has many words");
774 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[&]"), 0);
775 do_test("& in sregex target",
776 mpdm_cmp(v, MPDM_LS(L"[this] [string] [has] [many] [words]")) == 0);
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("escaped & in sregex target",
781 mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 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"\\this \\string \\has \\many \\words")) == 0);
788 v = MPDM_LS(L"hola ");
789 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, MPDM_LS(L""), 0);
790 do_test("sregex output size 1", v->size == 4);
792 v = MPDM_LS(L"hola ");
793 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, NULL, 0);
794 do_test("sregex output size 2", v->size == 4);
796 v = MPDM_LS(L"hola ");
797 v = mpdm_sregex(MPDM_LS(L"/[ \t]$/"), v, MPDM_LS(L"!"), 0);
798 do_test("sregex output size 3", v->size == 5);
800 v = MPDM_LS(L"holo");
801 v = mpdm_sregex(MPDM_LS(L"/o/g"), v, MPDM_LS(L"!!"), 0);
802 do_test("sregex output size 4", v->size == 6);
804 /* multiple regex tests */
805 w = MPDM_A(0);
807 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
808 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
809 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
810 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
811 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
813 v = mpdm_regex(w, MPDM_LS(L"key=value"), 0);
814 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
815 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
817 v = mpdm_regex(w, MPDM_LS(L" key = value"), 0);
818 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
819 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
821 v = mpdm_regex(w, MPDM_LS(L"\t\tkey\t=\tvalue "), 0);
822 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"key")) == 0);
823 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"value")) == 0);
825 /* v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
826 do_test("multi-regex 4", v == NULL);
828 printf("Multiple line regexes\n");
829 w = MPDM_LS(L"/* this is\na C-like comment */");
830 v = mpdm_regex(MPDM_LS(L"|/\\*.+\\*/|"), w, 0);
831 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
833 v = mpdm_regex(MPDM_LS(L"/is$/"), w, 0);
834 do_test("Multiline regex 2", v == NULL);
836 v = mpdm_regex(MPDM_LS(L"/is$/m"), w, 0);
837 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
839 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
841 w = MPDM_LS(L"-\x03a9-");
843 v = mpdm_regex(MPDM_LS(L"/-$/"), w, 0);
844 do_test("Multibyte environment regex 1", mpdm_cmp(v, MPDM_LS(L"-")) == 0);
846 if (do_multibyte_sregex_tests) {
847 v = mpdm_sregex(MPDM_LS(L"/-$/"), w, MPDM_LS(L"~"), 0);
848 do_test("Multibyte environment sregex 1",
849 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
851 v = mpdm_sregex(MPDM_LS(L"/-/g"), w, MPDM_LS(L"~"), 0);
852 do_test("Multibyte environment sregex 2",
853 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
855 else
856 printf("Multibyte sregex test omitted; activate with -m\n");
858 /* 'last' flag tests */
859 v = MPDM_LS(L"this string has many words");
860 v = mpdm_regex(MPDM_LS(L"/[a-z]+/l"), v, 0);
861 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
865 static mpdm_t dumper(mpdm_t args)
866 /* executable value */
868 mpdm_dump(args);
869 return (NULL);
873 static mpdm_t sum(mpdm_t args)
874 /* executable value: sum all args */
876 int n, t = 0;
878 if (args != NULL) {
879 for (n = t = 0; n < args->size; n++)
880 t += mpdm_ival(mpdm_aget(args, n));
883 return (MPDM_I(t));
887 static mpdm_t calculator(mpdm_t c, mpdm_t args)
888 /* 2 argument version: calculator. c contains a 'script' to
889 do things with the arguments */
891 int n, t;
892 mpdm_t v;
893 mpdm_t a;
894 mpdm_t o;
896 mpdm_ref(args);
898 /* to avoid destroying args */
899 a = mpdm_clone(args);
901 /* shift first argument */
902 v = mpdm_shift(a);
903 t = mpdm_ival(v);
905 for (n = 0; n < mpdm_size(c); n++) {
906 /* gets operator */
907 o = mpdm_aget(c, n);
909 /* gets next value */
910 v = mpdm_shift(a);
912 switch (*(wchar_t *) o->data) {
913 case '+':
914 t += mpdm_ival(v);
915 break;
916 case '-':
917 t -= mpdm_ival(v);
918 break;
919 case '*':
920 t *= mpdm_ival(v);
921 break;
922 case '/':
923 t /= mpdm_ival(v);
924 break;
928 mpdm_unref(args);
930 return (MPDM_I(t));
934 void test_exec(void)
936 mpdm_t x;
937 mpdm_t w;
938 mpdm_t p;
940 printf("test_exec\n");
942 x = MPDM_X(dumper);
944 /* a simple value */
945 mpdm_ref(x);
946 mpdm_exec(x, NULL);
947 mpdm_exec(x, x);
948 mpdm_unref(x);
950 x = mpdm_ref(MPDM_X(sum));
951 w = mpdm_ref(MPDM_A(3));
952 mpdm_aset(w, MPDM_I(100), 0);
953 mpdm_aset(w, MPDM_I(220), 1);
954 mpdm_aset(w, MPDM_I(333), 2);
956 do_test("exec 0", mpdm_ival(mpdm_exec(x, w)) == 653);
957 x = mpdm_unref(x);
959 mpdm_push(w, MPDM_I(1));
961 /* multiple executable value: vm and compiler support */
963 /* calculator 'script' */
964 p = mpdm_ref(MPDM_A(0));
965 mpdm_push(p, MPDM_LS(L"+"));
966 mpdm_push(p, MPDM_LS(L"-"));
967 mpdm_push(p, MPDM_LS(L"+"));
969 /* the value */
970 x = mpdm_ref(MPDM_A(2));
971 x->flags |= MPDM_EXEC;
973 mpdm_aset(x, MPDM_X(calculator), 0);
974 mpdm_aset(x, p, 1);
976 do_test("exec 1", mpdm_ival(mpdm_exec(x, w)) == -12);
978 mpdm_unref(p);
980 /* another 'script', different operations with the same values */
981 p = mpdm_ref(MPDM_A(0));
982 mpdm_push(p, MPDM_LS(L"*"));
983 mpdm_push(p, MPDM_LS(L"/"));
984 mpdm_push(p, MPDM_LS(L"+"));
986 mpdm_aset(x, p, 1);
988 do_test("exec 2", mpdm_ival(mpdm_exec(x, w)) == 67);
989 x = mpdm_unref(x);
990 p = mpdm_unref(p);
991 w = mpdm_unref(w);
995 void test_encoding(void)
997 mpdm_t f;
998 mpdm_t v;
999 mpdm_t w;
1000 const wchar_t *ptr;
1002 v = MPDM_MBS("?Espa?a!\n");
1004 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
1006 if ((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL) {
1007 printf("Can't write test.txt; no further file test possible.\n");
1008 return;
1011 mpdm_write(f, v);
1012 mpdm_close(f);
1014 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1015 w = mpdm_read(f);
1016 mpdm_dump(w);
1017 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
1018 mpdm_close(f);
1020 printf
1021 ("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1023 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1024 w = mpdm_read(f);
1025 mpdm_dump(w);
1026 mpdm_close(f);
1028 for (ptr = w->data; *ptr != L'\0'; ptr++)
1029 printf("%d", mpdm_wcwidth(*ptr));
1030 printf("\n");
1032 if (mpdm_encoding(MPDM_LS(L"UTF-8")) < 0) {
1033 printf("No multiple encoding (iconv) support; no more tests possible.\n");
1034 return;
1037 printf
1038 ("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
1040 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
1041 w = mpdm_read(f);
1042 mpdm_dump(w);
1043 mpdm_close(f);
1045 /* new open file will use the specified encoding */
1046 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
1047 mpdm_write(f, v);
1048 mpdm_close(f);
1050 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
1051 w = mpdm_read(f);
1052 mpdm_dump(w);
1053 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
1054 mpdm_close(f);
1056 mpdm_encoding(NULL);
1060 void test_gettext(void)
1062 mpdm_t v;
1063 mpdm_t h;
1065 printf("\nTesting gettext...\n");
1067 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
1069 printf("Should follow a translated string of 'This is a test string':\n");
1070 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1071 mpdm_dump(v);
1073 printf("The same, but cached:\n");
1074 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
1075 mpdm_dump(v);
1077 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
1078 mpdm_dump(v);
1080 printf("Ad-hoc translation hash:\n");
1081 h = MPDM_H(0);
1082 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
1084 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
1085 v = mpdm_gettext(MPDM_LS(L"test string"));
1086 mpdm_dump(v);
1090 void timer(int secs)
1092 static clock_t clks = 0;
1094 switch (secs) {
1095 case 0:
1096 clks = clock();
1097 break;
1099 case -1:
1100 printf("%.2f seconds\n", (float) (clock() - clks) / (float) CLOCKS_PER_SEC);
1101 break;
1106 void bench_hash(int i, mpdm_t l, int buckets)
1108 mpdm_t h;
1109 mpdm_t v;
1110 int n;
1112 printf("Hash of %d buckets: \n", buckets);
1113 h = MPDM_H(buckets);
1115 timer(0);
1116 for (n = 0; n < i; n++) {
1117 v = mpdm_aget(l, n);
1118 mpdm_hset(h, v, v);
1120 timer(-1);
1122 printf("Bucket usage:\n");
1123 for(n=0;n < mpdm_size(h);n++)
1124 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1126 mpdm_sweep(-1);
1130 void benchmark(void)
1132 mpdm_t l;
1133 int i, n;
1134 char tmp[64];
1136 printf("\n");
1138 if (!do_benchmarks) {
1139 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1140 return;
1143 printf("BENCHMARKS\n");
1145 i = 500000;
1147 printf("Creating %d values...\n", i);
1149 l = mpdm_ref(MPDM_A(i));
1150 for (n = 0; n < i; n++) {
1151 sprintf(tmp, "%08x", n);
1152 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1153 mpdm_aset(l, MPDM_I(n), n);
1156 printf("OK\n");
1158 bench_hash(i, l, 0);
1159 bench_hash(i, l, 61);
1160 bench_hash(i, l, 89);
1161 bench_hash(i, l, 127);
1163 mpdm_unref(l);
1167 void test_conversion(void)
1169 wchar_t *wptr = NULL;
1170 char *ptr = NULL;
1171 int size = 0;
1173 ptr = mpdm_wcstombs(L"", &size);
1174 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1176 wptr = mpdm_mbstowcs("", &size, 0);
1177 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1181 void test_pipes(void)
1183 mpdm_t f;
1185 printf("\n");
1187 if ((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL) {
1188 mpdm_t v;
1190 v = mpdm_read(f);
1191 mpdm_pclose(f);
1193 printf("Pipe from 'date':\n");
1194 mpdm_dump(v);
1196 else
1197 printf("Can't pipe to 'date'\n");
1201 void test_misc(void)
1203 printf("Home dir:\n");
1204 mpdm_dump(mpdm_home_dir());
1205 printf("App dir:\n");
1206 mpdm_dump(mpdm_app_dir());
1210 void test_sprintf(void)
1212 mpdm_t v;
1213 mpdm_t w;
1215 printf("sprintf tests\n");
1217 v = MPDM_A(0);
1218 mpdm_push(v, MPDM_I(100));
1219 mpdm_push(v, MPDM_LS(L"beers"));
1221 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1222 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1224 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1225 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me 0")) == 0);
1227 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1228 do_test("sprintf 3", mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1230 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1231 do_test("sprintf 4", mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1233 v = MPDM_A(0);
1234 mpdm_push(v, MPDM_R(3.1416));
1236 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1237 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1239 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1240 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1242 v = MPDM_A(0);
1243 mpdm_push(v, MPDM_LS(L"stress"));
1245 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1246 do_test("sprintf 3.1", mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1248 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1249 do_test("sprintf 3.2", mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1251 v = MPDM_A(0);
1252 mpdm_push(v, MPDM_I(0x263a));
1254 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1255 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1257 v = MPDM_A(0);
1258 mpdm_push(v, MPDM_I(75));
1260 w = mpdm_sprintf(MPDM_LS(L"%d%%"), v);
1261 do_test("sprintf 4.1", mpdm_cmp(w, MPDM_LS(L"75%")) == 0);
1263 w = mpdm_sprintf(MPDM_LS(L"%b"), v);
1264 do_test("sprintf 5.1", mpdm_cmp(w, MPDM_LS(L"1001011")) == 0);
1268 void test_ulc(void)
1270 mpdm_t v = MPDM_S(L"string");
1271 mpdm_t w = mpdm_ulc(v, 1);
1273 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1274 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1278 void test_scanf(void)
1280 mpdm_t v;
1282 v = mpdm_sscanf(MPDM_LS(L"%d %d"), MPDM_LS(L"1234 5678"), 0);
1283 do_test("mpdm_sscanf_1.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1284 do_test("mpdm_sscanf_1.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1286 v = mpdm_sscanf(MPDM_LS(L"%s %f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1287 do_test("mpdm_sscanf_2.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1288 do_test("mpdm_sscanf_2.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1289 do_test("mpdm_sscanf_2.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678")) == 0);
1291 v = mpdm_sscanf(MPDM_LS(L"%s %*f %d"), MPDM_LS(L"this 12.34 5678"), 0);
1292 do_test("mpdm_sscanf_3.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this")) == 0);
1293 do_test("mpdm_sscanf_3.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1295 v = mpdm_sscanf(MPDM_LS(L"%4d%4d%2d%10d"), MPDM_LS(L"12341234121234567890"), 0);
1296 do_test("mpdm_sscanf_4.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1297 do_test("mpdm_sscanf_4.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"1234")) == 0);
1298 do_test("mpdm_sscanf_4.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"12")) == 0);
1299 do_test("mpdm_sscanf_4.4", mpdm_cmp(mpdm_aget(v, 3), MPDM_LS(L"1234567890")) == 0);
1301 v = mpdm_sscanf(MPDM_LS(L"%[abc]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1302 do_test("mpdm_sscanf_5.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1303 do_test("mpdm_sscanf_5.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1305 v = mpdm_sscanf(MPDM_LS(L"%[a-d]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1306 do_test("mpdm_sscanf_6.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1307 do_test("mpdm_sscanf_6.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1309 v = mpdm_sscanf(MPDM_LS(L"%[^x]%s"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1310 do_test("mpdm_sscanf_7.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"ccbaabc")) == 0);
1311 do_test("mpdm_sscanf_7.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"xaaae")) == 0);
1313 v = mpdm_sscanf(MPDM_LS(L"%[^:]: %s"), MPDM_LS(L"key: value"), 0);
1314 do_test("mpdm_sscanf_8.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"key")) == 0);
1315 do_test("mpdm_sscanf_8.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"value")) == 0);
1317 v = mpdm_sscanf(MPDM_LS(L"%*[^/]/* %s */"), MPDM_LS(L"this is code /* comment */ more code"), 0);
1318 do_test("mpdm_sscanf_9.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"comment")) == 0);
1320 v = mpdm_sscanf(MPDM_LS(L"%d%%%d"), MPDM_LS(L"1234%5678"), 0);
1321 do_test("mpdm_sscanf_10.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"1234")) == 0);
1322 do_test("mpdm_sscanf_10.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"5678")) == 0);
1324 v = mpdm_sscanf(MPDM_LS(L"%*[abc]%n%*[^ ]%n"), MPDM_LS(L"ccbaabcxaaae and more"), 0);
1325 do_test("mpdm_sscanf_11.1", mpdm_ival(mpdm_aget(v, 0)) == 7);
1326 do_test("mpdm_sscanf_11.2", mpdm_ival(mpdm_aget(v, 1)) == 12);
1328 v = mpdm_sscanf(MPDM_LS(L"/* %S */"), MPDM_LS(L"/* inside the comment */"), 0);
1329 do_test("mpdm_sscanf_12.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"inside the comment")) == 0);
1331 v = mpdm_sscanf(MPDM_LS(L"/* %S */%s"), MPDM_LS(L"/* inside the comment */outside"), 0);
1332 do_test("mpdm_sscanf_13.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"inside the comment")) == 0);
1333 do_test("mpdm_sscanf_13.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"outside")) == 0);
1335 v = mpdm_sscanf(MPDM_LS(L"%n"), MPDM_LS(L""), 0);
1336 do_test("mpdm_sscanf_14.1", mpdm_size(v) == 1 && mpdm_ival(mpdm_aget(v, 0)) == 0);
1338 v = mpdm_sscanf(MPDM_LS(L"%[^%f]%f %[#%d@]"), MPDM_LS(L"this 12.34 5678#12@34"), 0);
1339 do_test("mpdm_sscanf_15.1", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"this ")) == 0);
1340 do_test("mpdm_sscanf_15.2", mpdm_cmp(mpdm_aget(v, 1), MPDM_LS(L"12.34")) == 0);
1341 do_test("mpdm_sscanf_15.3", mpdm_cmp(mpdm_aget(v, 2), MPDM_LS(L"5678#12@34")) == 0);
1343 v = mpdm_sscanf(MPDM_LS(L"%*S\"%[^\n\"]\""), MPDM_LS(L"a \"bbb\" c;"), 0);
1344 do_test("mpdm_sscanf_16", mpdm_cmp(mpdm_aget(v, 0), MPDM_LS(L"bbb")) == 0);
1346 do_test("mpdm_sscanf_17", mpdm_aget(v, 0)->size == 3);
1350 void (*func)(void) = NULL;
1352 int main(int argc, char *argv[])
1354 if (argc > 1) {
1355 if (strcmp(argv[1], "-b") == 0)
1356 do_benchmarks = 1;
1357 if (strcmp(argv[1], "-m") == 0)
1358 do_multibyte_sregex_tests = 1;
1361 mpdm_startup();
1363 printf("sizeof(struct mpdm_val): %ld\n", (long) sizeof(struct mpdm_val));
1364 printf("sizeof(void *): %d\n", sizeof(void *));
1365 printf("sizeof(void (*func)(void)): %d\n", sizeof(func));
1367 test_counter();
1368 test_basic();
1369 test_array();
1370 test_hash();
1371 test_splice();
1372 test_strcat();
1373 test_split();
1374 test_join();
1375 test_sym();
1376 test_file();
1377 test_regex();
1378 test_exec();
1379 test_encoding();
1380 test_gettext();
1381 test_conversion();
1382 test_pipes();
1383 test_misc();
1384 test_sprintf();
1385 test_ulc();
1386 test_scanf();
1388 benchmark();
1390 /* mpdm_dump_unref();*/
1392 printf("memory: %d\n", mpdm->memory_usage);
1393 mpdm_sweep(-1);
1394 mpdm_sweep(-1);
1395 mpdm_sweep(-1);
1396 mpdm_sweep(-1);
1397 mpdm_sweep(-1);
1398 mpdm_sweep(-1);
1399 mpdm_sweep(-1);
1400 mpdm_sweep(-1);
1401 mpdm_sweep(-1);
1402 printf("memory: %d\n", mpdm->memory_usage);
1404 /* mpdm_dump_unref();*/
1406 mpdm_shutdown();
1408 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1410 if (oks == tests)
1411 printf("*** ALL TESTS PASSED\n");
1412 else {
1413 int n;
1415 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1417 printf("\nFailed tests:\n\n");
1418 for (n = 0; n < i_failed_msgs; n++)
1419 printf("%s", failed_msgs[n]);
1422 return (0);