test/main.c

changeset 390
d345541018fa
parent 378
952c2df7e7ac
child 471
e9ef2637e101
equal deleted inserted replaced
389:92e482410453 390:d345541018fa
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include <ucx/test.h>
33
34 #include "main.h"
35
36 #include "array_tests.h"
37 #include "allocator_tests.h"
38 #include "logging_tests.h"
39 #include "list_tests.h"
40 #include "string_tests.h"
41 #include "mpool_tests.h"
42 #include "stack_tests.h"
43 #include "map_tests.h"
44 #include "prop_tests.h"
45 #include "buffer_tests.h"
46 #include "utils_tests.h"
47 #include "avl_tests.h"
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 UCX_TEST(testTestSuitePositive) {
53 UCX_TEST_BEGIN
54 UCX_TEST_ASSERT(2*2 == 4, "the test framework fails");
55 UCX_TEST_END
56 }
57
58 UCX_TEST(testTestSuiteNegative) {
59 UCX_TEST_BEGIN
60 UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works");
61 UCX_TEST_END
62 }
63
64 UCX_TEST_SUBROUTINE(testTestSuiteRoutineRoutine, float f) {
65 UCX_TEST_ASSERT(f == 3.14f, "calling routine in a routine fails");
66 }
67
68 UCX_TEST_SUBROUTINE(testTestSuiteRoutine2Param, int i, float f) {
69 UCX_TEST_ASSERT(i == 42, "two parameter routine fails");
70 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineRoutine, f);
71 }
72
73 UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess, int* i) {
74 *i += 2;
75 UCX_TEST_ASSERT(*i==4, "the test framework fails");
76 }
77
78 UCX_TEST_SUBROUTINE(testTestSuiteRoutineFailure, int* i) {
79 *i += 2;
80 // Next test shall fail!
81 UCX_TEST_ASSERT(*i==4, "the test framework works");
82 }
83
84 UCX_TEST(testTestSuiteRoutinePositive) {
85 int i = 2;
86 UCX_TEST_BEGIN
87 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineSuccess, &i);
88 UCX_TEST_ASSERT(i==4, "the test framework fails");
89 UCX_TEST_END
90 }
91
92 UCX_TEST(testTestSuiteRoutineNegative) {
93 int i = 0;
94 UCX_TEST_BEGIN
95 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutineFailure, &i);
96 UCX_TEST_ASSERT(1, "the test framework fails");
97 UCX_TEST_END
98 }
99
100 UCX_TEST(testTestSuiteRoutineMultiparam) {
101 UCX_TEST_BEGIN
102 UCX_TEST_CALL_SUBROUTINE(testTestSuiteRoutine2Param, 42, 3.14f);
103 UCX_TEST_END
104 }
105 #ifdef __cplusplus
106 }
107 #endif
108
109 int main(int argc, char **argv) {
110 printf("UCX Tests\n---------\n");
111
112 printf("\nUcxTestSuite tests (2 failures are intended!)\n");
113 UcxTestSuite* suite = ucx_test_suite_new();
114 ucx_test_register(suite, testTestSuitePositive);
115 ucx_test_register(suite, testTestSuiteNegative);
116 ucx_test_register(suite, testTestSuiteRoutinePositive);
117 ucx_test_register(suite, testTestSuiteRoutineNegative);
118 ucx_test_register(suite, testTestSuiteRoutineMultiparam);
119 ucx_test_run(suite, stdout);
120 if (suite->failure == 2 && suite->success == 3) {
121 ucx_test_suite_free(suite);
122
123 printf("\nLibrary function tests\n");
124 suite = ucx_test_suite_new();
125 /* UcxAllocator Tests */
126 ucx_test_register(suite, test_ucx_default_allocator);
127
128 /* sstring Tests */
129 ucx_test_register(suite, test_sstr_macros);
130 ucx_test_register(suite, test_sstr);
131 ucx_test_register(suite, test_sstr_len);
132 ucx_test_register(suite, test_sstrcmp);
133 ucx_test_register(suite, test_sstrcasecmp);
134 ucx_test_register(suite, test_sstrcat);
135 ucx_test_register(suite, test_sstrchr_sstrrchr);
136 ucx_test_register(suite, test_sstrstr);
137 ucx_test_register(suite, test_sstrsplit);
138 ucx_test_register(suite, test_sstrtrim);
139 ucx_test_register(suite, test_sstrprefixsuffix);
140 ucx_test_register(suite, test_sstrcaseprefixsuffix);
141 ucx_test_register(suite, test_sstrreplace);
142
143 /* UcxLogger Tests */
144 ucx_test_register(suite, test_ucx_logger_new);
145 ucx_test_register(suite, test_ucx_logger_log);
146
147 /* UcxArray Tests */
148 ucx_test_register(suite, test_ucx_array_destroy);
149 ucx_test_register(suite, test_ucx_array_new);
150 ucx_test_register(suite, test_ucx_array_at);
151 ucx_test_register(suite, test_ucx_array_append_from);
152 ucx_test_register(suite, test_ucx_array_append_from_struct);
153 ucx_test_register(suite, test_ucx_array_prepend_from);
154 ucx_test_register(suite, test_ucx_array_set_from);
155 ucx_test_register(suite, test_ucx_array_equals);
156 ucx_test_register(suite, test_ucx_array_concat);
157 ucx_test_register(suite, test_ucx_array_find);
158 ucx_test_register(suite, test_ucx_array_contains);
159 ucx_test_register(suite, test_ucx_array_remove);
160 ucx_test_register(suite, test_ucx_array_clone);
161 ucx_test_register(suite, test_ucx_array_sort);
162 ucx_test_register(suite, test_ucx_array_shrink);
163 ucx_test_register(suite, test_ucx_array_resize);
164 ucx_test_register(suite, test_ucx_array_reserve);
165 ucx_test_register(suite, test_ucx_array_grow);
166 ucx_test_register(suite, test_ucx_array_util_set);
167 ucx_test_register(suite, test_ucx_array_util_setptr);
168
169 /* UcxList Tests */
170 ucx_test_register(suite, test_ucx_list_append);
171 ucx_test_register(suite, test_ucx_list_prepend);
172 ucx_test_register(suite, test_ucx_list_equals);
173 ucx_test_register(suite, test_ucx_list_concat);
174 ucx_test_register(suite, test_ucx_list_size);
175 ucx_test_register(suite, test_ucx_list_first);
176 ucx_test_register(suite, test_ucx_list_last);
177 ucx_test_register(suite, test_ucx_list_get);
178 ucx_test_register(suite, test_ucx_list_indexof);
179 ucx_test_register(suite, test_ucx_list_find);
180 ucx_test_register(suite, test_ucx_list_contains);
181 ucx_test_register(suite, test_ucx_list_remove);
182 ucx_test_register(suite, test_ucx_list_clone);
183 ucx_test_register(suite, test_ucx_list_sort);
184 ucx_test_register(suite, test_ucx_list_union);
185 ucx_test_register(suite, test_ucx_list_intersection);
186 ucx_test_register(suite, test_ucx_list_difference);
187
188 /* UcxMemPool Tests */
189 ucx_test_register(suite, test_ucx_mempool_new);
190 ucx_test_register(suite, test_ucx_mempool_malloc);
191 ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
192 ucx_test_register(suite, test_ucx_mempool_calloc);
193 ucx_test_register(suite, test_ucx_mempool_free);
194 ucx_test_register(suite, test_ucx_mempool_set_destr);
195 ucx_test_register(suite, test_ucx_mempool_reg_destr);
196 ucx_test_register(suite, test_ucx_mempool_realloc);
197
198 /* UcxStack Tests */
199 ucx_test_register(suite, test_ucx_stack_init);
200 ucx_test_register(suite, test_ucx_stack_malloc);
201 ucx_test_register(suite, test_ucx_stack_calloc);
202 ucx_test_register(suite, test_ucx_stack_free);
203 ucx_test_register(suite, test_ucx_stack_realloc);
204 ucx_test_register(suite, test_ucx_stack_pop);
205
206 /* UcxMap Tests */
207 ucx_test_register(suite, test_ucx_map_new);
208 ucx_test_register(suite, test_ucx_key);
209 ucx_test_register(suite, test_ucx_map_put);
210 ucx_test_register(suite, test_ucx_map_get);
211 ucx_test_register(suite, test_ucx_map_remove);
212 ucx_test_register(suite, test_ucx_map_clear);
213 ucx_test_register(suite, test_ucx_map_iterator);
214 ucx_test_register(suite, test_ucx_map_iterator_chain);
215 ucx_test_register(suite, test_ucx_map_clone);
216 ucx_test_register(suite, test_ucx_map_rehash);
217 ucx_test_register(suite, test_ucx_map_union);
218 ucx_test_register(suite, test_ucx_map_intersection);
219 ucx_test_register(suite, test_ucx_map_difference);
220
221 /* UcxPropertiesParser Tests */
222 ucx_test_register(suite, test_ucx_properties_new);
223 ucx_test_register(suite, test_ucx_properties_next);
224 ucx_test_register(suite, test_ucx_properties_next_multi);
225 ucx_test_register(suite, test_ucx_properties_next_part);
226 ucx_test_register(suite, test_ucx_properties_next_long);
227 ucx_test_register(suite, test_ucx_properties2map);
228 ucx_test_register(suite, test_ucx_properties_load);
229 ucx_test_register(suite, test_ucx_properties_store);
230
231 /* UcxBuffer Tests */
232 ucx_test_register(suite, test_ucx_buffer_new);
233 ucx_test_register(suite, test_ucx_buffer_new_prealloc);
234 ucx_test_register(suite, test_ucx_buffer_eof);
235 ucx_test_register(suite, test_ucx_buffer_seek_set);
236 ucx_test_register(suite, test_ucx_buffer_seek_cur);
237 ucx_test_register(suite, test_ucx_buffer_seek_end);
238 ucx_test_register(suite, test_ucx_buffer_seek_oob);
239 ucx_test_register(suite, test_ucx_buffer_seek_invalid);
240 ucx_test_register(suite, test_ucx_buffer_seek_overflow);
241 ucx_test_register(suite, test_ucx_buffer_putc);
242 ucx_test_register(suite, test_ucx_buffer_putc_ae);
243 ucx_test_register(suite, test_ucx_buffer_putc_oob);
244 ucx_test_register(suite, test_ucx_buffer_putc_oobae);
245 ucx_test_register(suite, test_ucx_buffer_putc_size);
246 ucx_test_register(suite, test_ucx_buffer_getc);
247 ucx_test_register(suite, test_ucx_buffer_read);
248 ucx_test_register(suite, test_ucx_buffer_read_oob);
249 ucx_test_register(suite, test_ucx_buffer_extract);
250 ucx_test_register(suite, test_ucx_buffer_extract_oob);
251 ucx_test_register(suite, test_ucx_buffer_extract_overflow);
252 ucx_test_register(suite, test_ucx_buffer_extend);
253 ucx_test_register(suite, test_ucx_buffer_write);
254 ucx_test_register(suite, test_ucx_buffer_write_oob);
255 ucx_test_register(suite, test_ucx_buffer_write_ax);
256 ucx_test_register(suite, test_ucx_buffer_shl);
257 ucx_test_register(suite, test_ucx_buffer_shr);
258 ucx_test_register(suite, test_ucx_buffer_shr_ax);
259
260 /* Utils Tests*/
261 ucx_test_register(suite, test_ucx_fprintf);
262 ucx_test_register(suite, test_ucx_asprintf);
263 ucx_test_register(suite, test_ucx_sprintf);
264 ucx_test_register(suite, test_ucx_bprintf);
265 ucx_test_register(suite, test_ucx_stream_copy);
266
267 /* AVL Tests */
268 ucx_test_register(suite, test_ucx_avl_put);
269 ucx_test_register(suite, test_ucx_avl_remove);
270 ucx_test_register(suite, test_ucx_avl_find);
271 ucx_test_register(suite, test_ucx_avl_traverse);
272
273 ucx_test_run(suite, stdout);
274 fflush(stdout);
275
276 int exit_code = suite->failure > 0 ? EXIT_FAILURE: EXIT_SUCCESS;
277
278 ucx_test_suite_free(suite);
279
280 return exit_code;
281 } else {
282 ucx_test_suite_free(suite);
283 return EXIT_FAILURE;
284 }
285 }
286

mercurial