Set CFLAGS to "-g -Wall" only if CC is gcc.
[mpdm.git] / stress.c
blobc54a8cece7af4b137f76fd9c0ea57514b98d6a0f
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);
167 mpdm_t sort_cb(mpdm_t args)
169 int d;
171 /* sorts reversely */
172 d = mpdm_cmp(mpdm_aget(args, 1), mpdm_aget(args, 0));
173 return(MPDM_I(d));
177 void test_array(void)
179 int n;
180 mpdm_t a;
181 mpdm_t v;
183 a = MPDM_A(0);
184 do_test("a->size == 0", (a->size == 0));
186 mpdm_push(a, MPDM_LS(L"sunday"));
187 mpdm_push(a, MPDM_LS(L"monday"));
188 mpdm_push(a, MPDM_LS(L"tuesday"));
189 mpdm_push(a, MPDM_LS(L"wednesday"));
190 mpdm_push(a, MPDM_LS(L"thursday"));
191 mpdm_push(a, MPDM_LS(L"friday"));
192 mpdm_push(a, MPDM_LS(L"saturday"));
193 mpdm_dump(a);
194 do_test("a->size == 7", (a->size == 7));
196 v = mpdm_aset(a, NULL, 3);
197 do_test("v->ref == 0", (v->ref == 0));
198 mpdm_dump(a);
200 a = mpdm_sort(a, 1);
201 do_test("NULLs are sorted on top", (mpdm_aget(a, 0) == NULL));
203 mpdm_aset(a, v, 0);
204 v = mpdm_aget(a, 3);
205 do_test("v is referenced again", (v != NULL && v->ref > 0));
207 a = mpdm_sort(a, 1);
208 do_test("mpdm_asort() works (1)",
209 mpdm_cmp(mpdm_aget(a,0), MPDM_LS(L"friday")) == 0);
210 do_test("mpdm_asort() works (2)",
211 mpdm_cmp(mpdm_aget(a,6), MPDM_LS(L"wednesday")) == 0);
213 /* asort_cb sorts reversely */
214 a = mpdm_sort_cb(a, 1, MPDM_X(sort_cb));
216 do_test("mpdm_asort_cb() works (1)",
217 mpdm_cmp(mpdm_aget(a,6), MPDM_LS(L"friday")) == 0);
218 do_test("mpdm_asort_cb() works (2)",
219 mpdm_cmp(mpdm_aget(a,0), MPDM_LS(L"wednesday")) == 0);
221 n = v->ref;
222 v = mpdm_aget(a, 3);
223 mpdm_collapse(a, 3, 1);
224 do_test("acollapse unrefs values", (v->ref < n));
226 /* test queues */
227 a = MPDM_A(0);
229 /* add several values */
230 for(n = 0;n < 10;n++)
231 v = mpdm_queue(a, MPDM_I(n), 10);
233 do_test("queue should still output NULL", (v == NULL));
235 v = mpdm_queue(a, MPDM_I(11), 10);
236 do_test("queue should no longer output NULL", (v != NULL));
238 v = mpdm_queue(a, MPDM_I(12), 10);
239 do_test("queue should return 1", mpdm_ival(v) == 1);
240 v = mpdm_queue(a, MPDM_I(13), 10);
241 do_test("queue should return 2", mpdm_ival(v) == 2);
242 do_test("queue size should be 10", a->size == 10);
244 mpdm_dump(a);
245 v = mpdm_queue(a, MPDM_I(14), 5);
246 mpdm_dump(a);
248 do_test("queue size should be 5", a->size == 5);
249 do_test("last taken value should be 8", mpdm_ival(v) == 8);
251 a = MPDM_A(4);
252 mpdm_aset(a, MPDM_I(666), 6000);
254 do_test("array should have been automatically expanded",
255 mpdm_size(a) == 6001);
257 v = mpdm_aget(a, -1);
258 do_test("negative offsets in arrays 1", mpdm_ival(v) == 666);
260 mpdm_aset(a, MPDM_I(777), -2);
261 v = mpdm_aget(a, 5999);
262 do_test("negative offsets in arrays 2", mpdm_ival(v) == 777);
264 mpdm_push(a, MPDM_I(888));
265 v = mpdm_aget(a, -1);
266 do_test("negative offsets in arrays 3", mpdm_ival(v) == 888);
268 v = MPDM_A(0);
269 mpdm_push(v, MPDM_I(100));
270 mpdm_pop(v);
272 /* array comparisons with mpdm_cmp() */
273 a = MPDM_A(2);
274 mpdm_aset(a, MPDM_I(10), 0);
275 mpdm_aset(a, MPDM_I(60), 1);
277 v = mpdm_clone(a);
278 do_test("mpdm_cmp: array clones are equal", mpdm_cmp(a, v) == 0);
280 mpdm_adel(v, -1);
281 do_test("mpdm_cmp: shorter arrays are lesser", mpdm_cmp(a, v) > 0);
283 mpdm_push(v, MPDM_I(80));
284 do_test("mpdm_cmp: 2# element is bigger, so array is bigger", mpdm_cmp(a, v) < 0);
288 void test_hash(void)
290 mpdm_t h;
291 mpdm_t v;
292 int i, n;
294 h = MPDM_H(0);
296 do_test("hsize 1", mpdm_hsize(h) == 0);
298 mpdm_hset(h, MPDM_S(L"mp"), MPDM_I(6));
299 v = mpdm_hget(h, MPDM_S(L"mp"));
301 do_test("hsize 2", mpdm_hsize(h) == 1);
303 do_test("hash: v != NULL", (v != NULL));
304 i = mpdm_ival(v);
305 do_test("hash: v == 6", (i == 6));
307 mpdm_hset(h, MPDM_S(L"mp2"), MPDM_I(66));
308 v = mpdm_hget(h, MPDM_S(L"mp2"));
310 do_test("hsize 3", mpdm_hsize(h) == 2);
312 do_test("hash: v != NULL", (v != NULL));
313 i = mpdm_ival(v);
314 do_test("hash: v == 66", (i == 66));
316 /* fills 100 values */
317 for(n = 0;n < 50;n++)
318 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
319 for(n = 100;n >= 50;n--)
320 mpdm_hset(h, MPDM_I(n), MPDM_I(n * 10));
322 do_test("hsize 4", mpdm_hsize(h) == 103);
324 /* tests 100 values */
325 for(n = 0;n < 100;n++)
327 v = mpdm_hget(h, MPDM_I(n));
329 if(v != NULL)
331 i = mpdm_ival(v);
332 if(!(i == n * 10))
333 do_test("hash: ival", (i == n * 10));
335 else
336 do_test("hash: hget", (v != NULL));
339 printf("h's size: %d\n", mpdm_hsize(h));
341 v = mpdm_hdel(h, MPDM_LS(L"mp"));
342 do_test("hdel", mpdm_ival(v) == 6);
343 do_test("hsize 5", mpdm_hsize(h) == 102);
346 mpdm_dump(h);
348 v=mpdm_hkeys(h);
349 mpdm_dump(v);
351 /* use of non-strings as hashes */
352 h = MPDM_H(0);
354 v = MPDM_A(0);
355 mpdm_hset(h, v, MPDM_I(1234));
356 v = MPDM_H(0);
357 mpdm_hset(h, v, MPDM_I(12345));
358 v = MPDM_H(0);
359 mpdm_hset(h, v, MPDM_I(9876));
360 v = MPDM_A(0);
361 mpdm_hset(h, v, MPDM_I(6543));
362 i = mpdm_ival(mpdm_hget(h, v));
364 mpdm_dump(h);
365 do_test("hash: using non-strings as hash keys", (i == 6543));
367 mpdm_hset(h, MPDM_LS(L"ok"), MPDM_I(666));
369 do_test("exists 1", mpdm_exists(h, MPDM_LS(L"ok")));
370 do_test("exists 2", ! mpdm_exists(h, MPDM_LS(L"notok")));
372 v = mpdm_hget_s(h, L"ok");
373 do_test("hget_s 1", mpdm_ival(v) == 666);
375 mpdm_hset_s(h, L"ok", MPDM_I(777));
377 v = mpdm_hget_s(h, L"ok");
378 do_test("hget_s + hset_s", mpdm_ival(v) == 777);
382 void test_splice(void)
384 mpdm_t w;
385 mpdm_t v;
387 w = mpdm_splice(MPDM_LS(L"I'm agent Johnson"),
388 MPDM_LS(L"special "), 4, 0);
389 do_test("splice insertion",
390 mpdm_cmp(mpdm_aget(w, 0),
391 MPDM_LS(L"I'm special agent Johnson")) == 0);
392 mpdm_dump(w);
394 w = mpdm_splice(MPDM_LS(L"Life is a shit"), MPDM_LS(L"cheat"), 10, 4);
395 do_test("splice insertion and deletion (1)",
396 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"Life is a cheat")) == 0);
397 do_test("splice insertion and deletion (2)",
398 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"shit")) == 0);
399 mpdm_dump(w);
401 w = mpdm_splice(MPDM_LS(L"I'm with dumb"), NULL, 4, 4);
402 do_test("splice deletion (1)",
403 mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm dumb")) == 0);
404 do_test("splice deletion (2)",
405 mpdm_cmp(mpdm_aget(w, 1), MPDM_LS(L"with")) == 0);
406 mpdm_dump(w);
408 v = MPDM_LS(L"It doesn't matter");
409 w = mpdm_splice(v, MPDM_LS(L" two"), v->size, 0);
410 do_test("splice insertion at the end",
411 mpdm_cmp(mpdm_aget(w, 0),
412 MPDM_LS(L"It doesn't matter two")) == 0);
413 mpdm_dump(w);
415 w = mpdm_splice(NULL, NULL, 0, 0);
416 do_test("splice with two NULLS", (mpdm_aget(w, 0) == NULL));
418 w = mpdm_splice(NULL, MPDM_LS(L"foo"), 0, 0);
419 do_test("splice with first value NULL",
420 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
422 w = mpdm_splice(MPDM_LS(L"foo"), NULL, 0, 0);
423 do_test("splice with second value NULL",
424 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"foo")) == 0));
426 v = MPDM_LS(L"I'm testing");
428 w = mpdm_splice(v, NULL, 0, -1);
429 do_test("splice with negative del (1)",
430 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"")) == 0));
432 w = mpdm_splice(v, NULL, 4, -1);
433 do_test("splice with negative del (2)",
434 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm ")) == 0));
436 w = mpdm_splice(v, NULL, 4, -2);
437 do_test("splice with negative del (3)",
438 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm g")) == 0));
440 w = mpdm_splice(v, NULL, 0, -4);
441 do_test("splice with negative del (4)",
442 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"ing")) == 0));
443 mpdm_dump(mpdm_aget(w, 0));
445 w = mpdm_splice(v, NULL, 4, -20);
446 do_test("splice with out-of-bounds negative del",
447 (mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I'm testing")) == 0));
451 void test_strcat(void)
453 mpdm_t v;
454 mpdm_t w;
456 w = MPDM_LS(L"something");
458 v = mpdm_strcat(NULL, NULL);
459 do_test("mpdm_strcat(NULL, NULL) returns NULL", v == NULL);
461 v = mpdm_strcat(NULL, w);
462 do_test("mpdm_strcat(NULL, w) returns w", mpdm_cmp(v, w) == 0);
464 v = mpdm_strcat(w, NULL);
465 do_test("mpdm_strcat(w, NULL) returns w", mpdm_cmp(v, w) == 0);
467 w = MPDM_LS(L"");
468 v = mpdm_strcat(NULL, w);
469 do_test("mpdm_strcat(NULL, \"\") returns \"\"", mpdm_cmp(v, w) == 0);
471 v = mpdm_strcat(w, NULL);
472 do_test("mpdm_strcat(\"\", NULL) returns \"\"", mpdm_cmp(v, w) == 0);
474 v = mpdm_strcat(w, w);
475 do_test("mpdm_strcat(\"\", \"\") returns \"\"", mpdm_cmp(v, w) == 0);
480 void test_split(void)
482 mpdm_t w;
484 printf("mpdm_split test\n\n");
486 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"four.elems.in.string"));
487 mpdm_dump(w);
488 do_test("4 elems: ", (w->size == 4));
490 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"unseparated string"));
491 mpdm_dump(w);
492 do_test("1 elem: ", (w->size == 1));
494 w = mpdm_split(MPDM_S(L"."), MPDM_S(L".dot.at start"));
495 mpdm_dump(w);
496 do_test("3 elems: ", (w->size == 3));
498 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"dot.at end."));
499 mpdm_dump(w);
500 do_test("3 elems: ", (w->size == 3));
502 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"three...dots (two empty elements)"));
503 mpdm_dump(w);
504 do_test("4 elems: ", (w->size == 4));
506 w = mpdm_split(MPDM_S(L"."), MPDM_S(L"."));
507 mpdm_dump(w);
508 do_test("2 elems: ", (w->size == 2));
510 w = mpdm_split(NULL, MPDM_S(L"I am the man"));
511 do_test("NULL split 1: ", mpdm_size(w) == 12);
512 do_test("NULL split 2: ", mpdm_cmp(mpdm_aget(w, 0), MPDM_LS(L"I")) == 0);
516 void test_join(void)
518 mpdm_t v;
519 mpdm_t s;
520 mpdm_t w;
522 printf("mpdm_join test\n\n");
524 /* separator */
525 s = MPDM_LS(L"--");
527 w = MPDM_A(1);
528 mpdm_aset(w, MPDM_S(L"ce"), 0);
530 v = mpdm_join(NULL, w);
531 do_test("1 elem, no separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
533 v = mpdm_join(s, w);
534 do_test("1 elem, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce")) == 0));
536 mpdm_push(w, MPDM_LS(L"n'est"));
537 v = mpdm_join(s, w);
538 do_test("2 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est")) == 0));
540 mpdm_push(w, MPDM_LS(L"pas"));
541 v = mpdm_join(s, w);
542 do_test("3 elems, '--' separator", (mpdm_cmp(v, MPDM_LS(L"ce--n'est--pas")) == 0));
544 v = mpdm_join(NULL, w);
545 do_test("3 elems, no separator", (mpdm_cmp(v, MPDM_LS(L"cen'estpas")) == 0));
549 void test_sym(void)
551 mpdm_t v;
552 int i;
554 printf("mpdm_sset / mpdm_sget tests\n\n");
556 mpdm_sset(NULL, MPDM_LS(L"mp"), MPDM_H(7));
557 mpdm_sset(NULL, MPDM_LS(L"mp.config"), MPDM_H(7));
558 mpdm_sset(NULL, MPDM_LS(L"mp.config.auto_indent"), MPDM_I(16384));
559 mpdm_sset(NULL, MPDM_LS(L"mp.config.use_regex"), MPDM_I(1357));
560 mpdm_sset(NULL, MPDM_LS(L"mp.config.gtk_font_face"), MPDM_LS(L"profontwindows"));
561 mpdm_sset(NULL, MPDM_LS(L"mp.lines"), MPDM_A(2));
562 mpdm_sset(NULL, MPDM_LS(L"mp.lines.0"), MPDM_LS(L"First post!"));
563 mpdm_sset(NULL, MPDM_LS(L"mp.lines.1"), MPDM_LS(L"Second post!"));
564 mpdm_dump(mpdm_root());
566 v = mpdm_sget(NULL, MPDM_LS(L"mp.config.auto_indent"));
567 i = mpdm_ival(v);
569 do_test("auto_indent == 16384", (i == 16384));
571 mpdm_sweep(-1);
575 void test_file(void)
577 mpdm_t f;
578 mpdm_t v;
579 mpdm_t eol = MPDM_LS(L"\n");
581 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
583 if(f == NULL)
585 printf("Can't create test.txt; no further file tests possible.\n");
586 return;
589 do_test("Create test.txt", f != NULL);
591 mpdm_write(f, MPDM_LS(L"0"));
592 mpdm_write(f, eol);
593 mpdm_write(f, MPDM_LS(L"1"));
594 mpdm_write(f, eol);
596 /* write WITHOUT eol */
597 mpdm_write(f, MPDM_LS(L"2"));
599 mpdm_close(f);
601 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
603 do_test("test written file 0", mpdm_cmp(mpdm_read(f), MPDM_LS(L"0\n")) == 0);
604 do_test("test written file 1", mpdm_cmp(mpdm_read(f), MPDM_LS(L"1\n")) == 0);
605 do_test("test written file 2", mpdm_cmp(mpdm_read(f), MPDM_LS(L"2")) == 0);
606 do_test("test written file 3", mpdm_read(f) == NULL);
608 mpdm_close(f);
610 mpdm_unlink(MPDM_LS(L"test.txt"));
611 do_test("unlink", mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r")) == NULL);
613 v = mpdm_stat(MPDM_LS(L"stress.c"));
614 printf("Stat from stress.c:\n");
615 mpdm_dump(v);
617 /* v=mpdm_glob(MPDM_LS(L"*"));*/
618 printf("Glob:\n");
619 v = mpdm_glob(NULL);
620 mpdm_dump(v);
624 void test_regex(void)
626 mpdm_t v;
627 mpdm_t w;
629 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_LS(L"123456"), 0);
630 do_test("regex 0", v != NULL);
632 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), MPDM_I(65536), 0);
633 do_test("regex 1", v != NULL);
635 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"12345678"), 0);
636 do_test("regex 2", v != NULL);
638 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_I(1), 0);
639 do_test("regex 3", v != NULL);
641 v = mpdm_regex(MPDM_LS(L"/^[0-9]+$/"), MPDM_LS(L"A12345-678"), 0);
642 do_test("regex 4", v == NULL);
644 w = MPDM_LS(L"Hell street, 666");
645 v = mpdm_regex(MPDM_LS(L"/[0-9]+/"), w, 0);
646 do_test("regex 5", mpdm_cmp(v, MPDM_I(666)) == 0);
648 mpdm_dump(v);
650 v = mpdm_regex(MPDM_LS(L"/regex/"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
651 do_test("regex 6.1 (case sensitive)", v == NULL);
653 v = mpdm_regex(MPDM_LS(L"/regex/i"), MPDM_LS(L"CASE-INSENSITIVE REGEX"), 0);
654 do_test("regex 6.2 (case insensitive)", v != NULL);
656 v=mpdm_regex(MPDM_LS(L"/[A-Z]+/"), MPDM_LS(L"case SENSITIVE regex"), 0);
657 do_test("regex 6.3 (case sensitive)", mpdm_cmp(v, MPDM_LS(L"SENSITIVE")) == 0);
659 v = mpdm_regex(MPDM_LS(L"/^\\s*/"), MPDM_LS(L"123456"), 0);
660 do_test("regex 7", v != NULL);
662 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), MPDM_LS(L"123456"), 0);
663 do_test("regex 8", v == NULL);
665 v = mpdm_regex(MPDM_LS(L"/^\\s+/"), NULL, 0);
666 do_test("regex 9 (NULL string to match)", v == NULL);
668 /* sregex */
670 v = mpdm_sregex(MPDM_LS(L"/A/"),MPDM_LS(L"change all A to A"),
671 MPDM_LS(L"E"),0);
672 do_test("sregex 0", mpdm_cmp(v, MPDM_LS(L"change all E to A")) == 0);
674 v = mpdm_sregex(MPDM_LS(L"/A/g"),MPDM_LS(L"change all A to A"),
675 MPDM_LS(L"E"),0);
676 do_test("sregex 1", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
678 v = mpdm_sregex(MPDM_LS(L"/A+/g"),MPDM_LS(L"change all AAAAAA to E"),
679 MPDM_LS(L"E"),0);
680 do_test("sregex 2", mpdm_cmp(v, MPDM_LS(L"change all E to E")) == 0);
682 v = mpdm_sregex(MPDM_LS(L"/A+/g"),MPDM_LS(L"change all A A A A A A to E"),
683 MPDM_LS(L"E"),0);
684 do_test("sregex 3", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
686 v = mpdm_sregex(MPDM_LS(L"/A+/g"),MPDM_LS(L"change all AAA A AA AAAAA A AAA to E"),
687 MPDM_LS(L"E"),0);
688 do_test("sregex 3.2", mpdm_cmp(v, MPDM_LS(L"change all E E E E E E to E")) == 0);
690 v = mpdm_sregex(MPDM_LS(L"/[0-9]+/g"),MPDM_LS(L"1, 20, 333, 40 all are numbers"),
691 MPDM_LS(L"numbers"),0);
692 do_test("sregex 4", mpdm_cmp(v, MPDM_LS(L"numbers, numbers, numbers, numbers all are numbers")) == 0);
694 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z_]+/g"),MPDM_LS(L"regex, mpdm_regex, TexMex"),
695 MPDM_LS(L"sex"),0);
696 do_test("sregex 5", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
698 v = mpdm_sregex(MPDM_LS(L"/[a-zA-Z]+/g"),MPDM_LS(L"regex, mpdm_regex, TexMex"),
699 NULL,0);
700 do_test("sregex 6", mpdm_cmp(v, MPDM_LS(L", _, ")) == 0);
702 v = mpdm_sregex(MPDM_LS(L"/\\\\/g"),MPDM_LS(L"\\MSDOS\\style\\path"),
703 MPDM_LS(L"/"),0);
704 do_test("sregex 7", mpdm_cmp(v, MPDM_LS(L"/MSDOS/style/path")) == 0);
706 v = mpdm_sregex(MPDM_LS(L"/regex/gi"),MPDM_LS(L"regex, Regex, REGEX"),
707 MPDM_LS(L"sex"),0);
708 do_test("sregex 8", mpdm_cmp(v, MPDM_LS(L"sex, sex, sex")) == 0);
710 v = mpdm_sregex(NULL, NULL, NULL, 0);
711 do_test("Previous sregex substitutions must be 3", mpdm_ival(v) == 3);
713 /* & in substitution tests */
714 v = MPDM_LS(L"this string has many words");
715 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[&]"), 0);
716 do_test("& in sregex target", mpdm_cmp(v, MPDM_LS(L"[this] [string] [has] [many] [words]")) == 0);
718 v = MPDM_LS(L"this string has many words");
719 v = mpdm_sregex(MPDM_LS(L"/[a-z]+/g"), v, MPDM_LS(L"[\\&]"), 0);
720 do_test("escaped & in sregex target", mpdm_cmp(v, MPDM_LS(L"[&] [&] [&] [&] [&]")) == 0);
722 /* multiple regex tests */
723 w = MPDM_A(0);
725 mpdm_push(w, MPDM_LS(L"/^[ \t]*/"));
726 mpdm_push(w, MPDM_LS(L"/[^ \t=]+/"));
727 mpdm_push(w, MPDM_LS(L"/[ \t]*=[ \t]*/"));
728 mpdm_push(w, MPDM_LS(L"/[^ \t]+/"));
729 mpdm_push(w, MPDM_LS(L"/[ \t]*$/"));
731 v = mpdm_regex(w, MPDM_LS(L"key=value"), 0);
732 do_test("multi-regex 1.1", mpdm_cmp(mpdm_aget(v, 1),MPDM_LS(L"key")) == 0);
733 do_test("multi-regex 1.2", mpdm_cmp(mpdm_aget(v, 3),MPDM_LS(L"value")) == 0);
735 v = mpdm_regex(w, MPDM_LS(L" key = value"), 0);
736 do_test("multi-regex 2.1", mpdm_cmp(mpdm_aget(v, 1),MPDM_LS(L"key")) == 0);
737 do_test("multi-regex 2.2", mpdm_cmp(mpdm_aget(v, 3),MPDM_LS(L"value")) == 0);
739 v = mpdm_regex(w, MPDM_LS(L"\t\tkey\t=\tvalue "), 0);
740 do_test("multi-regex 3.1", mpdm_cmp(mpdm_aget(v, 1),MPDM_LS(L"key")) == 0);
741 do_test("multi-regex 3.2", mpdm_cmp(mpdm_aget(v, 3),MPDM_LS(L"value")) == 0);
743 v = mpdm_regex(w, MPDM_LS(L"key= "), 0);
744 do_test("multi-regex 4", v == NULL);
746 printf("Multiple line regexes\n");
747 w = MPDM_LS(L"/* this is\na C-like comment */");
748 v = mpdm_regex(MPDM_LS(L"|/\\*.+\\*/|"), w, 0);
749 do_test("Multiline regex 1", mpdm_cmp(v, w) == 0);
751 v = mpdm_regex(MPDM_LS(L"/is$/"), w, 0);
752 do_test("Multiline regex 2", v == NULL);
754 v = mpdm_regex(MPDM_LS(L"/is$/m"), w, 0);
755 do_test("Multiline regex 3", mpdm_cmp(v, MPDM_LS(L"is")) == 0);
757 printf("Pitfalls on multibyte locales (f.e. utf-8)\n");
759 w = MPDM_LS(L"-\x03a9-");
761 v = mpdm_regex(MPDM_LS(L"/-$/"), w, 0);
762 do_test("Multibyte environment regex 1",
763 mpdm_cmp(v, MPDM_LS(L"-")) == 0);
765 if(do_multibyte_sregex_tests)
767 v = mpdm_sregex(MPDM_LS(L"/-$/"), w, MPDM_LS(L"~"), 0);
768 do_test("Multibyte environment sregex 1",
769 mpdm_cmp(v, MPDM_LS(L"-\x03a9~")) == 0);
771 v = mpdm_sregex(MPDM_LS(L"/-/g"), w, MPDM_LS(L"~"), 0);
772 do_test("Multibyte environment sregex 2",
773 mpdm_cmp(v, MPDM_LS(L"~\x03a9~")) == 0);
775 else
776 printf("Multibyte sregex test omitted; activate with -m\n");
778 /* 'last' flag tests */
779 v = MPDM_LS(L"this string has many words");
780 v = mpdm_regex(MPDM_LS(L"/[a-z]+/l"), v, 0);
781 do_test("Flag l in mpdm_regex", mpdm_cmp(v, MPDM_LS(L"words")) == 0);
785 static mpdm_t dumper(mpdm_t args)
786 /* executable value */
788 mpdm_dump(args);
789 return(NULL);
793 static mpdm_t sum(mpdm_t args)
794 /* executable value: sum all args */
796 int n,t = 0;
798 if(args != NULL)
800 for(n = t = 0;n < args->size;n++)
801 t += mpdm_ival(mpdm_aget(args, n));
804 return(MPDM_I(t));
808 static mpdm_t calculator(mpdm_t c, mpdm_t args)
809 /* 2 argument version: calculator. c contains a 'script' to
810 do things with the arguments */
812 int n,t;
813 mpdm_t v;
814 mpdm_t a;
815 mpdm_t o;
817 /* to avoid destroying args */
818 a = mpdm_clone(args);
820 /* unshift first argument */
821 v = mpdm_adel(a, 0);
822 t = mpdm_ival(v);
824 for(n = 0;n < mpdm_size(c);n++)
826 /* gets operator */
827 o = mpdm_aget(c, n);
829 /* gets next value */
830 v = mpdm_adel(a, 0);
832 switch(*(wchar_t *)o->data)
834 case '+': t += mpdm_ival(v); break;
835 case '-': t -= mpdm_ival(v); break;
836 case '*': t *= mpdm_ival(v); break;
837 case '/': t /= mpdm_ival(v); break;
841 return(MPDM_I(t));
845 void test_exec(void)
847 mpdm_t x;
848 mpdm_t w;
849 mpdm_t p;
851 printf("test_exec\n");
853 x = MPDM_X(dumper);
855 /* a simple value */
856 mpdm_exec(x, NULL);
857 mpdm_exec(x, x);
859 x = MPDM_X(sum);
860 w = MPDM_A(3);
861 mpdm_aset(w, MPDM_I(100), 0);
862 mpdm_aset(w, MPDM_I(220), 1);
863 mpdm_aset(w, MPDM_I(333), 2);
865 do_test("exec 0", mpdm_ival(mpdm_exec(x, w)) == 653);
867 mpdm_push(w, MPDM_I(1));
869 /* multiple executable value: vm and compiler support */
871 /* calculator 'script' */
872 p = MPDM_A(0);
873 mpdm_push(p, MPDM_LS(L"+"));
874 mpdm_push(p, MPDM_LS(L"-"));
875 mpdm_push(p, MPDM_LS(L"+"));
877 /* the value */
878 x = MPDM_A(2);
879 x->flags |= MPDM_EXEC;
881 mpdm_aset(x, MPDM_X(calculator), 0);
882 mpdm_aset(x, p, 1);
884 do_test("exec 1", mpdm_ival(mpdm_exec(x, w)) == -12);
886 /* another 'script', different operations with the same values */
887 p = MPDM_A(0);
888 mpdm_push(p, MPDM_LS(L"*"));
889 mpdm_push(p, MPDM_LS(L"/"));
890 mpdm_push(p, MPDM_LS(L"+"));
892 mpdm_aset(x, p, 1);
894 do_test("exec 2", mpdm_ival(mpdm_exec(x, w)) == 67);
898 void test_encoding(void)
900 mpdm_t f;
901 mpdm_t v;
902 mpdm_t w;
903 wchar_t * ptr;
905 v = MPDM_MBS("?Espa?a!\n");
907 printf("\nLocale encoding tests (will look bad if terminal is not ISO-8859-1)\n\n");
909 if((f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"))) == NULL)
911 printf("Can't write test.txt; no further file test possible.\n");
912 return;
915 mpdm_write(f, v);
916 mpdm_close(f);
918 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
919 w = mpdm_read(f);
920 mpdm_dump(w);
921 do_test("Locale encoding", mpdm_cmp(w, v) == 0);
922 mpdm_close(f);
924 printf("\nutf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
926 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
927 w = mpdm_read(f);
928 mpdm_dump(w);
929 mpdm_close(f);
931 for(ptr = w->data;*ptr != L'\0';ptr++)
932 printf("%d", mpdm_wcwidth(*ptr));
933 printf("\n");
935 if(mpdm_encoding(MPDM_LS(L"UTF-8")) < 0)
937 printf("No multiple encoding (iconv) support; no more tests possible.\n");
938 return;
941 printf("\nForced utf8.txt loading (should look good only in UTF-8 terminals with good fonts)\n");
943 f = mpdm_open(MPDM_LS(L"utf8.txt"), MPDM_LS(L"r"));
944 w = mpdm_read(f);
945 mpdm_dump(w);
946 mpdm_close(f);
948 /* new open file will use the specified encoding */
949 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"w"));
950 mpdm_write(f, v);
951 mpdm_close(f);
953 f = mpdm_open(MPDM_LS(L"test.txt"), MPDM_LS(L"r"));
954 w = mpdm_read(f);
955 mpdm_dump(w);
956 do_test("iconv encoding", mpdm_cmp(w, v) == 0);
957 mpdm_close(f);
959 mpdm_encoding(NULL);
963 void test_gettext(void)
965 mpdm_t v;
966 mpdm_t h;
968 printf("\nTesting gettext...\n");
970 mpdm_gettext_domain(MPDM_LS(L"stress"), MPDM_LS(L"./po"));
972 printf("Should follow a translated string of 'This is a test string':\n");
973 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
974 mpdm_dump(v);
976 printf("The same, but cached:\n");
977 v = mpdm_gettext(MPDM_LS(L"This is a test string"));
978 mpdm_dump(v);
980 v = mpdm_gettext(MPDM_LS(L"This string is not translated"));
981 mpdm_dump(v);
983 printf("Ad-hoc translation hash:\n");
984 h = MPDM_H(0);
985 mpdm_hset(h, MPDM_LS(L"test string"), MPDM_LS(L"cadena de prueba"));
987 mpdm_gettext_domain(MPDM_LS(L"stress"), h);
988 v = mpdm_gettext(MPDM_LS(L"test string"));
989 mpdm_dump(v);
993 void timer(int secs)
995 static clock_t clks = 0;
997 switch(secs)
999 case 0:
1000 clks = clock();
1001 break;
1003 case -1:
1004 printf("%.2f seconds\n",
1005 (float)(clock() - clks)/(float)CLOCKS_PER_SEC);
1006 break;
1011 void bench_hash(int i, mpdm_t l, int buckets)
1013 mpdm_t h;
1014 mpdm_t v;
1015 int n;
1017 printf("Hash of %d buckets: \n", buckets);
1018 h = MPDM_H(buckets);
1020 timer(0);
1021 for(n = 0;n < i;n++)
1023 v = mpdm_aget(l, n);
1024 mpdm_hset(h, v, v);
1026 timer(-1);
1028 printf("Bucket usage:\n");
1029 for(n=0;n < mpdm_size(h);n++)
1030 printf("\t%d: %d\n", n, mpdm_size(mpdm_aget(h, n)));
1032 mpdm_sweep(-1);
1036 void benchmark(void)
1038 mpdm_t l;
1039 int i, n;
1040 char tmp[64];
1042 printf("\n");
1044 if(!do_benchmarks)
1046 printf("Skipping benchmarks\nRun them with 'stress -b'\n");
1047 return;
1050 printf("BENCHMARKS\n");
1052 i = 500000;
1054 printf("Creating %d values...\n", i);
1056 l = mpdm_ref(MPDM_A(i));
1057 for(n = 0;n < i;n++)
1059 sprintf(tmp,"%08x",n);
1060 /* mpdm_aset(l, MPDM_MBS(tmp), n);*/
1061 mpdm_aset(l, MPDM_I(n), n);
1064 printf("OK\n");
1066 bench_hash(i, l, 0);
1067 bench_hash(i, l, 61);
1068 bench_hash(i, l, 89);
1069 bench_hash(i, l, 127);
1071 mpdm_unref(l);
1075 void test_conversion(void)
1077 wchar_t * wptr = NULL;
1078 char * ptr = NULL;
1079 int size = 0;
1081 ptr = mpdm_wcstombs(L"", &size);
1082 do_test("mpdm_wcstombs converts an empty string", ptr != NULL);
1084 wptr = mpdm_mbstowcs("", &size, 0);
1085 do_test("mpdm_mbstowcs converts an empty string", wptr != NULL);
1089 void test_pipes(void)
1091 mpdm_t f;
1093 printf("\n");
1095 if((f = mpdm_popen(MPDM_LS(L"date"), MPDM_LS(L"r"))) != NULL)
1097 mpdm_t v;
1099 v = mpdm_read(f);
1100 mpdm_pclose(f);
1102 printf("Pipe from 'date':\n");
1103 mpdm_dump(v);
1105 else
1106 printf("Can't pipe to 'date'\n");
1110 void test_misc(void)
1112 printf("Home dir:\n");
1113 mpdm_dump(mpdm_home_dir());
1114 printf("App dir:\n");
1115 mpdm_dump(mpdm_app_dir());
1119 void test_sprintf(void)
1121 mpdm_t v;
1122 mpdm_t w;
1124 printf("sprintf tests\n");
1126 v = MPDM_A(0);
1127 mpdm_push(v, MPDM_I(100));
1128 mpdm_push(v, MPDM_LS(L"beers"));
1130 w = mpdm_sprintf(MPDM_LS(L"%d %s for me"), v);
1131 do_test("sprintf 1", mpdm_cmp(w, MPDM_LS(L"100 beers for me")) == 0);
1133 w = mpdm_sprintf(MPDM_LS(L"%d %s for me %d"), v);
1134 do_test("sprintf 2", mpdm_cmp(w, MPDM_LS(L"100 beers for me %d")) == 0);
1136 w = mpdm_sprintf(MPDM_LS(L"%10d %s for me"), v);
1137 do_test("sprintf 3", mpdm_cmp(w, MPDM_LS(L" 100 beers for me")) == 0);
1139 w = mpdm_sprintf(MPDM_LS(L"%010d %s for me"), v);
1140 do_test("sprintf 4", mpdm_cmp(w, MPDM_LS(L"0000000100 beers for me")) == 0);
1142 v = MPDM_A(0);
1143 mpdm_push(v, MPDM_R(3.1416));
1145 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %6.4f"), v);
1146 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 3.1416")) == 0);
1148 w = mpdm_sprintf(MPDM_LS(L"Value for PI is %08.2f"), v);
1149 do_test("sprintf 2.1", mpdm_cmp(w, MPDM_LS(L"Value for PI is 00003.14")) == 0);
1151 v = MPDM_A(0);
1152 mpdm_push(v, MPDM_LS(L"stress"));
1154 w = mpdm_sprintf(MPDM_LS(L"This is a |%10s| test"), v);
1155 do_test("sprintf 3.1", mpdm_cmp(w, MPDM_LS(L"This is a | stress| test")) == 0);
1157 w = mpdm_sprintf(MPDM_LS(L"This is a |%-10s| test"), v);
1158 do_test("sprintf 3.2", mpdm_cmp(w, MPDM_LS(L"This is a |stress | test")) == 0);
1160 v = MPDM_A(0);
1161 mpdm_push(v, MPDM_I(0x263a));
1163 w = mpdm_sprintf(MPDM_LS(L"%c"), v);
1164 do_test("sprintf 3.3", mpdm_cmp(w, MPDM_LS(L"\x263a")) == 0);
1168 void test_ulc(void)
1170 mpdm_t v = MPDM_S(L"string");
1171 mpdm_t w = mpdm_ulc(v, 1);
1173 do_test("mpdm_ulc 1", mpdm_cmp(mpdm_ulc(v, 1), w) == 0);
1174 do_test("mpdm_ulc 2", mpdm_cmp(mpdm_ulc(w, 0), v) == 0);
1178 int main(int argc, char * argv[])
1180 if(argc > 1)
1182 if(strcmp(argv[1], "-b") == 0)
1183 do_benchmarks = 1;
1184 if(strcmp(argv[1], "-m") == 0)
1185 do_multibyte_sregex_tests = 1;
1188 mpdm_startup();
1190 test_basic();
1191 test_array();
1192 test_hash();
1193 test_splice();
1194 test_strcat();
1195 test_split();
1196 test_join();
1197 test_sym();
1198 test_file();
1199 test_regex();
1200 test_exec();
1201 test_encoding();
1202 test_gettext();
1203 test_conversion();
1204 test_pipes();
1205 test_misc();
1206 test_sprintf();
1207 test_ulc();
1209 benchmark();
1211 /* mpdm_dump_unref();*/
1213 printf("memory: %d\n", mpdm->memory_usage);
1214 mpdm_sweep(-1);
1215 mpdm_sweep(-1);
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 printf("memory: %d\n", mpdm->memory_usage);
1225 /* mpdm_dump_unref();*/
1227 mpdm_shutdown();
1229 printf("\n*** Total tests passed: %d/%d\n", oks, tests);
1231 if(oks == tests)
1232 printf("*** ALL TESTS PASSED\n");
1233 else
1235 int n;
1237 printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
1239 printf("\nFailed tests:\n\n");
1240 for(n = 0;n < i_failed_msgs;n++)
1241 printf("%s", failed_msgs[n]);
1244 return(0);