minunit.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2012 David Siñuela Pastor, [email protected]
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef MINUNIT_MINUNIT_H
  24. #define MINUNIT_MINUNIT_H
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif
  28. #if defined(_WIN32)
  29. #include <Windows.h>
  30. #if defined(_MSC_VER) && _MSC_VER < 1900
  31. #define snprintf _snprintf
  32. #define __func__ __FUNCTION__
  33. #endif
  34. #elif defined(__unix__) || defined(__unix) || defined(unix) || \
  35. (defined(__APPLE__) && defined(__MACH__))
  36. /* Change POSIX C SOURCE version for pure c99 compilers */
  37. #if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L
  38. #undef _POSIX_C_SOURCE
  39. #define _POSIX_C_SOURCE 200112L
  40. #endif
  41. #include <string.h>
  42. #include <sys/resource.h>
  43. #include <sys/time.h> /* gethrtime(), gettimeofday() */
  44. #include <sys/times.h>
  45. #include <time.h> /* clock_gettime(), time() */
  46. #include <unistd.h> /* POSIX flags */
  47. #if defined(__MACH__) && defined(__APPLE__)
  48. #include <mach/mach.h>
  49. #include <mach/mach_time.h>
  50. #endif
  51. #if __GNUC__ >= 5 && !defined(__STDC_VERSION__)
  52. #define __func__ __extension__ __FUNCTION__
  53. #endif
  54. #else
  55. #error "Unable to define timers for an unknown OS."
  56. #endif
  57. #include <math.h>
  58. #include <stdio.h>
  59. /* Maximum length of last message */
  60. #define MINUNIT_MESSAGE_LEN 1024
  61. /* Accuracy with which floats are compared */
  62. #define MINUNIT_EPSILON 1E-12
  63. /* Misc. counters */
  64. static int minunit_run = 0;
  65. static int minunit_assert = 0;
  66. static int minunit_fail = 0;
  67. static int minunit_status = 0;
  68. /* Timers */
  69. static double minunit_real_timer = 0;
  70. static double minunit_proc_timer = 0;
  71. /* Last message */
  72. static char minunit_last_message[MINUNIT_MESSAGE_LEN];
  73. /* Test setup and teardown function pointers */
  74. static void (*minunit_setup)(void) = NULL;
  75. static void (*minunit_teardown)(void) = NULL;
  76. /* Definitions */
  77. #define MU_TEST(method_name) static void method_name(void)
  78. #define MU_TEST_SUITE(suite_name) static void suite_name(void)
  79. #define MU__SAFE_BLOCK(block) \
  80. do { \
  81. block \
  82. } while (0)
  83. /* Run test suite and unset setup and teardown functions */
  84. #define MU_RUN_SUITE(suite_name) \
  85. MU__SAFE_BLOCK(suite_name(); minunit_setup = NULL; minunit_teardown = NULL;)
  86. /* Configure setup and teardown functions */
  87. #define MU_SUITE_CONFIGURE(setup_fun, teardown_fun) \
  88. MU__SAFE_BLOCK(minunit_setup = setup_fun; minunit_teardown = teardown_fun;)
  89. /* Test runner */
  90. #define MU_RUN_TEST(test) \
  91. MU__SAFE_BLOCK( \
  92. if (minunit_real_timer == 0 && minunit_proc_timer == 0) { \
  93. minunit_real_timer = mu_timer_real(); \
  94. minunit_proc_timer = mu_timer_cpu(); \
  95. } if (minunit_setup) (*minunit_setup)(); \
  96. minunit_status = 0; test(); minunit_run++; if (minunit_status) { \
  97. minunit_fail++; \
  98. printf("F"); \
  99. printf("\n%s\n", minunit_last_message); \
  100. } fflush(stdout); \
  101. if (minunit_teardown)(*minunit_teardown)();)
  102. /* Report */
  103. #define MU_REPORT() \
  104. MU__SAFE_BLOCK( \
  105. double minunit_end_real_timer; double minunit_end_proc_timer; \
  106. printf("\n\n%d tests, %d assertions, %d failures\n", minunit_run, \
  107. minunit_assert, minunit_fail); \
  108. minunit_end_real_timer = mu_timer_real(); \
  109. minunit_end_proc_timer = mu_timer_cpu(); \
  110. printf("\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n", \
  111. minunit_end_real_timer - minunit_real_timer, \
  112. minunit_end_proc_timer - minunit_proc_timer);)
  113. #define MU_EXIT_CODE minunit_fail
  114. /* Assertions */
  115. #define mu_check(test) \
  116. MU__SAFE_BLOCK( \
  117. minunit_assert++; if (!(test)) { \
  118. snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, \
  119. "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, \
  120. #test); \
  121. minunit_status = 1; \
  122. return; \
  123. } else { printf("."); })
  124. #define mu_fail(message) \
  125. MU__SAFE_BLOCK(minunit_assert++; \
  126. snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, \
  127. "%s failed:\n\t%s:%d: %s", __func__, __FILE__, \
  128. __LINE__, message); \
  129. minunit_status = 1; return;)
  130. #define mu_assert(test, message) \
  131. MU__SAFE_BLOCK( \
  132. minunit_assert++; if (!(test)) { \
  133. snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, \
  134. "%s failed:\n\t%s:%d: %s", __func__, __FILE__, __LINE__, \
  135. message); \
  136. minunit_status = 1; \
  137. return; \
  138. } else { printf("."); })
  139. #define mu_assert_int_eq(expected, result) \
  140. MU__SAFE_BLOCK( \
  141. int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; \
  142. minunit_tmp_e = (expected); minunit_tmp_r = (result); \
  143. if (minunit_tmp_e != minunit_tmp_r) { \
  144. snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, \
  145. "%s failed:\n\t%s:%d: %d expected but was %d", __func__, \
  146. __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r); \
  147. minunit_status = 1; \
  148. return; \
  149. } else { printf("."); })
  150. #define mu_assert_double_eq(expected, result) \
  151. MU__SAFE_BLOCK( \
  152. double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; \
  153. minunit_tmp_e = (expected); minunit_tmp_r = (result); \
  154. if (fabs(minunit_tmp_e - minunit_tmp_r) > MINUNIT_EPSILON) { \
  155. int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON); \
  156. snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, \
  157. "%s failed:\n\t%s:%d: %.*g expected but was %.*g", __func__, \
  158. __FILE__, __LINE__, minunit_significant_figures, \
  159. minunit_tmp_e, minunit_significant_figures, minunit_tmp_r); \
  160. minunit_status = 1; \
  161. return; \
  162. } else { printf("."); })
  163. #define mu_assert_string_eq(expected, result) \
  164. MU__SAFE_BLOCK( \
  165. const char *minunit_tmp_e = expected; \
  166. const char *minunit_tmp_r = result; minunit_assert++; \
  167. if (!minunit_tmp_e) { \
  168. minunit_tmp_e = "<null pointer>"; \
  169. } if (!minunit_tmp_r) { \
  170. minunit_tmp_r = "<null pointer>"; \
  171. } if (strcmp(minunit_tmp_e, minunit_tmp_r)) { \
  172. snprintf(minunit_last_message, MINUNIT_MESSAGE_LEN, \
  173. "%s failed:\n\t%s:%d: '%s' expected but was '%s'", __func__, \
  174. __FILE__, __LINE__, minunit_tmp_e, minunit_tmp_r); \
  175. minunit_status = 1; \
  176. return; \
  177. } else { printf("."); })
  178. /*
  179. * The following two functions were written by David Robert Nadeau
  180. * from http://NadeauSoftware.com/ and distributed under the
  181. * Creative Commons Attribution 3.0 Unported License
  182. */
  183. /**
  184. * Returns the real time, in seconds, or -1.0 if an error occurred.
  185. *
  186. * Time is measured since an arbitrary and OS-dependent start time.
  187. * The returned real time is only useful for computing an elapsed time
  188. * between two calls to this function.
  189. */
  190. static double mu_timer_real(void) {
  191. #if defined(_WIN32)
  192. /* Windows 2000 and later. ---------------------------------- */
  193. LARGE_INTEGER Time;
  194. LARGE_INTEGER Frequency;
  195. QueryPerformanceFrequency(&Frequency);
  196. QueryPerformanceCounter(&Time);
  197. Time.QuadPart *= 1000000;
  198. Time.QuadPart /= Frequency.QuadPart;
  199. return (double)Time.QuadPart / 1000000.0;
  200. #elif (defined(__hpux) || defined(hpux)) || \
  201. ((defined(__sun__) || defined(__sun) || defined(sun)) && \
  202. (defined(__SVR4) || defined(__svr4__)))
  203. /* HP-UX, Solaris. ------------------------------------------ */
  204. return (double)gethrtime() / 1000000000.0;
  205. #elif defined(__MACH__) && defined(__APPLE__)
  206. /* OSX. ----------------------------------------------------- */
  207. static double timeConvert = 0.0;
  208. if (timeConvert == 0.0) {
  209. mach_timebase_info_data_t timeBase;
  210. (void)mach_timebase_info(&timeBase);
  211. timeConvert =
  212. (double)timeBase.numer / (double)timeBase.denom / 1000000000.0;
  213. }
  214. return (double)mach_absolute_time() * timeConvert;
  215. #elif defined(_POSIX_VERSION)
  216. /* POSIX. --------------------------------------------------- */
  217. struct timeval tm;
  218. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  219. {
  220. struct timespec ts;
  221. #if defined(CLOCK_MONOTONIC_PRECISE)
  222. /* BSD. --------------------------------------------- */
  223. const clockid_t id = CLOCK_MONOTONIC_PRECISE;
  224. #elif defined(CLOCK_MONOTONIC_RAW)
  225. /* Linux. ------------------------------------------- */
  226. const clockid_t id = CLOCK_MONOTONIC_RAW;
  227. #elif defined(CLOCK_HIGHRES)
  228. /* Solaris. ----------------------------------------- */
  229. const clockid_t id = CLOCK_HIGHRES;
  230. #elif defined(CLOCK_MONOTONIC)
  231. /* AIX, BSD, Linux, POSIX, Solaris. ----------------- */
  232. const clockid_t id = CLOCK_MONOTONIC;
  233. #elif defined(CLOCK_REALTIME)
  234. /* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */
  235. const clockid_t id = CLOCK_REALTIME;
  236. #else
  237. const clockid_t id = (clockid_t)-1; /* Unknown. */
  238. #endif /* CLOCK_* */
  239. if (id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
  240. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  241. /* Fall thru. */
  242. }
  243. #endif /* _POSIX_TIMERS */
  244. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */
  245. gettimeofday(&tm, NULL);
  246. return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
  247. #else
  248. return -1.0; /* Failed. */
  249. #endif
  250. }
  251. /**
  252. * Returns the amount of CPU time used by the current process,
  253. * in seconds, or -1.0 if an error occurred.
  254. */
  255. static double mu_timer_cpu(void) {
  256. #if defined(_WIN32)
  257. /* Windows -------------------------------------------------- */
  258. FILETIME createTime;
  259. FILETIME exitTime;
  260. FILETIME kernelTime;
  261. FILETIME userTime;
  262. /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API
  263. * does not offer better */
  264. if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime,
  265. &userTime) != 0) {
  266. ULARGE_INTEGER userSystemTime;
  267. memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER));
  268. return (double)userSystemTime.QuadPart / 10000000.0;
  269. }
  270. #elif defined(__unix__) || defined(__unix) || defined(unix) || \
  271. (defined(__APPLE__) && defined(__MACH__))
  272. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */
  273. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  274. /* Prefer high-res POSIX timers, when available. */
  275. {
  276. clockid_t id;
  277. struct timespec ts;
  278. #if _POSIX_CPUTIME > 0
  279. /* Clock ids vary by OS. Query the id, if possible. */
  280. if (clock_getcpuclockid(0, &id) == -1)
  281. #endif
  282. #if defined(CLOCK_PROCESS_CPUTIME_ID)
  283. /* Use known clock id for AIX, Linux, or Solaris. */
  284. id = CLOCK_PROCESS_CPUTIME_ID;
  285. #elif defined(CLOCK_VIRTUAL)
  286. /* Use known clock id for BSD or HP-UX. */
  287. id = CLOCK_VIRTUAL;
  288. #else
  289. id = (clockid_t)-1;
  290. #endif
  291. if (id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
  292. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  293. }
  294. #endif
  295. #if defined(RUSAGE_SELF)
  296. {
  297. struct rusage rusage;
  298. if (getrusage(RUSAGE_SELF, &rusage) != -1)
  299. return (double)rusage.ru_utime.tv_sec +
  300. (double)rusage.ru_utime.tv_usec / 1000000.0;
  301. }
  302. #endif
  303. #if defined(_SC_CLK_TCK)
  304. {
  305. const double ticks = (double)sysconf(_SC_CLK_TCK);
  306. struct tms tms;
  307. if (times(&tms) != (clock_t)-1)
  308. return (double)tms.tms_utime / ticks;
  309. }
  310. #endif
  311. #if defined(CLOCKS_PER_SEC)
  312. {
  313. clock_t cl = clock();
  314. if (cl != (clock_t)-1)
  315. return (double)cl / (double)CLOCKS_PER_SEC;
  316. }
  317. #endif
  318. #endif
  319. return -1; /* Failed. */
  320. }
  321. #ifdef __cplusplus
  322. }
  323. #endif
  324. #endif /* MINUNIT_MINUNIT_H */