ucx
UAP Common Extensions
Loading...
Searching...
No Matches
test.h
Go to the documentation of this file.
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
66
67#ifndef UCX_TEST_H
68#define UCX_TEST_H
69
70#include <stdlib.h>
71#include <stdio.h>
72#include <string.h>
73#include <setjmp.h>
74
75#ifndef __FUNCTION__
82#define __FUNCTION__ __func__
83#endif
84
85#if !defined(__clang__) && __GNUC__ > 3
86#pragma GCC diagnostic ignored "-Wclobbered"
87#endif
88
90typedef struct CxTestSuite CxTestSuite;
91
93typedef size_t (*cx_test_output_func)(const void*, size_t, size_t, void*);
94
96typedef void(*CxTest)(CxTestSuite *, void *, cx_test_output_func);
97
99typedef struct CxTestSet CxTestSet;
100
102struct CxTestSet {
103
106
109};
110
115
117 unsigned int success;
118
120 unsigned int failure;
121
123 const char *name;
124
130};
131
137static inline CxTestSuite* cx_test_suite_new(const char *name) {
138 CxTestSuite* suite = (CxTestSuite*) malloc(sizeof(CxTestSuite));
139 if (suite != NULL) {
140 suite->name = name;
141 suite->success = 0;
142 suite->failure = 0;
143 suite->tests = NULL;
144 }
145
146 return suite;
147}
148
154static inline void cx_test_suite_free(CxTestSuite* suite) {
155 if (suite == NULL) return;
156 CxTestSet *l = suite->tests;
157 while (l != NULL) {
158 CxTestSet *e = l;
159 l = l->next;
160 free(e);
161 }
162 free(suite);
163}
164
173static inline int cx_test_register(CxTestSuite* suite, CxTest test) {
174 CxTestSet *t = (CxTestSet*) malloc(sizeof(CxTestSet));
175 if (t) {
176 t->test = test;
177 t->next = NULL;
178 if (suite->tests == NULL) {
179 suite->tests = t;
180 } else {
181 CxTestSet *last = suite->tests;
182 while (last->next) {
183 last = last->next;
184 }
185 last->next = t;
186 }
187 return 0;
188 } else {
189 return 1;
190 }
191}
192
199static inline void cx_test_run(CxTestSuite *suite, void *out_target,
200 cx_test_output_func out_writer) {
201 if (suite->name == NULL) {
202 out_writer("*** Test Suite ***\n", 1, 19, out_target);
203 } else {
204 out_writer("*** Test Suite : ", 1, 17, out_target);
205 out_writer(suite->name, 1, strlen(suite->name), out_target);
206 out_writer(" ***\n", 1, 5, out_target);
207 }
208 suite->success = 0;
209 suite->failure = 0;
210 for (CxTestSet *elem = suite->tests; elem; elem = elem->next) {
211 elem->test(suite, out_target, out_writer);
212 }
213 out_writer("\nAll test completed.\n", 1, 21, out_target);
214 char total[80];
215 int len = snprintf(
216 total, 80,
217 " Tests: %5u\n Success: %5u\n Failure: %5u\n\n",
218 suite->success + suite->failure, suite->success, suite->failure
219 );
220 out_writer(total, 1, len, out_target);
221}
222
228#define cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, \
229 (cx_test_output_func) fwrite)
230
235#define cx_test_run_stdout(suite) cx_test_run_f(suite, stdout)
236
244#define CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, \
245 cx_test_output_func _writefnc_)
246
263#define CX_TEST_DO _writefnc_("Running ", 1, 8, _output_);\
264 _writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
265 _writefnc_("... ", 1, 4, _output_);\
266 jmp_buf _env_;\
267 for (unsigned int _cx_test_loop_ = 0 ;\
268 _cx_test_loop_ == 0 && !setjmp(_env_);\
269 _writefnc_("success.\n", 1, 9, _output_),\
270 _suite_->success++, _cx_test_loop_++)
271
280#define CX_TEST_ASSERTM(condition,message) if (!(condition)) { \
281 const char *_assert_msg_ = message; \
282 _writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \
283 _writefnc_(".\n", 1, 2, _output_); \
284 _suite_->failure++; \
285 longjmp(_env_, 1);\
286 } (void) 0
287
295#define CX_TEST_ASSERT(condition) \
296 CX_TEST_ASSERTM(condition, #condition " failed")
297
309#define CX_TEST_SUBROUTINE(name,...) void name(CxTestSuite* _suite_,\
310 void *_output_, cx_test_output_func _writefnc_, \
311 jmp_buf _env_, __VA_ARGS__)
312
326#define CX_TEST_CALL_SUBROUTINE(name,...) \
327 name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__)
328
329#endif /* UCX_TEST_H */
330
Strings that know their length.
Structure for the internal list of test cases.
Definition test.h:102
CxTest test
Test case.
Definition test.h:105
CxTestSet * next
Pointer to the next list element.
Definition test.h:108
A test suite containing multiple test cases.
Definition test.h:114
const char * name
The optional name of this test suite.
Definition test.h:123
unsigned int success
The number of successful tests after the suite has been run.
Definition test.h:117
unsigned int failure
The number of failed tests after the suite has been run.
Definition test.h:120
CxTestSet * tests
Internal list of test cases.
Definition test.h:129
static void cx_test_run(CxTestSuite *suite, void *out_target, cx_test_output_func out_writer)
Runs a test suite and writes the test log to the specified stream.
Definition test.h:199
static CxTestSuite * cx_test_suite_new(const char *name)
Creates a new test suite.
Definition test.h:137
static int cx_test_register(CxTestSuite *suite, CxTest test)
Registers a test function with the specified test suite.
Definition test.h:173
static void cx_test_suite_free(CxTestSuite *suite)
Deallocates a test suite.
Definition test.h:154
void(* CxTest)(CxTestSuite *, void *, cx_test_output_func)
Pointer to a test function.
Definition test.h:96
size_t(* cx_test_output_func)(const void *, size_t, size_t, void *)
Output function compatible to cx_write_func.
Definition test.h:93