6e79cb0a83346fbd7062a9b589960bf3a48719b1
[crypto.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[10];
10   int i, num_match = 10;
11   char *regex, *string, *sub;
12
13   if (argc > 1 && !strcmp(argv[1], "-d")) {
14     silc_log_debug(TRUE);
15     silc_log_quick(TRUE);
16     silc_log_debug_hexdump(TRUE);
17     silc_log_set_debug_string("*regex*,*errno*");
18   }
19
20   regex = "foo[0-9]*";
21   SILC_LOG_DEBUG(("Regex %s", regex));
22   if (!silc_regex_compile(&reg, regex, 0))
23     goto err;
24
25   string = "foo";
26   SILC_LOG_DEBUG(("Match %s", string));
27   if (!silc_regex_match(&reg, string, 0, NULL, 0))
28     goto err;
29
30   string = "foo20";
31   SILC_LOG_DEBUG(("Match %s", string));
32   if (!silc_regex_match(&reg, string, 0, NULL, 0))
33     goto err;
34
35   string = "foo20, bar, foo100, foo";
36   SILC_LOG_DEBUG(("Match all substrings in %s", string));
37   while (silc_regex_match(&reg, string, 1, match, 0)) {
38     SILC_LOG_DEBUG(("Match start %d", match[0].start));
39     sub = silc_memdup(string + match[0].start, match[0].end - match[0].start);
40     SILC_LOG_DEBUG(("Match substring '%s'", sub));
41     silc_free(sub);
42     string += match[0].end;
43   }
44
45   string = "foo20, bar, foo100, Foo, foo0";
46   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
47   if (!silc_regex_match(&reg, string, num_match, match, 0))
48     goto err;
49
50   for (i = 0; i < num_match; i++) {
51     if (match[i].start != -1) {
52       SILC_LOG_DEBUG(("Match start %d", match[i].start));
53       sub = silc_memdup(string + match[i].start, match[i].end - 
54                         match[i].start);
55       SILC_LOG_DEBUG(("Match substring '%s'", sub));
56       silc_free(sub);
57     }
58   }
59
60   silc_regex_free(&reg);
61
62   regex = "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)";
63   SILC_LOG_DEBUG(("Regex %s", regex));
64   if (!silc_regex_compile(&reg, regex, 0))
65     goto err;
66
67   string = "http://silcnet.org:443/foobar/pelle.html";
68   SILC_LOG_DEBUG(("Parse URI"));
69   if (!silc_regex_match(&reg, string, num_match, match, 0))
70     goto err;
71
72   for (i = 0; i < num_match; i++) {
73     if (match[i].start != -1) {
74       SILC_LOG_DEBUG(("Match start %d", match[i].start));
75       sub = silc_memdup(string + match[i].start, match[i].end - 
76                         match[i].start);
77       SILC_LOG_DEBUG(("Match substring '%s'", sub));
78       silc_free(sub);
79     }
80   }
81
82   string = "http://silcnet.org/";
83   SILC_LOG_DEBUG(("Parse URI"));
84   if (!silc_regex_match(&reg, string, num_match, match, 0))
85     goto err;
86
87   for (i = 0; i < num_match; i++) {
88     if (match[i].start != -1) {
89       SILC_LOG_DEBUG(("Match start %d", match[i].start));
90       sub = silc_memdup(string + match[i].start, match[i].end - 
91                         match[i].start);
92       SILC_LOG_DEBUG(("Match substring '%s'", sub));
93       silc_free(sub);
94     }
95   }
96
97   silc_regex_free(&reg);
98
99   regex = "((a)(b))";
100   SILC_LOG_DEBUG(("Regex %s", regex));
101   if (!silc_regex_compile(&reg, regex, 0))
102     goto err;
103
104   string = "ab";
105   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
106   if (!silc_regex_match(&reg, string, num_match, match, 0))
107     goto err;
108
109   for (i = 0; i < num_match; i++) {
110     if (match[i].start != -1) {
111       SILC_LOG_DEBUG(("Match start %d", match[i].start));
112       sub = silc_memdup(string + match[i].start, match[i].end - 
113                         match[i].start);
114       SILC_LOG_DEBUG(("Match substring '%s'", sub));
115       silc_free(sub);
116     }
117   }
118
119   silc_regex_free(&reg);
120
121   success = TRUE;
122
123  err:
124   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
125   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
126
127   return success;
128 }
129