minunit.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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:\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:\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:\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:\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:\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:\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:\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:\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:\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) > MINUNIT_EPSILON) { \
  288. int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON); \
  289. snprintf( \
  290. minunit_last_message, \
  291. MINUNIT_MESSAGE_LEN, \
  292. "%s failed:\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:\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:\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:\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:\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_null(result) \
  377. MU__SAFE_BLOCK( \
  378. minunit_assert++; if(result == NULL) { minunit_print_progress(); } else { \
  379. snprintf( \
  380. minunit_last_message, \
  381. MINUNIT_MESSAGE_LEN, \
  382. "%s failed:\n\t%s:%d: Expected result was not NULL", \
  383. __func__, \
  384. __FILE__, \
  385. __LINE__); \
  386. minunit_status = 1; \
  387. return; \
  388. })
  389. #define mu_assert_not_null(result) \
  390. MU__SAFE_BLOCK( \
  391. minunit_assert++; if(result != NULL) { minunit_print_progress(); } else { \
  392. snprintf( \
  393. minunit_last_message, \
  394. MINUNIT_MESSAGE_LEN, \
  395. "%s failed:\n\t%s:%d: Expected result was not NULL", \
  396. __func__, \
  397. __FILE__, \
  398. __LINE__); \
  399. minunit_status = 1; \
  400. return; \
  401. })
  402. #define mu_assert_pointers_eq(pointer1, pointer2) \
  403. MU__SAFE_BLOCK( \
  404. minunit_assert++; if(pointer1 == pointer2) { minunit_print_progress(); } else { \
  405. snprintf( \
  406. minunit_last_message, \
  407. MINUNIT_MESSAGE_LEN, \
  408. "%s failed:\n\t%s:%d: Expected the pointers to point to the same memory location", \
  409. __func__, \
  410. __FILE__, \
  411. __LINE__); \
  412. minunit_status = 1; \
  413. return; \
  414. })
  415. #define mu_assert_pointers_not_eq(pointer1, pointer2) \
  416. MU__SAFE_BLOCK( \
  417. minunit_assert++; if(pointer1 != pointer2) { minunit_print_progress(); } else { \
  418. snprintf( \
  419. minunit_last_message, \
  420. MINUNIT_MESSAGE_LEN, \
  421. "%s failed:\n\t%s:%d: Expected the pointers to point to the same memory location", \
  422. __func__, \
  423. __FILE__, \
  424. __LINE__); \
  425. minunit_status = 1; \
  426. return; \
  427. })
  428. /*
  429. * The following two functions were written by David Robert Nadeau
  430. * from http://NadeauSoftware.com/ and distributed under the
  431. * Creative Commons Attribution 3.0 Unported License
  432. */
  433. /**
  434. * Returns the real time, in seconds, or -1.0 if an error occurred.
  435. *
  436. * Time is measured since an arbitrary and OS-dependent start time.
  437. * The returned real time is only useful for computing an elapsed time
  438. * between two calls to this function.
  439. */
  440. __attribute__((unused)) static double mu_timer_real(void) {
  441. #if defined(_WIN32)
  442. /* Windows 2000 and later. ---------------------------------- */
  443. LARGE_INTEGER Time;
  444. LARGE_INTEGER Frequency;
  445. QueryPerformanceFrequency(&Frequency);
  446. QueryPerformanceCounter(&Time);
  447. Time.QuadPart *= 1000000;
  448. Time.QuadPart /= Frequency.QuadPart;
  449. return (double)Time.QuadPart / 1000000.0;
  450. #elif(defined(__hpux) || defined(hpux)) || \
  451. ((defined(__sun__) || defined(__sun) || defined(sun)) && \
  452. (defined(__SVR4) || defined(__svr4__)))
  453. /* HP-UX, Solaris. ------------------------------------------ */
  454. return (double)gethrtime() / 1000000000.0;
  455. #elif defined(__MACH__) && defined(__APPLE__)
  456. /* OSX. ----------------------------------------------------- */
  457. static double timeConvert = 0.0;
  458. if(timeConvert == 0.0) {
  459. mach_timebase_info_data_t timeBase;
  460. (void)mach_timebase_info(&timeBase);
  461. timeConvert = (double)timeBase.numer / (double)timeBase.denom / 1000000000.0;
  462. }
  463. return (double)mach_absolute_time() * timeConvert;
  464. #elif defined(_POSIX_VERSION)
  465. /* POSIX. --------------------------------------------------- */
  466. struct timeval tm;
  467. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  468. {
  469. struct timespec ts;
  470. #if defined(CLOCK_MONOTONIC_PRECISE)
  471. /* BSD. --------------------------------------------- */
  472. const clockid_t id = CLOCK_MONOTONIC_PRECISE;
  473. #elif defined(CLOCK_MONOTONIC_RAW)
  474. /* Linux. ------------------------------------------- */
  475. const clockid_t id = CLOCK_MONOTONIC_RAW;
  476. #elif defined(CLOCK_HIGHRES)
  477. /* Solaris. ----------------------------------------- */
  478. const clockid_t id = CLOCK_HIGHRES;
  479. #elif defined(CLOCK_MONOTONIC)
  480. /* AIX, BSD, Linux, POSIX, Solaris. ----------------- */
  481. const clockid_t id = CLOCK_MONOTONIC;
  482. #elif defined(CLOCK_REALTIME)
  483. /* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */
  484. const clockid_t id = CLOCK_REALTIME;
  485. #else
  486. const clockid_t id = (clockid_t)-1; /* Unknown. */
  487. #endif /* CLOCK_* */
  488. if(id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
  489. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  490. /* Fall thru. */
  491. }
  492. #endif /* _POSIX_TIMERS */
  493. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */
  494. gettimeofday(&tm, NULL);
  495. return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
  496. #else
  497. return -1.0; /* Failed. */
  498. #endif
  499. }
  500. /**
  501. * Returns the amount of CPU time used by the current process,
  502. * in seconds, or -1.0 if an error occurred.
  503. */
  504. __attribute__((unused)) static double mu_timer_cpu(void) {
  505. #if defined(_WIN32)
  506. /* Windows -------------------------------------------------- */
  507. FILETIME createTime;
  508. FILETIME exitTime;
  509. FILETIME kernelTime;
  510. FILETIME userTime;
  511. /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API does not offer better */
  512. if(GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime) != 0) {
  513. ULARGE_INTEGER userSystemTime;
  514. memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER));
  515. return (double)userSystemTime.QuadPart / 10000000.0;
  516. }
  517. #elif defined(__unix__) || defined(__unix) || defined(unix) || \
  518. (defined(__APPLE__) && defined(__MACH__))
  519. /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */
  520. #if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
  521. /* Prefer high-res POSIX timers, when available. */
  522. {
  523. clockid_t id;
  524. struct timespec ts;
  525. #if _POSIX_CPUTIME > 0
  526. /* Clock ids vary by OS. Query the id, if possible. */
  527. if(clock_getcpuclockid(0, &id) == -1)
  528. #endif
  529. #if defined(CLOCK_PROCESS_CPUTIME_ID)
  530. /* Use known clock id for AIX, Linux, or Solaris. */
  531. id = CLOCK_PROCESS_CPUTIME_ID;
  532. #elif defined(CLOCK_VIRTUAL)
  533. /* Use known clock id for BSD or HP-UX. */
  534. id = CLOCK_VIRTUAL;
  535. #else
  536. id = (clockid_t)-1;
  537. #endif
  538. if(id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
  539. return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
  540. }
  541. #endif
  542. #if defined(RUSAGE_SELF)
  543. {
  544. struct rusage rusage;
  545. if(getrusage(RUSAGE_SELF, &rusage) != -1)
  546. return (double)rusage.ru_utime.tv_sec + (double)rusage.ru_utime.tv_usec / 1000000.0;
  547. }
  548. #endif
  549. #if defined(_SC_CLK_TCK)
  550. {
  551. const double ticks = (double)sysconf(_SC_CLK_TCK);
  552. struct tms tms;
  553. if(times(&tms) != (clock_t)-1) return (double)tms.tms_utime / ticks;
  554. }
  555. #endif
  556. #if defined(CLOCKS_PER_SEC)
  557. {
  558. clock_t cl = clock();
  559. if(cl != (clock_t)-1) return (double)cl / (double)CLOCKS_PER_SEC;
  560. }
  561. #endif
  562. #endif
  563. return -1; /* Failed. */
  564. }
  565. #ifdef __cplusplus
  566. }
  567. #endif
  568. #endif /* MINUNIT_MINUNIT_H */