minunit.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. * Copyright (c) 2012 David Siñuela Pastor, siu.4coders@gmail.com
  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 <unistd.h> /* POSIX flags */
  42. #include <time.h> /* clock_gettime(), time() */
  43. #include <sys/time.h> /* gethrtime(), gettimeofday() */
  44. #include <sys/resource.h>
  45. #include <sys/times.h>
  46. #include <string.h>
  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 <stdio.h>
  58. #include <math.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. #include "minunit_vars_ex.h"
  64. /* Test setup and teardown function pointers */
  65. __attribute__((unused)) static void (*minunit_setup)(void) = NULL;
  66. __attribute__((unused)) static void (*minunit_teardown)(void) = NULL;
  67. void minunit_print_progress(void);
  68. void minunit_print_fail(const char* error);
  69. /* Definitions */
  70. #define MU_TEST(method_name) static void method_name(void)
  71. #define MU_TEST_1(method_name, arg_1) static void method_name(arg_1)
  72. #define MU_TEST_SUITE(suite_name) static void suite_name(void)
  73. #define MU__SAFE_BLOCK(block) \
  74. do { \
  75. block \
  76. } while(0)
  77. /* Run test suite and unset setup and teardown functions */
  78. #define MU_RUN_SUITE(suite_name) \
  79. MU__SAFE_BLOCK(suite_name(); minunit_setup = NULL; minunit_teardown = NULL;)
  80. /* Configure setup and teardown functions */
  81. #define MU_SUITE_CONFIGURE(setup_fun, teardown_fun) \
  82. MU__SAFE_BLOCK(minunit_setup = setup_fun; minunit_teardown = teardown_fun;)
  83. /* Test runner */
  84. #define MU_RUN_TEST(test) \
  85. MU__SAFE_BLOCK( \
  86. if(minunit_real_timer == 0 && minunit_proc_timer == 0) { \
  87. minunit_real_timer = mu_timer_real(); \
  88. minunit_proc_timer = mu_timer_cpu(); \
  89. } if(minunit_setup) (*minunit_setup)(); \
  90. minunit_status = 0; \
  91. printf(#test "()\r\n"); \
  92. test(); \
  93. minunit_run++; \
  94. if(minunit_status) { \
  95. minunit_fail++; \
  96. minunit_print_fail(minunit_last_message); \
  97. minunit_status = 0; \
  98. } fflush(stdout); \
  99. if(minunit_teardown)(*minunit_teardown)();)
  100. #define MU_RUN_TEST_1(test, arg_1) \
  101. MU__SAFE_BLOCK( \
  102. if(minunit_real_timer == 0 && minunit_proc_timer == 0) { \
  103. minunit_real_timer = mu_timer_real(); \
  104. minunit_proc_timer = mu_timer_cpu(); \
  105. } if(minunit_setup) (*minunit_setup)(); \
  106. minunit_status = 0; \
  107. printf(#test "(" #arg_1 ")\r\n"); \
  108. test(arg_1); \
  109. minunit_run++; \
  110. if(minunit_status) { \
  111. minunit_fail++; \
  112. minunit_print_fail(minunit_last_message); \
  113. minunit_status = 0; \
  114. } fflush(stdout); \
  115. if(minunit_teardown)(*minunit_teardown)();)
  116. /* Report */
  117. #define MU_REPORT() \
  118. MU__SAFE_BLOCK(double minunit_end_real_timer; double minunit_end_proc_timer; printf( \
  119. "\n\n%d tests, %d assertions, %d failures\n", \
  120. minunit_run, \
  121. minunit_assert, \
  122. minunit_fail); \
  123. minunit_end_real_timer = mu_timer_real(); \
  124. minunit_end_proc_timer = mu_timer_cpu(); \
  125. printf( \
  126. "\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n", \
  127. minunit_end_real_timer - minunit_real_timer, \
  128. minunit_end_proc_timer - minunit_proc_timer);)
  129. #define MU_EXIT_CODE minunit_fail
  130. /* Assertions */
  131. #define mu_check(test) \
  132. MU__SAFE_BLOCK( \
  133. minunit_assert++; if(!(test)) { \
  134. snprintf( \
  135. minunit_last_message, \
  136. MINUNIT_MESSAGE_LEN, \
  137. "%s failed:\r\n\t%s:%d: %s", \
  138. __func__, \
  139. __FILE__, \
  140. __LINE__, \
  141. #test); \
  142. minunit_status = 1; \
  143. return; \
  144. } else { minunit_print_progress(); })
  145. #define mu_fail(message) \
  146. MU__SAFE_BLOCK(minunit_assert++; snprintf( \
  147. minunit_last_message, \
  148. MINUNIT_MESSAGE_LEN, \
  149. "%s failed:\r\n\t%s:%d: %s", \
  150. __func__, \
  151. __FILE__, \
  152. __LINE__, \
  153. message); \
  154. minunit_status = 1; \
  155. return;)
  156. #define mu_assert(test, message) \
  157. MU__SAFE_BLOCK( \
  158. minunit_assert++; if(!(test)) { \
  159. snprintf( \
  160. minunit_last_message, \
  161. MINUNIT_MESSAGE_LEN, \
  162. "%s failed:\r\n\t%s:%d: %s", \
  163. __func__, \
  164. __FILE__, \
  165. __LINE__, \
  166. message); \
  167. minunit_status = 1; \
  168. return; \
  169. } else { minunit_print_progress(); })
  170. #define mu_assert_int_eq(expected, result) \
  171. MU__SAFE_BLOCK( \
  172. int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (expected); \
  173. minunit_tmp_r = (result); \
  174. if(minunit_tmp_e != minunit_tmp_r) { \
  175. snprintf( \
  176. minunit_last_message, \
  177. MINUNIT_MESSAGE_LEN, \
  178. "%s failed:\r\n\t%s:%d: %d expected but was %d", \
  179. __func__, \
  180. __FILE__, \
  181. __LINE__, \
  182. minunit_tmp_e, \
  183. minunit_tmp_r); \
  184. minunit_status = 1; \
  185. return; \
  186. } else { minunit_print_progress(); })
  187. #define mu_assert_int_not_eq(expected, result) \
  188. MU__SAFE_BLOCK( \
  189. int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (expected); \
  190. minunit_tmp_r = (result); \
  191. if(minunit_tmp_e == minunit_tmp_r) { \
  192. snprintf( \
  193. minunit_last_message, \
  194. MINUNIT_MESSAGE_LEN, \
  195. "%s failed:\r\n\t%s:%d: expected different results but both were %d", \
  196. __func__, \
  197. __FILE__, \
  198. __LINE__, \
  199. minunit_tmp_e); \
  200. minunit_status = 1; \
  201. return; \
  202. } else { minunit_print_progress(); })
  203. #define mu_assert_int_greater_than(val, result) \
  204. MU__SAFE_BLOCK( \
  205. int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
  206. minunit_tmp_r = (result); \
  207. if(val >= minunit_tmp_r) { \
  208. snprintf( \
  209. minunit_last_message, \
  210. MINUNIT_MESSAGE_LEN, \
  211. "%s failed:\r\n\t%s:%d: %d <= %d", \
  212. __func__, \
  213. __FILE__, \
  214. __LINE__, \
  215. minunit_tmp_r, \
  216. minunit_tmp_e); \
  217. minunit_status = 1; \
  218. return; \
  219. } else { minunit_print_progress(); })
  220. #define mu_assert_int_less_than(val, result) \
  221. MU__SAFE_BLOCK( \
  222. int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
  223. minunit_tmp_r = (result); \
  224. if(val <= minunit_tmp_r) { \
  225. snprintf( \
  226. minunit_last_message, \
  227. MINUNIT_MESSAGE_LEN, \
  228. "%s failed:\r\n\t%s:%d: %d >= %d", \
  229. __func__, \
  230. __FILE__, \
  231. __LINE__, \
  232. minunit_tmp_r, \
  233. minunit_tmp_e); \
  234. minunit_status = 1; \
  235. return; \
  236. } else { minunit_print_progress(); })
  237. #define mu_assert_int_between(expected_lower, expected_upper, result) \
  238. MU__SAFE_BLOCK( \
  239. int minunit_tmp_e; int minunit_tmp_m; int minunit_tmp_r; minunit_assert++; \
  240. minunit_tmp_e = (expected_lower); \
  241. minunit_tmp_m = (expected_upper); \
  242. minunit_tmp_r = (result); \
  243. if(result < minunit_tmp_e || result > minunit_tmp_m) { \
  244. snprintf( \
  245. minunit_last_message, \
  246. MINUNIT_MESSAGE_LEN, \
  247. "%s failed:\r\n\t%s:%d: %d was not between (inclusive) %d and %d", \
  248. __func__, \
  249. __FILE__, \
  250. __LINE__, \
  251. minunit_tmp_e, \
  252. minunit_tmp_r, \
  253. minunit_tmp_m); \
  254. minunit_status = 1; \
  255. return; \
  256. } else { minunit_print_progress(); })
  257. #define mu_assert_int_in(expected, array_length, result) \
  258. MU__SAFE_BLOCK( \
  259. int minunit_tmp_r; minunit_assert++; minunit_tmp_r = (result); int t = 0; int i; \
  260. for(i = 0; i < array_length; i++) { \
  261. if(expected[i] == minunit_tmp_r) t = 1; \
  262. } if(t == 0) { \
  263. char tmp[500] = {0}; \
  264. tmp[0] = '['; \
  265. for(i = 0; i < array_length; i++) { \
  266. sprintf(tmp + strlen(tmp), "%d, ", expected[i]); \
  267. } \
  268. int len = strlen(tmp); \
  269. tmp[len - 2] = ']'; \
  270. tmp[len - 1] = '\0'; \
  271. snprintf( \
  272. minunit_last_message, \
  273. MINUNIT_MESSAGE_LEN, \
  274. "%s failed:\r\n\t%s:%d: expected to be one of %s but was %d", \
  275. __func__, \
  276. __FILE__, \
  277. __LINE__, \
  278. tmp, \
  279. minunit_tmp_r); \
  280. minunit_status = 1; \
  281. return; \
  282. } else { minunit_print_progress(); })
  283. #define mu_assert_double_eq(expected, result) \
  284. MU__SAFE_BLOCK( \
  285. double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; minunit_tmp_e = (expected); \
  286. minunit_tmp_r = (result); \
  287. if(fabs(minunit_tmp_e - minunit_tmp_r) > (double)MINUNIT_EPSILON) { \
  288. int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON); \
  289. snprintf( \
  290. minunit_last_message, \
  291. MINUNIT_MESSAGE_LEN, \
  292. "%s failed:\r\n\t%s:%d: %.*g expected but was %.*g", \
  293. __func__, \
  294. __FILE__, \
  295. __LINE__, \
  296. minunit_significant_figures, \
  297. minunit_tmp_e, \
  298. minunit_significant_figures, \
  299. minunit_tmp_r); \
  300. minunit_status = 1; \
  301. return; \
  302. } else { minunit_print_progress(); })
  303. #define mu_assert_double_greater_than(val, result) \
  304. MU__SAFE_BLOCK( \
  305. double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
  306. minunit_tmp_r = (result); \
  307. if(val >= minunit_tmp_r) { \
  308. snprintf( \
  309. minunit_last_message, \
  310. MINUNIT_MESSAGE_LEN, \
  311. "%s failed:\r\n\t%s:%d: %f <= %f", \
  312. __func__, \
  313. __FILE__, \
  314. __LINE__, \
  315. minunit_tmp_r, \
  316. minunit_tmp_e); \
  317. minunit_status = 1; \
  318. return; \
  319. } else { minunit_print_progress(); })
  320. #define mu_assert_double_less_than(val, result) \
  321. MU__SAFE_BLOCK( \
  322. double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
  323. minunit_tmp_r = (result); \
  324. if(val <= minunit_tmp_r) { \
  325. snprintf( \
  326. minunit_last_message, \
  327. MINUNIT_MESSAGE_LEN, \
  328. "%s failed:\r\n\t%s:%d: %f >= %f", \
  329. __func__, \
  330. __FILE__, \
  331. __LINE__, \
  332. minunit_tmp_r, \
  333. minunit_tmp_e); \
  334. minunit_status = 1; \
  335. return; \
  336. } else { minunit_print_progress(); })
  337. #define mu_assert_double_between(expected_lower, expected_upper, result) \
  338. MU__SAFE_BLOCK( \
  339. double minunit_tmp_e; double minunit_tmp_m; double minunit_tmp_r; minunit_assert++; \
  340. minunit_tmp_e = (expected_lower); \
  341. minunit_tmp_m = (expected_upper); \
  342. minunit_tmp_r = (result); \
  343. if(result < minunit_tmp_e || result > minunit_tmp_m) { \
  344. snprintf( \
  345. minunit_last_message, \
  346. MINUNIT_MESSAGE_LEN, \
  347. "%s failed:\r\n\t%s:%d: %f was not between (inclusive) %f and %f", \
  348. __func__, \
  349. __FILE__, \
  350. __LINE__, \
  351. minunit_tmp_e, \
  352. minunit_tmp_r, \
  353. minunit_tmp_m); \
  354. minunit_status = 1; \
  355. return; \
  356. } else { minunit_print_progress(); })
  357. #define mu_assert_string_eq(expected, result) \
  358. MU__SAFE_BLOCK( \
  359. const char* minunit_tmp_e = expected; const char* minunit_tmp_r = result; \
  360. minunit_assert++; \
  361. if(!minunit_tmp_e) { minunit_tmp_e = "<null pointer>"; } if(!minunit_tmp_r) { \
  362. minunit_tmp_r = "<null pointer>"; \
  363. } if(strcmp(minunit_tmp_e, minunit_tmp_r)) { \
  364. snprintf( \
  365. minunit_last_message, \
  366. MINUNIT_MESSAGE_LEN, \
  367. "%s failed:\r\n\t%s:%d: '%s' expected but was '%s'", \
  368. __func__, \
  369. __FILE__, \
  370. __LINE__, \
  371. minunit_tmp_e, \
  372. minunit_tmp_r); \
  373. minunit_status = 1; \
  374. return; \
  375. } else { minunit_print_progress(); })
  376. #define mu_assert_mem_eq(expected, result, size) \
  377. MU__SAFE_BLOCK( \
  378. const void* minunit_tmp_e = expected; const void* minunit_tmp_r = result; \
  379. minunit_assert++; \
  380. if(memcmp(minunit_tmp_e, minunit_tmp_r, size)) { \
  381. snprintf( \
  382. minunit_last_message, \
  383. MINUNIT_MESSAGE_LEN, \
  384. "%s failed:\r\n\t%s:%d: mem not equal\r\n\tEXP RES", \
  385. __func__, \
  386. __FILE__, \
  387. __LINE__); \
  388. for(size_t __index = 0; __index < size; __index++) { \
  389. if(strlen(minunit_last_message) > MINUNIT_MESSAGE_LEN - 20) break; \
  390. uint8_t __e = ((uint8_t*)minunit_tmp_e)[__index]; \
  391. uint8_t __r = ((uint8_t*)minunit_tmp_r)[__index]; \
  392. snprintf( \
  393. minunit_last_message + strlen(minunit_last_message), \
  394. MINUNIT_MESSAGE_LEN - strlen(minunit_last_message), \
  395. "\r\n\t%02X %s %02X", \
  396. __e, \
  397. ((__e == __r) ? ".." : "!="), \
  398. __r); \
  399. } \
  400. minunit_status = 1; \
  401. return; \
  402. } else { minunit_print_progress(); })
  403. #define mu_assert_null(result) \
  404. MU__SAFE_BLOCK( \
  405. minunit_assert++; if(result == NULL) { minunit_print_progress(); } else { \
  406. snprintf( \
  407. minunit_last_message, \
  408. MINUNIT_MESSAGE_LEN, \
  409. "%s failed:\r\n\t%s:%d: Expected result was not NULL", \
  410. __func__, \
  411. __FILE__, \
  412. __LINE__); \
  413. minunit_status = 1; \
  414. return; \
  415. })
  416. #define mu_assert_not_null(result) \
  417. MU__SAFE_BLOCK( \
  418. minunit_assert++; if(result != NULL) { minunit_print_progress(); } else { \
  419. snprintf( \
  420. minunit_last_message, \
  421. MINUNIT_MESSAGE_LEN, \
  422. "%s failed:\r\n\t%s:%d: Expected result was not NULL", \
  423. __func__, \
  424. __FILE__, \
  425. __LINE__); \
  426. minunit_status = 1; \
  427. return; \
  428. })
  429. #define mu_assert_pointers_eq(pointer1, pointer2) \
  430. MU__SAFE_BLOCK( \
  431. minunit_assert++; if(pointer1 == pointer2) { minunit_print_progress(); } else { \
  432. snprintf( \
  433. minunit_last_message, \
  434. MINUNIT_MESSAGE_LEN, \
  435. "%s failed:\r\n\t%s:%d: Expected the pointers to point to the same memory location", \
  436. __func__, \
  437. __FILE__, \
  438. __LINE__); \
  439. minunit_status = 1; \
  440. return; \
  441. })
  442. #define mu_assert_pointers_not_eq(pointer1, pointer2) \
  443. MU__SAFE_BLOCK( \
  444. minunit_assert++; if(pointer1 != pointer2) { minunit_print_progress(); } else { \
  445. snprintf( \
  446. minunit_last_message, \
  447. MINUNIT_MESSAGE_LEN, \
  448. "%s failed:\r\n\t%s:%d: Expected the pointers to point to the same memory location", \
  449. __func__, \
  450. __FILE__, \
  451. __LINE__); \
  452. minunit_status = 1; \
  453. return; \
  454. })
  455. /*
  456. * The following two functions were written by David Robert Nadeau
  457. * from http://NadeauSoftware.com/ and distributed under the
  458. * Creative Commons Attribution 3.0 Unported License
  459. */
  460. /**
  461. * Returns the real time, in seconds, or -1.0 if an error occurred.
  462. *
  463. * Time is measured since an arbitrary and OS-dependent start time.
  464. * The returned real time is only useful for computing an elapsed time
  465. * between two calls to this function.
  466. */
  467. __attribute__((unused)) static double mu_timer_real(void) {
  468. #if defined(_WIN32)
  469. /* Windows 2000 and later. ---------------------------------- */
  470. LARGE_INTEGER Time;
  471. LARGE_INTEGER Frequency;
  472. QueryPerformanceFrequency(&Frequency);
  473. QueryPerformanceCounter(&Time);
  474. Time.QuadPart *= 1000000;
  475. Time.QuadPart /= Frequency.QuadPart;
  476. return (double)Time.QuadPart / 1000000.0;
  477. #elif(defined(__hpux) || defined(hpux)) || \
  478. ((defined(__sun__) || defined(__sun) || defined(sun)) && \
  479. (defined(__SVR4) || defined(__svr4__)))
  480. /* HP-UX, Solaris. ------------------------------------------ */
  481. return (double)gethrtime() / 1000000000.0;
  482. #elif defined(__MACH__) && defined(__APPLE__)
  483. /* OSX. ----------------------------------------------------- */
  484. static double timeConvert = 0.0;
  485. if(timeConvert == 0.0) {
  486. mach_timebase_info_data_t timeBase;
  487. (void)mach_timebase_info(&timeBase);
  488. timeConvert = (double)timeBase.numer / (double)timeBase.denom / 1000000000.0;
  489. }
  490. return (double)mach_absolute_time() * timeConvert;
  491. #elif defined(_POSIX_VERSION)
  492. /* POSIX. --------------------------------------------------- */
  493. struct timeval tm;
  494. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  495. {
  496. struct timespec ts;
  497. #if defined(CLOCK_MONOTONIC_PRECISE)
  498. /* BSD. --------------------------------------------- */
  499. const clockid_t id = CLOCK_MONOTONIC_PRECISE;
  500. #elif defined(CLOCK_MONOTONIC_RAW)
  501. /* Linux. ------------------------------------------- */
  502. const clockid_t id = CLOCK_MONOTONIC_RAW;
  503. #elif defined(CLOCK_HIGHRES)
  504. /* Solaris. ----------------------------------------- */
  505. const clockid_t id = CLOCK_HIGHRES;
  506. #elif defined(CLOCK_MONOTONIC)
  507. /* AIX, BSD, Linux, POSIX, Solaris. ----------------- */
  508. const clockid_t id = CLOCK_MONOTONIC;
  509. #elif defined(CLOCK_REALTIME)
  510. /* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */
  511. const clockid_t id = CLOCK_REALTIME;
  512. #else
  513. const clockid_t id = (clockid_t)-1; /* Unknown. */
  514. #endif /* CLOCK_* */
  515. if(id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
  516. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  517. /* Fall thru. */
  518. }
  519. #endif /* _POSIX_TIMERS */
  520. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */
  521. gettimeofday(&tm, NULL);
  522. return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
  523. #else
  524. return -1.0; /* Failed. */
  525. #endif
  526. }
  527. /**
  528. * Returns the amount of CPU time used by the current process,
  529. * in seconds, or -1.0 if an error occurred.
  530. */
  531. __attribute__((unused)) static double mu_timer_cpu(void) {
  532. #if defined(_WIN32)
  533. /* Windows -------------------------------------------------- */
  534. FILETIME createTime;
  535. FILETIME exitTime;
  536. FILETIME kernelTime;
  537. FILETIME userTime;
  538. /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API does not offer better */
  539. if(GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime) != 0) {
  540. ULARGE_INTEGER userSystemTime;
  541. memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER));
  542. return (double)userSystemTime.QuadPart / 10000000.0;
  543. }
  544. #elif defined(__unix__) || defined(__unix) || defined(unix) || \
  545. (defined(__APPLE__) && defined(__MACH__))
  546. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */
  547. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  548. /* Prefer high-res POSIX timers, when available. */
  549. {
  550. clockid_t id;
  551. struct timespec ts;
  552. #if _POSIX_CPUTIME > 0
  553. /* Clock ids vary by OS. Query the id, if possible. */
  554. if(clock_getcpuclockid(0, &id) == -1)
  555. #endif
  556. #if defined(CLOCK_PROCESS_CPUTIME_ID)
  557. /* Use known clock id for AIX, Linux, or Solaris. */
  558. id = CLOCK_PROCESS_CPUTIME_ID;
  559. #elif defined(CLOCK_VIRTUAL)
  560. /* Use known clock id for BSD or HP-UX. */
  561. id = CLOCK_VIRTUAL;
  562. #else
  563. id = (clockid_t)-1;
  564. #endif
  565. if(id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
  566. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  567. }
  568. #endif
  569. #if defined(RUSAGE_SELF)
  570. {
  571. struct rusage rusage;
  572. if(getrusage(RUSAGE_SELF, &rusage) != -1)
  573. return (double)rusage.ru_utime.tv_sec + (double)rusage.ru_utime.tv_usec / 1000000.0;
  574. }
  575. #endif
  576. #if defined(_SC_CLK_TCK)
  577. {
  578. const double ticks = (double)sysconf(_SC_CLK_TCK);
  579. struct tms tms;
  580. if(times(&tms) != (clock_t)-1) return (double)tms.tms_utime / ticks;
  581. }
  582. #endif
  583. #if defined(CLOCKS_PER_SEC)
  584. {
  585. clock_t cl = clock();
  586. if(cl != (clock_t)-1) return (double)cl / (double)CLOCKS_PER_SEC;
  587. }
  588. #endif
  589. #endif
  590. return -1; /* Failed. */
  591. }
  592. #ifdef __cplusplus
  593. }
  594. #endif
  595. #endif /* MINUNIT_MINUNIT_H */