unit-tests: Move test suites to its own subfolder
[strongswan.git] / src / libstrongswan / tests / suites / test_enum.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "test_suite.h"
17
18 #include <utils/enum.h>
19 #include <utils/utils.h>
20
21 /*******************************************************************************
22 * continuous enum
23 */
24 enum {
25 CONT1,
26 CONT2,
27 CONT3,
28 CONT4,
29 CONT5,
30 } test_enum_cont;
31
32 /* can't be static */
33 enum_name_t *test_enum_cont_names;
34
35 ENUM_BEGIN(test_enum_cont_names, CONT1, CONT5,
36 "CONT1", "CONT2", "CONT3", "CONT4", "CONT5");
37 ENUM_END(test_enum_cont_names, CONT5);
38
39 /*******************************************************************************
40 * split enum
41 */
42 enum {
43 SPLIT1 = 1,
44 SPLIT2,
45 SPLIT3 = 5,
46 SPLIT4,
47 SPLIT5 = 255,
48 } test_enum_split;
49
50 /* can't be static */
51 enum_name_t *test_enum_split_names;
52
53 ENUM_BEGIN(test_enum_split_names, SPLIT1, SPLIT2,
54 "SPLIT1", "SPLIT2");
55 ENUM_NEXT(test_enum_split_names, SPLIT3, SPLIT4, SPLIT2,
56 "SPLIT3", "SPLIT4");
57 ENUM_NEXT(test_enum_split_names, SPLIT5, SPLIT5, SPLIT4,
58 "SPLIT5");
59 ENUM_END(test_enum_split_names, SPLIT5);
60
61 /*******************************************************************************
62 * enum_to_name
63 */
64
65 static struct {
66 int val;
67 char *str;
68 } name_tests_cont[] = {
69 {-1, NULL},
70 {CONT1, "CONT1"},
71 {CONT2, "CONT2"},
72 {CONT3, "CONT3"},
73 {CONT4, "CONT4"},
74 {CONT5, "CONT5"},
75 {5, NULL},
76 }, name_tests_split[] = {
77 {-1, NULL},
78 {0, NULL},
79 {SPLIT1, "SPLIT1"},
80 {SPLIT2, "SPLIT2"},
81 {3, NULL},
82 {4, NULL},
83 {SPLIT3, "SPLIT3"},
84 {SPLIT4, "SPLIT4"},
85 {7, NULL},
86 {254, NULL},
87 {SPLIT5, "SPLIT5"},
88 {256, NULL},
89 };
90
91 START_TEST(test_enum_to_name_cont)
92 {
93 char *str = enum_to_name(test_enum_cont_names, name_tests_cont[_i].val);
94 if (str)
95 {
96 ck_assert_str_eq(str, name_tests_cont[_i].str);
97 }
98 else
99 {
100 ck_assert(str == name_tests_cont[_i].str);
101 }
102 }
103 END_TEST
104
105 START_TEST(test_enum_to_name_split)
106 {
107 char *str = enum_to_name(test_enum_split_names, name_tests_split[_i].val);
108 if (str)
109 {
110 ck_assert_str_eq(str, name_tests_split[_i].str);
111 }
112 else
113 {
114 ck_assert(str == name_tests_split[_i].str);
115 }
116 }
117 END_TEST
118
119 /*******************************************************************************
120 * enum_from_name
121 */
122
123 static struct {
124 int val;
125 char *str;
126 } enum_tests_cont[] = {
127 {CONT1, "CONT1"},
128 {CONT2, "CONT2"},
129 {CONT2, "CoNt2"},
130 {CONT3, "CONT3"},
131 {CONT4, "CONT4"},
132 {CONT5, "CONT5"},
133 {-1, "asdf"},
134 {-1, ""},
135 {-1, NULL},
136 }, enum_tests_split[] = {
137 {SPLIT1, "SPLIT1"},
138 {SPLIT1, "split1"},
139 {SPLIT2, "SPLIT2"},
140 {SPLIT2, "SpLiT2"},
141 {SPLIT3, "SPLIT3"},
142 {SPLIT4, "SPLIT4"},
143 {SPLIT5, "SPLIT5"},
144 {-1, "asdf"},
145 {-1, ""},
146 {-1, NULL},
147 };
148
149 START_TEST(test_enum_from_name_cont)
150 {
151 int val = enum_from_name(test_enum_cont_names, enum_tests_cont[_i].str);
152 ck_assert_int_eq(val, enum_tests_cont[_i].val);
153 }
154 END_TEST
155
156 START_TEST(test_enum_from_name_split)
157 {
158 int val = enum_from_name(test_enum_split_names, enum_tests_split[_i].str);
159 ck_assert_int_eq(val, enum_tests_split[_i].val);
160 }
161 END_TEST
162
163 /*******************************************************************************
164 * enum_printf_hook
165 */
166
167 static struct {
168 int val;
169 char *str;
170 } printf_tests_cont[] = {
171 {-1, "(-1)"},
172 {CONT1, "CONT1"},
173 {CONT2, "CONT2"},
174 {CONT3, "CONT3"},
175 {CONT4, "CONT4"},
176 {CONT5, "CONT5"},
177 {5, "(5)"},
178 }, printf_tests_split[] = {
179 {-1, "(-1)"},
180 {0, "(0)"},
181 {SPLIT1, "SPLIT1"},
182 {SPLIT2, "SPLIT2"},
183 {3, "(3)"},
184 {4, "(4)"},
185 {SPLIT3, "SPLIT3"},
186 {SPLIT4, "SPLIT4"},
187 {7, "(7)"},
188 {254, "(254)"},
189 {SPLIT5, "SPLIT5"},
190 {256, "(256)"},
191 };
192
193 START_TEST(test_enum_printf_hook_cont)
194 {
195 char buf[128];
196
197 snprintf(buf, sizeof(buf), "%N", test_enum_cont_names, printf_tests_cont[_i].val);
198 ck_assert_str_eq(printf_tests_cont[_i].str, buf);
199 }
200 END_TEST
201
202 START_TEST(test_enum_printf_hook_split)
203 {
204 char buf[128];
205
206 snprintf(buf, sizeof(buf), "%N", test_enum_split_names, printf_tests_split[_i].val);
207 ck_assert_str_eq(printf_tests_split[_i].str, buf);
208 }
209 END_TEST
210
211 START_TEST(test_enum_printf_hook_width)
212 {
213 char buf[128];
214
215 snprintf(buf, sizeof(buf), "%10N", test_enum_cont_names, CONT1);
216 ck_assert_str_eq(" CONT1", buf);
217 snprintf(buf, sizeof(buf), "%-*N", 10, test_enum_cont_names, CONT2);
218 ck_assert_str_eq("CONT2 ", buf);
219 snprintf(buf, sizeof(buf), "%3N", test_enum_cont_names, CONT3);
220 ck_assert_str_eq("CONT3", buf);
221 }
222 END_TEST
223
224 Suite *enum_suite_create()
225 {
226 Suite *s;
227 TCase *tc;
228
229 s = suite_create("enum");
230
231 tc = tcase_create("enum_to_name");
232 tcase_add_loop_test(tc, test_enum_to_name_cont, 0, countof(name_tests_cont));
233 tcase_add_loop_test(tc, test_enum_to_name_split, 0, countof(name_tests_split));
234 suite_add_tcase(s, tc);
235
236 tc = tcase_create("enum_from_name");
237 tcase_add_loop_test(tc, test_enum_from_name_cont, 0, countof(enum_tests_cont));
238 tcase_add_loop_test(tc, test_enum_from_name_split, 0, countof(enum_tests_split));
239 suite_add_tcase(s, tc);
240
241 tc = tcase_create("enum_printf_hook");
242 tcase_add_loop_test(tc, test_enum_printf_hook_cont, 0, countof(printf_tests_cont));
243 tcase_add_loop_test(tc, test_enum_printf_hook_split, 0, countof(printf_tests_split));
244 tcase_add_test(tc, test_enum_printf_hook_width);
245 suite_add_tcase(s, tc);
246
247 return s;
248 }