You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ctest.h 22 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /* Copyright 2011-2016 Bas van den Berg
  2. *
  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
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  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.
  14. */
  15. #ifndef CTEST_H
  16. #define CTEST_H
  17. #if defined _WIN32 || defined __CYGWIN__
  18. #ifndef WIN32
  19. #define WIN32
  20. #endif
  21. #endif
  22. #ifndef WIN32
  23. #define WEAK __attribute__ ((weak))
  24. #else
  25. #define WEAK
  26. #endif
  27. #include <inttypes.h> /* intmax_t, uintmax_t, PRI* */
  28. #include <stddef.h> /* size_t */
  29. typedef void (*SetupFunc)(void*);
  30. typedef void (*TearDownFunc)(void*);
  31. typedef void (*RunWithDataFunc)(void*);
  32. struct ctest {
  33. const char* ssname; // suite name
  34. const char* ttname; // test name
  35. void (*run)();
  36. int skip;
  37. void* data;
  38. SetupFunc setup;
  39. TearDownFunc teardown;
  40. struct ctest *next;
  41. unsigned int magic;
  42. };
  43. #define __FNAME(sname, tname) __ctest_##sname##_##tname##_run
  44. #define __TNAME(sname, tname) __ctest_##sname##_##tname
  45. #define __PNAME(sname, tname) __ctest_##sname##_##tname##_pointer
  46. #ifdef __APPLE__
  47. #define __CTEST_APPLE
  48. #endif
  49. #ifdef __MINGW32__
  50. #undef CTEST_SEGFAULT
  51. #endif
  52. #if defined(_WIN32) && defined(_MSC_VER)
  53. #define __CTEST_MSVC
  54. #endif
  55. //config for MSVC compiler
  56. #ifdef __CTEST_MSVC
  57. #define __CTEST_NO_TIME
  58. #define CTEST_NO_COLORS
  59. #ifndef CTEST_ADD_TESTS_MANUALLY
  60. #pragma section(".ctest$a")
  61. #pragma section(".ctest$u")
  62. #pragma section(".ctest$z")
  63. #endif
  64. //clear this flag for msvc
  65. #ifdef CTEST_SEGFAULT
  66. #undef CTEST_SEGFAULT
  67. #endif
  68. #if _MSC_VER < 1900
  69. #define snprintf _snprintf_s
  70. #endif
  71. #ifndef __cplusplus
  72. #define inline __inline
  73. #endif
  74. #endif
  75. #ifdef CTEST_NO_JMP
  76. #define __CTEST_NO_JMP
  77. #endif
  78. #define __CTEST_MAGIC (0xdeadbeef)
  79. #ifdef CTEST_ADD_TESTS_MANUALLY
  80. # define __Test_Section
  81. #else
  82. #ifdef __CTEST_APPLE
  83. #define __Test_Section __attribute__ ((used, section ("__DATA, .ctest")))
  84. #elif defined (__CTEST_MSVC)
  85. #define __Test_Section __declspec( allocate(".ctest$u"))
  86. #else
  87. #define __Test_Section __attribute__ ((used, section (".ctest")))
  88. #endif
  89. #endif
  90. #ifndef __CTEST_MSVC
  91. #define __CTEST_STRUCT(sname, tname, _skip, __data, __setup, __teardown) \
  92. static struct ctest __TNAME(sname, tname) = { \
  93. .ssname=#sname, \
  94. .ttname=#tname, \
  95. .run = __FNAME(sname, tname), \
  96. .skip = _skip, \
  97. .data = __data, \
  98. .setup = (SetupFunc)__setup, \
  99. .teardown = (TearDownFunc)__teardown, \
  100. .next = NULL, \
  101. .magic = __CTEST_MAGIC}; \
  102. static void * __PNAME(sname, tname)[2] __Test_Section = {(void*)& __TNAME(sname,tname), (void*)__CTEST_MAGIC};
  103. #else
  104. //for msvc
  105. #define __CTEST_STRUCT(sname, tname, _skip, __data, __setup, __teardown) \
  106. static struct ctest __TNAME(sname, tname) = { \
  107. #sname, \
  108. #tname, \
  109. __FNAME(sname, tname), \
  110. _skip, \
  111. __data, \
  112. (SetupFunc)__setup, \
  113. (TearDownFunc)__teardown, \
  114. NULL, \
  115. __CTEST_MAGIC}; \
  116. __Test_Section static void * __PNAME(sname, tname)[2]= {(void*)& __TNAME(sname,tname), (void *)__CTEST_MAGIC};
  117. #endif
  118. #define CTEST_DATA(sname) struct sname##_data
  119. #define CTEST_SETUP(sname) \
  120. void WEAK sname##_setup(struct sname##_data* data)
  121. #define CTEST_TEARDOWN(sname) \
  122. void WEAK sname##_teardown(struct sname##_data* data)
  123. #define __CTEST_INTERNAL(sname, tname, _skip) \
  124. void __FNAME(sname, tname)(); \
  125. __CTEST_STRUCT(sname, tname, _skip, NULL, NULL, NULL) \
  126. void __FNAME(sname, tname)()
  127. #ifdef __CTEST_APPLE
  128. #define SETUP_FNAME(sname) NULL
  129. #define TEARDOWN_FNAME(sname) NULL
  130. #else
  131. #define SETUP_FNAME(sname) sname##_setup
  132. #define TEARDOWN_FNAME(sname) sname##_teardown
  133. #endif
  134. #define __CTEST2_INTERNAL(sname, tname, _skip) \
  135. static struct sname##_data __ctest_##sname##_data; \
  136. CTEST_SETUP(sname); \
  137. CTEST_TEARDOWN(sname); \
  138. void __FNAME(sname, tname)(struct sname##_data* data); \
  139. __CTEST_STRUCT(sname, tname, _skip, &__ctest_##sname##_data, SETUP_FNAME(sname), TEARDOWN_FNAME(sname)) \
  140. void __FNAME(sname, tname)(struct sname##_data* data)
  141. void CTEST_LOG(const char* fmt, ...);
  142. void CTEST_ERR(const char* fmt, ...); // doesn't return
  143. #define CTEST(sname, tname) __CTEST_INTERNAL(sname, tname, 0)
  144. #define CTEST_SKIP(sname, tname) __CTEST_INTERNAL(sname, tname, 1)
  145. #define CTEST2(sname, tname) __CTEST2_INTERNAL(sname, tname, 0)
  146. #define CTEST2_SKIP(sname, tname) __CTEST2_INTERNAL(sname, tname, 1)
  147. #ifdef CTEST_ADD_TESTS_MANUALLY
  148. void __ctest_addTest(struct ctest *);
  149. #define CTEST_ADD(sname, tname) do { \
  150. extern struct ctest __TNAME(sname, tname); \
  151. __ctest_addTest(&__TNAME(sname, tname)); \
  152. } while (0)
  153. #define CTEST_ADD2(sname, tname) do { \
  154. extern struct ctest __TNAME(sname, tname); \
  155. __ctest_addTest(&__TNAME(sname, tname)); \
  156. } while (0)
  157. #endif // CTEST_ADD_TESTS_MANUALLY
  158. void assert_str(const char* exp, const char* real, const char* caller, int line);
  159. #define ASSERT_STR(exp, real) assert_str(exp, real, __FILE__, __LINE__)
  160. void assert_data(const unsigned char* exp, size_t expsize,
  161. const unsigned char* real, size_t realsize,
  162. const char* caller, int line);
  163. #define ASSERT_DATA(exp, expsize, real, realsize) \
  164. assert_data(exp, expsize, real, realsize, __FILE__, __LINE__)
  165. void assert_equal(intmax_t exp, intmax_t real, const char* caller, int line);
  166. #define ASSERT_EQUAL(exp, real) assert_equal(exp, real, __FILE__, __LINE__)
  167. void assert_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line);
  168. #define ASSERT_EQUAL_U(exp, real) assert_equal_u(exp, real, __FILE__, __LINE__)
  169. void assert_not_equal(intmax_t exp, intmax_t real, const char* caller, int line);
  170. #define ASSERT_NOT_EQUAL(exp, real) assert_not_equal(exp, real, __FILE__, __LINE__)
  171. void assert_not_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line);
  172. #define ASSERT_NOT_EQUAL_U(exp, real) assert_not_equal_u(exp, real, __FILE__, __LINE__)
  173. void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line);
  174. #define ASSERT_INTERVAL(exp1, exp2, real) assert_interval(exp1, exp2, real, __FILE__, __LINE__)
  175. void assert_null(void* real, const char* caller, int line);
  176. #define ASSERT_NULL(real) assert_null((void*)real, __FILE__, __LINE__)
  177. void assert_not_null(const void* real, const char* caller, int line);
  178. #define ASSERT_NOT_NULL(real) assert_not_null(real, __FILE__, __LINE__)
  179. void assert_true(int real, const char* caller, int line);
  180. #define ASSERT_TRUE(real) assert_true(real, __FILE__, __LINE__)
  181. void assert_false(int real, const char* caller, int line);
  182. #define ASSERT_FALSE(real) assert_false(real, __FILE__, __LINE__)
  183. void assert_fail(const char* caller, int line);
  184. #define ASSERT_FAIL() assert_fail(__FILE__, __LINE__)
  185. /* If longjmp() is not available, integer flag will be used instead of jmp_buf.
  186. *
  187. * __CTEST_SETJMP() will clear the flag and return zero, and __CTEST_LONGJMP()
  188. * will set the flag to its argument. __CTEST_ERROR_CODE() will return that flag.
  189. *
  190. * If longjmp() is available, jmp_buf will be used as usual and __CTEST_ERROR_CODE()
  191. * will always return zero.
  192. *
  193. * You can check both __CTEST_SETJMP() and __CTEST_ERROR_CODE() return value
  194. * to detect error in a portable way.
  195. */
  196. #ifdef __CTEST_NO_JMP
  197. # define __CTEST_JMPBUF int
  198. # define __CTEST_ERROR_CODE(_var) (_var)
  199. # define __CTEST_SETJMP(_var) (_var = 0)
  200. # define __CTEST_LONGJMP(_var, _err) (_var = _err)
  201. #else // !__CTEST_NO_JMP
  202. # define __CTEST_JMPBUF jmp_buf
  203. # define __CTEST_ERROR_CODE(_var) (0)
  204. # define __CTEST_SETJMP(_var) setjmp(_var)
  205. # define __CTEST_LONGJMP(_var, _err) longjmp(_var, _err)
  206. #endif // __CTEST_NO_JMP
  207. void assert_dbl_near(double exp, double real, double tol, const char* caller, int line);
  208. #define ASSERT_DBL_NEAR(exp, real) assert_dbl_near(exp, real, 1e-4, __FILE__, __LINE__)
  209. #define ASSERT_DBL_NEAR_TOL(exp, real, tol) assert_dbl_near(exp, real, tol, __FILE__, __LINE__)
  210. void assert_dbl_far(double exp, double real, double tol, const char* caller, int line);
  211. #define ASSERT_DBL_FAR(exp, real) assert_dbl_far(exp, real, 1e-4, __FILE__, __LINE__)
  212. #define ASSERT_DBL_FAR_TOL(exp, real, tol) assert_dbl_far(exp, real, tol, __FILE__, __LINE__)
  213. #ifdef CTEST_MAIN
  214. #ifndef __CTEST_NO_JMP
  215. #include <setjmp.h>
  216. #endif
  217. #include <stdarg.h>
  218. #include <stdio.h>
  219. #include <string.h>
  220. #ifndef __CTEST_NO_TIME
  221. #include <sys/time.h>
  222. #endif
  223. #include <stdint.h>
  224. #ifdef __CTEST_MSVC
  225. #include <io.h>
  226. #else
  227. #include <unistd.h>
  228. #endif
  229. #include <stdlib.h>
  230. #ifdef __CTEST_APPLE
  231. #include <dlfcn.h>
  232. #endif
  233. static size_t ctest_errorsize;
  234. static char* ctest_errormsg;
  235. #define MSG_SIZE 4096
  236. static char ctest_errorbuffer[MSG_SIZE];
  237. static __CTEST_JMPBUF ctest_err;
  238. static int color_output = 1;
  239. static const char* suite_name;
  240. static const char* test_name;
  241. typedef int (*filter_func)(struct ctest*);
  242. #define ANSI_BLACK "\033[0;30m"
  243. #define ANSI_RED "\033[0;31m"
  244. #define ANSI_GREEN "\033[0;32m"
  245. #define ANSI_YELLOW "\033[0;33m"
  246. #define ANSI_BLUE "\033[0;34m"
  247. #define ANSI_MAGENTA "\033[0;35m"
  248. #define ANSI_CYAN "\033[0;36m"
  249. #define ANSI_GREY "\033[0;37m"
  250. #define ANSI_DARKGREY "\033[01;30m"
  251. #define ANSI_BRED "\033[01;31m"
  252. #define ANSI_BGREEN "\033[01;32m"
  253. #define ANSI_BYELLOW "\033[01;33m"
  254. #define ANSI_BBLUE "\033[01;34m"
  255. #define ANSI_BMAGENTA "\033[01;35m"
  256. #define ANSI_BCYAN "\033[01;36m"
  257. #define ANSI_WHITE "\033[01;37m"
  258. #define ANSI_NORMAL "\033[0m"
  259. #ifdef __CTEST_MSVC
  260. #ifndef CTEST_ADD_TESTS_MANUALLY
  261. __declspec(allocate(".ctest$a")) struct ctest * ctest_win_begin;
  262. __declspec(allocate(".ctest$z")) struct ctest * ctest_win_end;
  263. #endif
  264. #endif
  265. static CTEST(suite, test) { }
  266. #define __CTEST_POINTER_NEXT(_test) (struct ctest **)((struct ctest **)(_test) + 2)
  267. #define __CTEST_POINTER_PREV(_test) (struct ctest **)((struct ctest **)(_test) - 2)
  268. /* First element of test list.
  269. */
  270. static struct ctest * * __ctest_head_p = (struct ctest **)__PNAME(suite, test);
  271. #ifdef CTEST_ADD_TESTS_MANUALLY
  272. /* Last element of test list.
  273. */
  274. static struct ctest *__ctest_tail = &__TNAME(suite, test);
  275. /* Add test to linked list manually.
  276. */
  277. void __ctest_addTest(struct ctest *test)
  278. {
  279. __ctest_tail->next = test;
  280. __ctest_tail = test;
  281. }
  282. #else // !CTEST_ADD_TESTS_MANUALLY
  283. #ifndef __CTEST_MSVC
  284. /* Add all tests to linked list automatically.
  285. */
  286. static void __ctest_linkTests()
  287. {
  288. struct ctest ** test;
  289. struct ctest ** ctest_begin = (struct ctest **)__PNAME(suite, test);
  290. struct ctest ** ctest_end = (struct ctest **)__PNAME(suite, test);
  291. // find begin and end of section by comparing magics
  292. while (1) {
  293. struct ctest** t = __CTEST_POINTER_PREV(ctest_begin);
  294. if (t[0] == NULL) break;
  295. if (t[1] != (struct ctest*)__CTEST_MAGIC) break;
  296. ctest_begin = t;
  297. }
  298. while (1) {
  299. struct ctest** t = __CTEST_POINTER_NEXT(ctest_end);
  300. if (t[0] == NULL) break;
  301. if (t[1] != (struct ctest*)__CTEST_MAGIC) break;
  302. ctest_end = t;
  303. }
  304. ctest_end = __CTEST_POINTER_NEXT(ctest_end); // end after last one
  305. for (test = ctest_begin; test != ctest_end; test = __CTEST_POINTER_NEXT(test)) {
  306. struct ctest ** next_p = __CTEST_POINTER_NEXT(test);
  307. struct ctest * next;
  308. if (next_p == ctest_end)
  309. next = NULL;
  310. else
  311. next = next_p[0];
  312. (*test)->next = next;
  313. }
  314. __ctest_head_p = ctest_begin;
  315. }
  316. #else //for msvc
  317. static void __ctest_linkTests()
  318. {
  319. struct ctest ** ctest_start = __ctest_head_p;
  320. struct ctest ** test;
  321. struct ctest * cur=ctest_start[0];
  322. for(test=&ctest_win_begin; test!=&ctest_win_end; test++){
  323. //check
  324. if(test[1] == (struct ctest*)__CTEST_MAGIC){
  325. //skip the start
  326. if((test[0]) == ctest_start[0]) continue;
  327. cur->next = test[0];
  328. cur=cur->next;
  329. cur->next=NULL;
  330. }
  331. }
  332. }
  333. #endif
  334. #endif
  335. inline static void vprint_errormsg(const char* const fmt, va_list ap) {
  336. // (v)snprintf returns the number that would have been written
  337. const int ret = vsnprintf(ctest_errormsg, ctest_errorsize, fmt, ap);
  338. if (ret < 0) {
  339. ctest_errormsg[0] = 0x00;
  340. } else {
  341. const size_t size = (size_t) ret;
  342. const size_t s = (ctest_errorsize <= size ? size -ctest_errorsize : size);
  343. // ctest_errorsize may overflow at this point
  344. ctest_errorsize -= s;
  345. ctest_errormsg += s;
  346. }
  347. }
  348. inline static void print_errormsg(const char* const fmt, ...) {
  349. va_list argp;
  350. va_start(argp, fmt);
  351. vprint_errormsg(fmt, argp);
  352. va_end(argp);
  353. }
  354. static void msg_start(const char* color, const char* title) {
  355. if (color_output) {
  356. print_errormsg("%s", color);
  357. }
  358. print_errormsg(" %s: ", title);
  359. }
  360. static void msg_end() {
  361. if (color_output) {
  362. print_errormsg(ANSI_NORMAL);
  363. }
  364. print_errormsg("\n");
  365. }
  366. void CTEST_LOG(const char* fmt, ...)
  367. {
  368. va_list argp;
  369. msg_start(ANSI_BLUE, "LOG");
  370. va_start(argp, fmt);
  371. vprint_errormsg(fmt, argp);
  372. va_end(argp);
  373. msg_end();
  374. }
  375. void CTEST_ERR(const char* fmt, ...)
  376. {
  377. va_list argp;
  378. msg_start(ANSI_YELLOW, "ERR");
  379. va_start(argp, fmt);
  380. vprint_errormsg(fmt, argp);
  381. va_end(argp);
  382. msg_end();
  383. __CTEST_LONGJMP(ctest_err, 1);
  384. }
  385. void assert_str(const char* exp, const char* real, const char* caller, int line) {
  386. if ((exp == NULL && real != NULL) ||
  387. (exp != NULL && real == NULL) ||
  388. (exp && real && strcmp(exp, real) != 0)) {
  389. CTEST_ERR("%s:%d expected '%s', got '%s'", caller, line, exp, real);
  390. }
  391. }
  392. void assert_data(const unsigned char* exp, size_t expsize,
  393. const unsigned char* real, size_t realsize,
  394. const char* caller, int line) {
  395. size_t i;
  396. if (expsize != realsize) {
  397. CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize);
  398. }
  399. for (i=0; i<expsize; i++) {
  400. if (exp[i] != real[i]) {
  401. CTEST_ERR("%s:%d expected 0x%02x at offset %" PRIuMAX " got 0x%02x",
  402. caller, line, exp[i], (uintmax_t) i, real[i]);
  403. }
  404. }
  405. }
  406. void assert_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
  407. if (exp != real) {
  408. CTEST_ERR("%s:%d expected %" PRIdMAX ", got %" PRIdMAX, caller, line, exp, real);
  409. }
  410. }
  411. void assert_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
  412. if (exp != real) {
  413. CTEST_ERR("%s:%d expected %" PRIuMAX ", got %" PRIuMAX, caller, line, exp, real);
  414. }
  415. }
  416. void assert_not_equal(intmax_t exp, intmax_t real, const char* caller, int line) {
  417. if ((exp) == (real)) {
  418. CTEST_ERR("%s:%d should not be %" PRIdMAX, caller, line, real);
  419. }
  420. }
  421. void assert_not_equal_u(uintmax_t exp, uintmax_t real, const char* caller, int line) {
  422. if ((exp) == (real)) {
  423. CTEST_ERR("%s:%d should not be %" PRIuMAX, caller, line, real);
  424. }
  425. }
  426. void assert_interval(intmax_t exp1, intmax_t exp2, intmax_t real, const char* caller, int line) {
  427. if (real < exp1 || real > exp2) {
  428. CTEST_ERR("%s:%d expected %" PRIdMAX "-%" PRIdMAX ", got %" PRIdMAX, caller, line, exp1, exp2, real);
  429. }
  430. }
  431. void assert_dbl_near(double exp, double real, double tol, const char* caller, int line) {
  432. double diff = exp - real;
  433. double absdiff = diff;
  434. /* avoid using fabs and linking with a math lib */
  435. if(diff < 0) {
  436. absdiff *= -1;
  437. }
  438. if (absdiff > tol) {
  439. CTEST_ERR("%s:%d expected %0.3e, got %0.3e (diff %0.3e, tol %0.3e)", caller, line, exp, real, diff, tol);
  440. }
  441. }
  442. void assert_dbl_far(double exp, double real, double tol, const char* caller, int line) {
  443. double diff = exp - real;
  444. double absdiff = diff;
  445. /* avoid using fabs and linking with a math lib */
  446. if(diff < 0) {
  447. absdiff *= -1;
  448. }
  449. if (absdiff <= tol) {
  450. CTEST_ERR("%s:%d expected %0.3e, got %0.3e (diff %0.3e, tol %0.3e)", caller, line, exp, real, diff, tol);
  451. }
  452. }
  453. void assert_null(void* real, const char* caller, int line) {
  454. if ((real) != NULL) {
  455. CTEST_ERR("%s:%d should be NULL", caller, line);
  456. }
  457. }
  458. void assert_not_null(const void* real, const char* caller, int line) {
  459. if (real == NULL) {
  460. CTEST_ERR("%s:%d should not be NULL", caller, line);
  461. }
  462. }
  463. void assert_true(int real, const char* caller, int line) {
  464. if ((real) == 0) {
  465. CTEST_ERR("%s:%d should be true", caller, line);
  466. }
  467. }
  468. void assert_false(int real, const char* caller, int line) {
  469. if ((real) != 0) {
  470. CTEST_ERR("%s:%d should be false", caller, line);
  471. }
  472. }
  473. void assert_fail(const char* caller, int line) {
  474. CTEST_ERR("%s:%d shouldn't come here", caller, line);
  475. }
  476. static int suite_all(struct ctest* t) {
  477. (void) t; // fix unused parameter warning
  478. return 1;
  479. }
  480. static int suite_filter(struct ctest* t) {
  481. return strncmp(suite_name, t->ssname, strlen(suite_name)) == 0;
  482. }
  483. static int suite_test_filter(struct ctest* t) {
  484. int suit_match, test_match;
  485. suit_match=(strncmp(suite_name, t->ssname, strlen(suite_name)) == 0);
  486. test_match=(strncmp(test_name, t->ttname, strlen(test_name)) == 0);
  487. return (suit_match & test_match);
  488. }
  489. #ifndef __CTEST_NO_TIME
  490. static uint64_t getCurrentTime() {
  491. struct timeval now;
  492. gettimeofday(&now, NULL);
  493. uint64_t now64 = (uint64_t) now.tv_sec;
  494. now64 *= 1000000;
  495. now64 += ((uint64_t) now.tv_usec);
  496. return now64;
  497. }
  498. #endif
  499. static void color_print(const char* color, const char* text) {
  500. if (color_output)
  501. printf("%s%s"ANSI_NORMAL"\n", color, text);
  502. else
  503. printf("%s\n", text);
  504. }
  505. #ifdef __CTEST_APPLE
  506. static void *find_symbol(struct ctest *test, const char *fname)
  507. {
  508. size_t len = strlen(test->ssname) + 1 + strlen(fname);
  509. char *symbol_name = (char *) malloc(len + 1);
  510. memset(symbol_name, 0, len + 1);
  511. snprintf(symbol_name, len + 1, "%s_%s", test->ssname, fname);
  512. //fprintf(stderr, ">>>> dlsym: loading %s\n", symbol_name);
  513. void *symbol = dlsym(RTLD_DEFAULT, symbol_name);
  514. if (!symbol) {
  515. //fprintf(stderr, ">>>> ERROR: %s\n", dlerror());
  516. }
  517. // returns NULL on error
  518. free(symbol_name);
  519. return symbol;
  520. }
  521. #endif
  522. #ifdef CTEST_SEGFAULT
  523. #include <signal.h>
  524. static void sighandler(int signum)
  525. {
  526. char msg[128];
  527. snprintf(msg, sizeof(msg), "[SIGNAL %d: %s]", signum, strsignal(signum));
  528. color_print(ANSI_BRED, msg);
  529. fflush(stdout);
  530. /* "Unregister" the signal handler and send the signal back to the process
  531. * so it can terminate as expected */
  532. signal(signum, SIG_DFL);
  533. kill(getpid(), signum);
  534. }
  535. #endif
  536. int ctest_main(int argc, const char *argv[])
  537. {
  538. static int total = 0;
  539. static int num_ok = 0;
  540. static int num_fail = 0;
  541. static int num_skip = 0;
  542. static int index = 1;
  543. static filter_func filter = suite_all;
  544. const char* color = (num_fail) ? ANSI_BRED : ANSI_GREEN;
  545. char results[80];
  546. static struct ctest* test;
  547. #ifdef CTEST_SEGFAULT
  548. signal(SIGSEGV, sighandler);
  549. #endif
  550. if (argc == 2) {
  551. suite_name = argv[1];
  552. filter = suite_filter;
  553. }else if (argc == 3) {
  554. suite_name = argv[1];
  555. test_name = argv[2];
  556. filter = suite_test_filter;
  557. }
  558. #ifdef CTEST_NO_COLORS
  559. color_output = 0;
  560. #else
  561. color_output = isatty(1);
  562. #endif
  563. #ifndef __CTEST_NO_TIME
  564. uint64_t t1 = getCurrentTime();
  565. #endif
  566. #ifndef CTEST_ADD_TESTS_MANUALLY
  567. __ctest_linkTests();
  568. #endif
  569. for (test = *(__ctest_head_p); test != NULL; test=test->next) {
  570. if (test == &__ctest_suite_test) continue;
  571. if (filter(test)) total++;
  572. }
  573. for (test = *(__ctest_head_p); test != NULL; test=test->next) {
  574. if (test == &__ctest_suite_test) continue;
  575. if (filter(test)) {
  576. ctest_errorbuffer[0] = 0;
  577. ctest_errorsize = MSG_SIZE-1;
  578. ctest_errormsg = ctest_errorbuffer;
  579. printf("TEST %d/%d %s:%s ", index, total, test->ssname, test->ttname);
  580. fflush(stdout);
  581. if (test->skip) {
  582. color_print(ANSI_BYELLOW, "[SKIPPED]");
  583. num_skip++;
  584. } else {
  585. int result = __CTEST_SETJMP(ctest_err);
  586. if (result == 0) {
  587. #ifdef __CTEST_APPLE
  588. if (!test->setup) {
  589. test->setup = (SetupFunc) find_symbol(test, "setup");
  590. }
  591. if (!test->teardown) {
  592. test->teardown = (TearDownFunc) find_symbol(test, "teardown");
  593. }
  594. #endif
  595. if (test->setup) test->setup(test->data);
  596. if (test->data)
  597. ((RunWithDataFunc)test->run)(test->data);
  598. else
  599. test->run();
  600. if (test->teardown) test->teardown(test->data);
  601. // if we got here it's ok
  602. #ifdef CTEST_COLOR_OK
  603. color_print(ANSI_BGREEN, "[OK]");
  604. #else
  605. printf("[OK]\n");
  606. #endif
  607. num_ok++;
  608. } else {
  609. color_print(ANSI_BRED, "[FAIL]");
  610. num_fail++;
  611. }
  612. if (ctest_errorsize != MSG_SIZE-1) printf("%s", ctest_errorbuffer);
  613. }
  614. index++;
  615. }
  616. }
  617. #ifndef __CTEST_NO_TIME
  618. uint64_t t2 = getCurrentTime();
  619. #endif
  620. #ifndef __CTEST_NO_TIME
  621. 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);
  622. #else
  623. sprintf(results, "RESULTS: %d tests (%d ok, %d failed, %d skipped)", total, num_ok, num_fail, num_skip);
  624. #endif
  625. color_print(color, results);
  626. return num_fail;
  627. }
  628. #endif
  629. #endif