Tue, 06 Aug 2019 16:26:46 +0200
removes cumbersome array accessor macros
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 */
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include <ucx/test.h>
34 #include "main.h"
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"
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 }
58 UCX_TEST(testTestSuiteNegative) {
59 UCX_TEST_BEGIN
60 UCX_TEST_ASSERT(2*(-2) == 4, "the test framework works");
61 UCX_TEST_END
62 }
64 UCX_TEST_SUBROUTINE(testTestSuiteRoutineRoutine, float f) {
65 UCX_TEST_ASSERT(f == 3.14f, "calling routine in a routine fails");
66 }
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 }
73 UCX_TEST_SUBROUTINE(testTestSuiteRoutineSuccess, int* i) {
74 *i += 2;
75 UCX_TEST_ASSERT(*i==4, "the test framework fails");
76 }
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 }
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 }
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 }
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
109 int main(int argc, char **argv) {
110 printf("UCX Tests\n---------\n");
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);
123 printf("\nLibrary function tests\n");
124 suite = ucx_test_suite_new();
125 /* UcxAllocator Tests */
126 ucx_test_register(suite, test_ucx_default_allocator);
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);
141 /* UcxLogger Tests */
142 ucx_test_register(suite, test_ucx_logger_new);
143 ucx_test_register(suite, test_ucx_logger_log);
145 /* UcxArray Tests */
146 ucx_test_register(suite, test_ucx_array_free);
147 ucx_test_register(suite, test_ucx_array_new);
148 ucx_test_register(suite, test_ucx_array_append);
149 ucx_test_register(suite, test_ucx_array_prepend);
150 ucx_test_register(suite, test_ucx_array_set);
151 ucx_test_register(suite, test_ucx_array_autogrow);
152 ucx_test_register(suite, test_ucx_array_equals);
153 ucx_test_register(suite, test_ucx_array_concat);
154 ucx_test_register(suite, test_ucx_array_find);
155 ucx_test_register(suite, test_ucx_array_contains);
156 ucx_test_register(suite, test_ucx_array_remove);
157 ucx_test_register(suite, test_ucx_array_clone);
158 ucx_test_register(suite, test_ucx_array_sort);
159 ucx_test_register(suite, test_ucx_array_shrink);
160 ucx_test_register(suite, test_ucx_array_resize);
161 ucx_test_register(suite, test_ucx_array_reserve);
163 /* UcxList Tests */
164 ucx_test_register(suite, test_ucx_list_append);
165 ucx_test_register(suite, test_ucx_list_prepend);
166 ucx_test_register(suite, test_ucx_list_equals);
167 ucx_test_register(suite, test_ucx_list_concat);
168 ucx_test_register(suite, test_ucx_list_size);
169 ucx_test_register(suite, test_ucx_list_first);
170 ucx_test_register(suite, test_ucx_list_last);
171 ucx_test_register(suite, test_ucx_list_get);
172 ucx_test_register(suite, test_ucx_list_indexof);
173 ucx_test_register(suite, test_ucx_list_find);
174 ucx_test_register(suite, test_ucx_list_contains);
175 ucx_test_register(suite, test_ucx_list_remove);
176 ucx_test_register(suite, test_ucx_list_clone);
177 ucx_test_register(suite, test_ucx_list_sort);
179 /* UcxMemPool Tests */
180 ucx_test_register(suite, test_ucx_mempool_new);
181 ucx_test_register(suite, test_ucx_mempool_malloc);
182 ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
183 ucx_test_register(suite, test_ucx_mempool_calloc);
184 ucx_test_register(suite, test_ucx_mempool_free);
185 ucx_test_register(suite, test_ucx_mempool_set_destr);
186 ucx_test_register(suite, test_ucx_mempool_reg_destr);
187 ucx_test_register(suite, test_ucx_mempool_realloc);
189 /* UcxStack Tests */
190 ucx_test_register(suite, test_ucx_stack_init);
191 ucx_test_register(suite, test_ucx_stack_malloc);
192 ucx_test_register(suite, test_ucx_stack_calloc);
193 ucx_test_register(suite, test_ucx_stack_free);
194 ucx_test_register(suite, test_ucx_stack_realloc);
195 ucx_test_register(suite, test_ucx_stack_pop);
197 /* UcxMap Tests */
198 ucx_test_register(suite, test_ucx_map_new);
199 ucx_test_register(suite, test_ucx_key);
200 ucx_test_register(suite, test_ucx_map_put);
201 ucx_test_register(suite, test_ucx_map_get);
202 ucx_test_register(suite, test_ucx_map_remove);
203 ucx_test_register(suite, test_ucx_map_clear);
204 ucx_test_register(suite, test_ucx_map_iterator);
205 ucx_test_register(suite, test_ucx_map_iterator_chain);
206 ucx_test_register(suite, test_ucx_map_clone);
207 ucx_test_register(suite, test_ucx_map_rehash);
209 /* UcxPropertiesParser Tests */
210 ucx_test_register(suite, test_ucx_properties_new);
211 ucx_test_register(suite, test_ucx_properties_next);
212 ucx_test_register(suite, test_ucx_properties_next_multi);
213 ucx_test_register(suite, test_ucx_properties_next_part);
214 ucx_test_register(suite, test_ucx_properties_next_long);
215 ucx_test_register(suite, test_ucx_properties2map);
216 ucx_test_register(suite, test_ucx_properties_load);
217 ucx_test_register(suite, test_ucx_properties_store);
219 /* UcxBuffer Tests */
220 ucx_test_register(suite, test_ucx_buffer_new);
221 ucx_test_register(suite, test_ucx_buffer_new_prealloc);
222 ucx_test_register(suite, test_ucx_buffer_eof);
223 ucx_test_register(suite, test_ucx_buffer_seek_set);
224 ucx_test_register(suite, test_ucx_buffer_seek_cur);
225 ucx_test_register(suite, test_ucx_buffer_seek_end);
226 ucx_test_register(suite, test_ucx_buffer_seek_oob);
227 ucx_test_register(suite, test_ucx_buffer_seek_invalid);
228 ucx_test_register(suite, test_ucx_buffer_seek_overflow);
229 ucx_test_register(suite, test_ucx_buffer_putc);
230 ucx_test_register(suite, test_ucx_buffer_putc_ae);
231 ucx_test_register(suite, test_ucx_buffer_putc_oob);
232 ucx_test_register(suite, test_ucx_buffer_putc_oobae);
233 ucx_test_register(suite, test_ucx_buffer_putc_size);
234 ucx_test_register(suite, test_ucx_buffer_getc);
235 ucx_test_register(suite, test_ucx_buffer_read);
236 ucx_test_register(suite, test_ucx_buffer_read_oob);
237 ucx_test_register(suite, test_ucx_buffer_extract);
238 ucx_test_register(suite, test_ucx_buffer_extract_oob);
239 ucx_test_register(suite, test_ucx_buffer_extract_overflow);
240 ucx_test_register(suite, test_ucx_buffer_extend);
241 ucx_test_register(suite, test_ucx_buffer_write);
242 ucx_test_register(suite, test_ucx_buffer_write_oob);
243 ucx_test_register(suite, test_ucx_buffer_write_ax);
244 ucx_test_register(suite, test_ucx_buffer_shl);
245 ucx_test_register(suite, test_ucx_buffer_shr);
246 ucx_test_register(suite, test_ucx_buffer_shr_ax);
248 /* Utils Tests*/
249 ucx_test_register(suite, test_ucx_fprintf);
250 ucx_test_register(suite, test_ucx_asprintf);
251 ucx_test_register(suite, test_ucx_sprintf);
252 ucx_test_register(suite, test_ucx_bprintf);
253 ucx_test_register(suite, test_ucx_stream_copy);
255 /* AVL Tests */
256 ucx_test_register(suite, test_ucx_avl_put);
257 ucx_test_register(suite, test_ucx_avl_remove);
258 ucx_test_register(suite, test_ucx_avl_find);
259 ucx_test_register(suite, test_ucx_avl_traverse);
261 ucx_test_run(suite, stdout);
262 fflush(stdout);
264 int exit_code = suite->failure > 0 ? EXIT_FAILURE: EXIT_SUCCESS;
266 ucx_test_suite_free(suite);
268 return exit_code;
269 } else {
270 ucx_test_suite_free(suite);
271 return EXIT_FAILURE;
272 }
273 }