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 23 kB

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