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
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
extern inline void libj2_j2u_sub_ju(struct libj2_j2u *a, uintmax_t b);
/* TODO Add man page */
#else
static uintmax_t
random_ju(void)
{
size_t n = LIBJ2_JU_BIT;
uintmax_t r = 0;
while (n--)
if (rand() < rand())
r |= (uintmax_t)1 << n;
return r;
}
static void
check(uintmax_t a_high, uintmax_t a_low, uintmax_t b)
{
struct libj2_j2u a, r, expected;
int expected_overflow, expected_roverflow;
a = (struct libj2_j2u){.high = a_high, .low = a_low};
expected_overflow = libj2_j2u_lt_ju(&a, b);
expected_roverflow = libj2_ju_lt_j2u(b, &a);
libj2_ju_to_j2u(b, &expected);
libj2_minus_j2u(&expected);
libj2_j2u_add_j2u_overflow(&expected, &a);
a = (struct libj2_j2u){.high = a_high, .low = a_low};
libj2_j2u_sub_ju(&a, b);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
EXPECT(libj2_j2u_sub_ju_overflow(&a, b) == expected_overflow);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
r = (struct libj2_j2u){111, 222};
a = (struct libj2_j2u){.high = a_high, .low = a_low};
libj2_j2u_sub_ju_to_j2u(&a, b, &r);
EXPECT(a.high == a_high);
EXPECT(a.low == a_low);
EXPECT(libj2_j2u_eq_j2u(&r, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
libj2_j2u_sub_ju_to_j2u(&a, b, &a);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
r = (struct libj2_j2u){111, 222};
a = (struct libj2_j2u){.high = a_high, .low = a_low};
EXPECT(libj2_j2u_sub_ju_to_j2u_overflow(&a, b, &r) == expected_overflow);
EXPECT(a.high == a_high);
EXPECT(a.low == a_low);
EXPECT(libj2_j2u_eq_j2u(&r, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
EXPECT(libj2_j2u_sub_ju_to_j2u_overflow(&a, b, &a) == expected_overflow);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
r = (struct libj2_j2u){111, 222};
a = (struct libj2_j2u){.high = a_high, .low = a_low};
libj2_ju_sub_j2u_to_j2u(b, &a, &r);
EXPECT(a.high == a_high);
EXPECT(a.low == a_low);
libj2_minus_j2u(&r);
EXPECT(libj2_j2u_eq_j2u(&r, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
libj2_ju_sub_j2u_to_j2u(b, &a, &a);
libj2_minus_j2u(&a);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
libj2_j2u_rsub_ju(&a, b);
libj2_minus_j2u(&a);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
EXPECT(libj2_j2u_rsub_ju_overflow(&a, b) == expected_roverflow);
libj2_minus_j2u(&a);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
r = (struct libj2_j2u){111, 222};
a = (struct libj2_j2u){.high = a_high, .low = a_low};
EXPECT(libj2_ju_sub_j2u_to_j2u_overflow(b, &a, &r) == expected_roverflow);
EXPECT(a.high == a_high);
EXPECT(a.low == a_low);
libj2_minus_j2u(&r);
EXPECT(libj2_j2u_eq_j2u(&r, &expected));
a = (struct libj2_j2u){.high = a_high, .low = a_low};
EXPECT(libj2_ju_sub_j2u_to_j2u_overflow(b, &a, &a) == expected_roverflow);
libj2_minus_j2u(&a);
EXPECT(libj2_j2u_eq_j2u(&a, &expected));
}
int
main(void)
{
unsigned i;
srand((unsigned)time(NULL));
check(0, 0, 0);
check(0, 0, UINTMAX_MAX);
check(UINTMAX_MAX, 0, 0);
check(UINTMAX_MAX, 0, UINTMAX_MAX);
for (i = 0; i < 256; i++) {
check(0, 0, random_ju());
check(random_ju(), 0, 0);
check(random_ju(), 0, random_ju());
check(random_ju(), 0, UINTMAX_MAX);
check(UINTMAX_MAX, 0, random_ju());
}
check(0, UINTMAX_MAX, 0);
check(0, UINTMAX_MAX, UINTMAX_MAX);
check(UINTMAX_MAX, UINTMAX_MAX, 0);
check(UINTMAX_MAX, UINTMAX_MAX, UINTMAX_MAX);
for (i = 0; i < 256; i++) {
check(0, UINTMAX_MAX, random_ju());
check(random_ju(), UINTMAX_MAX, 0);
check(random_ju(), UINTMAX_MAX, 1);
check(random_ju(), UINTMAX_MAX, random_ju());
check(random_ju(), UINTMAX_MAX, UINTMAX_MAX);
check(UINTMAX_MAX, UINTMAX_MAX, random_ju());
}
for (i = 0; i < 256; i++)
check(random_ju(), random_ju(), random_ju());
return 0;
}
#endif
|