minunit.h 35 KB

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