Sun, 03 Nov 2019 16:34:29 +0100
adds case independent versions of sstrprefix() and sstrsuffix()
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);
140 ucx_test_register(suite, test_sstrcaseprefixsuffix);
142 /* UcxLogger Tests */
143 ucx_test_register(suite, test_ucx_logger_new);
144 ucx_test_register(suite, test_ucx_logger_log);
146 /* UcxArray Tests */
147 ucx_test_register(suite, test_ucx_array_destroy);
148 ucx_test_register(suite, test_ucx_array_new);
149 ucx_test_register(suite, test_ucx_array_at);
150 ucx_test_register(suite, test_ucx_array_append_from);
151 ucx_test_register(suite, test_ucx_array_prepend_from);
152 ucx_test_register(suite, test_ucx_array_set_from);
153 ucx_test_register(suite, test_ucx_array_append);
154 ucx_test_register(suite, test_ucx_array_prepend);
155 ucx_test_register(suite, test_ucx_array_set);
156 ucx_test_register(suite, test_ucx_array_autogrow);
157 ucx_test_register(suite, test_ucx_array_equals);
158 ucx_test_register(suite, test_ucx_array_concat);
159 ucx_test_register(suite, test_ucx_array_find);
160 ucx_test_register(suite, test_ucx_array_contains);
161 ucx_test_register(suite, test_ucx_array_remove);
162 ucx_test_register(suite, test_ucx_array_clone);
163 ucx_test_register(suite, test_ucx_array_sort);
164 ucx_test_register(suite, test_ucx_array_shrink);
165 ucx_test_register(suite, test_ucx_array_resize);
166 ucx_test_register(suite, test_ucx_array_reserve);
167 ucx_test_register(suite, test_ucx_array_util_set);
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);
185 /* UcxMemPool Tests */
186 ucx_test_register(suite, test_ucx_mempool_new);
187 ucx_test_register(suite, test_ucx_mempool_malloc);
188 ucx_test_register(suite, test_ucx_mempool_malloc_with_chcap);
189 ucx_test_register(suite, test_ucx_mempool_calloc);
190 ucx_test_register(suite, test_ucx_mempool_free);
191 ucx_test_register(suite, test_ucx_mempool_set_destr);
192 ucx_test_register(suite, test_ucx_mempool_reg_destr);
193 ucx_test_register(suite, test_ucx_mempool_realloc);
195 /* UcxStack Tests */
196 ucx_test_register(suite, test_ucx_stack_init);
197 ucx_test_register(suite, test_ucx_stack_malloc);
198 ucx_test_register(suite, test_ucx_stack_calloc);
199 ucx_test_register(suite, test_ucx_stack_free);
200 ucx_test_register(suite, test_ucx_stack_realloc);
201 ucx_test_register(suite, test_ucx_stack_pop);
203 /* UcxMap Tests */
204 ucx_test_register(suite, test_ucx_map_new);
205 ucx_test_register(suite, test_ucx_key);
206 ucx_test_register(suite, test_ucx_map_put);
207 ucx_test_register(suite, test_ucx_map_get);
208 ucx_test_register(suite, test_ucx_map_remove);
209 ucx_test_register(suite, test_ucx_map_clear);
210 ucx_test_register(suite, test_ucx_map_iterator);
211 ucx_test_register(suite, test_ucx_map_iterator_chain);
212 ucx_test_register(suite, test_ucx_map_clone);
213 ucx_test_register(suite, test_ucx_map_rehash);
215 /* UcxPropertiesParser Tests */
216 ucx_test_register(suite, test_ucx_properties_new);
217 ucx_test_register(suite, test_ucx_properties_next);
218 ucx_test_register(suite, test_ucx_properties_next_multi);
219 ucx_test_register(suite, test_ucx_properties_next_part);
220 ucx_test_register(suite, test_ucx_properties_next_long);
221 ucx_test_register(suite, test_ucx_properties2map);
222 ucx_test_register(suite, test_ucx_properties_load);
223 ucx_test_register(suite, test_ucx_properties_store);
225 /* UcxBuffer Tests */
226 ucx_test_register(suite, test_ucx_buffer_new);
227 ucx_test_register(suite, test_ucx_buffer_new_prealloc);
228 ucx_test_register(suite, test_ucx_buffer_eof);
229 ucx_test_register(suite, test_ucx_buffer_seek_set);
230 ucx_test_register(suite, test_ucx_buffer_seek_cur);
231 ucx_test_register(suite, test_ucx_buffer_seek_end);
232 ucx_test_register(suite, test_ucx_buffer_seek_oob);
233 ucx_test_register(suite, test_ucx_buffer_seek_invalid);
234 ucx_test_register(suite, test_ucx_buffer_seek_overflow);
235 ucx_test_register(suite, test_ucx_buffer_putc);
236 ucx_test_register(suite, test_ucx_buffer_putc_ae);
237 ucx_test_register(suite, test_ucx_buffer_putc_oob);
238 ucx_test_register(suite, test_ucx_buffer_putc_oobae);
239 ucx_test_register(suite, test_ucx_buffer_putc_size);
240 ucx_test_register(suite, test_ucx_buffer_getc);
241 ucx_test_register(suite, test_ucx_buffer_read);
242 ucx_test_register(suite, test_ucx_buffer_read_oob);
243 ucx_test_register(suite, test_ucx_buffer_extract);
244 ucx_test_register(suite, test_ucx_buffer_extract_oob);
245 ucx_test_register(suite, test_ucx_buffer_extract_overflow);
246 ucx_test_register(suite, test_ucx_buffer_extend);
247 ucx_test_register(suite, test_ucx_buffer_write);
248 ucx_test_register(suite, test_ucx_buffer_write_oob);
249 ucx_test_register(suite, test_ucx_buffer_write_ax);
250 ucx_test_register(suite, test_ucx_buffer_shl);
251 ucx_test_register(suite, test_ucx_buffer_shr);
252 ucx_test_register(suite, test_ucx_buffer_shr_ax);
254 /* Utils Tests*/
255 ucx_test_register(suite, test_ucx_fprintf);
256 ucx_test_register(suite, test_ucx_asprintf);
257 ucx_test_register(suite, test_ucx_sprintf);
258 ucx_test_register(suite, test_ucx_bprintf);
259 ucx_test_register(suite, test_ucx_stream_copy);
261 /* AVL Tests */
262 ucx_test_register(suite, test_ucx_avl_put);
263 ucx_test_register(suite, test_ucx_avl_remove);
264 ucx_test_register(suite, test_ucx_avl_find);
265 ucx_test_register(suite, test_ucx_avl_traverse);
267 ucx_test_run(suite, stdout);
268 fflush(stdout);
270 int exit_code = suite->failure > 0 ? EXIT_FAILURE: EXIT_SUCCESS;
272 ucx_test_suite_free(suite);
274 return exit_code;
275 } else {
276 ucx_test_suite_free(suite);
277 return EXIT_FAILURE;
278 }
279 }