• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "iowow.h"
2 #include "iwcfg.h"
3 #include <CUnit/Basic.h>
4 #include "iwutils.h"
5 #include "iwpool.h"
6 
init_suite(void)7 int init_suite(void) {
8   return iw_init();
9 }
10 
clean_suite(void)11 int clean_suite(void) {
12   return 0;
13 }
14 
_replace_mapper1(const char * key,void * op)15 static const char  *_replace_mapper1(const char *key, void *op) {
16   if (!strcmp(key, "{}")) {
17     return "Mother";
18   } else if (!strcmp(key, "you")) {
19     return "I";
20   } else if (!strcmp(key, "?")) {
21     return "?!!";
22   } else {
23     return 0;
24   }
25 }
26 
test_iwu_replace_into(void)27 void test_iwu_replace_into(void) {
28   IWXSTR *res = 0;
29   const char *data = "What you said about my {}?";
30   const char *keys[] = {"{}", "$", "?", "you", "my"};
31   iwrc rc = iwu_replace(&res, data, strlen(data), keys, 5, _replace_mapper1, 0);
32   CU_ASSERT_EQUAL_FATAL(rc, 0);
33   CU_ASSERT_PTR_NOT_NULL_FATAL(res);
34   fprintf(stderr, "\n%s", iwxstr_ptr(res));
35   CU_ASSERT_STRING_EQUAL(iwxstr_ptr(res), "What I said about my Mother?!!");
36   iwxstr_destroy(res);
37 }
38 
39 
test_iwpool_split_string()40 void test_iwpool_split_string() {
41   IWPOOL *pool = iwpool_create(128);
42   CU_ASSERT_PTR_NOT_NULL_FATAL(pool);
43   char **res = iwpool_split_string(pool, " foo , bar:baz,,z,", ",:", true);
44   CU_ASSERT_PTR_NOT_NULL_FATAL(res);
45   int i = 0;
46   for (; res[i]; ++i) {
47     switch (i) {
48       case 0:
49         CU_ASSERT_STRING_EQUAL(res[i], "foo");
50         break;
51       case 1:
52         CU_ASSERT_STRING_EQUAL(res[i], "bar");
53         break;
54       case 2:
55         CU_ASSERT_STRING_EQUAL(res[i], "baz");
56         break;
57       case 3:
58         CU_ASSERT_STRING_EQUAL(res[i], "");
59         break;
60       case 4:
61         CU_ASSERT_STRING_EQUAL(res[i], "z");
62         break;
63     }
64   }
65   CU_ASSERT_EQUAL(i, 5);
66 
67   res = iwpool_split_string(pool, " foo , bar:baz,,z,", ",:", false);
68   CU_ASSERT_PTR_NOT_NULL_FATAL(res);
69   i = 0;
70   for (; res[i]; ++i) {
71     switch (i) {
72       case 0:
73         CU_ASSERT_STRING_EQUAL(res[i], " foo ");
74         break;
75       case 1:
76         CU_ASSERT_STRING_EQUAL(res[i], " bar");
77         break;
78       case 2:
79         CU_ASSERT_STRING_EQUAL(res[i], "baz");
80         break;
81       case 3:
82         CU_ASSERT_STRING_EQUAL(res[i], "");
83         break;
84       case 4:
85         CU_ASSERT_STRING_EQUAL(res[i], "z");
86         break;
87     }
88   }
89   CU_ASSERT_EQUAL(i, 5);
90 
91   res = iwpool_split_string(pool, " foo ", ",", false);
92   CU_ASSERT_PTR_NOT_NULL_FATAL(res);
93   i = 0;
94   for (; res[i]; ++i) {
95     switch (i) {
96       case 0:
97         CU_ASSERT_STRING_EQUAL(res[i], " foo ");
98         break;
99     }
100   }
101   CU_ASSERT_EQUAL(i, 1);
102 
103 
104   res = iwpool_printf_split(pool, ",", true, "%s,%s", "foo", "bar");
105   CU_ASSERT_PTR_NOT_NULL_FATAL(res);
106   i = 0;
107   for (; res[i]; ++i) {
108     switch (i) {
109       case 0:
110         CU_ASSERT_STRING_EQUAL(res[i], "foo");
111         break;
112       case 1:
113         CU_ASSERT_STRING_EQUAL(res[i], "bar");
114         break;
115     }
116   }
117   CU_ASSERT_EQUAL(i, 2);
118 
119 
120   iwpool_destroy(pool);
121 }
122 
123 
test_iwpool_printf()124 void test_iwpool_printf() {
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 
main()133 int main() {
134   CU_pSuite pSuite = NULL;
135 
136   /* Initialize the CUnit test registry */
137   if (CUE_SUCCESS != CU_initialize_registry())
138     return CU_get_error();
139 
140   /* Add a suite to the registry */
141   pSuite = CU_add_suite("iwutils_test1", init_suite, clean_suite);
142 
143   if (NULL == pSuite) {
144     CU_cleanup_registry();
145     return CU_get_error();
146   }
147 
148   /* Add the tests to the suite */
149   if (
150     (NULL == CU_add_test(pSuite, "test_iwu_replace_into", test_iwu_replace_into)) ||
151     (NULL == CU_add_test(pSuite, "test_iwpool_split_string", test_iwpool_split_string)) ||
152     (NULL == CU_add_test(pSuite, "test_iwpool_printf", test_iwpool_printf))
153   ) {
154     CU_cleanup_registry();
155     return CU_get_error();
156   }
157 
158   /* Run all tests using the CUnit Basic interface */
159   CU_basic_set_mode(CU_BRM_VERBOSE);
160   CU_basic_run_tests();
161   int ret = CU_get_error() || CU_get_number_of_failures();
162   CU_cleanup_registry();
163   return ret;
164 }
165