Components in mpdm_sym() can be executable values (Closes: #1100).
[mpdm.git] / stress.c
blobefd6cd976371eee78ae70467b15eacd9e6b7b01a
1 /*
3 MPDM - Minimum Profit Data Manager
4 Copyright (C) 2003/2007 Angel Ortega <angel@triptico.com>
6 stress.c - Stress tests for MPDM.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 http://www.triptico.com
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);
169 mpdm_t sort_cb(mpdm_t args)
171 int d;
173 /* sorts reversely */
174 d = mpdm_cmp(mpdm_aget(args, 1), mpdm_aget(args, 0));
175 return(MPDM_I(d));
179 void test_array(void)
181 int n;
182 mpdm_t a;
183 mpdm_t v;
185 a = MPDM_A(0);
186 do_test("a->size == 0", (a->size == 0));
188 mpdm_push(a, MPDM_LS(L"sunday"));
189 mpdm_push(a, MPDM_LS(L"monday"));
190 mpdm_push(a, MPDM_LS(L"tuesday"));
191 mpdm_push(a, MPDM_LS(L"wednesday"));
192 mpdm_push(a, MPDM_LS(L"thursday"));
193 mpdm_push(a, MPDM_LS(L"friday"));
194 mpdm_push(a, MPDM_LS(L"saturday"));
195 mpdm_dump(a);
196 do_test("a->size == 7", (a->size == 7));
198 v = mpdm_aset(a, NULL, 3);
199 do_test("v->ref == 0", (v->ref == 0));
200 mpdm_dump(a);
202 a = mpdm_sort(a, 1);
203 do_test("NULLs are sorted on top", (mpdm_aget(a, 0) == NULL));
205 mpdm_aset(a, v, 0);
206 v = mpdm_aget(a, 3);
207 do_test("v is referenced again", (v != NULL && v->ref > 0));
209 a = mpdm_sort(a, 1);
210 do_test("mpdm_asort() works (1)",
211 mpdm_cmp(mpdm_aget(a,0), MPDM_LS(L"friday")) == 0);
212 do_test("mpdm_asort() works (2)",
213 mpdm_cmp(mpdm_aget(a,6), MPDM_LS(L"wednesday")) == 0);
215 /* asort_cb sorts reversely */
216 a = mpdm_sort_cb(a, 1, MPDM_X(sort_cb));
218 do_test("mpdm_asort_cb() works (1)",
219 mpdm_cmp(mpdm_aget(a,6), MPDM_LS(L"friday")) == 0);
220 do_test("mpdm_asort_cb() works (2)",
221 mpdm_cmp(mpdm_aget(a,0), MPDM_LS(L"wednesday")) == 0);
223 n = v->ref;
224 v = mpdm_aget(a, 3);
225 mpdm_collapse(a, 3, 1);
226 do_test("acollapse unrefs values", (v->ref < n));
228 /* test queues */
229 a = MPDM_A(0);
231 /* add several values */
232 for(n = 0;n < 10;n++)
233 v = mpdm_queue(a, MPDM_I(n), 10);
235 do_test("queue should still output NULL", (v == NULL));
237 v = mpdm_queue(a, MPDM_I(11), 10);
238 do_test("queue should no longer output NULL", (v != NULL));
240 v = mpdm_queue(a, MPDM_I(12), 10);
241 do_test("queue should return 1", mpdm_ival(v) == 1);
242 v = mpdm_queue(a, MPDM_I(13), 10);
243 do_test("queue should return 2", mpdm_ival(v) == 2);
244 do_test("queue size should be 10", a->size == 10);
246 mpdm_dump(a);
247 v = mpdm_queue(a, MPDM_I(14), 5);
248 mpdm_dump(a);
250 do_test("queue size should be 5", a->size == 5);
251 do_test("last taken value should be 8", mpdm_ival(v) == 8);
253 a = MPDM_A(4);
254 mpdm_aset(a, MPDM_I(666), 6000);
256 do_test("array should have been automatically expanded",
257 mpdm_size(a) == 6001);
259 v = mpdm_aget(a, -1);
260 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
262 mpdm_aset(a, MPDM_I(777), -2);
263 v = mpdm_aget(a, 5999);
264 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
266 mpdm_push(a, MPDM_I(888));
267 v = mpdm_aget(a, -1);
268 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
270 v = MPDM_A(0);
271 mpdm_push(v, MPDM_I(100));
272 mpdm_pop(v);
274 /* array comparisons with mpdm_cmp() */
275 a = MPDM_A(2);
276 mpdm_aset(a, MPDM_I(10), 0);
277 mpdm_aset(a, MPDM_I(60), 1);
279 v = mpdm_clone(a);
280 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
282 mpdm_adel(v, -1);
283 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
285 mpdm_push(v, MPDM_I(80));
286 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a, v) < 0);
290 void test_hash(void)
292 mpdm_t h;
293 mpdm_t v;
294 int i, n;
296 h = MPDM_H(0);
298 do_test("hsize 1", mpdm_hsize(h) == 0);
300 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
301 v = mpdm_hget(h, MPDM_S(L"mp"));
303 do_test("hsize 2", mpdm_hsize(h) == 1);
305 do_test("hash: v != NULL", (v != NULL));
306 i = mpdm_ival(v);
307 do_test("hash: v == 6", (i == 6));
309 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
310 v = mpdm_hget(h, MPDM_S(L"mp2"));
312 do_test("hsize 3", mpdm_hsize(h) == 2);
314 do_test("hash: v != NULL", (v != NULL));
315 i = mpdm_ival(v);
316 do_test("hash: v == 66", (i == 66));
318 /* fills 100 values */
319 for(n = 0;n < 50;n++)
320 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
321 for(n = 100;n >= 50;n--)
322 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
324 do_test("hsize 4", mpdm_hsize(h) == 103);
326 /* tests 100 values */
327 for(n = 0;n < 100;n++)
329 v = mpdm_hget(h, MPDM_I(n));
331 if(v != NULL)
333 i = mpdm_ival(v);
334 if(!(i == n * 10))
335 do_test("hash: ival", (i == n * 10));
337 else
338 do_test("hash: hget", (v != NULL));
341 printf("h's size: %d\n", mpdm_hsize(h));
343 v = mpdm_hdel(h, MPDM_LS(L"mp"));
344 do_test("hdel", mpdm_ival(v) == 6);
345 do_test("hsize 5", mpdm_hsize(h) == 102);
348 mpdm_dump(h);
350 v=mpdm_hkeys(h);
351 mpdm_dump(v);
353 /* use of non-strings as hashes */
354 h = MPDM_H(0);
356 v = MPDM_A(0);
357 mpdm_hset(h, v, MPDM_I(1234));
358 v = MPDM_H(0);
359 mpdm_hset(h, v, MPDM_I(12345));
360 v = MPDM_H(0);
361 mpdm_hset(h, v, MPDM_I(9876));
362 v = MPDM_A(0);
363 mpdm_hset(h, v, MPDM_I(6543));
364 i = mpdm_ival(mpdm_hget(h, v));
366 mpdm_dump(h);
367 do_test("hash: using non-strings as hash keys", (i == 6543));
369 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
371 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
372 do_test("exists 2", ! mpdm_exists(h, MPDM_LS(L"notok")));
374 v = mpdm_hget_s(h, L"ok");
375 do_test("hget_s 1", mpdm_ival(v) == 666);
377 mpdm_hset_s(h, L"ok", MPDM_I(777));
379 v = mpdm_hget_s(h, L"ok");
380 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
384 void test_splice(void)
386 mpdm_t w;
387 mpdm_t v;
389 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"),
390 MPDM_LS(L"special "), 4, 0);
391 do_test("splice insertion",
392 mpdm_cmp(mpdm_aget(w, 0),
393 MPDM_LS(L"I'm special agent Johnson")) == 0);
394 mpdm_dump(w);
396 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
397 do_test("splice insertion and deletion (1)",
398 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
399 do_test("splice insertion and deletion (2)",
400 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
401 mpdm_dump(w);
403 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
404 do_test("splice deletion (1)",
405 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
406 do_test("splice deletion (2)",
407 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
408 mpdm_dump(w);
410 v = MPDM_LS(L"It doesn't matter");
411 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
412 do_test("splice insertion at the end",
413 mpdm_cmp(mpdm_aget(w, 0),
414 MPDM_LS(L"It doesn't matter two")) == 0);
415 mpdm_dump(w);
417 w = mpdm_splice(NULL, NULL, 0, 0);
418 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
420 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
421 do_test("splice with first value NULL",
422 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
424 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
425 do_test("splice with second value NULL",
426 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
428 v = MPDM_LS(L"I'm testing");
430 w = mpdm_splice(v, NULL, 0, -1);
431 do_test("splice with negative del (1)",
432 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
434 w = mpdm_splice(v, NULL, 4, -1);
435 do_test("splice with negative del (2)",
436 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
438 w = mpdm_splice(v, NULL, 4, -2);
439 do_test("splice with negative del (3)",
440 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
442 w = mpdm_splice(v, NULL, 0, -4);
443 do_test("splice with negative del (4)",
444 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
445 mpdm_dump(mpdm_aget(w, 0));
447 w = mpdm_splice(v, NULL, 4, -20);
448 do_test("splice with out-of-bounds negative del",
449 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
453 void test_strcat(void)
455 mpdm_t v;
456 mpdm_t w;
458 w = MPDM_LS(L"something");
460 v = mpdm_strcat(NULL, NULL);
461 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
463 v = mpdm_strcat(NULL, w);
464 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
466 v = mpdm_strcat(w, NULL);
467 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
469 w = MPDM_LS(L"");
470 v = mpdm_strcat(NULL, w);
471 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
473 v = mpdm_strcat(w, NULL);
474 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
476 v = mpdm_strcat(w, w);
477 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
482 void test_split(void)
484 mpdm_t w;
486 printf("mpdm_split test\n\n");
488 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"four.elems.in.string"));
489 mpdm_dump(w);
490 do_test("4 elems: ", (w->size == 4));
492 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"unseparated string"));
493 mpdm_dump(w);
494 do_test("1 elem: ", (w->size == 1));
496 w = mpdm_split(MPDM_S(L"."), MPDM_S(L".dot.at start"));
497 mpdm_dump(w);
498 do_test("3 elems: ", (w->size == 3));
500 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"dot.at end."));
501 mpdm_dump(w);
502 do_test("3 elems: ", (w->size == 3));
504 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"three...dots (two empty elements)"));
505 mpdm_dump(w);
506 do_test("4 elems: ", (w->size == 4));
508 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
509 mpdm_dump(w);
510 do_test("2 elems: ", (w->size == 2));
512 w = mpdm_split(NULL, MPDM_S(L"I am the man"));
513 do_test("NULL split 1: ", mpdm_size(w) == 12);
514 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
518 void test_join(void)
520 mpdm_t v;
521 mpdm_t s;
522 mpdm_t w;
524 printf("mpdm_join test\n\n");
526 /* separator */
527 s = MPDM_LS(L"--");
529 w = MPDM_A(1);
530 mpdm_aset(w, MPDM_S(L"ce"), 0);
532 v = mpdm_join(NULL, w);
533 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
535 v = mpdm_join(s, w);
536 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
538 mpdm_push(w, MPDM_LS(L"n'est"));
539 v = mpdm_join(s, w);
540 do_test("2 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
542 mpdm_push(w, MPDM_LS(L"pas"));
543 v = mpdm_join(s, w);
544 do_test("3 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
546 v = mpdm_join(NULL, w);
547 do_test("3 elems, no separator", (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
551 void test_sym(void)
553 mpdm_t v;
554 int i;
556 printf("mpdm_sset / mpdm_sget tests\n\n");
558 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
559 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
560 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
561 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
562 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"), MPDM_LS(L"profontwindows"));
563 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
564 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
565 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
566 mpdm_dump(mpdm_root());
568 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
569 i = mpdm_ival(v);
571 do_test("auto_indent == 16384", (i == 16384));
573 mpdm_sweep(-1);
577 void test_file(void)
579 mpdm_t f;
580 mpdm_t v;
581 mpdm_t eol = MPDM_LS(L"\n");
583 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
585 if(f == NULL)
587 printf("Can't create test.txt; no further file tests possible.\n");
588 return;
591 do_test("Create test.txt", f != NULL);
593 mpdm_write(f, MPDM_LS(L"0"));
594 mpdm_write(f, eol);
595 mpdm_write(f, MPDM_LS(L"1"));
596 mpdm_write(f, eol);
598 /* write WITHOUT eol */
599 mpdm_write(f, MPDM_LS(L"2"));
601 mpdm_close(f);
603 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
605 do_test("test written file 0", mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
606 do_test("test written file 1", mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
607 do_test("test written file 2", mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
608 do_test("test written file 3", mpdm_read(f) == NULL);
610 mpdm_close(f);
612 mpdm_unlink(MPDM_LS(L"test.txt"));
613 do_test("unlink", mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
615 v = mpdm_stat(MPDM_LS(L"stress.c"));
616 printf("Stat from stress.c:\n");
617 mpdm_dump(v);
619 /* v=mpdm_glob(MPDM_LS(L"*"));*/
620 printf("Glob:\n");
621 v = mpdm_glob(NULL);
622 mpdm_dump(v);
626 void test_regex(void)
628 mpdm_t v;
629 mpdm_t w;
631 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_LS(L"123456"), 0);
632 do_test("regex 0", v != NULL);
634 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_I(65536), 0);
635 do_test("regex 1", v != NULL);
637 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"12345678"), 0);
638 do_test("regex 2", v != NULL);
640 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_I(1), 0);
641 do_test("regex 3", v != NULL);
643 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"A12345-678"), 0);
644 do_test("regex 4", v == NULL);
646 w = MPDM_LS(L"Hell street, 666");
647 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), w, 0);
648 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
650 mpdm_dump(v);
652 v = mpdm_regex(MPDM_LS(L"/regex/"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
653 do_test("regex 6.1 (case sensitive)", v == NULL);
655 v = mpdm_regex(MPDM_LS(L"/regex/i"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
656 do_test("regex 6.2 (case insensitive)", v != NULL);
658 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
659 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
661 v = mpdm_regex(MPDM_LS(L"/^\\s*/"), MPDM_LS(L"123456"), 0);
662 do_test("regex 7", v != NULL);
664 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), MPDM_LS(L"123456"), 0);
665 do_test("regex 8", v == NULL);
667 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), NULL, 0);
668 do_test("regex 9 (NULL string to match)", v == NULL);
670 /* sregex */
672 v = mpdm_sregex(MPDM_LS(L"/A/"),MPDM_LS(L"change all A to A"),
673 MPDM_LS(L"E"),0);
674 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
676 v = mpdm_sregex(MPDM_LS(L"/A/g"),MPDM_LS(L"change all A to A"),
677 MPDM_LS(L"E"),0);
678 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
680 v = mpdm_sregex(MPDM_LS(L"/A+/g"),MPDM_LS(L"change all AAAAAA to E"),
681 MPDM_LS(L"E"),0);
682 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
684 v = mpdm_sregex(MPDM_LS(L"/A+/g"),MPDM_LS(L"change all A A A A A A to E"),
685 MPDM_LS(L"E"),0);
686 do_test("sregex 3", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
688 v = mpdm_sregex(MPDM_LS(L"/A+/g"),MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
689 MPDM_LS(L"E"),0);
690 do_test("sregex 3.2", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
692 v = mpdm_sregex(MPDM_LS(L"/[0-9]+/g"),MPDM_LS(L"1, 20, 333, 40 all are numbers"),
693 MPDM_LS(L"numbers"),0);
694 do_test("sregex 4", mpdm_cmp(v, MPDM_LS(L"numbers, numbers, numbers, numbers all are numbers")) == 0);
696 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z_]+/g"),MPDM_LS(L"regex, mpdm_regex, TexMex"),
697 MPDM_LS(L"sex"),0);
698 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
700 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z]+/g"),MPDM_LS(L"regex, mpdm_regex, TexMex"),
701 NULL,0);
702 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
704 v = mpdm_sregex(MPDM_LS(L"/\\\\/g"),MPDM_LS(L"\\MSDOS\\style\\path"),
705 MPDM_LS(L"/"),0);
706 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
708 v = mpdm_sregex(MPDM_LS(L"/regex/gi"),MPDM_LS(L"regex, Regex, REGEX"),
709 MPDM_LS(L"sex"),0);
710 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
712 v = mpdm_sregex(NULL, NULL, NULL, 0);
713 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
715 /* & in substitution tests */
716 v = MPDM_LS(L"this string has many words");
717 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[&]"), 0);
718 do_test("& in sregex target", mpdm_cmp(v, MPDM_LS(L"[this] [string] [has] [many] [words]")) == 0);
720 v = MPDM_LS(L"this string has many words");
721 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[\\&]"), 0);
722 do_test("escaped & in sregex target", mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 0);
724 /* multiple regex tests */
725 w = MPDM_A(0);
727 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
728 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
729 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
730 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
731 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
733 v = mpdm_regex(w, MPDM_LS(L"key=value"), 0);
734 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v, 1),MPDM_LS(L"key")) == 0);
735 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v, 3),MPDM_LS(L"value")) == 0);
737 v = mpdm_regex(w, MPDM_LS(L" key = value"), 0);
738 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v, 1),MPDM_LS(L"key")) == 0);
739 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v, 3),MPDM_LS(L"value")) == 0);
741 v = mpdm_regex(w, MPDM_LS(L"\t\tkey\t=\tvalue "), 0);
742 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v, 1),MPDM_LS(L"key")) == 0);
743 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v, 3),MPDM_LS(L"value")) == 0);
745 v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
746 do_test("multi-regex 4", v == NULL);
748 printf("Multiple line regexes\n");
749 w = MPDM_LS(L"/* this is\na C-like comment */");
750 v = mpdm_regex(MPDM_LS(L"|/\\*.+\\*/|"), w, 0);
751 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
753 v = mpdm_regex(MPDM_LS(L"/is$/"), w, 0);
754 do_test("Multiline regex 2", v == NULL);
756 v = mpdm_regex(MPDM_LS(L"/is$/m"), w, 0);
757 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
759 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
761 w = MPDM_LS(L"-\x03a9-");
763 v = mpdm_regex(MPDM_LS(L"/-$/"), w, 0);
764 do_test("Multibyte environment regex 1",
765 mpdm_cmp(v, MPDM_LS(L"-")) == 0);
767 if(do_multibyte_sregex_tests)
769 v = mpdm_sregex(MPDM_LS(L"/-$/"), w, MPDM_LS(L"~"), 0);
770 do_test("Multibyte environment sregex 1",
771 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
773 v = mpdm_sregex(MPDM_LS(L"/-/g"), w, MPDM_LS(L"~"), 0);
774 do_test("Multibyte environment sregex 2",
775 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
777 else
778 printf("Multibyte sregex test omitted; activate with -m\n");
780 /* 'last' flag tests */
781 v = MPDM_LS(L"this string has many words");
782 v = mpdm_regex(MPDM_LS(L"/[a-z]+/l"), v, 0);
783 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
787 static mpdm_t dumper(mpdm_t args)
788 /* executable value */
790 mpdm_dump(args);
791 return(NULL);
795 static mpdm_t sum(mpdm_t args)
796 /* executable value: sum all args */
798 int n,t = 0;
800 if(args != NULL)
802 for(n = t = 0;n < args->size;n++)
803 t += mpdm_ival(mpdm_aget(args, n));
806 return(MPDM_I(t));
810 static mpdm_t calculator(mpdm_t c, mpdm_t args)
811 /* 2 argument version: calculator. c contains a 'script' to
812 do things with the arguments */
814 int n,t;
815 mpdm_t v;
816 mpdm_t a;
817 mpdm_t o;
819 /* to avoid destroying args */
820 a = mpdm_clone(args);
822 /* unshift first argument */
823 v = mpdm_adel(a, 0);
824 t = mpdm_ival(v);
826 for(n = 0;n < mpdm_size(c);n++)
828 /* gets operator */
829 o = mpdm_aget(c, n);
831 /* gets next value */
832 v = mpdm_adel(a, 0);
834 switch(*(wchar_t *)o->data)
836 case '+': t += mpdm_ival(v); break;
837 case '-': t -= mpdm_ival(v); break;
838 case '*': t *= mpdm_ival(v); break;
839 case '/': t /= mpdm_ival(v); break;
843 return(MPDM_I(t));
847 void test_exec(void)
849 mpdm_t x;
850 mpdm_t w;
851 mpdm_t p;
853 printf("test_exec\n");
855 x = MPDM_X(dumper);
857 /* a simple value */
858 mpdm_exec(x, NULL);
859 mpdm_exec(x, x);
861 x = MPDM_X(sum);
862 w = MPDM_A(3);
863 mpdm_aset(w, MPDM_I(100), 0);
864 mpdm_aset(w, MPDM_I(220), 1);
865 mpdm_aset(w, MPDM_I(333), 2);
867 do_test("exec 0", mpdm_ival(mpdm_exec(x, w)) == 653);
869 mpdm_push(w, MPDM_I(1));
871 /* multiple executable value: vm and compiler support */
873 /* calculator 'script' */
874 p = MPDM_A(0);
875 mpdm_push(p, MPDM_LS(L"+"));
876 mpdm_push(p, MPDM_LS(L"-"));
877 mpdm_push(p, MPDM_LS(L"+"));
879 /* the value */
880 x = MPDM_A(2);
881 x->flags |= MPDM_EXEC;
883 mpdm_aset(x, MPDM_X(calculator), 0);
884 mpdm_aset(x, p, 1);
886 do_test("exec 1", mpdm_ival(mpdm_exec(x, w)) == -12);
888 /* another 'script', different operations with the same values */
889 p = MPDM_A(0);
890 mpdm_push(p, MPDM_LS(L"*"));
891 mpdm_push(p, MPDM_LS(L"/"));
892 mpdm_push(p, MPDM_LS(L"+"));
894 mpdm_aset(x, p, 1);
896 do_test("exec 2", mpdm_ival(mpdm_exec(x, w)) == 67);
900 void test_encoding(void)
902 mpdm_t f;
903 mpdm_t v;
904 mpdm_t w;
905 wchar_t * ptr;
907 v = MPDM_MBS("?Espa?a!\n");
909 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
911 if((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL)
913 printf("Can't write test.txt; no further file test possible.\n");
914 return;
917 mpdm_write(f, v);
918 mpdm_close(f);
920 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
921 w = mpdm_read(f);
922 mpdm_dump(w);
923 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
924 mpdm_close(f);
926 printf("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
928 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
929 w = mpdm_read(f);
930 mpdm_dump(w);
931 mpdm_close(f);
933 for(ptr = w->data;*ptr != L'\0';ptr++)
934 printf("%d", mpdm_wcwidth(*ptr));
935 printf("\n");
937 if(mpdm_encoding(MPDM_LS(L"UTF-8")) < 0)
939 printf("No multiple encoding (iconv) support; no more tests possible.\n");
940 return;
943 printf("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
945 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
946 w = mpdm_read(f);
947 mpdm_dump(w);
948 mpdm_close(f);
950 /* new open file will use the specified encoding */
951 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
952 mpdm_write(f, v);
953 mpdm_close(f);
955 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
956 w = mpdm_read(f);
957 mpdm_dump(w);
958 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
959 mpdm_close(f);
961 mpdm_encoding(NULL);
965 void test_gettext(void)
967 mpdm_t v;
968 mpdm_t h;
970 printf("\nTesting gettext...\n");
972 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
974 printf("Should follow a translated string of 'This is a test string':\n");
975 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
976 mpdm_dump(v);
978 printf("The same, but cached:\n");
979 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
980 mpdm_dump(v);
982 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
983 mpdm_dump(v);
985 printf("Ad-hoc translation hash:\n");
986 h = MPDM_H(0);
987 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
989 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
990 v = mpdm_gettext(MPDM_LS(L"test string"));
991 mpdm_dump(v);
995 void timer(int secs)
997 static clock_t clks = 0;
999 switch(secs)
1001 case 0:
1002 clks = clock();
1003 break;
1005 case -1:
1006 printf("%.2f seconds\n",
1007 (float)(clock() - clks)/(float)CLOCKS_PER_SEC);
1008 break;
1013 void bench_hash(int i, mpdm_t l, int buckets)
1015 mpdm_t h;
1016 mpdm_t v;
1017 int n;
1019 printf("Hash of %d buckets: \n", buckets);
1020 h = MPDM_H(buckets);
1022 timer(0);
1023 for(n = 0;n < i;n++)
1025 v = mpdm_aget(l, n);
1026 mpdm_hset(h, v, v);
1028 timer(-1);
1030 printf("Bucket usage:\n");
1031 for(n=0;n < mpdm_size(h);n++)
1032 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1034 mpdm_sweep(-1);
1038 void benchmark(void)
1040 mpdm_t l;
1041 int i, n;
1042 char tmp[64];
1044 printf("\n");
1046 if(!do_benchmarks)
1048 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1049 return;
1052 printf("BENCHMARKS\n");
1054 i = 500000;
1056 printf("Creating %d values...\n", i);
1058 l = mpdm_ref(MPDM_A(i));
1059 for(n = 0;n < i;n++)
1061 sprintf(tmp,"%08x",n);
1062 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1063 mpdm_aset(l, MPDM_I(n), n);
1066 printf("OK\n");
1068 bench_hash(i, l, 0);
1069 bench_hash(i, l, 61);
1070 bench_hash(i, l, 89);
1071 bench_hash(i, l, 127);
1073 mpdm_unref(l);
1077 void test_conversion(void)
1079 wchar_t * wptr = NULL;
1080 char * ptr = NULL;
1081 int size = 0;
1083 ptr = mpdm_wcstombs(L"", &size);
1084 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1086 wptr = mpdm_mbstowcs("", &size, 0);
1087 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1091 void test_pipes(void)
1093 mpdm_t f;
1095 printf("\n");
1097 if((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL)
1099 mpdm_t v;
1101 v = mpdm_read(f);
1102 mpdm_pclose(f);
1104 printf("Pipe from 'date':\n");
1105 mpdm_dump(v);
1107 else
1108 printf("Can't pipe to 'date'\n");
1112 void test_misc(void)
1114 printf("Home dir:\n");
1115 mpdm_dump(mpdm_home_dir());
1116 printf("App dir:\n");
1117 mpdm_dump(mpdm_app_dir());
1121 void test_sprintf(void)
1123 mpdm_t v;
1124 mpdm_t w;
1126 printf("sprintf tests\n");
1128 v = MPDM_A(0);
1129 mpdm_push(v, MPDM_I(100));
1130 mpdm_push(v, MPDM_LS(L"beers"));
1132 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1133 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1135 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1136 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me %d")) == 0);
1138 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1139 do_test("sprintf 3", mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1141 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1142 do_test("sprintf 4", mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1144 v = MPDM_A(0);
1145 mpdm_push(v, MPDM_R(3.1416));
1147 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1148 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1150 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1151 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1153 v = MPDM_A(0);
1154 mpdm_push(v, MPDM_LS(L"stress"));
1156 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1157 do_test("sprintf 3.1", mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1159 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1160 do_test("sprintf 3.2", mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1162 v = MPDM_A(0);
1163 mpdm_push(v, MPDM_I(0x263a));
1165 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1166 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1170 void test_ulc(void)
1172 mpdm_t v = MPDM_S(L"string");
1173 mpdm_t w = mpdm_ulc(v, 1);
1175 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1176 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1180 int main(int argc, char * argv[])
1182 if(argc > 1)
1184 if(strcmp(argv[1], "-b") == 0)
1185 do_benchmarks = 1;
1186 if(strcmp(argv[1], "-m") == 0)
1187 do_multibyte_sregex_tests = 1;
1190 mpdm_startup();
1192 test_basic();
1193 test_array();
1194 test_hash();
1195 test_splice();
1196 test_strcat();
1197 test_split();
1198 test_join();
1199 test_sym();
1200 test_file();
1201 test_regex();
1202 test_exec();
1203 test_encoding();
1204 test_gettext();
1205 test_conversion();
1206 test_pipes();
1207 test_misc();
1208 test_sprintf();
1209 test_ulc();
1211 benchmark();
1213 /* mpdm_dump_unref();*/
1215 printf("memory: %d\n", mpdm->memory_usage);
1216 mpdm_sweep(-1);
1217 mpdm_sweep(-1);
1218 mpdm_sweep(-1);
1219 mpdm_sweep(-1);
1220 mpdm_sweep(-1);
1221 mpdm_sweep(-1);
1222 mpdm_sweep(-1);
1223 mpdm_sweep(-1);
1224 mpdm_sweep(-1);
1225 printf("memory: %d\n", mpdm->memory_usage);
1227 /* mpdm_dump_unref();*/
1229 mpdm_shutdown();
1231 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1233 if(oks == tests)
1234 printf("*** ALL TESTS PASSED\n");
1235 else
1237 int n;
1239 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1241 printf("\nFailed tests:\n\n");
1242 for(n = 0;n < i_failed_msgs;n++)
1243 printf("%s", failed_msgs[n]);
1246 return(0);