1 #include "iowow.h"
2 #include "iwcfg.h"
3 #include <CUnit/Basic.h>
4 #include "iwutils.h"
5 #include "iwpool.h"
6 #include "iwrb.h"
7 #include "iwconv.h"
8
init_suite(void)9 static int init_suite(void) {
10 return iw_init();
11 }
12
clean_suite(void)13 static int clean_suite(void) {
14 return 0;
15 }
16
_replace_mapper1(const char * key,void * op)17 static const char* _replace_mapper1(const char *key, void *op) {
18 if (!strcmp(key, "{}")) {
19 return "Mother";
20 } else if (!strcmp(key, "you")) {
21 return "I";
22 } else if (!strcmp(key, "?")) {
23 return "?!!";
24 } else {
25 return 0;
26 }
27 }
28
test_iwu_replace_into(void)29 static void test_iwu_replace_into(void) {
30 IWXSTR *res = 0;
31 const char *data = "What you said about my {}?";
32 const char *keys[] = { "{}", "$", "?", "you", "my" };
33 iwrc rc = iwu_replace(&res, data, strlen(data), keys, 5, _replace_mapper1, 0);
34 CU_ASSERT_EQUAL_FATAL(rc, 0);
35 CU_ASSERT_PTR_NOT_NULL_FATAL(res);
36 fprintf(stderr, "\n%s", iwxstr_ptr(res));
37 CU_ASSERT_STRING_EQUAL(iwxstr_ptr(res), "What I said about my Mother?!!");
38 iwxstr_destroy(res);
39 }
40
test_iwpool_split_string(void)41 static void test_iwpool_split_string(void) {
42 IWPOOL *pool = iwpool_create(128);
43 CU_ASSERT_PTR_NOT_NULL_FATAL(pool);
44 const char **res = iwpool_split_string(pool, " foo , bar:baz,,z,", ",:", true);
45 CU_ASSERT_PTR_NOT_NULL_FATAL(res);
46 int i = 0;
47 for ( ; res[i]; ++i) {
48 switch (i) {
49 case 0:
50 CU_ASSERT_STRING_EQUAL(res[i], "foo");
51 break;
52 case 1:
53 CU_ASSERT_STRING_EQUAL(res[i], "bar");
54 break;
55 case 2:
56 CU_ASSERT_STRING_EQUAL(res[i], "baz");
57 break;
58 case 3:
59 CU_ASSERT_STRING_EQUAL(res[i], "");
60 break;
61 case 4:
62 CU_ASSERT_STRING_EQUAL(res[i], "z");
63 break;
64 }
65 }
66 CU_ASSERT_EQUAL(i, 5);
67
68 res = iwpool_split_string(pool, " foo , bar:baz,,z,", ",:", false);
69 CU_ASSERT_PTR_NOT_NULL_FATAL(res);
70 i = 0;
71 for ( ; res[i]; ++i) {
72 switch (i) {
73 case 0:
74 CU_ASSERT_STRING_EQUAL(res[i], " foo ");
75 break;
76 case 1:
77 CU_ASSERT_STRING_EQUAL(res[i], " bar");
78 break;
79 case 2:
80 CU_ASSERT_STRING_EQUAL(res[i], "baz");
81 break;
82 case 3:
83 CU_ASSERT_STRING_EQUAL(res[i], "");
84 break;
85 case 4:
86 CU_ASSERT_STRING_EQUAL(res[i], "z");
87 break;
88 }
89 }
90 CU_ASSERT_EQUAL(i, 5);
91
92 res = iwpool_split_string(pool, " foo ", ",", false);
93 CU_ASSERT_PTR_NOT_NULL_FATAL(res);
94 i = 0;
95 for ( ; res[i]; ++i) {
96 switch (i) {
97 case 0:
98 CU_ASSERT_STRING_EQUAL(res[i], " foo ");
99 break;
100 }
101 }
102 CU_ASSERT_EQUAL(i, 1);
103
104
105 res = iwpool_printf_split(pool, ",", true, "%s,%s", "foo", "bar");
106 CU_ASSERT_PTR_NOT_NULL_FATAL(res);
107 i = 0;
108 for ( ; res[i]; ++i) {
109 switch (i) {
110 case 0:
111 CU_ASSERT_STRING_EQUAL(res[i], "foo");
112 break;
113 case 1:
114 CU_ASSERT_STRING_EQUAL(res[i], "bar");
115 break;
116 }
117 }
118 CU_ASSERT_EQUAL(i, 2);
119
120
121 iwpool_destroy(pool);
122 }
123
test_iwpool_printf(void)124 static void test_iwpool_printf(void) {
125 IWPOOL *pool = iwpool_create(128);
126 CU_ASSERT_PTR_NOT_NULL_FATAL(pool);
127 const char *res = iwpool_printf(pool, "%s=%s", "foo", "bar");
128 CU_ASSERT_PTR_NOT_NULL_FATAL(pool);
129 CU_ASSERT_STRING_EQUAL(res, "foo=bar");
130 iwpool_destroy(pool);
131 }
132
test_iwrb1(void)133 static void test_iwrb1(void) {
134 int *p;
135 IWRB_ITER iter;
136 IWRB *rb = iwrb_create(sizeof(int), 7);
137 CU_ASSERT_PTR_NOT_NULL_FATAL(rb);
138 CU_ASSERT_EQUAL(iwrb_num_cached(rb), 0);
139 int idx = 0;
140 int data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
141
142 iwrb_put(rb, &data[idx++]);
143 CU_ASSERT_EQUAL(iwrb_num_cached(rb), 1);
144
145 iwrb_iter_init(rb, &iter);
146 p = iwrb_iter_prev(&iter);
147 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
148 CU_ASSERT_EQUAL(*p, 1);
149 p = iwrb_iter_prev(&iter);
150 CU_ASSERT_PTR_NULL(p);
151 p = iwrb_peek(rb);
152 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
153 CU_ASSERT_EQUAL(*p, 1);
154
155 for (int i = 0; i < 6; ++i) {
156 iwrb_put(rb, &data[i + 1]);
157 }
158 p = iwrb_peek(rb);
159 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
160 CU_ASSERT_EQUAL(*p, 7);
161
162 iwrb_iter_init(rb, &iter);
163 for (int i = 7; i > 0; --i) {
164 p = iwrb_iter_prev(&iter);
165 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
166 CU_ASSERT_EQUAL(*p, i);
167 }
168 CU_ASSERT_PTR_NULL(iwrb_iter_prev(&iter));
169 iwrb_put(rb, &data[7]);
170 p = iwrb_peek(rb);
171 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
172 CU_ASSERT_EQUAL(*p, 8);
173
174 iwrb_iter_init(rb, &iter);
175 for (int i = 8; i > 1; --i) {
176 p = iwrb_iter_prev(&iter);
177 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
178 CU_ASSERT_EQUAL(*p, i);
179 }
180 CU_ASSERT_PTR_NULL(iwrb_iter_prev(&iter));
181
182 for (int i = 8; i < 14; ++i) {
183 iwrb_put(rb, &data[i]);
184 }
185
186 iwrb_iter_init(rb, &iter);
187 for (int i = 0; i < 7; ++i) {
188 p = iwrb_iter_prev(&iter);
189 CU_ASSERT_PTR_NOT_NULL_FATAL(p);
190 CU_ASSERT_EQUAL(*p, 14 - i);
191 }
192 CU_ASSERT_PTR_NULL(iwrb_iter_prev(&iter));
193
194 iwrb_destroy(&rb);
195 CU_ASSERT_PTR_NULL(rb);
196 }
197
iwitoa_issue48(void)198 static void iwitoa_issue48(void) {
199 char buf[IWNUMBUF_SIZE];
200 int len = iwitoa(INT64_MIN, buf, sizeof(buf));
201 CU_ASSERT_EQUAL(len, 20);
202 CU_ASSERT_STRING_EQUAL("-9223372036854775808", buf);
203 }
204
main(void)205 int main(void) {
206 CU_pSuite pSuite = NULL;
207
208 /* Initialize the CUnit test registry */
209 if (CUE_SUCCESS != CU_initialize_registry()) {
210 return CU_get_error();
211 }
212
213 /* Add a suite to the registry */
214 pSuite = CU_add_suite("iwutils_test1", init_suite, clean_suite);
215
216 if (NULL == pSuite) {
217 CU_cleanup_registry();
218 return CU_get_error();
219 }
220
221 /* Add the tests to the suite */
222 if ( (NULL == CU_add_test(pSuite, "test_iwu_replace_into", test_iwu_replace_into))
223 || (NULL == CU_add_test(pSuite, "test_iwpool_split_string", test_iwpool_split_string))
224 || (NULL == CU_add_test(pSuite, "test_iwpool_printf", test_iwpool_printf))
225 || (NULL == CU_add_test(pSuite, "test_iwrb1", test_iwrb1))
226 || (NULL == CU_add_test(pSuite, "iwitoa_issue48", iwitoa_issue48))) {
227 CU_cleanup_registry();
228 return CU_get_error();
229 }
230
231 /* Run all tests using the CUnit Basic interface */
232 CU_basic_set_mode(CU_BRM_VERBOSE);
233 CU_basic_run_tests();
234 int ret = CU_get_error() || CU_get_number_of_failures();
235 CU_cleanup_registry();
236 return ret;
237 }
238