Added SILC_STR_REGEX macro to SILC Buffer Format API.
[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];
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\n");
42   silc_buffer_set(&buf, string, strlen(string));
43   SILC_LOG_DEBUG(("sed 's/\\n/\\0/'"));
44   SILC_LOG_DEBUG(("string: %s", string));
45   if (silc_buffer_format(&buf,
46                          SILC_STR_REGEX("\n", 0),
47                            SILC_STR_UINT8(0),
48                          SILC_STR_END,
49                          SILC_STR_END) < 0)
50     goto err;
51   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
52   if (strcmp("This is foobar", silc_buffer_data(&buf)))
53     goto err;
54
55   silc_snprintf(string, sizeof(string), "foo\nfoobar\nbarfoofoo\nbar\n\nfoo");
56   silc_buffer_set(&buf, string, strlen(string));
57   SILC_LOG_DEBUG(("sed 's/foo/bar/g'"));
58   SILC_LOG_DEBUG(("string: %s", string));
59   if (silc_buffer_format(&buf,
60                          SILC_STR_REGEX("foo", SILC_STR_REGEX_NL |
61                                                SILC_STR_REGEX_ALL),
62                            SILC_STR_STRING("bar"),
63                          SILC_STR_END,
64                          SILC_STR_END) < 0)
65     goto err;
66   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
67   if (strcmp("bar\nbarbar\nbarbarbar\nbar\n\nbar", silc_buffer_data(&buf)))
68     goto err;
69
70   silc_snprintf(string, sizeof(string),
71         "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
72   silc_buffer_set(&buf, string, strlen(string));
73   SILC_LOG_DEBUG(("sed '/baz/s/foo/bar/"));
74   SILC_LOG_DEBUG(("string: %s", string));
75   if (silc_buffer_format(&buf,
76                          SILC_STR_REGEX("baz", SILC_STR_REGEX_NL),
77                            SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
78                              SILC_STR_STRING("bar"),
79                            SILC_STR_END,
80                          SILC_STR_END, SILC_STR_END) < 0)
81     goto err;
82   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
83   if (strcmp("foo\nbazbarbar\nbarfoofoo\nbar\nbaz\nbazbar",
84         silc_buffer_data(&buf)))
85     goto err;
86
87   silc_snprintf(string, sizeof(string),
88         "foo\nbazfoobar\nbarfoofoo\nbar\nbaz\nbazfoo");
89   silc_buffer_set(&buf, string, strlen(string));
90   SILC_LOG_DEBUG(("sed '/baz/!s/foo/bar/"));
91   SILC_LOG_DEBUG(("string: %s", string));
92   if (silc_buffer_format(&buf,
93                          SILC_STR_REGEX("baz", SILC_STR_REGEX_NL |
94                                                SILC_STR_REGEX_NOT),
95                            SILC_STR_REGEX("foo", SILC_STR_REGEX_ALL),
96                              SILC_STR_STRING("bar"),
97                            SILC_STR_END,
98                          SILC_STR_END, SILC_STR_END) < 0)
99     goto err;
100   SILC_LOG_DEBUG(("string: %s", silc_buffer_data(&buf)));
101   if (strcmp("bar\nbazfoobar\nbarbarbar\nbar\nbaz\nbazfoo",
102         silc_buffer_data(&buf)))
103     goto err;
104
105   SILC_LOG_DEBUG(("Print all lines starting with 'R'"));
106   silc_snprintf(string, sizeof(string),
107         "Rfoo\nbazfoobar\nRbarfoofoo\nRbar\nbaz\nRbazfoo");
108   silc_buffer_set(&buf, string, strlen(string));
109   SILC_LOG_DEBUG(("string: %s", string));
110   if (silc_buffer_unformat(&buf,
111                            SILC_STR_REGEX("^R", SILC_STR_REGEX_NL),
112                              SILC_STR_FUNC(print, NULL, NULL),
113                            SILC_STR_END, SILC_STR_END) < 0)
114     goto err;
115
116   success = TRUE;
117
118  err:
119   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
120   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
121
122   return success;
123 }