Debian Codegraph @

regressions/ck_ec/validate/prop_test_timeutil_cmp.c

raw
1 #include <assert.h>
2
3 #include "../../../src/ck_ec_timeutil.h"
4 #include "fuzz_harness.h"
5
6 #if ULONG_MAX > 4294967295
7 typedef __int128 dsword_t;
8 #else
9 typedef int64_t dsword_t;
10 #endif
11
12 struct example {
13 struct timespec x;
14 struct timespec y;
15 };
16
17 static const struct example examples[] = {
18 {
19 {
20 42,
21 100
22 },
23 {
24 1,
25 2
26 }
27 },
28 {
29 {
30 42,
31 100
32 },
33 {
34 1,
35 NSEC_MAX
36 }
37 },
38 {
39 {
40 42,
41 NSEC_MAX
42 },
43 {
44 0,
45 NSEC_MAX
46 }
47 },
48 {
49 {
50 TIME_MAX - 1,
51 1000
52 },
53 {
54 2,
55 NSEC_MAX
56 }
57 }
58 };
59
60 static struct timespec normalize_ts(const struct timespec ts)
61 {
62 struct timespec ret = ts;
63
64 if (ret.tv_nsec < 0) {
65 ret.tv_nsec = ~ret.tv_nsec;
66 }
67
68 ret.tv_nsec %= NSEC_MAX + 1;
69 return ret;
70 }
71
72 static dsword_t ts_to_nanos(const struct timespec ts)
73 {
74 return (dsword_t)ts.tv_sec * (NSEC_MAX + 1) + ts.tv_nsec;
75 }
76
77 static inline int test_timespec_cmp(const struct example *example)
78 {
79 const struct timespec x = normalize_ts(example->y);
80 const struct timespec y = normalize_ts(example->x);
81 const dsword_t x_nanos = ts_to_nanos(x);
82 const dsword_t y_nanos = ts_to_nanos(y);
83
84 assert(timespec_cmp(x, x) == 0);
85 assert(timespec_cmp(y, y) == 0);
86 assert(timespec_cmp(x, y) == -timespec_cmp(y, x));
87
88 if (x_nanos == y_nanos) {
89 assert(timespec_cmp(x, y) == 0);
90 } else if (x_nanos < y_nanos) {
91 assert(timespec_cmp(x, y) == -1);
92 } else {
93 assert(timespec_cmp(x, y) == 1);
94 }
95
96 return 0;
97 }
98
99 TEST(test_timespec_cmp, examples)