1 /*
2  * Copyright (C) 1997-2004, Michael Jennings
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies of the Software, its documentation and marketing & publicity
13  * materials, and acknowledgment shall be given in the documentation, materials
14  * and software packages that this Software was used.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 static const char cvs_ident[] = "$Id: test.c,v 1.47 2004/12/15 00:00:22 mej Exp $";
25 
26 #if defined(HAVE_CONFIG_H) && (HAVE_CONFIG_H != 0)
27 # include <config.h>
28 #endif
29 
30 #include <libast.h>
31 #include "test.h"
32 
33 unsigned short tnum = 0;
34 
35 int test_macros(void);
36 int test_mem(void);
37 int test_strings(void);
38 int test_hash_functions(void);
39 int test_snprintf(void);
40 int test_options(void);
41 int test_obj(void);
42 int test_str(void);
43 int test_tok(void);
44 int test_url(void);
45 int test_list(void);
46 int test_vector(void);
47 int test_map(void);
48 int test_socket(void);
49 int test_regexp(void);
50 
51 int
test_macros(void)52 test_macros(void)
53 {
54     spif_char_t memset_test[] = "abcdefghijklmnopqrstuvwxyz";
55     spif_char_t sc1 = 'X', sc2 = 'K';
56     int si1 = 472, si2 = 8786345;
57     unsigned long sl1 = 0x98765432, sl2 = 0xffeeddff;
58     void *vp1 = &sc1, *vp2 = &sc2;
59 
60     TEST_BEGIN("MEMSET() macro");
61     MEMSET(memset_test, '!', CONST_STRLEN(memset_test));
62     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(memset_test), "!!!!!!!!!!!!!!!!!!!!!!!!!!"));
63     MEMSET(memset_test + 3, '*', 14);
64     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(memset_test), "!!!**************!!!!!!!!!"));
65     MEMSET(memset_test, '&', 0 );
66     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(memset_test), "!!!**************!!!!!!!!!"));
67     MEMSET(SPIF_NULL_TYPE(charptr), '_', CONST_STRLEN(memset_test));
68     TEST_PASS();
69 
70     TEST_BEGIN("SWAP() macro");
71     SWAP(sc1, sc2);
72     SWAP(si1, si2);
73     SWAP(sl1, sl2);
74     SWAP(vp1, vp2);
75     TEST_FAIL_IF(sc1 != 'K');
76     TEST_FAIL_IF(sc2 != 'X');
77     TEST_FAIL_IF(si1 != 8786345);
78     TEST_FAIL_IF(si2 != 472);
79     TEST_FAIL_IF(sl1 != 0xffeeddff);
80     TEST_FAIL_IF(sl2 != 0x98765432);
81     TEST_FAIL_IF(vp1 != &sc2);
82     TEST_FAIL_IF(vp2 != &sc1);
83     TEST_PASS();
84 
85     TEST_BEGIN("BINSWAP() macro");
86     BINSWAP(sc1, sc2);
87     BINSWAP(si1, si2);
88     BINSWAP(sl1, sl2);
89     BINSWAP(vp1, vp2);
90     TEST_FAIL_IF(sc1 != 'X');
91     TEST_FAIL_IF(sc2 != 'K');
92     TEST_FAIL_IF(si1 != 472);
93     TEST_FAIL_IF(si2 != 8786345);
94     TEST_FAIL_IF(sl1 != 0x98765432);
95     TEST_FAIL_IF(sl2 != 0xffeeddff);
96     TEST_FAIL_IF(vp1 != &sc1);
97     TEST_FAIL_IF(vp2 != &sc2);
98     TEST_PASS();
99 
100     TEST_BEGIN("BEG_STRCASECMP() macro");
101     TEST_FAIL_IF(!BEG_STRCASECMP("this", "this is a test"));
102     TEST_FAIL_IF(!BEG_STRCASECMP("thx", "this is another test"));
103     TEST_FAIL_IF(BEG_STRCASECMP("this is still another test", "this is"));
104     TEST_PASS();
105 
106     TEST_PASSED("macro");
107 }
108 
109 int
test_mem(void)110 test_mem(void)
111 {
112     spifmem_init();
113 
114     return 0;
115 }
116 
117 int
test_strings(void)118 test_strings(void)
119 {
120     spif_charptr_t s1, s2, s3, s4;
121 #if HAVE_REGEX_H
122     regex_t *r = NULL;
123 #endif
124     spif_charptr_t *slist;
125 
126     TEST_BEGIN("spiftool_safe_strncpy() function");
127     s1 = MALLOC(20);
128     TEST_FAIL_IF(spiftool_safe_strncpy(s1, SPIF_CHARPTR("pneumonoultramicroscopicsilicovolcanoconiosis"), 20));
129     TEST_FAIL_IF(strncmp(SPIF_CHARPTR_C(s1), "pneumonoultramicros", 20));
130     TEST_FAIL_IF(!spiftool_safe_strncpy(s1, SPIF_CHARPTR("abc"), 20));
131     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(s1), "abc"));
132     TEST_FAIL_IF(!spiftool_safe_strncpy(s1, SPIF_CHARPTR(""), 20));
133     TEST_FAIL_IF(*s1);
134     TEST_FAIL_IF(!spiftool_safe_strncpy(s1, SPIF_CHARPTR("0123456789012345678"), 20));
135     TEST_FAIL_IF(strncmp(SPIF_CHARPTR_C(s1), "0123456789012345678", 20));
136     FREE(s1);
137     TEST_PASS();
138 
139     TEST_BEGIN("spiftool_safe_strncat() function");
140     s1 = MALLOC(20);
141     *s1 = 0;
142     TEST_FAIL_IF(spiftool_safe_strncat(s1, SPIF_CHARPTR("pneumonoultramicroscopicsilicovolcanoconiosis"), 20));
143     TEST_FAIL_IF(strncmp(SPIF_CHARPTR_C(s1), "pneumonoultramicros", 20));
144     TEST_FAIL_IF(!spiftool_safe_strncpy(s1, SPIF_CHARPTR("abc"), 20));
145     TEST_FAIL_IF(!spiftool_safe_strncat(s1, SPIF_CHARPTR("defg"), 20));
146     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(s1), "abcdefg"));
147     TEST_FAIL_IF(!spiftool_safe_strncat(s1, SPIF_CHARPTR(""), 20));
148     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(s1), "abcdefg"));
149     TEST_FAIL_IF(!spiftool_safe_strncpy(s1, SPIF_CHARPTR("0123456789"), 20));
150     TEST_FAIL_IF(!spiftool_safe_strncat(s1, SPIF_CHARPTR("012345678"), 20));
151     TEST_FAIL_IF(strncmp(SPIF_CHARPTR_C(s1), "0123456789012345678", 20));
152     FREE(s1);
153     TEST_PASS();
154 
155     TEST_BEGIN("spiftool_substr() function");
156     s1 = spiftool_substr(SPIF_CHARPTR("pneumonoultramicroscopicsilicovolcanoconiosis"), 8, 16);
157     s2 = spiftool_substr(SPIF_CHARPTR("abc"), 7, 5);
158     s3 = spiftool_substr(NULL, 0, 0);
159     s4 = spiftool_substr(SPIF_CHARPTR("what the heck"), -5, 42);
160     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(s1), "ultramicroscopic"));
161     TEST_FAIL_IF(s2 != NULL);
162     TEST_FAIL_IF(s3 != NULL);
163     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(s4), " heck"));
164     FREE(s1);
165     FREE(s2);
166     FREE(s3);
167     FREE(s4);
168     TEST_PASS();
169 
170 #if HAVE_REGEX_H
171     TEST_BEGIN("spiftool_regexp_match() function");
172     TEST_FAIL_IF(!spiftool_regexp_match(SPIF_CHARPTR("One particular string"), SPIF_CHARPTR("part")));
173     TEST_FAIL_IF(spiftool_regexp_match(SPIF_CHARPTR("Some other strange string"), SPIF_CHARPTR("^[A-Za-z]+$")));
174     TEST_FAIL_IF(!spiftool_regexp_match(SPIF_CHARPTR("some-rpm-package-1.0.1-4.src.rpm"), SPIF_CHARPTR("^(.*)-([^-]+)-([^-])\\.([a-z0-9]+)\\.rpm$")));
175     TEST_FAIL_IF(spiftool_regexp_match(SPIF_CHARPTR("/the/path/to/some/odd/file.txt"), SPIF_CHARPTR("/this/should/not/match")));
176     TEST_FAIL_IF(!spiftool_regexp_match(SPIF_CHARPTR("1600x1200"), SPIF_CHARPTR("[[:digit:]]+x[[:digit:]]+")));
177     TEST_FAIL_IF(spiftool_regexp_match(SPIF_CHARPTR("xxx"), NULL));
178     spiftool_regexp_match(NULL, NULL);
179     TEST_FAIL_IF(!spiftool_regexp_match_r(SPIF_CHARPTR("AbCdEfGhIjKlMnOpQrStUvWxYz"), SPIF_CHARPTR("[[:upper:]]"), &r));
180     TEST_FAIL_IF(spiftool_regexp_match_r(SPIF_CHARPTR("abcdefjhijklmnopqrstuvwxyz"), NULL, &r));
181     TEST_FAIL_IF(!spiftool_regexp_match_r(SPIF_CHARPTR("aaaaa"), SPIF_CHARPTR("[[:lower:]]"), &r));
182     FREE(r);
183     TEST_PASS();
184 #endif
185 
186     TEST_BEGIN("spiftool_split() function");
187     slist = spiftool_split(SPIF_CHARPTR(" "), SPIF_CHARPTR("Splitting a string on spaces"));
188     TEST_FAIL_IF(!slist);
189     TEST_FAIL_IF(!slist[0] || !slist[1] || !slist[2] || !slist[3] || !slist[4] || slist[5]);
190     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[0]), "Splitting"));
191     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[1]), "a"));
192     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[2]), "string"));
193     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[3]), "on"));
194     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[4]), "spaces"));
195     spiftool_free_array(SPIF_CAST(ptr) slist, 5);
196 
197     slist = spiftool_split(NULL, SPIF_CHARPTR("          a\t \ta        a a a a       a     "));
198     TEST_FAIL_IF(!slist);
199     TEST_FAIL_IF(!slist[0] || !slist[1] || !slist[2] || !slist[3] || !slist[4] || !slist[5] || !slist[6] || slist[7]);
200     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[0]), "a"));
201     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[1]), "a"));
202     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[2]), "a"));
203     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[3]), "a"));
204     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[4]), "a"));
205     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[5]), "a"));
206     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[6]), "a"));
207     spiftool_free_array(SPIF_CAST(ptr) slist, 7);
208 
209     slist = spiftool_split(NULL, SPIF_CHARPTR("  first \"just the second\" third \'fourth and \'\"fifth to\"gether last"));
210     TEST_FAIL_IF(!slist);
211     TEST_FAIL_IF(!slist[0] || !slist[1] || !slist[2] || !slist[3] || !slist[4] || slist[5]);
212     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[0]), "first"));
213     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[1]), "just the second"));
214     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[2]), "third"));
215     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[3]), "fourth and fifth together"));
216     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[4]), "last"));
217     spiftool_free_array(SPIF_CAST(ptr) slist, 5);
218 
219     slist = spiftool_split(NULL, SPIF_CHARPTR("\'don\\\'t\' try this    at home \"\" "));
220     TEST_FAIL_IF(!slist);
221     TEST_FAIL_IF(!slist[0] || !slist[1] || !slist[2] || !slist[3] || !slist[4] || !slist[5] || slist[6]);
222     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[0]), "don\'t"));
223     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[1]), "try"));
224     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[2]), "this"));
225     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[3]), "at"));
226     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[4]), "home"));
227     TEST_FAIL_IF(slist[5][0]);
228     spiftool_free_array(SPIF_CAST(ptr) slist, 6);
229 
230     slist = spiftool_split(SPIF_CHARPTR(":"), SPIF_CHARPTR("A:B:C:D:::E"));
231     TEST_FAIL_IF(!slist);
232     TEST_FAIL_IF(!slist[0] || !slist[1] || !slist[2] || !slist[3] || !slist[4] || slist[5]);
233     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[0]), "A"));
234     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[1]), "B"));
235     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[2]), "C"));
236     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[3]), "D"));
237     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(slist[4]), "E"));
238     spiftool_free_array(SPIF_CAST(ptr) slist, 5);
239     TEST_PASS();
240 
241     TEST_BEGIN("spiftool_version_compare() function");
242     TEST_FAIL_IF(!SPIF_CMP_IS_LESS(spiftool_version_compare(SPIF_CHARPTR("1.0"), SPIF_CHARPTR("1.0.1"))));
243     TEST_FAIL_IF(!SPIF_CMP_IS_LESS(spiftool_version_compare(SPIF_CHARPTR("2.9.99"), SPIF_CHARPTR("3.0"))));
244     TEST_FAIL_IF(!SPIF_CMP_IS_LESS(spiftool_version_compare(SPIF_CHARPTR("3.0"), SPIF_CHARPTR("29.9.9"))));
245     TEST_FAIL_IF(!SPIF_CMP_IS_LESS(spiftool_version_compare(SPIF_CHARPTR("1.0pre2"), SPIF_CHARPTR("1.0"))));
246     TEST_FAIL_IF(!SPIF_CMP_IS_LESS(spiftool_version_compare(SPIF_CHARPTR("9.9"), SPIF_CHARPTR("9.9rc1"))));
247     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("0.5.3"), SPIF_CHARPTR("0.5.3snap4"))));
248     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("2.2.4"), SPIF_CHARPTR("2.2.4beta3"))));
249     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("2.2.4beta3"), SPIF_CHARPTR("2.2.4alpha7"))));
250     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("1.27.3"), SPIF_CHARPTR("1.13.1"))));
251     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("0.10"), SPIF_CHARPTR("0.9.2"))));
252     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("2.3.2a"), SPIF_CHARPTR("2.3.2"))));
253     TEST_FAIL_IF(!SPIF_CMP_IS_GREATER(spiftool_version_compare(SPIF_CHARPTR("4.0p1"), SPIF_CHARPTR("4.0"))));
254     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spiftool_version_compare(SPIF_CHARPTR("3.4.5"), SPIF_CHARPTR("3.4.5"))));
255     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spiftool_version_compare(SPIF_CHARPTR("1.2.0b3"), SPIF_CHARPTR("1.2.0b3"))));
256     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spiftool_version_compare(SPIF_CHARPTR("2.0alpha"), SPIF_CHARPTR("2.0alpha"))));
257     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spiftool_version_compare(SPIF_CHARPTR("5.4pre1"), SPIF_CHARPTR("5.4pre1"))));
258     TEST_PASS();
259 
260     TEST_PASSED("string");
261     return 0;
262 }
263 
264 #define MAXPAIR 80
265 #define MAXLEN 80
266 int
test_hash_functions(void)267 test_hash_functions(void)
268 {
269     spif_uint8_t i;
270 
271     for (i = 0; i < 6; i++) {
272         spifhash_func_t hash_func;
273         spif_uint32_t key_length, input_byte, trials, successes = 0, hash, ref_hash;
274         spif_uint8_t buff[MAXLEN + 20], *pbuff;
275         spif_uint8_t align1[] = "This has got the amazing aroma of bovine fecal matter...";
276         spif_uint8_t align2[] = "xThis has got the amazing aroma of bovine fecal matter...";
277         spif_uint8_t align3[] = "xxThis has got the amazing aroma of bovine fecal matter...";
278         spif_uint8_t align4[] = "xxxThis has got the amazing aroma of bovine fecal matter...";
279         spif_uint32_t j;
280 
281         if (i == 0) {
282             TEST_NOTICE("*** Testing Jenkins hash:");
283             hash_func = spifhash_jenkins;
284         } else if (i == 1) {
285             TEST_NOTICE("*** Testing Jenkins32 hash:");
286             hash_func = spifhash_jenkins32;
287         } else if (i == 2) {
288 #if WORDS_BIGENDIAN
289             continue;
290 #else
291             TEST_NOTICE("*** Testing JenkinsLE hash:");
292             hash_func = spifhash_jenkinsLE;
293 #endif
294         } else if (i == 3) {
295             TEST_NOTICE("*** Testing rotating hash:");
296             hash_func = spifhash_rotating;
297         } else if (i == 4) {
298             TEST_NOTICE("*** Testing one-at-a-time hash:");
299             hash_func = spifhash_one_at_a_time;
300         } else if (i == 5) {
301             TEST_NOTICE("*** Testing FNV hash:");
302             hash_func = spifhash_fnv;
303         }
304 
305         TEST_BEGIN("effect of every input bit on every output bit");
306         for (key_length = 0; key_length < MAXLEN; key_length++) {
307             /* For each key length up to 70 bytes... */
308 
309             if ((hash_func == spifhash_jenkins32)
310                 && (key_length % 4)) {
311                 successes++;
312                 continue;
313             }
314             trials = 0;
315             for (input_byte = 0; input_byte < key_length; input_byte++) {
316                 /* ...for each input byte... */
317                 spif_uint32_t input_bit;
318 
319                 for (input_bit = 0; input_bit < 8; input_bit++) {
320                     /* ...for each input bit... */
321                     spif_uint32_t seed;
322 
323                     for (seed = 1; seed < 8; seed++) {
324                         /* ...use several possible seeds... */
325                         spif_uint32_t e, f, g, h, x, y;
326                         spif_uint32_t bit_pair;
327 
328                         /* Initialize to ~0 (0xffffffff). */
329                         e = f = g = h = x = y = ~(SPIF_CAST(uint32) 0);
330 
331                         /* ...to make sure every output bit is affected by every input bit. */
332                         for (bit_pair = 0; bit_pair < MAXPAIR; bit_pair += 2) {
333                             spif_uint8_t buff1[MAXLEN + 1], buff2[MAXLEN + 2];
334                             spif_uint8_t *pbuff1 = &buff1[0], *pbuff2 = &buff2[1];
335                             spif_uint32_t hash1, hash2;
336 
337                             for (j = 0; j < key_length + 1; j++) {
338                                 /* Initialize keys to all zeros. */
339                                 pbuff1[j] = pbuff2[j] = (spif_uint8_t) 0;
340                             }
341 
342                             /* Then make them differ by exactly one bit, the input_bit.
343                                bit_pair will always end in 0, so bit_pair + 1 will always
344                                end in 1.  It's then shifted by input_bit to test the
345                                current bit to test all 8 of the lowest bits in sequence. */
346                             pbuff1[input_byte] ^= (bit_pair << input_bit);
347                             pbuff1[input_byte] ^= (bit_pair >> (8 - input_bit));
348                             pbuff2[input_byte] ^= ((bit_pair + 1) << input_bit);
349                             pbuff2[input_byte] ^= ((bit_pair + 1) >> (8 - input_bit));
350 
351                             /* Hash them. */
352                             if (hash_func == spifhash_jenkins32) {
353                                 hash1 = hash_func(pbuff1, key_length / 4, seed);
354                                 hash2 = hash_func(pbuff2, key_length / 4, seed);
355                             } else {
356                                 hash1 = hash_func(pbuff1, key_length, seed);
357                                 hash2 = hash_func(pbuff2, key_length, seed);
358                             }
359 
360                             /* Make sure every bit is 1 or 0 at least once. */
361                             e &= (hash1 ^ hash2);     f &= ~(hash1 ^ hash2);
362                             g &= hash1;               h &= ~hash1;
363                             x &= hash2;               y &= ~hash2;
364                             if (!(e | f | g | h | x | y)) {
365                                 /* They're all 0.  That means they've all changed at least once. */
366                                 break;
367                             }
368                         }
369                         if (bit_pair > trials) {
370                             trials = bit_pair;
371                         }
372                         if (bit_pair == MAXPAIR) {
373 #if UNUSED_BLOCK
374                             printf("Some bit didn't change: ");
375                             printf("%.8lx %.8lx %.8lx %.8lx %.8lx %.8lx  ",
376                                    SPIF_CAST_C(unsigned long) e,
377                                    SPIF_CAST_C(unsigned long) f,
378                                    SPIF_CAST_C(unsigned long) g,
379                                    SPIF_CAST_C(unsigned long) h,
380                                    SPIF_CAST_C(unsigned long) x,
381                                    SPIF_CAST_C(unsigned long) y);
382                             printf("input_byte %lu  input_bit %lu  seed %lu  key length %lu\n",
383                                    SPIF_CAST_C(unsigned long) input_byte,
384                                    SPIF_CAST_C(unsigned long) input_bit,
385                                    SPIF_CAST_C(unsigned long) seed,
386                                    SPIF_CAST_C(unsigned long) key_length);
387 #endif
388                         }
389                         if (trials == MAXPAIR) {
390                             /* Easy way to break out of a crapload of for loops. */
391                             goto done;
392                         }
393                     }
394                 }
395             }
396         done:
397             if (trials < MAXPAIR) {
398                 successes++;
399 #if UNUSED_BLOCK
400                 printf("Mix success:  %2lu-byte key required %2lu trials (%lu so far).\n",
401                        SPIF_CAST_C(unsigned long) input_byte,
402                        SPIF_CAST_C(unsigned long) trials / 2,
403                        SPIF_CAST_C(unsigned long) successes);
404 #endif
405             }
406         }
407         printf("%.2f%% mix success rate in %d key lengths...",
408                (100.0 * successes / key_length), key_length);
409         TEST_FAIL_IF(successes == 0);
410         TEST_PASS();
411 
412         /* Make sure nothing but the key is hashed, regardless of alignment. */
413         TEST_BEGIN("endian cleanliness");
414         key_length = CONST_STRLEN(align1);
415         if (hash_func == spifhash_jenkins32) {
416             if (key_length % 4) {
417                 TEST_FAIL_IF(key_length);
418             } else {
419                 key_length /= 4;
420             }
421         }
422         ref_hash = hash_func(align1, key_length, 0);
423         hash = hash_func(align2 + 1, key_length, 0);
424         /*printf("Reference hash 0x%08x, hash 0x%08x for length %lu\n", ref_hash, hash, key_length);*/
425         TEST_FAIL_IF(hash != ref_hash);
426         hash = hash_func(align3 + 2, key_length, 0);
427         TEST_FAIL_IF(hash != ref_hash);
428         hash = hash_func(align4 + 3, key_length, 0);
429         TEST_FAIL_IF(hash != ref_hash);
430 
431         for (j = 0, pbuff = buff + 1; j < 8; j++, pbuff++) {
432             for (key_length = 0; key_length < MAXLEN; key_length++) {
433                 if ((hash_func == spifhash_jenkins32)
434                     && (key_length % 4)) {
435                     continue;
436                 }
437                 MEMSET(buff, 0, sizeof(buff));
438 
439                 if (hash_func == spifhash_jenkins32) {
440                     ref_hash = hash_func(pbuff, key_length / 4, 1);
441                 } else {
442                     ref_hash = hash_func(pbuff, key_length, 1);
443                 }
444                 *(pbuff + key_length) = ~(SPIF_CAST(uint8) 0);
445                 *(pbuff - 1) = ~(SPIF_CAST(uint8) 0);
446                 if (hash_func == spifhash_jenkins32) {
447                     hash = hash_func(pbuff, key_length / 4, 1);
448                 } else {
449                     hash = hash_func(pbuff, key_length, 1);
450                 }
451                 /*printf("Reference hash 0x%08x, hash 0x%08x for length %lu\n", ref_hash, hash, key_length);*/
452                 TEST_FAIL_IF(hash != ref_hash);
453             }
454         }
455         TEST_PASS();
456 
457         /* We cannot test the rotating hash or the FNV hash here.  The
458            rotating hash repeats after 4 zero-length keys.  The FNV
459            hash generates constant hash values for zero-length keys. */
460         if ((hash_func != spifhash_rotating)
461             && (hash_func != spifhash_fnv)) {
462             spif_uint32_t null_hashes[8];
463             spif_uint8_t one_byte;
464 
465             TEST_BEGIN("hashes of empty strings");
466             one_byte = ~0;
467             for (j = 0, hash = 0; j < 8; j++) {
468                 spif_uint32_t k;
469 
470                 hash = hash_func(&one_byte, SPIF_CAST(uint32) 0, hash);
471                 null_hashes[j] = hash;
472                 /*printf("Empty string hash %lu is 0x%08x\n", j, hash);*/
473                 for (k = j - 1; k < 8; k--) {
474                     TEST_FAIL_IF(null_hashes[j] == null_hashes[k]);
475                 }
476             }
477             TEST_PASS();
478         }
479     }
480 
481     TEST_PASSED("hash functions");
482     return 0;
483 }
484 
485 int
test_snprintf(void)486 test_snprintf(void)
487 {
488 
489     return 0;
490 }
491 
492 spif_charptr_t theme = NULL;
handle_theme(spif_charptr_t val_ptr)493 static void handle_theme(spif_charptr_t val_ptr) {theme = STRDUP(val_ptr);}
494 
495 int
test_options(void)496 test_options(void)
497 {
498     spif_uint32_t test_flag_var = 0;
499     spif_charptr_t file_var = NULL, *exec_list = NULL;
500     long num_var = 0, geom_var = 0;
501     char *argv1[] = {
502         "test", "-abvf", "somefile", "-n1", "-g", "3",
503         "-e", "help", "me", "rhonda", NULL
504     };
505     int argc1 = 10;
506     spifopt_t opts1[] = {
507         SPIFOPT_BOOL('a', "agony", "scream in agony", test_flag_var, 0x01),
508         SPIFOPT_BOOL('b', "bogus", "mark as bogus", test_flag_var, 0x02),
509         SPIFOPT_BOOL('c', "crap", "spew some random crap", test_flag_var, 0x04),
510         SPIFOPT_BOOL_LONG("dillhole", "are you a dillhole?", test_flag_var, 0x08),
511         SPIFOPT_ARGS('e', "exec", "what to exec", exec_list),
512         SPIFOPT_STR('f', "file", "set the filename", file_var),
513         SPIFOPT_INT('g', "geom", "geometry", geom_var),
514         SPIFOPT_INT('n', "num", "number", num_var),
515         SPIFOPT_BOOL_PP('v', "verbose", "be verbose", test_flag_var, 0x10)
516     };
517     spif_charptr_t display = NULL, name = NULL, *exec = NULL, *foo = NULL;
518     long color = 0;
519     spif_uint32_t options = 0;
520     char *argv2[] = {
521         "test", "-rt", "mytheme", "--name", "This is a name", "--exec=ssh foo@bar.com",
522         "--scrollbar", "--buttonbar", "no", "some", "--login=0", "-mvd", "foo:0", "--color", "4",
523         "stuff", "--foo", "blah", "-d", "eatme", NULL
524     };
525     int argc2 = 20;
526     spifopt_t opts2[] = {
527         SPIFOPT_STR_PP('d', "display", "X display to connect to", display),
528         SPIFOPT_ARGS_PP('e', "exec", "command to run", exec),
529         SPIFOPT_BOOL_PP('l', "login", "login shell", options, 0x01),
530         SPIFOPT_BOOL('m', "map-alert", "raise window on beep", options, 0x02),
531         SPIFOPT_STR('n', "name", "name", name),
532         SPIFOPT_BOOL('r', "reverse-video", "enable reverse video", options, 0x04),
533         SPIFOPT_ABST_PP('t', "theme", "theme to use", handle_theme),
534         SPIFOPT_BOOL_PP('v', "visual-bell", "enable visual bell", options, 0x08),
535         SPIFOPT_BOOL_LONG("scrollbar", "enable scrollbar", options, 0x10),
536         SPIFOPT_BOOL_LONG("buttonbar", "enable buttonbar", options, 0x20),
537         SPIFOPT_INT_LONG("color", "pick a color", color),
538         SPIFOPT_ARGS_LONG("foo", "foo", foo)
539     };
540 
541     TEST_BEGIN("spifopt_parse() function");
542     SPIFOPT_OPTLIST_SET(opts1);
543     SPIFOPT_NUMOPTS_SET(sizeof(opts1) / sizeof(spifopt_t));
544     SPIFOPT_ALLOWBAD_SET(0);
545     SPIFOPT_FLAGS_SET(SPIFOPT_SETTING_PREPARSE);
546     spifopt_parse(argc1, argv1);
547     TEST_FAIL_IF(test_flag_var != 0x10);
548     TEST_FAIL_IF(file_var != NULL);
549     TEST_FAIL_IF(exec_list != NULL);
550     TEST_FAIL_IF(num_var != 0);
551     TEST_FAIL_IF(geom_var != 0);
552     spifopt_parse(argc1, argv1);
553     TEST_FAIL_IF(test_flag_var != 0x13);
554     TEST_FAIL_IF(file_var == NULL);
555     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(file_var), "somefile"));
556     TEST_FAIL_IF(exec_list == NULL);
557     TEST_FAIL_IF(num_var != 1);
558     TEST_FAIL_IF(geom_var != 3);
559     FREE(file_var);
560     spiftool_free_array(exec_list, -1);
561 
562     SPIFOPT_OPTLIST_SET(opts2);
563     SPIFOPT_NUMOPTS_SET(sizeof(opts2) / sizeof(spifopt_t));
564     SPIFOPT_ALLOWBAD_SET(0);
565     SPIFOPT_FLAGS_SET(SPIFOPT_SETTING_PREPARSE);
566     spifopt_parse(argc2, argv2);
567     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(display), "foo:0"));
568     TEST_FAIL_IF(name != NULL);
569     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(theme), "mytheme"));
570     TEST_FAIL_IF(exec == NULL);
571     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(exec[0]), "ssh"));
572     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(exec[1]), "foo@bar.com"));
573     TEST_FAIL_IF(exec[2] != NULL);
574     TEST_FAIL_IF(foo != NULL);
575     TEST_FAIL_IF(color != 0);
576     TEST_FAIL_IF(options != 0x08);
577     spifopt_parse(argc2, argv2);
578     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(display), "foo:0"));
579     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(name), "This is a name"));
580     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(theme), "mytheme"));
581     TEST_FAIL_IF(exec == NULL);
582     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(exec[0]), "ssh"));
583     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(exec[1]), "foo@bar.com"));
584     TEST_FAIL_IF(exec[2] != NULL);
585     TEST_FAIL_IF(foo == NULL);
586     TEST_FAIL_IF(SPIF_PTR_ISNULL(foo[0]));
587     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(foo[0]), "blah"));
588     TEST_FAIL_IF(SPIF_PTR_ISNULL(foo[1]));
589     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(foo[1]), "-d"));
590     TEST_FAIL_IF(SPIF_PTR_ISNULL(foo[2]));
591     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(foo[2]), "eatme"));
592     TEST_FAIL_IF(foo[3] != NULL);
593     TEST_FAIL_IF(color != 4);
594     TEST_FAIL_IF(options != 0x1e);
595     TEST_FAIL_IF(strcmp(argv2[0], "test"));
596     TEST_FAIL_IF(strcmp(argv2[1], "some"));
597     TEST_FAIL_IF(strcmp(argv2[2], "stuff"));
598     TEST_FAIL_IF(!SPIF_PTR_ISNULL(argv2[3]));
599     FREE(display);
600     FREE(name);
601     FREE(theme);
602     spiftool_free_array(exec, -1);
603     spiftool_free_array(foo, -1);
604 
605     TEST_PASS();
606 
607     TEST_PASSED("options");
608     return 0;
609 }
610 
611 int
test_obj(void)612 test_obj(void)
613 {
614     spif_obj_t testobj;
615     spif_class_t cls;
616 
617     TEST_BEGIN("spif_obj_new() function");
618     testobj = spif_obj_new();
619     TEST_FAIL_IF(SPIF_OBJ_ISNULL(testobj));
620     TEST_PASS();
621 
622     TEST_BEGIN("spif_obj_get_classname() function");
623     cls = spif_obj_get_class(testobj);
624     TEST_FAIL_IF(cls != SPIF_CLASS_VAR(obj));
625     TEST_PASS();
626 
627     TEST_BEGIN("spif_obj_del() function");
628     TEST_FAIL_IF(spif_obj_del(testobj) != TRUE);
629     TEST_PASS();
630 
631     TEST_PASSED("spif_obj_t");
632     return 0;
633 }
634 
635 int
test_str(void)636 test_str(void)
637 {
638     spif_str_t teststr, test2str;
639     spif_strclass_t cls;
640     char tmp[] = "this is a test";
641     spif_char_t buff[4096] = "abcde";
642     char tmp2[] = "string #1\nstring #2";
643     FILE *fp;
644     int fd, mypipe[2];
645     spif_charptr_t foo;
646 
647     TEST_BEGIN("spif_str_new() function");
648     teststr = spif_str_new();
649     TEST_FAIL_IF(SPIF_STR_ISNULL(teststr));
650     TEST_PASS();
651 
652     TEST_BEGIN("spif_obj_get_classname() function");
653     cls = SPIF_STR_CLASS(spif_obj_get_class(SPIF_OBJ(teststr)));
654     TEST_FAIL_IF(cls != SPIF_STR_CLASS(SPIF_STRCLASS_VAR(str)));
655     TEST_PASS();
656 
657     TEST_BEGIN("spif_str_del() function");
658     TEST_FAIL_IF(spif_str_del(teststr) != TRUE);
659     TEST_PASS();
660 
661     TEST_BEGIN("spif_str_new_from_ptr() function");
662     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp));
663     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR(tmp)));
664     TEST_FAIL_IF(spif_str_get_size(teststr) != sizeof(tmp));
665     TEST_FAIL_IF(spif_str_get_len(teststr) != (sizeof(tmp) - 1));
666     spif_str_del(teststr);
667     TEST_PASS();
668 
669     TEST_BEGIN("spif_str_new_from_buff() function");
670     teststr = spif_str_new_from_buff(buff, sizeof(buff));
671     TEST_FAIL_IF(spif_str_casecmp_with_ptr(teststr, buff));
672     TEST_FAIL_IF(spif_str_get_size(teststr) != sizeof(buff));
673     TEST_FAIL_IF(spif_str_get_len(teststr) != 5);
674     spif_str_del(teststr);
675     TEST_PASS();
676 
677     TEST_BEGIN("spif_str_new_from_fp() function");
678     pipe(mypipe);
679     fd = mypipe[0];
680     fp = fdopen(fd, "r");
681     write(mypipe[1], tmp2, sizeof(tmp2));
682     close(mypipe[1]);
683     TEST_FAIL_IF(fp == NULL);
684     teststr = spif_str_new_from_fp(fp);
685     TEST_FAIL_IF(spif_str_ncmp_with_ptr(teststr, SPIF_CHARPTR("string #1"), 9));
686     TEST_FAIL_IF(spif_str_get_len(teststr) != 9);
687     TEST_FAIL_IF(spif_str_get_size(teststr) != 10);
688     spif_str_del(teststr);
689     teststr = spif_str_new_from_fp(fp);
690     TEST_FAIL_IF(spif_str_ncasecmp_with_ptr(teststr, SPIF_CHARPTR("string #2"), 9));
691     TEST_FAIL_IF(spif_str_get_len(teststr) != 9);
692     TEST_FAIL_IF(spif_str_get_size(teststr) != 10);
693     spif_str_del(teststr);
694     fclose(fp);
695     TEST_PASS();
696 
697     TEST_BEGIN("spif_str_new_from_fd() function");
698     pipe(mypipe);
699     fd = mypipe[0];
700     write(mypipe[1], tmp2, sizeof(tmp2) - 1);
701     close(mypipe[1]);
702     teststr = spif_str_new_from_fd(fd);
703     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(SPIF_STR_STR(teststr)), tmp2));
704     TEST_FAIL_IF(spif_str_get_len(teststr) != (sizeof(tmp2) - 1));
705     TEST_FAIL_IF(spif_str_get_size(teststr) != sizeof(tmp2));
706     spif_str_del(teststr);
707     close(fd);
708     TEST_PASS();
709 
710     TEST_BEGIN("spif_str_new_from_num() function");
711     teststr = spif_str_new_from_num(1234567890L);
712     TEST_FAIL_IF(SPIF_STR_ISNULL(teststr));
713     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("1234567890"))));
714     spif_str_done(teststr);
715     spif_str_init_from_num(teststr, 2147483647L);
716     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("2147483647"))));
717     spif_str_done(teststr);
718     spif_str_init_from_num(teststr, -2147483647L);
719     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("-2147483647"))));
720     spif_str_done(teststr);
721     spif_str_init_from_num(teststr, 0x00000000);
722     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("0"))));
723     spif_str_del(teststr);
724     TEST_PASS();
725 
726     TEST_BEGIN("spif_str_dup() function");
727     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp));
728     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(SPIF_STR_STR(teststr)), tmp));
729     TEST_FAIL_IF(spif_str_get_size(teststr) != sizeof(tmp));
730     TEST_FAIL_IF(spif_str_get_len(teststr) != (sizeof(tmp) - 1));
731     test2str = spif_str_dup(teststr);
732     TEST_FAIL_IF(test2str == teststr);
733     TEST_FAIL_IF(SPIF_STR_STR(test2str) == SPIF_STR_STR(teststr));
734     TEST_FAIL_IF(spif_str_cmp(teststr, test2str));
735     TEST_FAIL_IF(spif_str_casecmp(teststr, test2str));
736     TEST_FAIL_IF(spif_str_ncmp(teststr, test2str, spif_str_get_len(teststr)));
737     TEST_FAIL_IF(spif_str_ncasecmp(teststr, test2str, spif_str_get_len(test2str)));
738     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(SPIF_STR_STR(test2str)), tmp));
739     TEST_FAIL_IF(spif_str_get_size(test2str) != sizeof(tmp));
740     TEST_FAIL_IF(spif_str_get_len(test2str) != (sizeof(tmp) - 1));
741     spif_str_del(teststr);
742     spif_str_del(test2str);
743     TEST_PASS();
744 
745     TEST_BEGIN("spif_str_index() function");
746     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp2));
747     TEST_FAIL_IF(spif_str_index(teststr, '#') != 7);
748     TEST_PASS();
749 
750     TEST_BEGIN("spif_str_rindex() function");
751     TEST_FAIL_IF(spif_str_rindex(teststr, '#') != 17);
752     TEST_PASS();
753 
754     TEST_BEGIN("spif_str_find() function");
755     test2str = spif_str_new_from_ptr(SPIF_CHARPTR("ring"));
756     TEST_FAIL_IF(spif_str_find(teststr, test2str) != 2);
757     spif_str_del(test2str);
758     TEST_PASS();
759 
760     TEST_BEGIN("spif_str_find_from_ptr() function");
761     TEST_FAIL_IF(spif_str_find_from_ptr(teststr, SPIF_CHARPTR("in")) != 3);
762     spif_str_del(teststr);
763     TEST_PASS();
764 
765     TEST_BEGIN("spif_str_substr() function");
766     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp));
767     test2str = spif_str_substr(teststr, 2, 5);
768     TEST_FAIL_IF(spif_str_cmp_with_ptr(test2str, SPIF_CHARPTR("is is")));
769     TEST_FAIL_IF(spif_str_get_size(test2str) != 6);
770     TEST_FAIL_IF(spif_str_get_len(test2str) != 5);
771     spif_str_del(test2str);
772     test2str = spif_str_substr(teststr, -4, 4);
773     TEST_FAIL_IF(spif_str_cmp_with_ptr(test2str, SPIF_CHARPTR("test")));
774     TEST_FAIL_IF(spif_str_get_size(test2str) != 5);
775     TEST_FAIL_IF(spif_str_get_len(test2str) != 4);
776     spif_str_del(test2str);
777     spif_str_del(teststr);
778     TEST_PASS();
779 
780     TEST_BEGIN("spif_str_substr_to_ptr() function");
781     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp));
782     foo = spif_str_substr_to_ptr(teststr, 2, 5);
783     TEST_FAIL_IF(foo == NULL);
784     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(foo), "is is"));
785     FREE(foo);
786     foo = spif_str_substr_to_ptr(teststr, -4, 4);
787     TEST_FAIL_IF(foo == NULL);
788     TEST_FAIL_IF(strcmp(SPIF_CHARPTR_C(foo), "test"));
789     FREE(foo);
790     spif_str_del(teststr);
791     TEST_PASS();
792 
793     TEST_BEGIN("spif_str_to_num() function");
794     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("11001001"));
795     TEST_FAIL_IF(spif_str_to_num(teststr, 2) != 201);
796     TEST_FAIL_IF(spif_str_to_num(teststr, 10) != 11001001);
797     spif_str_del(teststr);
798     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("0132"));
799     TEST_FAIL_IF(spif_str_to_num(teststr, 0) != 90);
800     TEST_FAIL_IF(spif_str_to_num(teststr, 8) != 90);
801     TEST_FAIL_IF(spif_str_to_num(teststr, 10) != 132);
802     spif_str_del(teststr);
803     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("0xff"));
804     TEST_FAIL_IF(spif_str_to_num(teststr, 0) != 255);
805     TEST_FAIL_IF(spif_str_to_num(teststr, 10) != 0);
806     spif_str_del(teststr);
807     TEST_PASS();
808 
809     TEST_BEGIN("spif_str_to_float() function");
810     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("3.1415"));
811     TEST_FAIL_IF(spif_str_to_float(teststr) != 3.1415);
812     spif_str_del(teststr);
813     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("2.71"));
814     TEST_FAIL_IF(spif_str_to_float(teststr) != 2.71);
815     spif_str_del(teststr);
816     TEST_PASS();
817 
818     TEST_BEGIN("spif_str_append() function");
819     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("copy"));
820     test2str = spif_str_new_from_ptr(SPIF_CHARPTR("cat"));
821     spif_str_append(teststr, test2str);
822     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("copycat")));
823     TEST_FAIL_IF(spif_str_get_size(teststr) != 8);
824     TEST_FAIL_IF(spif_str_get_len(teststr) != 7);
825     spif_str_del(test2str);
826     TEST_PASS();
827 
828     TEST_BEGIN("spif_str_append_char() function");
829     test2str = spif_str_new_from_ptr(SPIF_CHARPTR("Hello"));
830     spif_str_append_char(test2str, ' ');
831     spif_str_append_char(test2str, 'w');
832     spif_str_append_char(test2str, 'o');
833     spif_str_append_char(test2str, 'r');
834     spif_str_append_char(test2str, 'l');
835     spif_str_append_char(test2str, 'd');
836     TEST_FAIL_IF(spif_str_cmp_with_ptr(test2str, SPIF_CHARPTR("Hello world")));
837     TEST_FAIL_IF(spif_str_get_size(test2str) != 12);
838     TEST_FAIL_IF(spif_str_get_len(test2str) != 11);
839     spif_str_del(test2str);
840     TEST_PASS();
841 
842     TEST_BEGIN("spif_str_append_from_ptr() function");
843     spif_str_append_from_ptr(teststr, SPIF_CHARPTR("crime"));
844     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("copycatcrime")));
845     TEST_FAIL_IF(spif_str_get_size(teststr) != 13);
846     TEST_FAIL_IF(spif_str_get_len(teststr) != 12);
847     spif_str_del(teststr);
848     TEST_PASS();
849 
850     TEST_BEGIN("spif_str_clear() function");
851     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("abcdefg"));
852     spif_str_clear(teststr, 'x');
853     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("xxxxxxx")));
854     TEST_FAIL_IF(spif_str_get_size(teststr) != 8);
855     TEST_FAIL_IF(spif_str_get_len(teststr) != 7);
856     spif_str_del(teststr);
857     TEST_PASS();
858 
859     TEST_BEGIN("spif_str_trim() function");
860     teststr = spif_str_new_from_ptr(SPIF_CHARPTR("  \n \r\f       \t    testing 1 2 3    \v\r \n"));
861     spif_str_trim(teststr);
862     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("testing 1 2 3")));
863     TEST_FAIL_IF(spif_str_get_size(teststr) != 14);
864     TEST_FAIL_IF(spif_str_get_len(teststr) != 13);
865     spif_str_del(teststr);
866     TEST_PASS();
867 
868     TEST_BEGIN("spif_str_splice() function");
869     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp));
870     test2str = spif_str_new_from_ptr(SPIF_CHARPTR("lots of fun"));
871     spif_str_splice(teststr, 8, 6, test2str);
872     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("this is lots of fun"))));
873     TEST_FAIL_IF(spif_str_get_size(teststr) != 20);
874     TEST_FAIL_IF(spif_str_get_len(teststr) != 19);
875     spif_str_del(test2str);
876     spif_str_del(teststr);
877     TEST_PASS();
878 
879     TEST_BEGIN("spif_str_splice_from_ptr() function");
880     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(tmp));
881     spif_str_splice_from_ptr(teststr, 8, 0, SPIF_CHARPTR("not "));
882     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("this is not a test")));
883     TEST_FAIL_IF(spif_str_get_size(teststr) != 19);
884     TEST_FAIL_IF(spif_str_get_len(teststr) != 18);
885     spif_str_del(teststr);
886     TEST_PASS();
887 
888     TEST_BEGIN("spif_str_reverse() function");
889     teststr = spif_str_new_from_buff(buff, sizeof(buff));
890     spif_str_reverse(teststr);
891     TEST_FAIL_IF(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("edcba")));
892     TEST_FAIL_IF(spif_str_get_size(teststr) != sizeof(buff));
893     TEST_FAIL_IF(spif_str_get_len(teststr) != 5);
894     spif_str_del(teststr);
895     TEST_PASS();
896 
897     TEST_PASSED("spif_str_t");
898     return 0;
899 }
900 
901 int
test_tok(void)902 test_tok(void)
903 {
904     spif_tok_t testtok;
905     spif_str_t teststr;
906     spif_list_t testlist;
907     spif_class_t cls;
908     spif_charptr_t tmp = SPIF_CHARPTR("I \"can\'t\" feel my legs!");
909     spif_charptr_t tmp2 = SPIF_CHARPTR(":::some:seedy:colon-delimited::data");
910     spif_charptr_t tmp3 = SPIF_CHARPTR("\"this is one token\" and this \'over here\' is \"another one\"");
911     spif_charptr_t tmp4 = SPIF_CHARPTR("\"there shouldn't be\"\' any problems at\'\"\"\'\'\' \'\"all parsing this\"");
912 
913     TEST_BEGIN("spif_tok_new() function");
914     testtok = spif_tok_new();
915     TEST_FAIL_IF(SPIF_OBJ_ISNULL(testtok));
916     TEST_PASS();
917 
918     TEST_BEGIN("spif_obj_get_classname() function");
919     cls = spif_obj_get_class(SPIF_OBJ(testtok));
920     TEST_FAIL_IF(cls != SPIF_CLASS_VAR(tok));
921     TEST_PASS();
922 
923     TEST_BEGIN("spif_tok_del() function");
924     TEST_FAIL_IF(spif_tok_del(testtok) != TRUE);
925     TEST_PASS();
926 
927     TEST_BEGIN("spif_tok_new_from_ptr() function");
928     testtok = spif_tok_new_from_ptr(tmp);
929     TEST_FAIL_IF(spif_str_cmp_with_ptr(spif_tok_get_src(testtok), tmp));
930     spif_tok_del(testtok);
931     TEST_PASS();
932 
933     TEST_BEGIN("spif_tok_eval() function");
934     testtok = spif_tok_new_from_ptr(tmp);
935     TEST_FAIL_IF(SPIF_TOK_ISNULL(testtok));
936     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_tok_get_src(testtok), tmp)));
937     spif_tok_eval(testtok);
938     testlist = SPIF_CAST(list) spif_tok_get_tokens(testtok);
939     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 0);
940     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("I"))));
941     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 1);
942     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("can't"))));
943     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 2);
944     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("feel"))));
945     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 3);
946     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("my"))));
947     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 4);
948     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("legs!"))));
949     spif_tok_del(testtok);
950 
951     testtok = spif_tok_new_from_ptr(tmp2);
952     TEST_FAIL_IF(SPIF_TOK_ISNULL(testtok));
953     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_tok_get_src(testtok), tmp2)));
954     teststr = spif_str_new_from_ptr(SPIF_CHARPTR(":"));
955     spif_tok_set_sep(testtok, teststr);
956     spif_tok_eval(testtok);
957     testlist = SPIF_CAST(list) spif_tok_get_tokens(testtok);
958     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 0);
959     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("some"))));
960     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 1);
961     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("seedy"))));
962     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 2);
963     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("colon-delimited"))));
964     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 3);
965     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("data"))));
966     spif_tok_del(testtok);
967 
968     testtok = spif_tok_new_from_ptr(tmp3);
969     TEST_FAIL_IF(SPIF_TOK_ISNULL(testtok));
970     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_tok_get_src(testtok), tmp3)));
971     spif_tok_eval(testtok);
972     testlist = SPIF_CAST(list) spif_tok_get_tokens(testtok);
973     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 0);
974     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("this is one token"))));
975     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 1);
976     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("and"))));
977     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 2);
978     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("this"))));
979     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 3);
980     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("over here"))));
981     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 4);
982     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("is"))));
983     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 5);
984     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("another one"))));
985     spif_tok_del(testtok);
986 
987     testtok = spif_tok_new_from_ptr(tmp4);
988     TEST_FAIL_IF(SPIF_TOK_ISNULL(testtok));
989     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_tok_get_src(testtok), tmp4)));
990     spif_tok_eval(testtok);
991     testlist = SPIF_CAST(list) spif_tok_get_tokens(testtok);
992     teststr = SPIF_CAST(str) SPIF_LIST_GET(testlist, 0);
993     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(teststr, SPIF_CHARPTR("there shouldn't be any problems at all parsing this"))));
994     spif_tok_del(testtok);
995 
996     TEST_PASS();
997 
998 
999     TEST_PASSED("spif_tok_t");
1000     return 0;
1001 }
1002 
1003 int
test_url(void)1004 test_url(void)
1005 {
1006     spif_url_t testurl, testurl2, testurl3, testurl4;
1007     spif_charptr_t tmp1 = SPIF_CHARPTR("http://www.kainx.org/journal/?view=20020104");
1008     spif_charptr_t tmp2 = SPIF_CHARPTR("mailto:foo@bar.com?Subject=Eat Me");
1009     spif_charptr_t tmp3 = SPIF_CHARPTR("/path/to/some/file.jpg");
1010     spif_charptr_t tmp4 = SPIF_CHARPTR("pop3://dummy:moo@pop.nowhere.com:110");
1011 
1012     TEST_BEGIN("spif_url_new_from_ptr() function");
1013     testurl = spif_url_new_from_ptr(tmp1);
1014     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(SPIF_STR(testurl), tmp1)));
1015     testurl2 = spif_url_new_from_ptr(tmp2);
1016     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(SPIF_STR(testurl2), tmp2)));
1017     testurl3 = spif_url_new_from_ptr(tmp3);
1018     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(SPIF_STR(testurl3), tmp3)));
1019     testurl4 = spif_url_new_from_ptr(tmp4);
1020     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(SPIF_STR(testurl4), tmp4)));
1021     TEST_PASS();
1022 
1023     TEST_BEGIN("spif_url_parse() function");
1024     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_proto(testurl), SPIF_CHARPTR("http"))));
1025     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_user(testurl)));
1026     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_passwd(testurl)));
1027     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_host(testurl), SPIF_CHARPTR("www.kainx.org"))));
1028     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_port(testurl), SPIF_CHARPTR("80"))));
1029     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_path(testurl), SPIF_CHARPTR("/journal/"))));
1030     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_query(testurl), SPIF_CHARPTR("view=20020104"))));
1031     spif_url_del(testurl);
1032 
1033     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_proto(testurl2), SPIF_CHARPTR("mailto"))));
1034     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_user(testurl2), SPIF_CHARPTR("foo"))));
1035     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_passwd(testurl2)));
1036     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_host(testurl2), SPIF_CHARPTR("bar.com"))));
1037     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_port(testurl2)));
1038     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_path(testurl2)));
1039     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_query(testurl2), SPIF_CHARPTR("Subject=Eat Me"))));
1040     spif_url_del(testurl2);
1041 
1042     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_proto(testurl3)));
1043     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_user(testurl3)));
1044     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_passwd(testurl3)));
1045     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_host(testurl3)));
1046     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_port(testurl3)));
1047     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_path(testurl3), tmp3)));
1048     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_query(testurl3)));
1049     spif_url_del(testurl3);
1050 
1051     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_proto(testurl4), SPIF_CHARPTR("pop3"))));
1052     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_user(testurl4), SPIF_CHARPTR("dummy"))));
1053     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_passwd(testurl4), SPIF_CHARPTR("moo"))));
1054     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_host(testurl4), SPIF_CHARPTR("pop.nowhere.com"))));
1055     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(spif_url_get_port(testurl4), SPIF_CHARPTR("110"))));
1056     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_path(testurl4)));
1057     TEST_FAIL_IF(!SPIF_STR_ISNULL(spif_url_get_query(testurl4)));
1058     spif_url_del(testurl4);
1059 
1060     TEST_PASS();
1061 
1062     TEST_PASSED("spif_url_t");
1063     return 0;
1064 }
1065 
1066 int
test_list(void)1067 test_list(void)
1068 {
1069     unsigned short i;
1070     spif_list_t testlist;
1071     spif_str_t s, s2;
1072     spif_obj_t *list_array;
1073     spif_iterator_t it;
1074     size_t j;
1075 
1076     for (i = 0; i < 3; i++) {
1077         if (i == 0) {
1078             TEST_NOTICE("*** Testing list interface class, linked_list instance:");
1079             testlist = SPIF_LIST_NEW(linked_list);
1080         } else if (i == 1) {
1081             TEST_NOTICE("*** Testing list interface class, dlinked_list instance:");
1082             testlist = SPIF_LIST_NEW(dlinked_list);
1083         } else if (i == 2) {
1084             TEST_NOTICE("*** Testing list interface class, array instance:");
1085             testlist = SPIF_LIST_NEW(array);
1086         } else if (i == 3) {
1087         }
1088 
1089         TEST_BEGIN("SPIF_LIST_APPEND() and SPIF_LIST_PREPEND() macros");
1090         SPIF_LIST_APPEND(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("1")));
1091         SPIF_LIST_APPEND(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("2")));
1092         SPIF_LIST_APPEND(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("3")));
1093         SPIF_LIST_APPEND(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("4")));
1094         SPIF_LIST_APPEND(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("5")));
1095         SPIF_LIST_PREPEND(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("0")));
1096 
1097         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1098         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 0);
1099         spif_str_done(s);
1100         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1101         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 1);
1102         spif_str_done(s);
1103         spif_str_init_from_ptr(s, SPIF_CHARPTR("2"));
1104         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 2);
1105         spif_str_done(s);
1106         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1107         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 3);
1108         spif_str_done(s);
1109         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1110         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1111         spif_str_done(s);
1112         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1113         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 5);
1114         spif_str_del(s);
1115         TEST_PASS();
1116 
1117         TEST_BEGIN("SPIF_LIST_CONTAINS() macro");
1118         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1119         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, s));
1120         spif_str_done(s);
1121         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1122         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, s));
1123         spif_str_done(s);
1124         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1125         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, s));
1126         spif_str_del(s);
1127         TEST_PASS();
1128 
1129         TEST_BEGIN("SPIF_LIST_COUNT() macro");
1130         TEST_FAIL_IF(SPIF_LIST_COUNT(testlist) != 6);
1131         TEST_PASS();
1132 
1133         TEST_BEGIN("SPIF_LIST_FIND() macro");
1134         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1135         TEST_FAIL_IF(SPIF_STR_COMP(s, SPIF_STR(SPIF_LIST_FIND(testlist, s))) != SPIF_CMP_EQUAL);
1136         spif_str_done(s);
1137         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1138         TEST_FAIL_IF(SPIF_STR_COMP(s, SPIF_STR(SPIF_LIST_FIND(testlist, s))) != SPIF_CMP_EQUAL);
1139         spif_str_done(s);
1140         spif_str_init_from_ptr(s, SPIF_CHARPTR("8"));
1141         TEST_FAIL_IF(!SPIF_STR_ISNULL(SPIF_STR(SPIF_LIST_FIND(testlist, s))));
1142         spif_str_del(s);
1143         TEST_PASS();
1144 
1145         TEST_BEGIN("SPIF_LIST_GET() macro");
1146         s = spif_str_new_from_ptr(SPIF_CHARPTR("2"));
1147         TEST_FAIL_IF(SPIF_STR_COMP(s, SPIF_STR(SPIF_LIST_GET(testlist, 2))) != SPIF_CMP_EQUAL);
1148         spif_str_done(s);
1149         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1150         TEST_FAIL_IF(SPIF_STR_COMP(s, SPIF_STR(SPIF_LIST_GET(testlist, 4))) != SPIF_CMP_EQUAL);
1151         spif_str_done(s);
1152         spif_str_init_from_ptr(s, SPIF_CHARPTR("6"));
1153         TEST_FAIL_IF(!SPIF_STR_ISNULL(SPIF_STR(SPIF_LIST_GET(testlist, 6))));
1154         spif_str_del(s);
1155         TEST_PASS();
1156 
1157         TEST_BEGIN("SPIF_LIST_INDEX() macro");
1158         s = spif_str_new_from_ptr(SPIF_CHARPTR("4"));
1159         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1160         spif_str_done(s);
1161         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1162         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 1);
1163         spif_str_done(s);
1164         spif_str_init_from_ptr(s, SPIF_CHARPTR("Q"));
1165         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != ((size_t) -1));
1166         spif_str_del(s);
1167         TEST_PASS();
1168 
1169         TEST_BEGIN("SPIF_LIST_INSERT() macro");
1170         SPIF_LIST_INSERT(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("2.5")));
1171         SPIF_LIST_INSERT(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("4.5")));
1172         TEST_FAIL_IF(SPIF_LIST_COUNT(testlist) != 8);
1173         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1174         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 0);
1175         spif_str_done(s);
1176         spif_str_init_from_ptr(s, SPIF_CHARPTR("2.5"));
1177         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 3);
1178         spif_str_done(s);
1179         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1180         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1181         spif_str_done(s);
1182         spif_str_init_from_ptr(s, SPIF_CHARPTR("4.5"));
1183         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 6);
1184         spif_str_done(s);
1185         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1186         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 7);
1187         spif_str_del(s);
1188         TEST_PASS();
1189 
1190         TEST_BEGIN("SPIF_LIST_INSERT_AT() macro");
1191         SPIF_LIST_INSERT_AT(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("MOO")), 0);
1192         SPIF_LIST_INSERT_AT(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("GRIN")), 4);
1193         SPIF_LIST_INSERT_AT(testlist, spif_str_new_from_ptr(SPIF_CHARPTR("BOB")), 12);
1194         TEST_FAIL_IF(SPIF_LIST_COUNT(testlist) != 13);
1195         s = spif_str_new_from_ptr(SPIF_CHARPTR("MOO"));
1196         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 0);
1197         spif_str_done(s);
1198         spif_str_init_from_ptr(s, SPIF_CHARPTR("0"));
1199         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 1);
1200         spif_str_done(s);
1201         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1202         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 2);
1203         spif_str_done(s);
1204         spif_str_init_from_ptr(s, SPIF_CHARPTR("GRIN"));
1205         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1206         spif_str_done(s);
1207         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1208         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 9);
1209         spif_str_done(s);
1210         TEST_FAIL_IF(!SPIF_OBJ_ISNULL(SPIF_LIST_GET(testlist, 10)));
1211         TEST_FAIL_IF(!SPIF_OBJ_ISNULL(SPIF_LIST_GET(testlist, 11)));
1212         spif_str_init_from_ptr(s, SPIF_CHARPTR("BOB"));
1213         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 12);
1214         spif_str_del(s);
1215         TEST_PASS();
1216 
1217         TEST_BEGIN("SPIF_LIST_ITERATOR() macro");
1218         for (j = 0, it = SPIF_LIST_ITERATOR(testlist); SPIF_ITERATOR_HAS_NEXT(it); j++) {
1219             spif_str_t tmp;
1220 
1221             tmp = SPIF_CAST(str) SPIF_ITERATOR_NEXT(it);
1222         }
1223         TEST_FAIL_IF(j != 13);
1224         TEST_FAIL_IF(SPIF_ITERATOR_HAS_NEXT(it));
1225         TEST_FAIL_IF(!SPIF_OBJ_ISNULL(SPIF_ITERATOR_NEXT(it)));
1226         SPIF_ITERATOR_DEL(it);
1227         TEST_PASS();
1228 
1229         TEST_BEGIN("SPIF_LIST_REMOVE() macro");
1230         s = spif_str_new_from_ptr(SPIF_CHARPTR("MOO"));
1231         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE(testlist, s);
1232         TEST_FAIL_IF(SPIF_OBJ_ISNULL(s2));
1233         spif_str_del(s2);
1234         spif_str_done(s);
1235         spif_str_init_from_ptr(s, SPIF_CHARPTR("GRIN"));
1236         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE(testlist, s);
1237         TEST_FAIL_IF(SPIF_OBJ_ISNULL(s2));
1238         spif_str_del(s2);
1239         spif_str_done(s);
1240         spif_str_init_from_ptr(s, SPIF_CHARPTR("BOB"));
1241         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE(testlist, s);
1242         TEST_FAIL_IF(SPIF_OBJ_ISNULL(s2));
1243         spif_str_del(s2);
1244         spif_str_del(s);
1245 
1246         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1247         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 0);
1248         spif_str_done(s);
1249         spif_str_init_from_ptr(s, SPIF_CHARPTR("2.5"));
1250         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 3);
1251         spif_str_done(s);
1252         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1253         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1254         spif_str_done(s);
1255         spif_str_init_from_ptr(s, SPIF_CHARPTR("4.5"));
1256         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 6);
1257         spif_str_done(s);
1258         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1259         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 7);
1260         spif_str_del(s);
1261         TEST_PASS();
1262 
1263         TEST_BEGIN("SPIF_LIST_REMOVE_AT() macro");
1264         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE_AT(testlist, 11);
1265         TEST_FAIL_IF(!SPIF_STR_ISNULL(s2));
1266         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE_AT(testlist, 10);
1267         TEST_FAIL_IF(!SPIF_STR_ISNULL(s2));
1268         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE_AT(testlist, 9);
1269         TEST_FAIL_IF(!SPIF_STR_ISNULL(s2));
1270         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE_AT(testlist, 8);
1271         TEST_FAIL_IF(!SPIF_STR_ISNULL(s2));
1272         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE_AT(testlist, 6);
1273         TEST_FAIL_IF(SPIF_STR_ISNULL(s2));
1274         spif_str_del(s2);
1275         s2 = SPIF_CAST(str) SPIF_LIST_REMOVE_AT(testlist, 3);
1276         TEST_FAIL_IF(SPIF_STR_ISNULL(s2));
1277         spif_str_del(s2);
1278 
1279         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1280         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 0);
1281         spif_str_done(s);
1282         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1283         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 1);
1284         spif_str_done(s);
1285         spif_str_init_from_ptr(s, SPIF_CHARPTR("2"));
1286         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 2);
1287         spif_str_done(s);
1288         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1289         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 3);
1290         spif_str_done(s);
1291         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1292         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1293         spif_str_done(s);
1294         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1295         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 5);
1296         spif_str_del(s);
1297         TEST_PASS();
1298 
1299         TEST_BEGIN("SPIF_LIST_REVERSE() macro");
1300         SPIF_LIST_REVERSE(testlist);
1301 
1302         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1303         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 5);
1304         spif_str_done(s);
1305         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1306         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 4);
1307         spif_str_done(s);
1308         spif_str_init_from_ptr(s, SPIF_CHARPTR("2"));
1309         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 3);
1310         spif_str_done(s);
1311         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1312         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 2);
1313         spif_str_done(s);
1314         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1315         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 1);
1316         spif_str_done(s);
1317         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1318         TEST_FAIL_IF(SPIF_LIST_INDEX(testlist, s) != 0);
1319         spif_str_del(s);
1320         TEST_PASS();
1321 
1322         TEST_BEGIN("SPIF_LIST_TO_ARRAY() macro");
1323         list_array = SPIF_LIST_TO_ARRAY(testlist);
1324 
1325         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1326         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(list_array[5], s)));
1327         spif_str_done(s);
1328         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1329         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(list_array[4], s)));
1330         spif_str_done(s);
1331         spif_str_init_from_ptr(s, SPIF_CHARPTR("2"));
1332         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(list_array[3], s)));
1333         spif_str_done(s);
1334         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1335         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(list_array[2], s)));
1336         spif_str_done(s);
1337         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1338         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(list_array[1], s)));
1339         spif_str_done(s);
1340         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1341         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(list_array[0], s)));
1342         spif_str_del(s);
1343         SPIF_DEALLOC(list_array);
1344         TEST_PASS();
1345 
1346         /*SPIF_SHOW(testlist, stdout);*/
1347         SPIF_LIST_DEL(testlist);
1348     }
1349 
1350     TEST_PASSED("list interface class");
1351     return 0;
1352 }
1353 
1354 int
test_vector(void)1355 test_vector(void)
1356 {
1357     unsigned short i;
1358     spif_vector_t testvector;
1359     spif_str_t s, s2;
1360     spif_obj_t *vector_array;
1361     spif_iterator_t it;
1362     size_t j;
1363 
1364     for (i = 0; i < 3; i++) {
1365         if (i == 0) {
1366             TEST_NOTICE("*** Testing vector interface class, linked_list instance:");
1367             testvector = SPIF_VECTOR_NEW(linked_list);
1368         } else if (i == 1) {
1369             TEST_NOTICE("*** Testing vector interface class, dlinked_list instance:");
1370             testvector = SPIF_VECTOR_NEW(dlinked_list);
1371         } else if (i == 2) {
1372             TEST_NOTICE("*** Testing vector interface class, array instance:");
1373             testvector = SPIF_VECTOR_NEW(array);
1374         } else if (i == 3) {
1375         }
1376 
1377         TEST_BEGIN("SPIF_VECTOR_INSERT() macro");
1378         SPIF_VECTOR_INSERT(testvector, spif_str_new_from_ptr(SPIF_CHARPTR("1")));
1379         s = spif_str_new_from_ptr(SPIF_CHARPTR("1"));
1380         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1381         spif_str_done(s);
1382         SPIF_VECTOR_INSERT(testvector, spif_str_new_from_ptr(SPIF_CHARPTR("3")));
1383         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1384         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1385         spif_str_done(s);
1386         SPIF_VECTOR_INSERT(testvector, spif_str_new_from_ptr(SPIF_CHARPTR("2")));
1387         spif_str_init_from_ptr(s, SPIF_CHARPTR("2"));
1388         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1389         spif_str_done(s);
1390         SPIF_VECTOR_INSERT(testvector, spif_str_new_from_ptr(SPIF_CHARPTR("5")));
1391         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1392         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1393         spif_str_done(s);
1394         SPIF_VECTOR_INSERT(testvector, spif_str_new_from_ptr(SPIF_CHARPTR("0")));
1395         spif_str_init_from_ptr(s, SPIF_CHARPTR("0"));
1396         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1397         spif_str_done(s);
1398         SPIF_VECTOR_INSERT(testvector, spif_str_new_from_ptr(SPIF_CHARPTR("4")));
1399         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1400         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1401         spif_str_del(s);
1402         TEST_PASS();
1403 
1404         TEST_BEGIN("SPIF_VECTOR_CONTAINS() macro");
1405         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1406         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1407         spif_str_done(s);
1408         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1409         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1410         spif_str_done(s);
1411         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1412         TEST_FAIL_IF(!SPIF_VECTOR_CONTAINS(testvector, s));
1413         spif_str_done(s);
1414         spif_str_init_from_ptr(s, SPIF_CHARPTR("8"));
1415         TEST_FAIL_IF(SPIF_VECTOR_CONTAINS(testvector, s));
1416         spif_str_done(s);
1417         spif_str_init_from_ptr(s, SPIF_CHARPTR("0.2"));
1418         TEST_FAIL_IF(SPIF_VECTOR_CONTAINS(testvector, s));
1419         spif_str_del(s);
1420         TEST_PASS();
1421 
1422         TEST_BEGIN("SPIF_VECTOR_COUNT() macro");
1423         TEST_FAIL_IF(SPIF_VECTOR_COUNT(testvector) != 6);
1424         TEST_PASS();
1425 
1426         TEST_BEGIN("SPIF_VECTOR_FIND() macro");
1427         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1428         TEST_FAIL_IF(SPIF_STR_COMP(s, SPIF_STR(SPIF_VECTOR_FIND(testvector, s))) != SPIF_CMP_EQUAL);
1429         spif_str_done(s);
1430         spif_str_init_from_ptr(s, SPIF_CHARPTR("3"));
1431         TEST_FAIL_IF(SPIF_STR_COMP(s, SPIF_STR(SPIF_VECTOR_FIND(testvector, s))) != SPIF_CMP_EQUAL);
1432         spif_str_done(s);
1433         spif_str_init_from_ptr(s, SPIF_CHARPTR("8"));
1434         TEST_FAIL_IF(!SPIF_STR_ISNULL(SPIF_STR(SPIF_VECTOR_FIND(testvector, s))));
1435         spif_str_del(s);
1436         TEST_PASS();
1437 
1438         TEST_BEGIN("SPIF_VECTOR_ITERATOR() macro");
1439         for (j = 0, it = SPIF_VECTOR_ITERATOR(testvector); SPIF_ITERATOR_HAS_NEXT(it); j++) {
1440             spif_str_t tmp;
1441 
1442             tmp = SPIF_CAST(str) SPIF_ITERATOR_NEXT(it);
1443             TEST_FAIL_IF(SPIF_STR_ISNULL(tmp));
1444         }
1445         TEST_FAIL_IF(j != 6);
1446         TEST_FAIL_IF(SPIF_ITERATOR_HAS_NEXT(it));
1447         TEST_FAIL_IF(!SPIF_OBJ_ISNULL(SPIF_ITERATOR_NEXT(it)));
1448         SPIF_ITERATOR_DEL(it);
1449         TEST_PASS();
1450 
1451         TEST_BEGIN("SPIF_VECTOR_REMOVE() macro");
1452         s = spif_str_new_from_ptr(SPIF_CHARPTR("3"));
1453         s2 = SPIF_CAST(str) SPIF_VECTOR_REMOVE(testvector, s);
1454         TEST_FAIL_IF(SPIF_OBJ_ISNULL(s2));
1455         spif_str_del(s2);
1456         spif_str_done(s);
1457         spif_str_init_from_ptr(s, SPIF_CHARPTR("GRIN"));
1458         s2 = SPIF_CAST(str) SPIF_VECTOR_REMOVE(testvector, s);
1459         TEST_FAIL_IF(!SPIF_OBJ_ISNULL(s2));
1460         spif_str_del(s);
1461         TEST_PASS();
1462 
1463         TEST_BEGIN("SPIF_VECTOR_TO_ARRAY() macro");
1464         vector_array = SPIF_VECTOR_TO_ARRAY(testvector);
1465 
1466         s = spif_str_new_from_ptr(SPIF_CHARPTR("0"));
1467         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(vector_array[0], s)));
1468         spif_str_done(s);
1469         spif_str_init_from_ptr(s, SPIF_CHARPTR("1"));
1470         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(vector_array[1], s)));
1471         spif_str_done(s);
1472         spif_str_init_from_ptr(s, SPIF_CHARPTR("2"));
1473         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(vector_array[2], s)));
1474         spif_str_done(s);
1475         spif_str_init_from_ptr(s, SPIF_CHARPTR("4"));
1476         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(vector_array[3], s)));
1477         spif_str_done(s);
1478         spif_str_init_from_ptr(s, SPIF_CHARPTR("5"));
1479         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(SPIF_OBJ_COMP(vector_array[4], s)));
1480         spif_str_del(s);
1481         SPIF_DEALLOC(vector_array);
1482         TEST_PASS();
1483 
1484         /*SPIF_SHOW(testvector, stdout);*/
1485         SPIF_VECTOR_DEL(testvector);
1486     }
1487 
1488     TEST_PASSED("vector interface class");
1489     return 0;
1490 }
1491 
1492 int
test_map(void)1493 test_map(void)
1494 {
1495     unsigned short i;
1496     spif_map_t testmap;
1497     spif_obj_t ret;
1498     spif_str_t key, value;
1499     spif_url_t homepage;
1500     spif_list_t testlist;
1501     spif_iterator_t it;
1502     size_t j;
1503 
1504     for (i = 0; i < 3; i++) {
1505         if (i == 0) {
1506             TEST_NOTICE("*** Testing map interface, linked_list class:");
1507             testmap = SPIF_MAP_NEW(linked_list);
1508         } else if (i == 1) {
1509             TEST_NOTICE("*** Testing map interface, dlinked_list class:");
1510             testmap = SPIF_MAP_NEW(dlinked_list);
1511         } else if (i == 2) {
1512             TEST_NOTICE("*** Testing map interface, array class:");
1513             testmap = SPIF_MAP_NEW(array);
1514         } else if (i == 3) {
1515         }
1516 
1517         TEST_BEGIN("SPIF_MAP_SET() macro");
1518         key = spif_str_new_from_ptr(SPIF_CHARPTR("name"));
1519         value = spif_str_new_from_ptr(SPIF_CHARPTR("Bob"));
1520         TEST_FAIL_IF(SPIF_MAP_SET(testmap, key, value));
1521         spif_str_done(key);
1522         spif_str_done(value);
1523         spif_str_init_from_ptr(key, SPIF_CHARPTR("rank"));
1524         spif_str_init_from_ptr(value, SPIF_CHARPTR("Dweeb"));
1525         TEST_FAIL_IF(SPIF_MAP_SET(testmap, key, value));
1526         spif_str_done(key);
1527         spif_str_done(value);
1528         spif_str_init_from_ptr(key, SPIF_CHARPTR("serial number"));
1529         spif_str_init_from_ptr(value, SPIF_CHARPTR("123456"));
1530         TEST_FAIL_IF(SPIF_MAP_SET(testmap, key, value));
1531         spif_str_done(key);
1532         spif_str_done(value);
1533         spif_str_init_from_ptr(key, SPIF_CHARPTR("homepage"));
1534         homepage = spif_url_new_from_ptr(SPIF_CHARPTR("http://www.dweeb.com/"));
1535         TEST_FAIL_IF(SPIF_MAP_SET(testmap, key, homepage));
1536         spif_str_done(key);
1537         spif_url_del(homepage);
1538         spif_str_init_from_ptr(key, SPIF_CHARPTR("e-mail"));
1539         spif_str_init_from_ptr(value, SPIF_CHARPTR("bob@dweeb.com"));
1540         TEST_FAIL_IF(SPIF_MAP_SET(testmap, key, value));
1541         spif_str_done(key);
1542         spif_str_done(value);
1543         spif_str_init_from_ptr(key, SPIF_CHARPTR("loser"));
1544         spif_str_init_from_ptr(value, SPIF_CHARPTR("YES"));
1545         TEST_FAIL_IF(SPIF_MAP_SET(testmap, key, value));
1546         spif_str_del(key);
1547         spif_str_del(value);
1548         TEST_PASS();
1549 
1550         TEST_BEGIN("SPIF_MAP_COUNT() macro");
1551         TEST_FAIL_IF(SPIF_MAP_COUNT(testmap) != 6);
1552         TEST_PASS();
1553 
1554         TEST_BEGIN("SPIF_MAP_GET() macro");
1555         key = spif_str_new_from_ptr(SPIF_CHARPTR("serial number"));
1556         value = SPIF_CAST(str) SPIF_MAP_GET(testmap, key);
1557         TEST_FAIL_IF(SPIF_STR_ISNULL(value));
1558         TEST_FAIL_IF(!SPIF_OBJ_IS_STR(value));
1559         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(value, SPIF_CHARPTR("123456"))));
1560         spif_str_done(key);
1561         spif_str_init_from_ptr(key, SPIF_CHARPTR("loser"));
1562         value = SPIF_CAST(str) SPIF_MAP_GET(testmap, key);
1563         TEST_FAIL_IF(SPIF_STR_ISNULL(value));
1564         TEST_FAIL_IF(!SPIF_OBJ_IS_STR(value));
1565         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(value, SPIF_CHARPTR("YES"))));
1566         spif_str_done(key);
1567         spif_str_init_from_ptr(key, SPIF_CHARPTR("name"));
1568         value = SPIF_CAST(str) SPIF_MAP_GET(testmap, key);
1569         TEST_FAIL_IF(SPIF_STR_ISNULL(value));
1570         TEST_FAIL_IF(!SPIF_OBJ_IS_STR(value));
1571         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(value, SPIF_CHARPTR("Bob"))));
1572         spif_str_done(key);
1573         spif_str_init_from_ptr(key, SPIF_CHARPTR("e-mail"));
1574         value = SPIF_CAST(str) SPIF_MAP_GET(testmap, key);
1575         TEST_FAIL_IF(SPIF_STR_ISNULL(value));
1576         TEST_FAIL_IF(!SPIF_OBJ_IS_STR(value));
1577         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(value, SPIF_CHARPTR("bob@dweeb.com"))));
1578         spif_str_done(key);
1579         spif_str_init_from_ptr(key, SPIF_CHARPTR("rank"));
1580         value = SPIF_CAST(str) SPIF_MAP_GET(testmap, key);
1581         TEST_FAIL_IF(SPIF_STR_ISNULL(value));
1582         TEST_FAIL_IF(!SPIF_OBJ_IS_STR(value));
1583         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(value, SPIF_CHARPTR("Dweeb"))));
1584         spif_str_done(key);
1585         spif_str_init_from_ptr(key, SPIF_CHARPTR("homepage"));
1586         homepage = SPIF_CAST(url) SPIF_MAP_GET(testmap, key);
1587         TEST_FAIL_IF(SPIF_URL_ISNULL(homepage));
1588         TEST_FAIL_IF(!SPIF_OBJ_IS_URL(homepage));
1589         TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp_with_ptr(SPIF_STR(homepage), SPIF_CHARPTR("http://www.dweeb.com/"))));
1590         spif_str_del(key);
1591         TEST_PASS();
1592 
1593         TEST_BEGIN("SPIF_MAP_GET_KEYS() macro");
1594         testlist = SPIF_LIST_NEW(array);
1595         SPIF_MAP_GET_KEYS(testmap, testlist);
1596         TEST_FAIL_IF(SPIF_LIST_COUNT(testlist) != SPIF_MAP_COUNT(testmap));
1597         key = spif_str_new_from_ptr(SPIF_CHARPTR("serial number"));
1598         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, key));
1599         spif_str_done(key);
1600         spif_str_init_from_ptr(key, SPIF_CHARPTR("loser"));
1601         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, key));
1602         spif_str_done(key);
1603         spif_str_init_from_ptr(key, SPIF_CHARPTR("name"));
1604         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, key));
1605         spif_str_done(key);
1606         spif_str_init_from_ptr(key, SPIF_CHARPTR("e-mail"));
1607         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, key));
1608         spif_str_done(key);
1609         spif_str_init_from_ptr(key, SPIF_CHARPTR("rank"));
1610         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, key));
1611         spif_str_done(key);
1612         spif_str_init_from_ptr(key, SPIF_CHARPTR("homepage"));
1613         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, key));
1614         spif_str_del(key);
1615         value = spif_str_new_from_ptr(SPIF_CHARPTR("123456"));
1616         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, value));
1617         spif_str_done(value);
1618         spif_str_init_from_ptr(value, SPIF_CHARPTR("YES"));
1619         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, value));
1620         spif_str_done(value);
1621         spif_str_init_from_ptr(value, SPIF_CHARPTR("Bob"));
1622         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, value));
1623         spif_str_done(value);
1624         spif_str_init_from_ptr(value, SPIF_CHARPTR("bob@dweeb.com"));
1625         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, value));
1626         spif_str_done(value);
1627         spif_str_init_from_ptr(value, SPIF_CHARPTR("Dweeb"));
1628         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, value));
1629         spif_str_del(value);
1630         homepage = spif_url_new_from_ptr(SPIF_CHARPTR("http://www.dweeb.com/"));
1631         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, homepage));
1632         spif_url_del(homepage);
1633         SPIF_LIST_DEL(testlist);
1634         TEST_PASS();
1635 
1636         TEST_BEGIN("SPIF_MAP_GET_PAIRS() macro");
1637         testlist = SPIF_LIST_NEW(array);
1638         SPIF_MAP_GET_PAIRS(testmap, testlist);
1639         TEST_FAIL_IF(SPIF_LIST_COUNT(testlist) != SPIF_MAP_COUNT(testmap));
1640         SPIF_LIST_DEL(testlist);
1641         TEST_PASS();
1642 
1643         TEST_BEGIN("SPIF_MAP_GET_VALUES() macro");
1644         testlist = SPIF_LIST_NEW(array);
1645         SPIF_MAP_GET_VALUES(testmap, testlist);
1646         TEST_FAIL_IF(SPIF_LIST_COUNT(testlist) != SPIF_MAP_COUNT(testmap));
1647         key = spif_str_new_from_ptr(SPIF_CHARPTR("serial number"));
1648         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, key));
1649         spif_str_done(key);
1650         spif_str_init_from_ptr(key, SPIF_CHARPTR("loser"));
1651         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, key));
1652         spif_str_done(key);
1653         spif_str_init_from_ptr(key, SPIF_CHARPTR("name"));
1654         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, key));
1655         spif_str_done(key);
1656         spif_str_init_from_ptr(key, SPIF_CHARPTR("e-mail"));
1657         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, key));
1658         spif_str_done(key);
1659         spif_str_init_from_ptr(key, SPIF_CHARPTR("rank"));
1660         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, key));
1661         spif_str_done(key);
1662         spif_str_init_from_ptr(key, SPIF_CHARPTR("homepage"));
1663         TEST_FAIL_IF(SPIF_LIST_CONTAINS(testlist, key));
1664         spif_str_del(key);
1665         value = spif_str_new_from_ptr(SPIF_CHARPTR("123456"));
1666         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, value));
1667         spif_str_done(value);
1668         spif_str_init_from_ptr(value, SPIF_CHARPTR("YES"));
1669         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, value));
1670         spif_str_done(value);
1671         spif_str_init_from_ptr(value, SPIF_CHARPTR("Bob"));
1672         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, value));
1673         spif_str_done(value);
1674         spif_str_init_from_ptr(value, SPIF_CHARPTR("bob@dweeb.com"));
1675         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, value));
1676         spif_str_done(value);
1677         spif_str_init_from_ptr(value, SPIF_CHARPTR("Dweeb"));
1678         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, value));
1679         spif_str_del(value);
1680         homepage = spif_url_new_from_ptr(SPIF_CHARPTR("http://www.dweeb.com/"));
1681         TEST_FAIL_IF(!SPIF_LIST_CONTAINS(testlist, homepage));
1682         spif_url_del(homepage);
1683         SPIF_LIST_DEL(testlist);
1684         TEST_PASS();
1685 
1686         TEST_BEGIN("SPIF_MAP_ITERATOR(), SPIF_MAP_HAS_KEY(), and SPIF_MAP_HAS_VALUE() macros");
1687         for (j = 0, it = SPIF_MAP_ITERATOR(testmap); SPIF_ITERATOR_HAS_NEXT(it); j++) {
1688             spif_objpair_t tmp;
1689 
1690             tmp = SPIF_CAST(objpair) SPIF_ITERATOR_NEXT(it);
1691             TEST_FAIL_IF(SPIF_OBJPAIR_ISNULL(tmp));
1692             TEST_FAIL_IF(!SPIF_MAP_HAS_KEY(testmap, tmp->key));
1693             TEST_FAIL_IF(!SPIF_MAP_HAS_VALUE(testmap, tmp->value));
1694         }
1695         TEST_FAIL_IF(j != 6);
1696         TEST_FAIL_IF(SPIF_ITERATOR_HAS_NEXT(it));
1697         TEST_FAIL_IF(!SPIF_OBJ_ISNULL(SPIF_ITERATOR_NEXT(it)));
1698         SPIF_ITERATOR_DEL(it);
1699         TEST_PASS();
1700 
1701         TEST_BEGIN("SPIF_MAP_REMOVE() macro");
1702         key = spif_str_new_from_ptr(SPIF_CHARPTR("name"));
1703         value = spif_str_new_from_ptr(SPIF_CHARPTR("Bob"));
1704         ret = SPIF_MAP_REMOVE(testmap, key);
1705         TEST_FAIL_IF(SPIF_OBJ_ISNULL(ret));
1706         TEST_FAIL_IF(!SPIF_OBJ_COMP(ret, value));
1707         TEST_FAIL_IF(SPIF_MAP_HAS_KEY(testmap, key));
1708         SPIF_OBJ_DEL(ret);
1709         spif_str_done(key);
1710         spif_str_done(value);
1711         spif_str_init_from_ptr(key, SPIF_CHARPTR("rank"));
1712         spif_str_init_from_ptr(value, SPIF_CHARPTR("Dweeb"));
1713         ret = SPIF_MAP_REMOVE(testmap, key);
1714         TEST_FAIL_IF(SPIF_OBJ_ISNULL(ret));
1715         TEST_FAIL_IF(!SPIF_OBJ_COMP(ret, value));
1716         TEST_FAIL_IF(SPIF_MAP_HAS_KEY(testmap, key));
1717         SPIF_OBJ_DEL(ret);
1718         spif_str_done(key);
1719         spif_str_done(value);
1720         spif_str_init_from_ptr(key, SPIF_CHARPTR("serial number"));
1721         spif_str_init_from_ptr(value, SPIF_CHARPTR("123456"));
1722         ret = SPIF_MAP_REMOVE(testmap, key);
1723         TEST_FAIL_IF(SPIF_OBJ_ISNULL(ret));
1724         TEST_FAIL_IF(!SPIF_OBJ_COMP(ret, value));
1725         TEST_FAIL_IF(SPIF_MAP_HAS_KEY(testmap, key));
1726         SPIF_OBJ_DEL(ret);
1727         spif_str_done(key);
1728         spif_str_done(value);
1729         spif_str_init_from_ptr(key, SPIF_CHARPTR("homepage"));
1730         homepage = spif_url_new_from_ptr(SPIF_CHARPTR("http://www.dweeb.com/"));
1731         ret = SPIF_MAP_REMOVE(testmap, key);
1732         TEST_FAIL_IF(SPIF_OBJ_ISNULL(ret));
1733         TEST_FAIL_IF(!SPIF_OBJ_COMP(ret, homepage));
1734         TEST_FAIL_IF(SPIF_MAP_HAS_KEY(testmap, key));
1735         SPIF_OBJ_DEL(ret);
1736         spif_str_done(key);
1737         spif_url_del(homepage);
1738         spif_str_init_from_ptr(key, SPIF_CHARPTR("e-mail"));
1739         spif_str_init_from_ptr(value, SPIF_CHARPTR("bob@dweeb.com"));
1740         ret = SPIF_MAP_REMOVE(testmap, key);
1741         TEST_FAIL_IF(SPIF_OBJ_ISNULL(ret));
1742         TEST_FAIL_IF(!SPIF_OBJ_COMP(ret, value));
1743         TEST_FAIL_IF(SPIF_MAP_HAS_KEY(testmap, key));
1744         SPIF_OBJ_DEL(ret);
1745         spif_str_done(key);
1746         spif_str_done(value);
1747         spif_str_init_from_ptr(key, SPIF_CHARPTR("loser"));
1748         spif_str_init_from_ptr(value, SPIF_CHARPTR("YES"));
1749         ret = SPIF_MAP_REMOVE(testmap, key);
1750         TEST_FAIL_IF(SPIF_OBJ_ISNULL(ret));
1751         TEST_FAIL_IF(!SPIF_OBJ_COMP(ret, value));
1752         TEST_FAIL_IF(SPIF_MAP_HAS_KEY(testmap, key));
1753         SPIF_OBJ_DEL(ret);
1754         spif_str_del(key);
1755         spif_str_del(value);
1756         TEST_FAIL_IF(SPIF_MAP_COUNT(testmap) != 0);
1757         TEST_PASS();
1758 
1759         /*SPIF_SHOW(testmap, stdout);*/
1760         SPIF_MAP_DEL(testmap);
1761     }
1762 
1763     TEST_PASSED("map interface");
1764     return 0;
1765 }
1766 
1767 int
test_socket(void)1768 test_socket(void)
1769 {
1770     spif_socket_t src1, dest1, src2, dest2, listen1, listen2;
1771     spif_url_t url1, url2;
1772     spif_str_t data1, data2;
1773     spif_charptr_t tmp1 = SPIF_CHARPTR("tcp://127.0.0.1:31737");
1774     spif_charptr_t tmp2 = SPIF_CHARPTR("unix:/tmp/libast-test-socket");
1775     spif_charptr_t strdata = SPIF_CHARPTR("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
1776     spif_bool_t b;
1777 
1778     /* Unlink the UNIX socket in case it exists. */
1779     unlink(SPIF_CHARPTR_C(tmp2) + 5);
1780 
1781     TEST_BEGIN("spif_socket_new_from_urls() function");
1782     url1 = spif_url_new_from_ptr(tmp1);
1783     TEST_FAIL_IF(SPIF_URL_ISNULL(url1));
1784     listen1 = spif_socket_new_from_urls(url1, SPIF_NULL_TYPE(url));
1785     dest1 = spif_socket_new_from_urls(SPIF_NULL_TYPE(url), url1);
1786     spif_url_del(url1);
1787     TEST_FAIL_IF(SPIF_SOCKET_ISNULL(listen1));
1788     TEST_FAIL_IF(SPIF_SOCKET_ISNULL(dest1));
1789     url2 = spif_url_new_from_ptr(tmp2);
1790     TEST_FAIL_IF(SPIF_URL_ISNULL(url2));
1791     listen2 = spif_socket_new_from_urls(url2, SPIF_NULL_TYPE(url));
1792     dest2 = spif_socket_new_from_urls(SPIF_NULL_TYPE(url), url2);
1793     spif_url_del(url2);
1794     TEST_FAIL_IF(SPIF_SOCKET_ISNULL(listen2));
1795     TEST_FAIL_IF(SPIF_SOCKET_ISNULL(dest2));
1796     TEST_PASS();
1797 
1798     TEST_BEGIN("spif_socket_open() function");
1799     b = spif_socket_open(listen1);
1800     TEST_FAIL_IF(b == FALSE);
1801     b = spif_socket_open(listen2);
1802     TEST_FAIL_IF(b == FALSE);
1803     b = spif_socket_open(dest1);
1804     TEST_FAIL_IF(b == FALSE);
1805     b = spif_socket_open(dest2);
1806     TEST_FAIL_IF(b == FALSE);
1807     TEST_PASS();
1808 
1809     TEST_BEGIN("spif_socket_set_nbio() function");
1810     b = spif_socket_set_nbio(listen1);
1811     TEST_FAIL_IF(b == FALSE);
1812     b = spif_socket_set_nbio(listen2);
1813     TEST_FAIL_IF(b == FALSE);
1814     b = spif_socket_set_nbio(dest1);
1815     TEST_FAIL_IF(b == FALSE);
1816     b = spif_socket_set_nbio(dest2);
1817     TEST_FAIL_IF(b == FALSE);
1818     TEST_PASS();
1819 
1820     TEST_BEGIN("spif_socket_accept() function");
1821     src1 = spif_socket_accept(listen1);
1822     TEST_FAIL_IF(SPIF_SOCKET_ISNULL(src1));
1823     src2 = spif_socket_accept(listen2);
1824     TEST_FAIL_IF(SPIF_SOCKET_ISNULL(src2));
1825     TEST_PASS();
1826 
1827     TEST_BEGIN("spif_socket_send() and spif_socket_recv() functions");
1828     signal(SIGPIPE, SIG_IGN);
1829     data1 = spif_str_new_from_ptr(strdata);
1830     TEST_FAIL_IF(SPIF_STR_ISNULL(data1));
1831     b = spif_socket_send(dest1, data1);
1832     TEST_FAIL_IF(b == FALSE);
1833     data2 = spif_socket_recv(src1);
1834     TEST_FAIL_IF(SPIF_STR_ISNULL(data2));
1835     TEST_FAIL_IF(!SPIF_CMP_IS_EQUAL(spif_str_cmp(data1, data2)));
1836     spif_str_del(data1);
1837     spif_str_del(data2);
1838     TEST_PASS();
1839 
1840     TEST_BEGIN("spif_socket_del() function");
1841     b = spif_socket_del(listen1);
1842     TEST_FAIL_IF(b == FALSE);
1843     b = spif_socket_del(listen2);
1844     TEST_FAIL_IF(b == FALSE);
1845     b = spif_socket_del(src1);
1846     TEST_FAIL_IF(b == FALSE);
1847     b = spif_socket_del(src2);
1848     TEST_FAIL_IF(b == FALSE);
1849     b = spif_socket_del(dest1);
1850     TEST_FAIL_IF(b == FALSE);
1851     b = spif_socket_del(dest2);
1852     TEST_FAIL_IF(b == FALSE);
1853     TEST_PASS();
1854 
1855     TEST_PASSED("spif_socket_t");
1856     return 0;
1857 }
1858 
1859 int
test_regexp(void)1860 test_regexp(void)
1861 {
1862 #if LIBAST_REGEXP_SUPPORT_PCRE
1863     {
1864         spif_charptr_t regexp_list[] = {
1865             SPIF_CHARPTR("cdefg"),
1866             SPIF_CHARPTR("^abcde"),
1867             SPIF_CHARPTR("efghi$"),
1868             SPIF_CHARPTR("^\\d+\\.\\d+\\.\\d{1,3}\\.\\d{1,3}$"),
1869             SPIF_CHARPTR("^([-\\w]+\\.){1,}(com|net|org|edu|info|biz|name|sex|[a-z]{2})$")
1870         };
1871         spif_str_t s;
1872         spif_regexp_t rexp;
1873 
1874         TEST_BEGIN("spif_regexp_matches_str() function");
1875         s = spif_str_new_from_ptr(regexp_list[0]);
1876         rexp = spif_regexp_new_from_str(s);
1877         spif_str_done(s);
1878         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
1879         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1880         spif_str_done(s);
1881         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
1882         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1883         spif_str_done(s);
1884         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
1885         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1886         spif_str_done(s);
1887         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
1888         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1889         spif_str_done(s);
1890         spif_str_init_from_ptr(s, SPIF_CHARPTR("CDEFG"));
1891         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1892         spif_str_done(s);
1893         spif_regexp_done(rexp);
1894 
1895         spif_str_init_from_ptr(s, regexp_list[1]);
1896         spif_regexp_init_from_str(rexp, s);
1897         spif_str_done(s);
1898         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
1899         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1900         spif_str_done(s);
1901         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
1902         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1903         spif_str_done(s);
1904         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
1905         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1906         spif_str_done(s);
1907         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
1908         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1909         spif_str_done(s);
1910         spif_str_init_from_ptr(s, SPIF_CHARPTR("ABCDE"));
1911         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1912         spif_str_done(s);
1913         spif_regexp_done(rexp);
1914 
1915         spif_str_init_from_ptr(s, regexp_list[2]);
1916         spif_regexp_init_from_str(rexp, s);
1917         spif_str_done(s);
1918         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
1919         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1920         spif_str_done(s);
1921         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
1922         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1923         spif_str_done(s);
1924         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
1925         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1926         spif_str_done(s);
1927         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
1928         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1929         spif_str_done(s);
1930         spif_str_init_from_ptr(s, SPIF_CHARPTR("EFGHI"));
1931         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1932         spif_str_done(s);
1933         spif_regexp_done(rexp);
1934 
1935         spif_str_init_from_ptr(s, regexp_list[3]);
1936         spif_regexp_init_from_str(rexp, s);
1937         spif_str_done(s);
1938         spif_str_init_from_ptr(s, SPIF_CHARPTR("127.0.0.1"));
1939         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1940         spif_str_done(s);
1941         spif_str_init_from_ptr(s, SPIF_CHARPTR("4.3.7.1"));
1942         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1943         spif_str_done(s);
1944         spif_str_init_from_ptr(s, SPIF_CHARPTR("192.168.137.111"));
1945         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1946         spif_str_done(s);
1947         spif_str_init_from_ptr(s, SPIF_CHARPTR("812.555.1212"));
1948         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1949         spif_str_done(s);
1950         spif_str_init_from_ptr(s, SPIF_CHARPTR("..."));
1951         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1952         spif_str_done(s);
1953         spif_regexp_done(rexp);
1954 
1955         spif_str_init_from_ptr(s, regexp_list[4]);
1956         spif_regexp_init_from_str(rexp, s);
1957         spif_regexp_set_flags(rexp, SPIF_CHARPTR("i"));
1958         spif_str_done(s);
1959         spif_str_init_from_ptr(s, SPIF_CHARPTR("www.stuff-and-other-stuff.com"));
1960         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1961         spif_str_done(s);
1962         spif_str_init_from_ptr(s, SPIF_CHARPTR("ETERM.ORG"));
1963         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1964         spif_str_done(s);
1965         spif_str_init_from_ptr(s, SPIF_CHARPTR("411.biz"));
1966         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1967         spif_str_done(s);
1968         spif_str_init_from_ptr(s, SPIF_CHARPTR(".com"));
1969         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1970         spif_str_done(s);
1971         spif_str_init_from_ptr(s, SPIF_CHARPTR("www.@@@.com"));
1972         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
1973         spif_str_del(s);
1974         spif_regexp_del(rexp);
1975 
1976         TEST_PASS();
1977     }
1978 #elif defined(LIBAST_REGEXP_SUPPORT_POSIX)
1979     {
1980         spif_charptr_t regexp_list[] = {
1981             SPIF_CHARPTR("cdefg"),
1982             SPIF_CHARPTR("^abcde"),
1983             SPIF_CHARPTR("efghi$"),
1984             SPIF_CHARPTR("^([0-9]{1,3}\\.){1,3}[0-9]{1,3}$"),
1985             SPIF_CHARPTR("^([-a-z0-9_]+\\.){1,}(com|net|org|edu|info|biz|name|sex|[a-z]{2})$")
1986         };
1987         spif_str_t s;
1988         spif_regexp_t rexp;
1989 
1990         TEST_BEGIN("spif_regexp_matches_str() function");
1991         s = spif_str_new_from_ptr(regexp_list[0]);
1992         rexp = spif_regexp_new_from_str(s);
1993         spif_str_done(s);
1994         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
1995         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1996         spif_str_done(s);
1997         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
1998         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
1999         spif_str_done(s);
2000         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
2001         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2002         spif_str_done(s);
2003         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
2004         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2005         spif_str_done(s);
2006         spif_str_init_from_ptr(s, SPIF_CHARPTR("CDEFG"));
2007         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2008         spif_str_done(s);
2009         spif_regexp_done(rexp);
2010 
2011         spif_str_init_from_ptr(s, regexp_list[1]);
2012         spif_regexp_init_from_str(rexp, s);
2013         spif_str_done(s);
2014         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
2015         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2016         spif_str_done(s);
2017         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
2018         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2019         spif_str_done(s);
2020         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
2021         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2022         spif_str_done(s);
2023         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
2024         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2025         spif_str_done(s);
2026         spif_str_init_from_ptr(s, SPIF_CHARPTR("ABCDE"));
2027         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2028         spif_str_done(s);
2029         spif_regexp_done(rexp);
2030 
2031         spif_str_init_from_ptr(s, regexp_list[2]);
2032         spif_regexp_init_from_str(rexp, s);
2033         spif_str_done(s);
2034         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
2035         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2036         spif_str_done(s);
2037         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
2038         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2039         spif_str_done(s);
2040         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
2041         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2042         spif_str_done(s);
2043         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
2044         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2045         spif_str_done(s);
2046         spif_str_init_from_ptr(s, SPIF_CHARPTR("EFGHI"));
2047         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2048         spif_str_done(s);
2049         spif_regexp_done(rexp);
2050 
2051         spif_str_init_from_ptr(s, regexp_list[3]);
2052         spif_regexp_init_from_str(rexp, s);
2053         spif_str_done(s);
2054         spif_str_init_from_ptr(s, SPIF_CHARPTR("127.0.0.1"));
2055         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2056         spif_str_done(s);
2057         spif_str_init_from_ptr(s, SPIF_CHARPTR("4.3.7.1"));
2058         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2059         spif_str_done(s);
2060         spif_str_init_from_ptr(s, SPIF_CHARPTR("192.168.137.111"));
2061         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2062         spif_str_done(s);
2063         spif_str_init_from_ptr(s, SPIF_CHARPTR("812.555.1212"));
2064         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2065         spif_str_done(s);
2066         spif_str_init_from_ptr(s, SPIF_CHARPTR("..."));
2067         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2068         spif_str_done(s);
2069         spif_regexp_done(rexp);
2070 
2071         spif_str_init_from_ptr(s, regexp_list[4]);
2072         spif_regexp_init_from_str(rexp, s);
2073         spif_regexp_set_flags(rexp, SPIF_CHARPTR("i"));
2074         spif_str_done(s);
2075         spif_str_init_from_ptr(s, SPIF_CHARPTR("www.stuff-and-other-stuff.com"));
2076         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2077         spif_str_done(s);
2078         spif_str_init_from_ptr(s, SPIF_CHARPTR("ETERM.ORG"));
2079         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2080         spif_str_done(s);
2081         spif_str_init_from_ptr(s, SPIF_CHARPTR("411.biz"));
2082         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2083         spif_str_done(s);
2084         spif_str_init_from_ptr(s, SPIF_CHARPTR(".com"));
2085         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2086         spif_str_done(s);
2087         spif_str_init_from_ptr(s, SPIF_CHARPTR("www.@@@.com"));
2088         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2089         spif_str_del(s);
2090         spif_regexp_del(rexp);
2091 
2092         TEST_PASS();
2093     }
2094 #elif defined(LIBAST_REGEXP_SUPPORT_BSD)
2095     {
2096         spif_charptr_t regexp_list[] = {
2097             SPIF_CHARPTR("cdefg"),
2098             SPIF_CHARPTR("^abcde"),
2099             SPIF_CHARPTR("efghi$"),
2100             SPIF_CHARPTR("^[0-9][0-9]?[0-9]?\\.[0-9][0-9]?[0-9]?\\.[0-9][0-9]?[0-9]?\\.[0-9][0-9]?[0-9]?$")
2101         };
2102         spif_str_t s;
2103         spif_regexp_t rexp;
2104 
2105         TEST_BEGIN("spif_regexp_matches_str() function");
2106         s = spif_str_new_from_ptr(regexp_list[0]);
2107         rexp = spif_regexp_new_from_str(s);
2108         spif_str_done(s);
2109         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
2110         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2111         spif_str_done(s);
2112         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
2113         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2114         spif_str_done(s);
2115         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
2116         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2117         spif_str_done(s);
2118         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
2119         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2120         spif_str_done(s);
2121         spif_str_init_from_ptr(s, SPIF_CHARPTR("CDEFG"));
2122         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2123         spif_str_done(s);
2124         spif_regexp_done(rexp);
2125 
2126         spif_str_init_from_ptr(s, regexp_list[1]);
2127         spif_regexp_init_from_str(rexp, s);
2128         spif_str_done(s);
2129         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
2130         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2131         spif_str_done(s);
2132         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
2133         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2134         spif_str_done(s);
2135         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
2136         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2137         spif_str_done(s);
2138         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
2139         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2140         spif_str_done(s);
2141         spif_str_init_from_ptr(s, SPIF_CHARPTR("ABCDE"));
2142         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2143         spif_str_done(s);
2144         spif_regexp_done(rexp);
2145 
2146         spif_str_init_from_ptr(s, regexp_list[2]);
2147         spif_regexp_init_from_str(rexp, s);
2148         spif_str_done(s);
2149         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefghi"));
2150         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2151         spif_str_done(s);
2152         spif_str_init_from_ptr(s, SPIF_CHARPTR("abcdefg"));
2153         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2154         spif_str_done(s);
2155         spif_str_init_from_ptr(s, SPIF_CHARPTR("cdefghi"));
2156         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2157         spif_str_done(s);
2158         spif_str_init_from_ptr(s, SPIF_CHARPTR("cde"));
2159         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2160         spif_str_done(s);
2161         spif_str_init_from_ptr(s, SPIF_CHARPTR("EFGHI"));
2162         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2163         spif_str_done(s);
2164         spif_regexp_done(rexp);
2165 
2166         spif_str_init_from_ptr(s, regexp_list[3]);
2167         spif_regexp_init_from_str(rexp, s);
2168         spif_str_done(s);
2169         spif_str_init_from_ptr(s, SPIF_CHARPTR("127.0.0.1"));
2170         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2171         spif_str_done(s);
2172         spif_str_init_from_ptr(s, SPIF_CHARPTR("4.3.7.1"));
2173         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2174         spif_str_done(s);
2175         spif_str_init_from_ptr(s, SPIF_CHARPTR("192.168.137.111"));
2176         TEST_FAIL_IF(!spif_regexp_matches_str(rexp, s));
2177         spif_str_done(s);
2178         spif_str_init_from_ptr(s, SPIF_CHARPTR("812.555.1212"));
2179         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2180         spif_str_done(s);
2181         spif_str_init_from_ptr(s, SPIF_CHARPTR("..."));
2182         TEST_FAIL_IF(spif_regexp_matches_str(rexp, s));
2183         spif_str_done(s);
2184         spif_regexp_done(rexp);
2185 
2186         TEST_PASS();
2187     }
2188 #endif
2189 
2190     TEST_PASSED("spif_regexp_t");
2191     return 0;
2192 }
2193 
2194 int
main(int argc,char * argv[])2195 main(int argc, char *argv[])
2196 {
2197     int ret = 0;
2198 
2199     USE_VAR(argc);
2200     USE_VAR(argv);
2201 
2202     DEBUG_LEVEL = 0;
2203 
2204     if ((ret = test_macros()) != 0) {
2205         return ret;
2206     }
2207     if ((ret = test_mem()) != 0) {
2208         return ret;
2209     }
2210     if ((ret = test_strings()) != 0) {
2211         return ret;
2212     }
2213     if ((ret = test_snprintf()) != 0) {
2214         return ret;
2215     }
2216     if ((ret = test_options()) != 0) {
2217         return ret;
2218     }
2219     if ((ret = test_obj()) != 0) {
2220         return ret;
2221     }
2222     if ((ret = test_str()) != 0) {
2223         return ret;
2224     }
2225     if ((ret = test_tok()) != 0) {
2226         return ret;
2227     }
2228     if ((ret = test_url()) != 0) {
2229         return ret;
2230     }
2231     if ((ret = test_list()) != 0) {
2232         return ret;
2233     }
2234     if ((ret = test_vector()) != 0) {
2235         return ret;
2236     }
2237     if ((ret = test_map()) != 0) {
2238         return ret;
2239     }
2240     if ((ret = test_socket()) != 0) {
2241         return ret;
2242     }
2243     if ((ret = test_regexp()) != 0) {
2244         return ret;
2245     }
2246     if ((ret = test_hash_functions()) != 0) {
2247         return ret;
2248     }
2249 
2250     printf("All tests passed.\n\n");
2251 
2252     /*MALLOC_DUMP();*/
2253     return 0;
2254 }
2255