Added silc_subst, regex matching and substitution with the
[silc.git] / lib / silcutil / tests / test_silcbuffmt.c
1 /* Buffer formatting tests */
2
3 #include "silc.h"
4
5 int print(SilcStack stack, SilcBuffer buf, void *value, void *context)
6 {
7   fwrite(silc_buffer_data(buf), 1, silc_buffer_len(buf), stdout);
8   if (!silc_buffer_strchr(buf, '\n', TRUE))
9     printf("\n");
10   fflush(stdout);
11   return silc_buffer_len(buf);
12 }
13
14 int main(int argc, char **argv)
15 {
16   SilcBool success = FALSE;
17   char string[1024], *astring;
18   SilcBufferStruct buf;
19
20   if (argc > 1 && !strcmp(argv[1], "-d")) {
21     silc_log_debug(TRUE);
22     silc_log_quick(TRUE);
23     silc_log_debug_hexdump(TRUE);
24     silc_log_set_debug_string("*buf*,*regex*,*errno*");
25   }
26
27   silc_snprintf(string, sizeof(string), "This is foobar");
28   silc_buffer_set(&buf, string, strlen(string));
29   SILC_LOG_DEBUG(("sed 's/foo/bar/'"));
30   SILC_LOG_DEBUG(("string: %s", string));
31   if (silc_buffer_format(&buf,
32                          SILC_STR_REGEX("foo", 0),
33                            SILC_STR_STRING("bar"),
34                          SILC_STR_END,
35                          SILC_STR_END) < 0)
36     goto err;
37   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
38   if (strcmp("This is barbar", silc_buffer_data(&buf)))
39     goto err;
40
41   silc_snprintf(string, sizeof(string), "This is foobar string!!");
42   astring = silc_memdup(string, strlen(string));
43   silc_buffer_set(&buf, astring, strlen(astring) + 1);
44   SILC_LOG_DEBUG(("sed 's/foo/barbar/g'"));
45   SILC_LOG_DEBUG(("string: %s", astring));
46   if (silc_buffer_format(&buf,
47                          SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
48                                                SILC_STR_REGEX_INCLUSIVE),
49                            SILC_STR_REPLACE("barbar", 6),
50                          SILC_STR_END,
51                          SILC_STR_END) < 0)
52     goto err;
53   silc_buffer_printf(&buf, TRUE);
54   if (strcmp("This is barbarbar string!!", silc_buffer_data(&buf)))
55     goto err;
56   silc_buffer_purge(&buf);
57
58   silc_snprintf(string, sizeof(string), "This is foobar string foo!!");
59   astring = silc_memdup(string, strlen(string));
60   silc_buffer_set(&buf, astring, strlen(astring) + 1);
61   SILC_LOG_DEBUG(("sed 's/foo//g'"));
62   SILC_LOG_DEBUG(("string: %s", astring));
63   if (silc_buffer_format(&buf,
64                          SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL |
65                                                SILC_STR_REGEX_INCLUSIVE),
66                            SILC_STR_REPLACE("", 0),
67                          SILC_STR_END,
68                          SILC_STR_END) < 0)
69     goto err;
70   silc_buffer_printf(&buf, TRUE);
71   if (strcmp("This is bar string !!", silc_buffer_data(&buf)))
72     goto err;
73   silc_buffer_purge(&buf);
74
75   silc_snprintf(string, sizeof(string), "This is foobar\n");
76   silc_buffer_set(&buf, string, strlen(string));
77   SILC_LOG_DEBUG(("sed 's/\\n/\\0/'"));
78   SILC_LOG_DEBUG(("string: %s", string));
79   if (silc_buffer_format(&buf,
80                          SILC_STR_REGEX("\n", 0),
81                            SILC_STR_UINT8(0),
82                          SILC_STR_END,
83                          SILC_STR_END) < 0)
84     goto err;
85   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
86   if (strcmp("This is foobar", silc_buffer_data(&buf)))
87     goto err;
88
89   silc_snprintf(string, sizeof(string), "foo\nfoobar\nbarfoofoo\nbar\n\nfoo");
90   silc_buffer_set(&buf, string, strlen(string));
91   SILC_LOG_DEBUG(("sed 's/foo/bar/g'"));
92   SILC_LOG_DEBUG(("string: %s", string));
93   if (silc_buffer_format(&buf,
94                          SILC_STR_REGEX("foo", SILC_STR_REGEX_NL |
95                                                SILC_STR_REGEX_ALL),
96                            SILC_STR_STRING("bar"),
97                          SILC_STR_END,
98                          SILC_STR_END) < 0)
99     goto err;
100   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
101   if (strcmp("bar\nbarbar\nbarbarbar\nbar\n\nbar", silc_buffer_data(&buf)))
102     goto err;
103
104   silc_snprintf(string, sizeof(string),
105         "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
106   silc_buffer_set(&buf, string, strlen(string));
107   SILC_LOG_DEBUG(("sed '/baz/s/foo/bar/"));
108   SILC_LOG_DEBUG(("string: %s", string));
109   if (silc_buffer_format(&buf,
110                          SILC_STR_REGEX("baz", SILC_STR_REGEX_NL),
111                            SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
112                              SILC_STR_STRING("bar"),
113                            SILC_STR_END,
114                          SILC_STR_END, SILC_STR_END) < 0)
115     goto err;
116   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
117   if (strcmp("foo\nbazbarbar\nbarfoofoo\nbar\nbaz\nbazbar",
118         silc_buffer_data(&buf)))
119     goto err;
120
121   silc_snprintf(string, sizeof(string),
122         "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
123   silc_buffer_set(&buf, string, strlen(string));
124   SILC_LOG_DEBUG(("sed '/baz/!s/foo/bar/"));
125   SILC_LOG_DEBUG(("string: %s", string));
126   if (silc_buffer_format(&buf,
127                          SILC_STR_REGEX("baz", SILC_STR_REGEX_NL |
128                                                SILC_STR_REGEX_NOT),
129                            SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
130                              SILC_STR_STRING("bar"),
131                            SILC_STR_END,
132                          SILC_STR_END, SILC_STR_END) < 0)
133     goto err;
134   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
135   if (strcmp("bar\nbazfoobar\nbarbarbar\nbar\nbaz\nbazfoo",
136         silc_buffer_data(&buf)))
137     goto err;
138
139   SILC_LOG_DEBUG(("Print all lines starting with 'R'"));
140   silc_snprintf(string, sizeof(string),
141         "Rfoo\nbazfoobar\nRbarfoofoo\nRbar\nbaz\nRbazfoo");
142   silc_buffer_set(&buf, string, strlen(string));
143   SILC_LOG_DEBUG(("string: %s", string));
144   if (silc_buffer_unformat(&buf,
145                            SILC_STR_REGEX("^R", SILC_STR_REGEX_NL),
146                              SILC_STR_FUNC(print, NULL, NULL),
147                            SILC_STR_END, SILC_STR_END) < 0)
148     goto err;
149
150   success = TRUE;
151
152  err:
153   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
154   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
155
156   return success;
157 }