minunit.h 34 KB

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