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