1 /* Copyright 2011-2016 Bas van den Berg
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
19 #if defined _WIN32 || defined __CYGWIN__
26 #define WEAK __attribute__ ((weak))
31 #include <inttypes.h> /* intmax_t, uintmax_t, PRI* */
32 #include <stddef.h> /* size_t */
34 typedef void (*SetupFunc)(void*);
35 typedef void (*TearDownFunc)(void*);
36 typedef void (*RunWithDataFunc)(void*);
39 const char* ssname; // suite name
40 const char* ttname; // test name
46 TearDownFunc teardown;
53 #define __FNAME(sname, tname) __ctest_##sname##_##tname##_run
54 #define __TNAME(sname, tname) __ctest_##sname##_##tname
55 #define __PNAME(sname, tname) __ctest_##sname##_##tname##_pointer
65 #if defined(_WIN32) && defined(_MSC_VER)
69 //config for MSVC compiler
72 #define __CTEST_NO_TIME
73 #define CTEST_NO_COLORS
75 #ifndef CTEST_ADD_TESTS_MANUALLY
76 #pragma section(".ctest$a")
77 #pragma section(".ctest$u")
78 #pragma section(".ctest$z")
81 //clear this flag for msvc
87 #define inline __inline
92 #define __CTEST_NO_JMP
95 #define __CTEST_MAGIC (0xdeadbeef)
97 #ifdef CTEST_ADD_TESTS_MANUALLY
98 # define __Test_Section
101 #define __Test_Section __attribute__ ((used, section ("__DATA, .ctest")))
102 #elif defined (__CTEST_MSVC)
103 #define __Test_Section __declspec( allocate(".ctest$u"))
105 #define __Test_Section __attribute__ ((used, section (".ctest")))
110 #define __CTEST_STRUCT(sname, tname, _skip, __data, __setup, __teardown) \
111 static struct ctest __TNAME(sname, tname) = { \
114 .run = __FNAME(sname, tname), \
117 .setup = (SetupFunc)__setup, \
118 .teardown = (TearDownFunc)__teardown, \
120 .magic = __CTEST_MAGIC}; \
121 static void * __PNAME(sname, tname)[2] __Test_Section = {(void*)& __TNAME(sname,tname), (void*)__CTEST_MAGIC};
124 #define __CTEST_STRUCT(sname, tname, _skip, __data, __setup, __teardown) \
125 static struct ctest __TNAME(sname, tname) = { \
128 __FNAME(sname, tname), \
131 (SetupFunc)__setup, \
132 (TearDownFunc)__teardown, \
135 __Test_Section static void * __PNAME(sname, tname)[2]= {(void*)& __TNAME(sname,tname), (void *)__CTEST_MAGIC};
138 #define CTEST_DATA(sname) struct sname##_data
140 #define CTEST_SETUP(sname) \
141 void WEAK sname##_setup(struct sname##_data* data)
143 #define CTEST_TEARDOWN(sname) \
144 void WEAK sname##_teardown(struct sname##_data* data)
146 #define __CTEST_INTERNAL(sname, tname, _skip) \
147 void __FNAME(sname, tname)(); \
148 __CTEST_STRUCT(sname, tname, _skip, NULL, NULL, NULL) \
149 void __FNAME(sname, tname)()
152 #define SETUP_FNAME(sname) NULL
153 #define TEARDOWN_FNAME(sname) NULL
155 #define SETUP_FNAME(sname) sname##_setup
156 #define TEARDOWN_FNAME(sname) sname##_teardown
159 #define __CTEST2_INTERNAL(sname, tname, _skip) \
160 static struct sname##_data __ctest_##sname##_data; \
161 CTEST_SETUP(sname); \
162 CTEST_TEARDOWN(sname); \
163 void __FNAME(sname, tname)(struct sname##_data* data); \
164 __CTEST_STRUCT(sname, tname, _skip, &__ctest_##sname##_data, SETUP_FNAME(sname), TEARDOWN_FNAME(sname)) \
165 void __FNAME(sname, tname)(struct sname##_data* data)
168 void CTEST_LOG(const char* fmt, ...);
169 void CTEST_ERR(const char* fmt, ...); // doesn't return
171 #define CTEST(sname, tname) __CTEST_INTERNAL(sname, tname, 0)
172 #define CTEST_SKIP(sname, tname) __CTEST_INTERNAL(sname, tname, 1)
174 #define CTEST2(sname, tname) __CTEST2_INTERNAL(sname, tname, 0)
175 #define CTEST2_SKIP(sname, tname) __CTEST2_INTERNAL(sname, tname, 1)
178 #ifdef CTEST_ADD_TESTS_MANUALLY
180 void __ctest_addTest(struct ctest *);
182 #define CTEST_ADD(sname, tname) do { \
183 extern struct ctest __TNAME(sname, tname); \
184 __ctest_addTest(&__TNAME(sname, tname)); \
187 #define CTEST_ADD2(sname, tname) do { \
188 extern struct ctest __TNAME(sname, tname); \
189 __ctest_addTest(&__TNAME(sname, tname)); \
192 #endif // CTEST_ADD_TESTS_MANUALLY
194 void assert_str(const char* exp, const char* real, const char* caller, int line);
195 #define ASSERT_STR(exp, real) assert_str(exp, real, __FILE__, __LINE__)
197 void assert_data(const unsigned char* exp, size_t expsize,
198 const unsigned char* real, size_t realsize,
199 const char* caller, int line);
200 #define ASSERT_DATA(exp, expsize, real, realsize) \
201 assert_data(exp, expsize, real, realsize, __FILE__, __LINE__)
203 void assert_equal(intmax_t exp, intmax_t real, const char* caller, int line);
204 #define ASSERT_EQUAL(exp, real) assert_equal(exp, real, __FILE__, __LINE__)
206 void assert_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line);
207 #define ASSERT_EQUAL_U(exp, real) assert_equal_u(exp, real, __FILE__, __LINE__)
209 void assert_not_equal(intmax_t exp, intmax_t real, const char* caller, int line);
210 #define ASSERT_NOT_EQUAL(exp, real) assert_not_equal(exp, real, __FILE__, __LINE__)
212 void assert_not_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line);
213 #define ASSERT_NOT_EQUAL_U(exp, real) assert_not_equal_u(exp, real, __FILE__, __LINE__)
215 void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line);
216 #define ASSERT_INTERVAL(exp1, exp2, real) assert_interval(exp1, exp2, real, __FILE__, __LINE__)
218 void assert_null(void* real, const char* caller, int line);
219 #define ASSERT_NULL(real) assert_null((void*)real, __FILE__, __LINE__)
221 void assert_not_null(const void* real, const char* caller, int line);
222 #define ASSERT_NOT_NULL(real) assert_not_null(real, __FILE__, __LINE__)
224 void assert_true(int real, const char* caller, int line);
225 #define ASSERT_TRUE(real) assert_true(real, __FILE__, __LINE__)
227 void assert_false(int real, const char* caller, int line);
228 #define ASSERT_FALSE(real) assert_false(real, __FILE__, __LINE__)
230 void assert_fail(const char* caller, int line);
231 #define ASSERT_FAIL() assert_fail(__FILE__, __LINE__)
233 /* If longjmp() is not available, integer flag will be used instead of jmp_buf.
235 * __CTEST_SETJMP() will clear the flag and return zero, and __CTEST_LONGJMP()
236 * will set the flag to its argument. __CTEST_ERROR_CODE() will return that flag.
238 * If longjmp() is available, jmp_buf will be used as usual and __CTEST_ERROR_CODE()
239 * will always return zero.
241 * You can check both __CTEST_SETJMP() and __CTEST_ERROR_CODE() return value
242 * to detect error in a portable way.
244 #ifdef __CTEST_NO_JMP
245 # define __CTEST_JMPBUF int
246 # define __CTEST_ERROR_CODE(_var) (_var)
247 # define __CTEST_SETJMP(_var) (_var = 0)
248 # define __CTEST_LONGJMP(_var, _err) (_var = _err)
249 #else // !__CTEST_NO_JMP
250 # define __CTEST_JMPBUF jmp_buf
251 # define __CTEST_ERROR_CODE(_var) (0)
252 # define __CTEST_SETJMP(_var) setjmp(_var)
253 # define __CTEST_LONGJMP(_var, _err) longjmp(_var, _err)
254 #endif // __CTEST_NO_JMP
256 void assert_dbl_near(double exp, double real, double tol, const char* caller, int line);
257 #define ASSERT_DBL_NEAR(exp, real) assert_dbl_near(exp, real, 1e-4, __FILE__, __LINE__)
258 #define ASSERT_DBL_NEAR_TOL(exp, real, tol) assert_dbl_near(exp, real, tol, __FILE__, __LINE__)
260 void assert_dbl_far(double exp, double real, double tol, const char* caller, int line);
261 #define ASSERT_DBL_FAR(exp, real) assert_dbl_far(exp, real, 1e-4, __FILE__, __LINE__)
262 #define ASSERT_DBL_FAR_TOL(exp, real, tol) assert_dbl_far(exp, real, tol, __FILE__, __LINE__)
266 #ifndef __CTEST_NO_JMP
274 #ifndef __CTEST_NO_TIME
275 #include <sys/time.h>
291 static size_t ctest_errorsize;
292 static char* ctest_errormsg;
293 #define MSG_SIZE 4096
294 static char ctest_errorbuffer[MSG_SIZE];
295 static __CTEST_JMPBUF ctest_err;
296 static int color_output = 1;
297 static const char* suite_name;
298 static const char* test_name;
300 typedef int (*filter_func)(struct ctest*);
302 #define ANSI_BLACK "\033[0;30m"
303 #define ANSI_RED "\033[0;31m"
304 #define ANSI_GREEN "\033[0;32m"
305 #define ANSI_YELLOW "\033[0;33m"
306 #define ANSI_BLUE "\033[0;34m"
307 #define ANSI_MAGENTA "\033[0;35m"
308 #define ANSI_CYAN "\033[0;36m"
309 #define ANSI_GREY "\033[0;37m"
310 #define ANSI_DARKGREY "\033[01;30m"
311 #define ANSI_BRED "\033[01;31m"
312 #define ANSI_BGREEN "\033[01;32m"
313 #define ANSI_BYELLOW "\033[01;33m"
314 #define ANSI_BBLUE "\033[01;34m"
315 #define ANSI_BMAGENTA "\033[01;35m"
316 #define ANSI_BCYAN "\033[01;36m"
317 #define ANSI_WHITE "\033[01;37m"
318 #define ANSI_NORMAL "\033[0m"
321 #ifndef CTEST_ADD_TESTS_MANUALLY
322 __declspec(allocate(".ctest$a")) struct ctest * ctest_win_begin;
323 __declspec(allocate(".ctest$z")) struct ctest * ctest_win_end;
327 static CTEST(suite, test) { }
329 #define __CTEST_POINTER_NEXT(_test) (struct ctest **)((struct ctest **)(_test) + 2)
330 #define __CTEST_POINTER_PREV(_test) (struct ctest **)((struct ctest **)(_test) - 2)
332 /* First element of test list.
334 static struct ctest * * __ctest_head_p = (struct ctest **)__PNAME(suite, test);
336 #ifdef CTEST_ADD_TESTS_MANUALLY
338 /* Last element of test list.
340 static struct ctest *__ctest_tail = &__TNAME(suite, test);
342 /* Add test to linked list manually.
344 void __ctest_addTest(struct ctest *test)
346 __ctest_tail->next = test;
349 #else // !CTEST_ADD_TESTS_MANUALLY
352 /* Add all tests to linked list automatically.
354 static void __ctest_linkTests()
356 struct ctest ** test;
357 struct ctest ** ctest_begin = (struct ctest **)__PNAME(suite, test);
358 struct ctest ** ctest_end = (struct ctest **)__PNAME(suite, test);
360 // find begin and end of section by comparing magics
362 struct ctest** t = __CTEST_POINTER_PREV(ctest_begin);
363 if (t[0] == NULL) break;
364 if (t[1] != (struct ctest*)__CTEST_MAGIC) break;
368 struct ctest** t = __CTEST_POINTER_NEXT(ctest_end);
369 if (t[0] == NULL) break;
370 if (t[1] != (struct ctest*)__CTEST_MAGIC) break;
373 ctest_end = __CTEST_POINTER_NEXT(ctest_end); // end after last one
375 for (test = ctest_begin; test != ctest_end; test = __CTEST_POINTER_NEXT(test)) {
376 struct ctest ** next_p = __CTEST_POINTER_NEXT(test);
378 if (next_p == ctest_end)
383 (*test)->next = next;
386 __ctest_head_p = ctest_begin;
389 static void __ctest_linkTests()
391 struct ctest ** ctest_start = __ctest_head_p;
392 struct ctest ** test;
393 struct ctest * cur=ctest_start[0];
395 for(test=&ctest_win_begin; test!=&ctest_win_end; test++){
397 if(test[1] == (struct ctest*)__CTEST_MAGIC){
399 if((test[0]) == ctest_start[0]) continue;
410 inline static void vprint_errormsg(const char* const fmt, va_list ap) {
411 // (v)snprintf returns the number that would have been written
412 const int ret = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, ap);
414 ctest_errormsg[0] = 0x00;
416 const size_t size = (size_t) ret;
417 const size_t s = (ctest_errorsize <= size ? size -ctest_errorsize : size);
418 // ctest_errorsize may overflow at this point
419 ctest_errorsize -= s;
424 inline static void print_errormsg(const char* const fmt, ...) {
427 vprint_errormsg(fmt, argp);
431 static void msg_start(const char* color, const char* title) {
433 print_errormsg("%s", color);
435 print_errormsg(" %s: ", title);
438 static void msg_end() {
440 print_errormsg(ANSI_NORMAL);
442 print_errormsg("\n");
445 void CTEST_LOG(const char* fmt, ...)
448 msg_start(ANSI_BLUE, "LOG");
451 vprint_errormsg(fmt, argp);
457 void CTEST_ERR(const char* fmt, ...)
460 msg_start(ANSI_YELLOW, "ERR");
463 vprint_errormsg(fmt, argp);
467 __CTEST_LONGJMP(ctest_err, 1);
470 void assert_str(const char* exp, const char* real, const char* caller, int line) {
471 if ((exp == NULL && real != NULL) ||
472 (exp != NULL && real == NULL) ||
473 (exp && real && strcmp(exp, real) != 0)) {
474 CTEST_ERR("%s:%d expected '%s', got '%s'", caller, line, exp, real);
478 void assert_data(const unsigned char* exp, size_t expsize,
479 const unsigned char* real, size_t realsize,
480 const char* caller, int line) {
482 if (expsize != realsize) {
483 CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize);
485 for (i=0; i<expsize; i++) {
486 if (exp[i] != real[i]) {
487 CTEST_ERR("%s:%d expected 0x%02x at offset %" PRIuMAX " got 0x%02x",
488 caller, line, exp[i], (uintmax_t) i, real[i]);
493 void assert_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
495 CTEST_ERR("%s:%d expected %" PRIdMAX ", got %" PRIdMAX, caller, line, exp, real);
499 void assert_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
501 CTEST_ERR("%s:%d expected %" PRIuMAX ", got %" PRIuMAX, caller, line, exp, real);
505 void assert_not_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
506 if ((exp) == (real)) {
507 CTEST_ERR("%s:%d should not be %" PRIdMAX, caller, line, real);
511 void assert_not_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
512 if ((exp) == (real)) {
513 CTEST_ERR("%s:%d should not be %" PRIuMAX, caller, line, real);
517 void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line) {
518 if (real < exp1 || real > exp2) {
519 CTEST_ERR("%s:%d expected %" PRIdMAX "-%" PRIdMAX ", got %" PRIdMAX, caller, line, exp1, exp2, real);
523 void assert_dbl_near(double exp, double real, double tol, const char* caller, int line) {
524 double diff = exp - real;
525 double absdiff = diff;
526 /* avoid using fabs and linking with a math lib */
531 CTEST_ERR("%s:%d expected %0.3e, got %0.3e (diff %0.3e, tol %0.3e)", caller, line, exp, real, diff, tol);
535 void assert_dbl_far(double exp, double real, double tol, const char* caller, int line) {
536 double diff = exp - real;
537 double absdiff = diff;
538 /* avoid using fabs and linking with a math lib */
542 if (absdiff <= tol) {
543 CTEST_ERR("%s:%d expected %0.3e, got %0.3e (diff %0.3e, tol %0.3e)", caller, line, exp, real, diff, tol);
547 void assert_null(void* real, const char* caller, int line) {
548 if ((real) != NULL) {
549 CTEST_ERR("%s:%d should be NULL", caller, line);
553 void assert_not_null(const void* real, const char* caller, int line) {
555 CTEST_ERR("%s:%d should not be NULL", caller, line);
559 void assert_true(int real, const char* caller, int line) {
561 CTEST_ERR("%s:%d should be true", caller, line);
565 void assert_false(int real, const char* caller, int line) {
567 CTEST_ERR("%s:%d should be false", caller, line);
571 void assert_fail(const char* caller, int line) {
572 CTEST_ERR("%s:%d shouldn't come here", caller, line);
576 static int suite_all(struct ctest* t) {
577 (void) t; // fix unused parameter warning
581 static int suite_filter(struct ctest* t) {
582 return strncmp(suite_name, t->ssname, strlen(suite_name)) == 0;
585 static int suite_test_filter(struct ctest* t) {
586 int suit_match, test_match;
587 suit_match=(strncmp(suite_name, t->ssname, strlen(suite_name)) == 0);
588 test_match=(strncmp(test_name, t->ttname, strlen(test_name)) == 0);
589 return (suit_match & test_match);
593 #ifndef __CTEST_NO_TIME
594 static uint64_t getCurrentTime() {
596 gettimeofday(&now, NULL);
597 uint64_t now64 = (uint64_t) now.tv_sec;
599 now64 += ((uint64_t) now.tv_usec);
604 static void color_print(const char* color, const char* text) {
606 printf("%s%s"ANSI_NORMAL"\n", color, text);
608 printf("%s\n", text);
612 static void *find_symbol(struct ctest *test, const char *fname)
614 size_t len = strlen(test->ssname) + 1 + strlen(fname);
615 char *symbol_name = (char *) malloc(len + 1);
616 memset(symbol_name, 0, len + 1);
617 snprintf(symbol_name, len + 1, "%s_%s", test->ssname, fname);
619 //fprintf(stderr, ">>>> dlsym: loading %s\n", symbol_name);
620 void *symbol = dlsym(RTLD_DEFAULT, symbol_name);
622 //fprintf(stderr, ">>>> ERROR: %s\n", dlerror());
624 // returns NULL on error
631 #ifdef CTEST_SEGFAULT
633 static void sighandler(int signum)
636 snprintf(msg, sizeof(msg), "[SIGNAL %d: %s]", signum, strsignal(signum));
637 color_print(ANSI_BRED, msg);
640 /* "Unregister" the signal handler and send the signal back to the process
641 * so it can terminate as expected */
642 signal(signum, SIG_DFL);
643 kill(getpid(), signum);
647 int ctest_main(int argc, const char *argv[])
649 static int total = 0;
650 static int num_ok = 0;
651 static int num_fail = 0;
652 static int num_skip = 0;
653 static int index = 1;
654 static filter_func filter = suite_all;
656 const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN;
658 static struct ctest* test;
660 #ifdef CTEST_SEGFAULT
661 signal(SIGSEGV, sighandler);
665 suite_name = argv[1];
666 filter = suite_filter;
667 }else if (argc == 3) {
668 suite_name = argv[1];
670 filter = suite_test_filter;
673 #ifdef CTEST_NO_COLORS
676 color_output = isatty(1);
679 #ifndef __CTEST_NO_TIME
680 uint64_t t1 = getCurrentTime();
683 #ifndef CTEST_ADD_TESTS_MANUALLY
688 for (test = *(__ctest_head_p); test != NULL; test=test->next) {
689 if (test == &__ctest_suite_test) continue;
690 if (filter(test)) total++;
693 for (test = *(__ctest_head_p); test != NULL; test=test->next) {
694 if (test == &__ctest_suite_test) continue;
696 ctest_errorbuffer[0] = 0;
697 ctest_errorsize = MSG_SIZE-1;
698 ctest_errormsg = ctest_errorbuffer;
699 printf("TEST %d/%d %s:%s ", index, total, test->ssname, test->ttname);
702 color_print(ANSI_BYELLOW, "[SKIPPED]");
705 int result = __CTEST_SETJMP(ctest_err);
709 test->setup = (SetupFunc) find_symbol(test, "setup");
711 if (!test->teardown) {
712 test->teardown = (TearDownFunc) find_symbol(test, "teardown");
716 if (test->setup) test->setup(test->data);
718 ((RunWithDataFunc)test->run)(test->data);
721 if (test->teardown) test->teardown(test->data);
722 // if we got here it's ok
723 #ifdef CTEST_COLOR_OK
724 color_print(ANSI_BGREEN, "[OK]");
730 color_print(ANSI_BRED, "[FAIL]");
733 if (ctest_errorsize != MSG_SIZE-1) printf("%s", ctest_errorbuffer);
738 #ifndef __CTEST_NO_TIME
739 uint64_t t2 = getCurrentTime();
742 #ifndef __CTEST_NO_TIME
743 sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped) ran in %"PRIu64" ms", total, num_ok, num_fail, num_skip, (t2 - t1)/1000);
745 sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped)", total, num_ok, num_fail, num_skip);
747 color_print(color, results);