ba98bbe036ac87007523f9df0931658ad1987a59
[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 = "(H..).(o..)";
21   SILC_LOG_DEBUG(("Regex %s", regex));
22   if (!silc_regex_compile(&reg, regex, 0))
23     goto err;
24
25   string = "Hello World";
26   SILC_LOG_DEBUG(("Match %s", string));
27   if (!silc_regex_match(&reg, string, num_match, match, 0))
28     goto err;
29   for (i = 0; i < num_match; i++) {
30     if (match[i].start != -1) {
31       SILC_LOG_DEBUG(("Match start %d, end %d", match[i].start,
32                       match[i].end));
33       sub = silc_memdup(string + match[i].start, match[i].end - 
34                         match[i].start);
35       SILC_LOG_DEBUG(("Match substring '%s'", sub));
36       silc_free(sub);
37     }
38   }
39
40   silc_regex_free(&reg);
41
42   regex = "foo[0-9]*";
43   SILC_LOG_DEBUG(("Regex %s", regex));
44   if (!silc_regex_compile(&reg, regex, 0))
45     goto err;
46
47   string = "foo";
48   SILC_LOG_DEBUG(("Match %s", string));
49   if (!silc_regex_match(&reg, string, 0, NULL, 0))
50     goto err;
51
52   string = "foo20";
53   SILC_LOG_DEBUG(("Match %s", string));
54   if (!silc_regex_match(&reg, string, 0, NULL, 0))
55     goto err;
56
57   string = "foo20, bar, foo100, foo";
58   SILC_LOG_DEBUG(("Match all substrings in %s", string));
59   while (silc_regex_match(&reg, string, 1, match, 0)) {
60     SILC_LOG_DEBUG(("Match start %d", match[0].start));
61     sub = silc_memdup(string + match[0].start, match[0].end - match[0].start);
62     SILC_LOG_DEBUG(("Match substring '%s'", sub));
63     silc_free(sub);
64     string += match[0].end;
65   }
66
67   string = "foo20, bar, foo100, Foo, foo0";
68   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
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   silc_regex_free(&reg);
83
84   regex = "^(([^:]+)://)?([^:/]+)(:([0-9]+))?(/.*)";
85   SILC_LOG_DEBUG(("Regex %s", regex));
86   if (!silc_regex_compile(&reg, regex, 0))
87     goto err;
88
89   string = "http://silcnet.org:443/foobar/pelle.html";
90   SILC_LOG_DEBUG(("Parse URI"));
91   if (!silc_regex_match(&reg, string, num_match, match, 0))
92     goto err;
93
94   for (i = 0; i < num_match; i++) {
95     if (match[i].start != -1) {
96       SILC_LOG_DEBUG(("Match start %d", match[i].start));
97       sub = silc_memdup(string + match[i].start, match[i].end - 
98                         match[i].start);
99       SILC_LOG_DEBUG(("Match substring '%s'", sub));
100       silc_free(sub);
101     }
102   }
103
104   string = "http://silcnet.org/";
105   SILC_LOG_DEBUG(("Parse URI"));
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   regex = "((a)(b))";
122   SILC_LOG_DEBUG(("Regex %s", regex));
123   if (!silc_regex_compile(&reg, regex, 0))
124     goto err;
125
126   string = "ab";
127   SILC_LOG_DEBUG(("Match all substrings at once in %s", string));
128   if (!silc_regex_match(&reg, string, num_match, match, 0))
129     goto err;
130
131   for (i = 0; i < num_match; i++) {
132     if (match[i].start != -1) {
133       SILC_LOG_DEBUG(("Match start %d", match[i].start));
134       sub = silc_memdup(string + match[i].start, match[i].end - 
135                         match[i].start);
136       SILC_LOG_DEBUG(("Match substring '%s'", sub));
137       silc_free(sub);
138     }
139   }
140
141   silc_regex_free(&reg);
142
143   success = TRUE;
144
145  err:
146   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
147   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
148
149   return success;
150 }
151