Welcome to mirror list, hosted at ThFree Co, Russian Federation.

minunit.h « tests « applications - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b12a87ca2c68070a9c4c67f974f7b704a88dfb8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
 * Copyright (c) 2012 David Siñuela Pastor, siu.4coders@gmail.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifndef MINUNIT_MINUNIT_H
#define MINUNIT_MINUNIT_H

#ifdef __cplusplus
extern "C" {
#endif

#if defined(_WIN32)
#include <Windows.h>
#if defined(_MSC_VER) && _MSC_VER < 1900
#define snprintf _snprintf
#define __func__ __FUNCTION__
#endif

#elif defined(__unix__) || defined(__unix) || defined(unix) || \
    (defined(__APPLE__) && defined(__MACH__))

/* Change POSIX C SOURCE version for pure c99 compilers */
#if !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200112L
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#endif

#include <unistd.h> /* POSIX flags */
#include <time.h> /* clock_gettime(), time() */
#include <sys/time.h> /* gethrtime(), gettimeofday() */
#include <sys/resource.h>
#include <sys/times.h>
#include <string.h>

#if defined(__MACH__) && defined(__APPLE__)
#include <mach/mach.h>
#include <mach/mach_time.h>
#endif

#if __GNUC__ >= 5 && !defined(__STDC_VERSION__)
#define __func__ __extension__ __FUNCTION__
#endif

#else

// #error "Unable to define timers for an unknown OS."

#endif

#include <stdio.h>
#include <math.h>

/*  Maximum length of last message */
#define MINUNIT_MESSAGE_LEN 1024
/*  Accuracy with which floats are compared */
#define MINUNIT_EPSILON 1E-12

#include "minunit_vars_ex.h"

/*  Test setup and teardown function pointers */
__attribute__((unused)) static void (*minunit_setup)(void) = NULL;
__attribute__((unused)) static void (*minunit_teardown)(void) = NULL;

void minunit_print_progress(void);
void minunit_print_fail(const char* error);

/*  Definitions */
#define MU_TEST(method_name) static void method_name(void)
#define MU_TEST_SUITE(suite_name) static void suite_name(void)

#define MU__SAFE_BLOCK(block) \
    do {                      \
        block                 \
    } while(0)

/*  Run test suite and unset setup and teardown functions */
#define MU_RUN_SUITE(suite_name) \
    MU__SAFE_BLOCK(suite_name(); minunit_setup = NULL; minunit_teardown = NULL;)

/*  Configure setup and teardown functions */
#define MU_SUITE_CONFIGURE(setup_fun, teardown_fun) \
    MU__SAFE_BLOCK(minunit_setup = setup_fun; minunit_teardown = teardown_fun;)

/*  Test runner */
#define MU_RUN_TEST(test)                                        \
    MU__SAFE_BLOCK(                                              \
        if(minunit_real_timer == 0 && minunit_proc_timer == 0) { \
            minunit_real_timer = mu_timer_real();                \
            minunit_proc_timer = mu_timer_cpu();                 \
        } if(minunit_setup) (*minunit_setup)();                  \
        minunit_status = 0;                                      \
        test();                                                  \
        minunit_run++;                                           \
        if(minunit_status) {                                     \
            minunit_fail++;                                      \
            minunit_print_fail(minunit_last_message);            \
        } fflush(stdout);                                        \
        if(minunit_teardown)(*minunit_teardown)();)

/*  Report */
#define MU_REPORT()                                                                      \
    MU__SAFE_BLOCK(double minunit_end_real_timer; double minunit_end_proc_timer; printf( \
                       "\n\n%d tests, %d assertions, %d failures\n",                     \
                       minunit_run,                                                      \
                       minunit_assert,                                                   \
                       minunit_fail);                                                    \
                   minunit_end_real_timer = mu_timer_real();                             \
                   minunit_end_proc_timer = mu_timer_cpu();                              \
                   printf(                                                               \
                       "\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n",      \
                       minunit_end_real_timer - minunit_real_timer,                      \
                       minunit_end_proc_timer - minunit_proc_timer);)
#define MU_EXIT_CODE minunit_fail

/*  Assertions */
#define mu_check(test)                     \
    MU__SAFE_BLOCK(                        \
        minunit_assert++; if(!(test)) {    \
            snprintf(                      \
                minunit_last_message,      \
                MINUNIT_MESSAGE_LEN,       \
                "%s failed:\n\t%s:%d: %s", \
                __func__,                  \
                __FILE__,                  \
                __LINE__,                  \
                #test);                    \
            minunit_status = 1;            \
            return;                        \
        } else { minunit_print_progress(); })

#define mu_fail(message)                          \
    MU__SAFE_BLOCK(minunit_assert++; snprintf(    \
                       minunit_last_message,      \
                       MINUNIT_MESSAGE_LEN,       \
                       "%s failed:\n\t%s:%d: %s", \
                       __func__,                  \
                       __FILE__,                  \
                       __LINE__,                  \
                       message);                  \
                   minunit_status = 1;            \
                   return;)

#define mu_assert(test, message)           \
    MU__SAFE_BLOCK(                        \
        minunit_assert++; if(!(test)) {    \
            snprintf(                      \
                minunit_last_message,      \
                MINUNIT_MESSAGE_LEN,       \
                "%s failed:\n\t%s:%d: %s", \
                __func__,                  \
                __FILE__,                  \
                __LINE__,                  \
                message);                  \
            minunit_status = 1;            \
            return;                        \
        } else { minunit_print_progress(); })

#define mu_assert_int_eq(expected, result)                                                  \
    MU__SAFE_BLOCK(                                                                         \
        int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (expected); \
        minunit_tmp_r = (result);                                                           \
        if(minunit_tmp_e != minunit_tmp_r) {                                                \
            snprintf(                                                                       \
                minunit_last_message,                                                       \
                MINUNIT_MESSAGE_LEN,                                                        \
                "%s failed:\n\t%s:%d: %d expected but was %d",                              \
                __func__,                                                                   \
                __FILE__,                                                                   \
                __LINE__,                                                                   \
                minunit_tmp_e,                                                              \
                minunit_tmp_r);                                                             \
            minunit_status = 1;                                                             \
            return;                                                                         \
        } else { minunit_print_progress(); })

#define mu_assert_int_not_eq(expected, result)                                              \
    MU__SAFE_BLOCK(                                                                         \
        int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (expected); \
        minunit_tmp_r = (result);                                                           \
        if(minunit_tmp_e == minunit_tmp_r) {                                                \
            snprintf(                                                                       \
                minunit_last_message,                                                       \
                MINUNIT_MESSAGE_LEN,                                                        \
                "%s failed:\n\t%s:%d: expected different results but both were %d",         \
                __func__,                                                                   \
                __FILE__,                                                                   \
                __LINE__,                                                                   \
                minunit_tmp_e);                                                             \
            minunit_status = 1;                                                             \
            return;                                                                         \
        } else { minunit_print_progress(); })

#define mu_assert_int_greater_than(val, result)                                        \
    MU__SAFE_BLOCK(                                                                    \
        int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
        minunit_tmp_r = (result);                                                      \
        if(val >= minunit_tmp_r) {                                                     \
            snprintf(                                                                  \
                minunit_last_message,                                                  \
                MINUNIT_MESSAGE_LEN,                                                   \
                "%s failed:\n\t%s:%d: %d <= %d",                                       \
                __func__,                                                              \
                __FILE__,                                                              \
                __LINE__,                                                              \
                minunit_tmp_r,                                                         \
                minunit_tmp_e);                                                        \
            minunit_status = 1;                                                        \
            return;                                                                    \
        } else { minunit_print_progress(); })

#define mu_assert_int_less_than(val, result)                                           \
    MU__SAFE_BLOCK(                                                                    \
        int minunit_tmp_e; int minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
        minunit_tmp_r = (result);                                                      \
        if(val <= minunit_tmp_r) {                                                     \
            snprintf(                                                                  \
                minunit_last_message,                                                  \
                MINUNIT_MESSAGE_LEN,                                                   \
                "%s failed:\n\t%s:%d: %d >= %d",                                       \
                __func__,                                                              \
                __FILE__,                                                              \
                __LINE__,                                                              \
                minunit_tmp_r,                                                         \
                minunit_tmp_e);                                                        \
            minunit_status = 1;                                                        \
            return;                                                                    \
        } else { minunit_print_progress(); })

#define mu_assert_int_between(expected_lower, expected_upper, result)              \
    MU__SAFE_BLOCK(                                                                \
        int minunit_tmp_e; int minunit_tmp_m; int minunit_tmp_r; minunit_assert++; \
        minunit_tmp_e = (expected_lower);                                          \
        minunit_tmp_m = (expected_upper);                                          \
        minunit_tmp_r = (result);                                                  \
        if(result < minunit_tmp_e || result > minunit_tmp_m) {                     \
            snprintf(                                                              \
                minunit_last_message,                                              \
                MINUNIT_MESSAGE_LEN,                                               \
                "%s failed:\n\t%s:%d: %d was not between (inclusive) %d and %d",   \
                __func__,                                                          \
                __FILE__,                                                          \
                __LINE__,                                                          \
                minunit_tmp_e,                                                     \
                minunit_tmp_r,                                                     \
                minunit_tmp_m);                                                    \
            minunit_status = 1;                                                    \
            return;                                                                \
        } else { minunit_print_progress(); })

#define mu_assert_int_in(expected, array_length, result)                                 \
    MU__SAFE_BLOCK(                                                                      \
        int minunit_tmp_r; minunit_assert++; minunit_tmp_r = (result); int t = 0; int i; \
        for(i = 0; i < array_length; i++) {                                              \
            if(expected[i] == minunit_tmp_r) t = 1;                                      \
        } if(t == 0) {                                                                   \
            char tmp[500] = {0};                                                         \
            tmp[0] = '[';                                                                \
            for(i = 0; i < array_length; i++) {                                          \
                sprintf(tmp + strlen(tmp), "%d, ", expected[i]);                         \
            }                                                                            \
            int len = strlen(tmp);                                                       \
            tmp[len - 2] = ']';                                                          \
            tmp[len - 1] = '\0';                                                         \
            snprintf(                                                                    \
                minunit_last_message,                                                    \
                MINUNIT_MESSAGE_LEN,                                                     \
                "%s failed:\n\t%s:%d: expected to be one of %s but was %d",              \
                __func__,                                                                \
                __FILE__,                                                                \
                __LINE__,                                                                \
                tmp,                                                                     \
                minunit_tmp_r);                                                          \
            minunit_status = 1;                                                          \
            return;                                                                      \
        } else { minunit_print_progress(); })

#define mu_assert_double_eq(expected, result)                                                     \
    MU__SAFE_BLOCK(                                                                               \
        double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; minunit_tmp_e = (expected); \
        minunit_tmp_r = (result);                                                                 \
        if(fabs(minunit_tmp_e - minunit_tmp_r) > MINUNIT_EPSILON) {                               \
            int minunit_significant_figures = 1 - log10(MINUNIT_EPSILON);                         \
            snprintf(                                                                             \
                minunit_last_message,                                                             \
                MINUNIT_MESSAGE_LEN,                                                              \
                "%s failed:\n\t%s:%d: %.*g expected but was %.*g",                                \
                __func__,                                                                         \
                __FILE__,                                                                         \
                __LINE__,                                                                         \
                minunit_significant_figures,                                                      \
                minunit_tmp_e,                                                                    \
                minunit_significant_figures,                                                      \
                minunit_tmp_r);                                                                   \
            minunit_status = 1;                                                                   \
            return;                                                                               \
        } else { minunit_print_progress(); })

#define mu_assert_double_greater_than(val, result)                                           \
    MU__SAFE_BLOCK(                                                                          \
        double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
        minunit_tmp_r = (result);                                                            \
        if(val >= minunit_tmp_r) {                                                           \
            snprintf(                                                                        \
                minunit_last_message,                                                        \
                MINUNIT_MESSAGE_LEN,                                                         \
                "%s failed:\n\t%s:%d: %f <= %f",                                             \
                __func__,                                                                    \
                __FILE__,                                                                    \
                __LINE__,                                                                    \
                minunit_tmp_r,                                                               \
                minunit_tmp_e);                                                              \
            minunit_status = 1;                                                              \
            return;                                                                          \
        } else { minunit_print_progress(); })

#define mu_assert_double_less_than(val, result)                                              \
    MU__SAFE_BLOCK(                                                                          \
        double minunit_tmp_e; double minunit_tmp_r; minunit_assert++; minunit_tmp_e = (val); \
        minunit_tmp_r = (result);                                                            \
        if(val <= minunit_tmp_r) {                                                           \
            snprintf(                                                                        \
                minunit_last_message,                                                        \
                MINUNIT_MESSAGE_LEN,                                                         \
                "%s failed:\n\t%s:%d: %f >= %f",                                             \
                __func__,                                                                    \
                __FILE__,                                                                    \
                __LINE__,                                                                    \
                minunit_tmp_r,                                                               \
                minunit_tmp_e);                                                              \
            minunit_status = 1;                                                              \
            return;                                                                          \
        } else { minunit_print_progress(); })

#define mu_assert_double_between(expected_lower, expected_upper, result)                    \
    MU__SAFE_BLOCK(                                                                         \
        double minunit_tmp_e; double minunit_tmp_m; double minunit_tmp_r; minunit_assert++; \
        minunit_tmp_e = (expected_lower);                                                   \
        minunit_tmp_m = (expected_upper);                                                   \
        minunit_tmp_r = (result);                                                           \
        if(result < minunit_tmp_e || result > minunit_tmp_m) {                              \
            snprintf(                                                                       \
                minunit_last_message,                                                       \
                MINUNIT_MESSAGE_LEN,                                                        \
                "%s failed:\n\t%s:%d: %f was not between (inclusive) %f and %f",            \
                __func__,                                                                   \
                __FILE__,                                                                   \
                __LINE__,                                                                   \
                minunit_tmp_e,                                                              \
                minunit_tmp_r,                                                              \
                minunit_tmp_m);                                                             \
            minunit_status = 1;                                                             \
            return;                                                                         \
        } else { minunit_print_progress(); })

#define mu_assert_string_eq(expected, result)                                         \
    MU__SAFE_BLOCK(                                                                   \
        const char* minunit_tmp_e = expected; const char* minunit_tmp_r = result;     \
        minunit_assert++;                                                             \
        if(!minunit_tmp_e) { minunit_tmp_e = "<null pointer>"; } if(!minunit_tmp_r) { \
            minunit_tmp_r = "<null pointer>";                                         \
        } if(strcmp(minunit_tmp_e, minunit_tmp_r)) {                                  \
            snprintf(                                                                 \
                minunit_last_message,                                                 \
                MINUNIT_MESSAGE_LEN,                                                  \
                "%s failed:\n\t%s:%d: '%s' expected but was '%s'",                    \
                __func__,                                                             \
                __FILE__,                                                             \
                __LINE__,                                                             \
                minunit_tmp_e,                                                        \
                minunit_tmp_r);                                                       \
            minunit_status = 1;                                                       \
            return;                                                                   \
        } else { minunit_print_progress(); })

#define mu_assert_null(result)                                                    \
    MU__SAFE_BLOCK(                                                               \
        minunit_assert++; if(result == NULL) { minunit_print_progress(); } else { \
            snprintf(                                                             \
                minunit_last_message,                                             \
                MINUNIT_MESSAGE_LEN,                                              \
                "%s failed:\n\t%s:%d: Expected result was not NULL",              \
                __func__,                                                         \
                __FILE__,                                                         \
                __LINE__);                                                        \
            minunit_status = 1;                                                   \
            return;                                                               \
        })

#define mu_assert_not_null(result)                                                \
    MU__SAFE_BLOCK(                                                               \
        minunit_assert++; if(result != NULL) { minunit_print_progress(); } else { \
            snprintf(                                                             \
                minunit_last_message,                                             \
                MINUNIT_MESSAGE_LEN,                                              \
                "%s failed:\n\t%s:%d: Expected result was not NULL",              \
                __func__,                                                         \
                __FILE__,                                                         \
                __LINE__);                                                        \
            minunit_status = 1;                                                   \
            return;                                                               \
        })

#define mu_assert_pointers_eq(pointer1, pointer2)                                                  \
    MU__SAFE_BLOCK(                                                                                \
        minunit_assert++; if(pointer1 == pointer2) { minunit_print_progress(); } else {            \
            snprintf(                                                                              \
                minunit_last_message,                                                              \
                MINUNIT_MESSAGE_LEN,                                                               \
                "%s failed:\n\t%s:%d: Expected the pointers to point to the same memory location", \
                __func__,                                                                          \
                __FILE__,                                                                          \
                __LINE__);                                                                         \
            minunit_status = 1;                                                                    \
            return;                                                                                \
        })

#define mu_assert_pointers_not_eq(pointer1, pointer2)                                              \
    MU__SAFE_BLOCK(                                                                                \
        minunit_assert++; if(pointer1 != pointer2) { minunit_print_progress(); } else {            \
            snprintf(                                                                              \
                minunit_last_message,                                                              \
                MINUNIT_MESSAGE_LEN,                                                               \
                "%s failed:\n\t%s:%d: Expected the pointers to point to the same memory location", \
                __func__,                                                                          \
                __FILE__,                                                                          \
                __LINE__);                                                                         \
            minunit_status = 1;                                                                    \
            return;                                                                                \
        })

/*
 * The following two functions were written by David Robert Nadeau
 * from http://NadeauSoftware.com/ and distributed under the
 * Creative Commons Attribution 3.0 Unported License
 */

/**
 * Returns the real time, in seconds, or -1.0 if an error occurred.
 *
 * Time is measured since an arbitrary and OS-dependent start time.
 * The returned real time is only useful for computing an elapsed time
 * between two calls to this function.
 */
__attribute__((unused)) static double mu_timer_real(void) {
#if defined(_WIN32)
    /* Windows 2000 and later. ---------------------------------- */
    LARGE_INTEGER Time;
    LARGE_INTEGER Frequency;

    QueryPerformanceFrequency(&Frequency);
    QueryPerformanceCounter(&Time);

    Time.QuadPart *= 1000000;
    Time.QuadPart /= Frequency.QuadPart;

    return (double)Time.QuadPart / 1000000.0;

#elif(defined(__hpux) || defined(hpux)) ||                   \
    ((defined(__sun__) || defined(__sun) || defined(sun)) && \
     (defined(__SVR4) || defined(__svr4__)))
    /* HP-UX, Solaris. ------------------------------------------ */
    return (double)gethrtime() / 1000000000.0;

#elif defined(__MACH__) && defined(__APPLE__)
    /* OSX. ----------------------------------------------------- */
    static double timeConvert = 0.0;
    if(timeConvert == 0.0) {
        mach_timebase_info_data_t timeBase;
        (void)mach_timebase_info(&timeBase);
        timeConvert = (double)timeBase.numer / (double)timeBase.denom / 1000000000.0;
    }
    return (double)mach_absolute_time() * timeConvert;

#elif defined(_POSIX_VERSION)
    /* POSIX. --------------------------------------------------- */
    struct timeval tm;
#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
    {
        struct timespec ts;
#if defined(CLOCK_MONOTONIC_PRECISE)
        /* BSD. --------------------------------------------- */
        const clockid_t id = CLOCK_MONOTONIC_PRECISE;
#elif defined(CLOCK_MONOTONIC_RAW)
        /* Linux. ------------------------------------------- */
        const clockid_t id = CLOCK_MONOTONIC_RAW;
#elif defined(CLOCK_HIGHRES)
        /* Solaris. ----------------------------------------- */
        const clockid_t id = CLOCK_HIGHRES;
#elif defined(CLOCK_MONOTONIC)
        /* AIX, BSD, Linux, POSIX, Solaris. ----------------- */
        const clockid_t id = CLOCK_MONOTONIC;
#elif defined(CLOCK_REALTIME)
        /* AIX, BSD, HP-UX, Linux, POSIX. ------------------- */
        const clockid_t id = CLOCK_REALTIME;
#else
        const clockid_t id = (clockid_t)-1; /* Unknown. */
#endif /* CLOCK_* */
        if(id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
            return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
        /* Fall thru. */
    }
#endif /* _POSIX_TIMERS */

    /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, POSIX, Solaris. ----- */
    gettimeofday(&tm, NULL);
    return (double)tm.tv_sec + (double)tm.tv_usec / 1000000.0;
#else
    return -1.0; /* Failed. */
#endif
}

/**
 * Returns the amount of CPU time used by the current process,
 * in seconds, or -1.0 if an error occurred.
 */
__attribute__((unused)) static double mu_timer_cpu(void) {
#if defined(_WIN32)
    /* Windows -------------------------------------------------- */
    FILETIME createTime;
    FILETIME exitTime;
    FILETIME kernelTime;
    FILETIME userTime;

    /* This approach has a resolution of 1/64 second. Unfortunately, Windows' API does not offer better */
    if(GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime) != 0) {
        ULARGE_INTEGER userSystemTime;
        memcpy(&userSystemTime, &userTime, sizeof(ULARGE_INTEGER));
        return (double)userSystemTime.QuadPart / 10000000.0;
    }

#elif defined(__unix__) || defined(__unix) || defined(unix) || \
    (defined(__APPLE__) && defined(__MACH__))
    /* AIX, BSD, Cygwin, HP-UX, Linux, OSX, and Solaris --------- */

#if defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
    /* Prefer high-res POSIX timers, when available. */
    {
        clockid_t id;
        struct timespec ts;
#if _POSIX_CPUTIME > 0
        /* Clock ids vary by OS.  Query the id, if possible. */
        if(clock_getcpuclockid(0, &id) == -1)
#endif
#if defined(CLOCK_PROCESS_CPUTIME_ID)
            /* Use known clock id for AIX, Linux, or Solaris. */
            id = CLOCK_PROCESS_CPUTIME_ID;
#elif defined(CLOCK_VIRTUAL)
        /* Use known clock id for BSD or HP-UX. */
        id = CLOCK_VIRTUAL;
#else
        id = (clockid_t)-1;
#endif
        if(id != (clockid_t)-1 && clock_gettime(id, &ts) != -1)
            return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
    }
#endif

#if defined(RUSAGE_SELF)
    {
        struct rusage rusage;
        if(getrusage(RUSAGE_SELF, &rusage) != -1)
            return (double)rusage.ru_utime.tv_sec + (double)rusage.ru_utime.tv_usec / 1000000.0;
    }
#endif

#if defined(_SC_CLK_TCK)
    {
        const double ticks = (double)sysconf(_SC_CLK_TCK);
        struct tms tms;
        if(times(&tms) != (clock_t)-1) return (double)tms.tms_utime / ticks;
    }
#endif

#if defined(CLOCKS_PER_SEC)
    {
        clock_t cl = clock();
        if(cl != (clock_t)-1) return (double)cl / (double)CLOCKS_PER_SEC;
    }
#endif

#endif

    return -1; /* Failed. */
}

#ifdef __cplusplus
}
#endif

#endif /* MINUNIT_MINUNIT_H */