Added SILC Thread Queue API
[silc.git] / lib / silcutil / tests / test_silcregex.c
1 /* Regex tests */
2
3 #include "silc.h"
4
5 int main(int argc, char **argv)
6 {
7   SilcBool success = FALSE;
8   SilcRegexStruct reg;
9   SilcRegexMatchStruct match[20];
10   int i, num_match = 20;
11   char *regex, *string, *sub;
12   SilcBufferStruct bmatch;
13
14   if (argc > 1 && !strcmp(argv[1], "-d")) {
15     silc_log_debug(TRUE);
16     silc_log_quick(TRUE);
17     silc_log_debug_hexdump(TRUE);
18     silc_log_set_debug_string("*regex*,*errno*,*buffmt*");
19   }
20
21   string = silc_strdup("foobar");
22   SILC_LOG_DEBUG(("Replace %s", string));
23   silc_buffer_set(&bmatch, string, strlen(string));
24   if (!silc_subst(&bmatch, "s/foo/bar/"))
25     goto err;
26   silc_buffer_printf(&bmatch, TRUE);
27   if (!silc_buffer_memcmp(&bmatch, "barbar", 6))
28     goto err;
29   silc_buffer_purge(&bmatch);
30
31   string = silc_strdup("foobar foobar");
32   SILC_LOG_DEBUG(("Replace %s", string));
33   silc_buffer_set(&bmatch, string, strlen(string));
34   if (!silc_subst(&bmatch, "s/foo/bar/g"))
35     goto err;
36   silc_buffer_printf(&bmatch, TRUE);
37   if (!silc_buffer_memcmp(&bmatch, "barbar barbar", 13))
38     goto err;
39   silc_buffer_purge(&bmatch);
40
41   string = silc_strdup("foobar foobar");
42   SILC_LOG_DEBUG(("Replace %s", string));
43   silc_buffer_set(&bmatch, string, strlen(string));
44   if (!silc_subst(&bmatch, "s/foo/bar/"))
45     goto err;
46   silc_buffer_printf(&bmatch, TRUE);
47   if (!silc_buffer_memcmp(&bmatch, "barbar foobar", 13))
48     goto err;
49   silc_buffer_purge(&bmatch);
50
51   string = silc_strdup("foobar\nfoobar foobar\nfoobar");
52   SILC_LOG_DEBUG(("Replace %s", string));
53   silc_buffer_set(&bmatch, string, strlen(string));
54   if (!silc_subst(&bmatch, "s/foo/BARBAR/g"))
55     goto err;
56   silc_buffer_printf(&bmatch, TRUE);
57   if (!silc_buffer_memcmp(&bmatch, "BARBARbar\nBARBARbar BARBARbar\nBARBARbar",
58                           39))
59     goto err;
60   silc_buffer_purge(&bmatch);
61
62   string = silc_strdup("foobar\nfoobar foobar\nfoobar");
63   SILC_LOG_DEBUG(("Replace %s", string));
64   silc_buffer_set(&bmatch, string, strlen(string));
65   if (!silc_subst(&bmatch, "s/foo/BARBAR/"))
66     goto err;
67   silc_buffer_printf(&bmatch, TRUE);
68   if (!silc_buffer_memcmp(&bmatch, "BARBARbar\nBARBARbar foobar\nBARBARbar",
69                           36))
70     goto err;
71   silc_buffer_purge(&bmatch);
72
73   string = silc_strdup("foobar\nfoobar foobar\nfoobar");
74   SILC_LOG_DEBUG(("Replace %s", string));
75   silc_buffer_set(&bmatch, string, strlen(string));
76   if (!silc_subst(&bmatch, "s/foo//"))
77     goto err;
78   silc_buffer_printf(&bmatch, TRUE);
79   if (!silc_buffer_memcmp(&bmatch, "bar\nbar foobar\nbar",
80                           18))
81     goto err;
82   silc_buffer_purge(&bmatch);
83
84   string = silc_strdup("foobar\nfoobar foobar\nfoobar");
85   SILC_LOG_DEBUG(("Replace %s", string));
86   silc_buffer_set(&bmatch, string, strlen(string));
87   if (!silc_subst(&bmatch, "s/foo/B/g"))
88     goto err;
89   silc_buffer_printf(&bmatch, TRUE);
90   if (!silc_buffer_memcmp(&bmatch, "Bbar\nBbar Bbar\nBbar",
91                           19))
92     goto err;
93   silc_buffer_purge(&bmatch);
94
95   string = silc_strdup("foobar\nfoobar foobar\nfoobar");
96   SILC_LOG_DEBUG(("Replace %s", string));
97   silc_buffer_set(&bmatch, string, strlen(string));
98   if (!silc_subst(&bmatch, "s/foo/B/"))
99     goto err;
100   silc_buffer_printf(&bmatch, TRUE);
101   if (!silc_buffer_memcmp(&bmatch, "Bbar\nBbar foobar\nBbar",
102                           21))
103     goto err;
104   silc_buffer_purge(&bmatch);
105
106   string = silc_strdup("foobar\nBfoobar foobar\nBfoobar\nfoo");
107   SILC_LOG_DEBUG(("Replace %s", string));
108   silc_buffer_set(&bmatch, string, strlen(string));
109   if (!silc_subst(&bmatch, "/^B/s/foo/B/g"))
110     goto err;
111   silc_buffer_printf(&bmatch, TRUE);
112   if (!silc_buffer_memcmp(&bmatch, "foobar\nBBbar Bbar\nBBbar\nfoo",
113                           27))
114     goto err;
115   silc_buffer_purge(&bmatch);
116
117   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
118   SILC_LOG_DEBUG(("Replace %s", string));
119   silc_buffer_set(&bmatch, string, strlen(string));
120   if (!silc_subst(&bmatch, "/baz/s/foo/BARBAR/g"))
121     goto err;
122   silc_buffer_printf(&bmatch, TRUE);
123   if (!silc_buffer_memcmp(&bmatch,
124                           "foobar\nBARBARbar baz BARBARbar\nbazBARBARbar\nfoo",
125                           47))
126     goto err;
127   silc_buffer_purge(&bmatch);
128
129   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
130   SILC_LOG_DEBUG(("Replace %s", string));
131   silc_buffer_set(&bmatch, string, strlen(string));
132   if (!silc_subst(&bmatch, "/baz/s/foo/BARBAR/"))
133     goto err;
134   silc_buffer_printf(&bmatch, TRUE);
135   if (!silc_buffer_memcmp(&bmatch,
136                           "foobar\nBARBARbar baz foobar\nbazBARBARbar\nfoo",
137                           44))
138     goto err;
139   silc_buffer_purge(&bmatch);
140
141   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
142   SILC_LOG_DEBUG(("Replace %s", string));
143   silc_buffer_set(&bmatch, string, strlen(string));
144   if (!silc_subst(&bmatch, "/baz/!s/foo/BARBAR/"))
145     goto err;
146   silc_buffer_printf(&bmatch, TRUE);
147   if (!silc_buffer_memcmp(&bmatch,
148                           "BARBARbar\nfoobar baz foobar\nbazfoobar\nBARBAR",
149                           44))
150     goto err;
151   silc_buffer_purge(&bmatch);
152
153   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
154   SILC_LOG_DEBUG(("Replace %s", string));
155   silc_buffer_set(&bmatch, string, strlen(string));
156   if (!silc_subst(&bmatch, "2s/foo/BARBAR/"))
157     goto err;
158   silc_buffer_printf(&bmatch, TRUE);
159   if (!silc_buffer_memcmp(&bmatch,
160                           "foobar\nBARBARbar baz foobar\nbazfoobar\nfoo",
161                           41))
162     goto err;
163   silc_buffer_purge(&bmatch);
164
165   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
166   SILC_LOG_DEBUG(("Replace %s", string));
167   silc_buffer_set(&bmatch, string, strlen(string));
168   if (!silc_subst(&bmatch, "2s/foo/BARBAR/g"))
169     goto err;
170   silc_buffer_printf(&bmatch, TRUE);
171   if (!silc_buffer_memcmp(&bmatch,
172                           "foobar\nBARBARbar baz BARBARbar\nbazfoobar\nfoo",
173                           44))
174     goto err;
175   silc_buffer_purge(&bmatch);
176
177   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
178   SILC_LOG_DEBUG(("Replace %s", string));
179   silc_buffer_set(&bmatch, string, strlen(string));
180   if (!silc_subst(&bmatch, "200s/foo/BARBAR/g"))
181     goto err;
182   silc_buffer_printf(&bmatch, TRUE);
183   if (!silc_buffer_memcmp(&bmatch,
184                           "foobar\nfoobar baz foobar\nbazfoobar\nfoo",
185                           38))
186     goto err;
187   silc_buffer_purge(&bmatch);
188
189   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
190   SILC_LOG_DEBUG(("Replace %s", string));
191   silc_buffer_set(&bmatch, string, strlen(string));
192   if (!silc_subst(&bmatch, "2!s/foo/BARBAR/g"))
193     goto err;
194   silc_buffer_printf(&bmatch, TRUE);
195   if (!silc_buffer_memcmp(&bmatch,
196                           "BARBARbar\nfoobar baz foobar\nbazBARBARbar\nBARBAR",
197                           47))
198     goto err;
199   silc_buffer_purge(&bmatch);
200
201   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
202   SILC_LOG_DEBUG(("Replace %s", string));
203   silc_buffer_set(&bmatch, string, strlen(string));
204   if (!silc_subst(&bmatch, "/xxx/s/foo/BARBAR/g"))
205     goto err;
206   silc_buffer_printf(&bmatch, TRUE);
207   if (!silc_buffer_memcmp(&bmatch,
208                           "foobar\nfoobar baz foobar\nbazfoobar\nfoo",
209                           38))
210     goto err;
211   silc_buffer_purge(&bmatch);
212
213   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
214   SILC_LOG_DEBUG(("Replace %s", string));
215   silc_buffer_set(&bmatch, string, strlen(string));
216   if (!silc_subst(&bmatch, "!s/foo/BARBAR/g"))
217     goto err;
218   silc_buffer_printf(&bmatch, TRUE);
219   if (!silc_buffer_memcmp(&bmatch,
220                           "foobar\nfoobar baz foobar\nbazfoobar\nfoo",
221                           38))
222     goto err;
223   silc_buffer_purge(&bmatch);
224
225   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
226   SILC_LOG_DEBUG(("Replace %s", string));
227   silc_buffer_set(&bmatch, string, strlen(string));
228   if (!silc_subst(&bmatch, "$s/foo/BARBAR/g"))
229     goto err;
230   silc_buffer_printf(&bmatch, TRUE);
231   if (!silc_buffer_memcmp(&bmatch,
232                           "foobar\nfoobar baz foobar\nbazfoobar\nBARBAR",
233                           41))
234     goto err;
235   silc_buffer_purge(&bmatch);
236
237   string = silc_strdup("foobar\nfoobar baz foobar\nbazfoobar\nfoo");
238   SILC_LOG_DEBUG(("Replace %s", string));
239   silc_buffer_set(&bmatch, string, strlen(string));
240   if (!silc_subst(&bmatch, "$!s/foo/BARBAR/g"))
241     goto err;
242   silc_buffer_printf(&bmatch, TRUE);
243   if (!silc_buffer_memcmp(&bmatch,
244                 "BARBARbar\nBARBARbar baz BARBARbar\nbazBARBARbar\nfoo",
245                           50))
246     goto err;
247   silc_buffer_purge(&bmatch);
248
249   string = silc_strdup("foobar\nfoobar /baz/ foobar\nbazfoobar\nfoo");
250   SILC_LOG_DEBUG(("Replace %s", string));
251   silc_buffer_set(&bmatch, string, strlen(string));
252   if (!silc_subst(&bmatch, "s/\\//BARBAR/g"))
253     goto err;
254   silc_buffer_printf(&bmatch, TRUE);
255   if (!silc_buffer_memcmp(&bmatch,
256                 "foobar\nfoobar BARBARbazBARBAR foobar\nbazfoobar\nfoo",
257                           50))
258     goto err;
259   silc_buffer_purge(&bmatch);
260
261   string = silc_strdup("foobar\nfoobar /baz/ foobar\nbazfoobar\nfoo");
262   SILC_LOG_DEBUG(("Replace %s", string));
263   silc_buffer_set(&bmatch, string, strlen(string));
264   if (!silc_subst(&bmatch, "s/\\//\\/\\//g"))
265     goto err;
266   silc_buffer_printf(&bmatch, TRUE);
267   if (!silc_buffer_memcmp(&bmatch,
268                 "foobar\nfoobar //baz// foobar\nbazfoobar\nfoo",
269                           42))
270     goto err;
271   silc_buffer_purge(&bmatch);
272
273   regex = ".{5}";
274   SILC_LOG_DEBUG(("Regex %s", regex));
275   string = "abcdefghijklmn";
276   SILC_LOG_DEBUG(("Match %s", string));
277   if (!silc_regex(string, regex, &bmatch, NULL))
278     goto err;
279   silc_buffer_printf(&bmatch, TRUE);
280
281   regex = ".....";
282   SILC_LOG_DEBUG(("Regex %s", regex));
283   string = "abcdefghijklmn";
284   SILC_LOG_DEBUG(("Match %s", string));
285   if (!silc_regex(string, regex, &bmatch, NULL))
286     goto err;
287   silc_buffer_printf(&bmatch, TRUE);
288
289   regex = "^a{0}$";
290   SILC_LOG_DEBUG(("Regex %s", regex));
291   string = "";
292   SILC_LOG_DEBUG(("Match %s", string));
293   if (!silc_regex(string, regex, &bmatch, NULL))
294     goto err;
295   silc_buffer_printf(&bmatch, TRUE);
296
297   regex = "^a{0,}$";
298   SILC_LOG_DEBUG(("Regex %s", regex));
299   string = "bbbb";
300   SILC_LOG_DEBUG(("Match %s", string));
301   if (!silc_regex(string, regex, &bmatch, NULL))
302     goto err;
303   silc_buffer_printf(&bmatch, TRUE);
304
305   regex = "^a{0,}$";
306   SILC_LOG_DEBUG(("Regex %s", regex));
307   string = "aaaaaaaaa";
308   SILC_LOG_DEBUG(("Match %s", string));
309   if (!silc_regex(string, regex, &bmatch, NULL))
310     goto err;
311   silc_buffer_printf(&bmatch, TRUE);
312
313   regex = "^a{0,0}$";
314   SILC_LOG_DEBUG(("Regex %s", regex));
315   string = "a";
316   SILC_LOG_DEBUG(("DO NOT Match %s", string));
317   if (silc_regex(string, regex, &bmatch, NULL))
318     goto err;
319
320   regex = "^a{3}$";
321   SILC_LOG_DEBUG(("Regex %s", regex));
322   string = "aaa";
323   SILC_LOG_DEBUG(("Match %s", string));
324   if (!silc_regex(string, regex, &bmatch, NULL))
325     goto err;
326   silc_buffer_printf(&bmatch, TRUE);
327
328   regex = "^a{3}$";
329   SILC_LOG_DEBUG(("Regex %s", regex));
330   string = "aaaa";
331   SILC_LOG_DEBUG(("DO NOT Match %s", string));
332   if (silc_regex(string, regex, &bmatch, NULL))
333     goto err;
334
335   regex = "^a{3,5}$";
336   SILC_LOG_DEBUG(("Regex %s", regex));
337   string = "aaa";
338   SILC_LOG_DEBUG(("Match %s", string));
339   if (!silc_regex(string, regex, &bmatch, NULL))
340     goto err;
341   silc_buffer_printf(&bmatch, TRUE);
342
343   regex = "^a{3,5}$";
344   SILC_LOG_DEBUG(("Regex %s", regex));
345   string = "aaaa";
346   SILC_LOG_DEBUG(("Match %s", string));
347   if (!silc_regex(string, regex, &bmatch, NULL))
348     goto err;
349   silc_buffer_printf(&bmatch, TRUE);
350
351   regex = "^a{3,5}$";
352   SILC_LOG_DEBUG(("Regex %s", regex));
353   string = "aaaaaa";
354   SILC_LOG_DEBUG(("DO NOT Match %s", string));
355   if (silc_regex(string, regex, &bmatch, NULL))
356     goto err;
357
358   regex = "^a{3,}$";
359   SILC_LOG_DEBUG(("Regex %s", regex));
360   string = "aaa";
361   SILC_LOG_DEBUG(("Match %s", string));
362   if (!silc_regex(string, regex, &bmatch, NULL))
363     goto err;
364   silc_buffer_printf(&bmatch, TRUE);
365
366   regex = "^a{3,}$";
367   SILC_LOG_DEBUG(("Regex %s", regex));
368   string = "aaaaaaaaaaaaa";
369   SILC_LOG_DEBUG(("Match %s", string));
370   if (!silc_regex(string, regex, &bmatch, NULL))
371     goto err;
372   silc_buffer_printf(&bmatch, TRUE);
373
374   regex = "^a{3,}$";
375   SILC_LOG_DEBUG(("Regex %s", regex));
376   string = "aa";
377   SILC_LOG_DEBUG(("DO NOT Match %s", string));
378   if (silc_regex(string, regex, &bmatch, NULL))
379     goto err;
380
381
382   regex = "a*b";
383   SILC_LOG_DEBUG(("Regex %s", regex));
384   string = "b";
385   SILC_LOG_DEBUG(("Match %s", string));
386   if (!silc_regex(string, regex, &bmatch, NULL))
387     goto err;
388   silc_buffer_printf(&bmatch, TRUE);
389
390   regex = "a*b";
391   SILC_LOG_DEBUG(("Regex %s", regex));
392   string = "ab";
393   SILC_LOG_DEBUG(("Match %s", string));
394   if (!silc_regex(string, regex, &bmatch, NULL))
395     goto err;
396   silc_buffer_printf(&bmatch, TRUE);
397
398   regex = "a*b";
399   SILC_LOG_DEBUG(("Regex %s", regex));
400   string = "aaaab";
401   SILC_LOG_DEBUG(("Match %s", string));
402   if (!silc_regex(string, regex, &bmatch, NULL))
403     goto err;
404   silc_buffer_printf(&bmatch, TRUE);
405
406
407   regex = "a+b";
408   SILC_LOG_DEBUG(("Regex %s", regex));
409   string = "ab";
410   SILC_LOG_DEBUG(("Match %s", string));
411   if (!silc_regex(string, regex, &bmatch, NULL))
412     goto err;
413   silc_buffer_printf(&bmatch, TRUE);
414
415   regex = "a+b";
416   SILC_LOG_DEBUG(("Regex %s", regex));
417   string = "aaaab";
418   SILC_LOG_DEBUG(("Match %s", string));
419   if (!silc_regex(string, regex, &bmatch, NULL))
420     goto err;
421   silc_buffer_printf(&bmatch, TRUE);
422
423   regex = "a+b";
424   SILC_LOG_DEBUG(("Regex %s", regex));
425   string = "b";
426   SILC_LOG_DEBUG(("DO NOT Match %s", string));
427   if (silc_regex(string, regex, &bmatch, NULL))
428     goto err;
429
430
431   regex = "ca?b";
432   SILC_LOG_DEBUG(("Regex %s", regex));
433   string = "cb";
434   SILC_LOG_DEBUG(("Match %s", string));
435   if (!silc_regex(string, regex, &bmatch, NULL))
436     goto err;
437   silc_buffer_printf(&bmatch, TRUE);
438
439   regex = "ca?b";
440   SILC_LOG_DEBUG(("Regex %s", regex));
441   string = "cab";
442   SILC_LOG_DEBUG(("Match %s", string));
443   if (!silc_regex(string, regex, &bmatch, NULL))
444     goto err;
445   silc_buffer_printf(&bmatch, TRUE);
446
447   regex = "ca?b";
448   SILC_LOG_DEBUG(("Regex %s", regex));
449   string = "caab";
450   SILC_LOG_DEBUG(("DO NOT Match %s", string));
451   if (silc_regex(string, regex, &bmatch, NULL))
452     goto err;
453
454   regex = "(H..).(o..)";
455   SILC_LOG_DEBUG(("Regex %s", regex));
456   if (!silc_regex_compile(&reg, regex, 0))
457     goto err;
458
459   string = "Hello World";
460   SILC_LOG_DEBUG(("Match %s", string));
461   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
462     goto err;
463   for (i = 0; i < num_match; i++) {
464     if (match[i].start != -1) {
465       SILC_LOG_DEBUG(("Match start %d, end %d", match[i].start,
466                       match[i].end));
467       sub = silc_memdup(string + match[i].start, match[i].end -
468                         match[i].start);
469       SILC_LOG_DEBUG(("Match substring '%s'", sub));
470       silc_free(sub);
471     }
472   }
473
474   silc_regex_free(&reg);
475
476   regex = "foo[0-9]*";
477   SILC_LOG_DEBUG(("Regex %s", regex));
478   if (!silc_regex_compile(&reg, regex, 0))
479     goto err;
480
481   string = "foo";
482   SILC_LOG_DEBUG(("Match %s", string));
483   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
484     goto err;
485
486   string = "foo20";
487   SILC_LOG_DEBUG(("Match %s", string));
488   if (!silc_regex_match(&reg, string, strlen(string), 0, NULL, 0))
489     goto err;
490
491   string = "foo20, bar, foo100, foo";
492   SILC_LOG_DEBUG(("Match all substrings in %s", string));
493   while (silc_regex_match(&reg, string, strlen(string), 1, match, 0)) {
494     SILC_LOG_DEBUG(("Match start %d", match[0].start));
495     sub = silc_memdup(string + match[0].start, match[0].end - match[0].start);
496     SILC_LOG_DEBUG(("Match substring '%s'", sub));
497     silc_free(sub);
498     string += match[0].end;
499   }
500
501   string = "foo20, bar, foo100, Foo, foo0";
502   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
503   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
504     goto err;
505
506   for (i = 0; i < num_match; i++) {
507     if (match[i].start != -1) {
508       SILC_LOG_DEBUG(("Match start %d", match[i].start));
509       sub = silc_memdup(string + match[i].start, match[i].end -
510                         match[i].start);
511       SILC_LOG_DEBUG(("Match substring '%s'", sub));
512       silc_free(sub);
513     }
514   }
515
516   silc_regex_free(&reg);
517
518   regex = "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)";
519   SILC_LOG_DEBUG(("Regex %s", regex));
520   if (!silc_regex_compile(&reg, regex, 0))
521     goto err;
522
523   string = "http://silcnet.org:443/foobar/pelle.html";
524   SILC_LOG_DEBUG(("Parse URI"));
525   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
526     goto err;
527
528   for (i = 0; i < num_match; i++) {
529     if (match[i].start != -1) {
530       SILC_LOG_DEBUG(("Match start %d", match[i].start));
531       sub = silc_memdup(string + match[i].start, match[i].end -
532                         match[i].start);
533       SILC_LOG_DEBUG(("Match substring '%s'", sub));
534       silc_free(sub);
535     }
536   }
537
538   string = "http://silcnet.org/";
539   SILC_LOG_DEBUG(("Parse URI"));
540   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
541     goto err;
542
543   for (i = 0; i < num_match; i++) {
544     if (match[i].start != -1) {
545       SILC_LOG_DEBUG(("Match start %d", match[i].start));
546       sub = silc_memdup(string + match[i].start, match[i].end -
547                         match[i].start);
548       SILC_LOG_DEBUG(("Match substring '%s'", sub));
549       silc_free(sub);
550     }
551   }
552
553   silc_regex_free(&reg);
554
555   regex = "((a)(b))";
556   SILC_LOG_DEBUG(("Regex %s", regex));
557   if (!silc_regex_compile(&reg, regex, 0))
558     goto err;
559
560   string = "ab";
561   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
562   if (!silc_regex_match(&reg, string, strlen(string), num_match, match, 0))
563     goto err;
564
565   for (i = 0; i < num_match; i++) {
566     if (match[i].start != -1) {
567       SILC_LOG_DEBUG(("Match start %d", match[i].start));
568       sub = silc_memdup(string + match[i].start, match[i].end -
569                         match[i].start);
570       SILC_LOG_DEBUG(("Match substring '%s'", sub));
571       silc_free(sub);
572     }
573   }
574
575   silc_regex_free(&reg);
576
577   regex = "^a";
578   SILC_LOG_DEBUG(("Regex %s", regex));
579   if (!silc_regex_compile(&reg, regex, 0))
580     goto err;
581
582   string = "a";
583   SILC_LOG_DEBUG(("Test NOTBOL flag", string));
584   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
585                        SILC_REGEX_NOTBOL))
586     goto err;
587   if (silc_errno != SILC_ERR_NOT_FOUND)
588     goto err;
589   SILC_LOG_DEBUG(("Did not match (OK)"));
590
591   silc_regex_free(&reg);
592
593   regex = "a$";
594   SILC_LOG_DEBUG(("Regex %s", regex));
595   if (!silc_regex_compile(&reg, regex, 0))
596     goto err;
597
598   string = "a";
599   SILC_LOG_DEBUG(("Test NOTEOL flag", string));
600   if (silc_regex_match(&reg, string, strlen(string), 0, NULL,
601                        SILC_REGEX_NOTEOL))
602     goto err;
603   if (silc_errno != SILC_ERR_NOT_FOUND)
604     goto err;
605   SILC_LOG_DEBUG(("Did not match (OK)"));
606
607   silc_regex_free(&reg);
608
609   success = TRUE;
610
611  err:
612   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
613   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
614
615   return success;
616 }