1 #include <gtest/gtest.h>
2
3 #include <fxdiv.h>
4
TEST(uint32_t,cases)5 TEST(uint32_t, cases) {
6 EXPECT_EQ(UINT32_C(42) / UINT32_C(7),
7 fxdiv_quotient_uint32_t(UINT32_C(42),
8 fxdiv_init_uint32_t(UINT32_C(7))));
9 EXPECT_EQ(UINT32_C(42) / UINT32_C(6),
10 fxdiv_quotient_uint32_t(UINT32_C(42),
11 fxdiv_init_uint32_t(UINT32_C(6))));
12 EXPECT_EQ(UINT32_C(42) / UINT32_C(5),
13 fxdiv_quotient_uint32_t(UINT32_C(42),
14 fxdiv_init_uint32_t(UINT32_C(5))));
15 EXPECT_EQ(UINT32_C(1) / UINT32_C(1),
16 fxdiv_quotient_uint32_t(UINT32_C(1),
17 fxdiv_init_uint32_t(UINT32_C(1))));
18 EXPECT_EQ(UINT32_MAX / UINT32_C(1),
19 fxdiv_quotient_uint32_t(UINT32_MAX,
20 fxdiv_init_uint32_t(UINT32_C(1))));
21 EXPECT_EQ(UINT32_MAX / UINT32_MAX,
22 fxdiv_quotient_uint32_t(UINT32_MAX,
23 fxdiv_init_uint32_t(UINT32_MAX)));
24 EXPECT_EQ((UINT32_MAX - 1) / UINT32_MAX,
25 fxdiv_quotient_uint32_t(UINT32_MAX - 1,
26 fxdiv_init_uint32_t(UINT32_MAX)));
27 EXPECT_EQ(UINT32_MAX / (UINT32_MAX - 1),
28 fxdiv_quotient_uint32_t(UINT32_MAX,
29 fxdiv_init_uint32_t(UINT32_MAX - 1)));
30 EXPECT_EQ(UINT32_C(0) / UINT32_C(1),
31 fxdiv_quotient_uint32_t(UINT32_C(0),
32 fxdiv_init_uint32_t(UINT32_C(1))));
33 }
34
TEST(uint32_t,divide_by_1)35 TEST(uint32_t, divide_by_1) {
36 const fxdiv_divisor_uint32_t d = fxdiv_init_uint32_t(UINT32_C(1));
37 const uint32_t step = UINT32_C(487);
38 for (uint32_t n = 0; n <= UINT32_MAX - step + 1; n += step) {
39 EXPECT_EQ(n, fxdiv_quotient_uint32_t(n, d));
40 }
41 }
42
TEST(uint32_t,divide_zero)43 TEST(uint32_t, divide_zero) {
44 const uint32_t step = UINT32_C(491);
45 for (uint32_t d = 1; d <= UINT32_MAX - step + 1; d += step) {
46 EXPECT_EQ(0,
47 fxdiv_quotient_uint32_t(0,
48 fxdiv_init_uint32_t(d)));
49 }
50 }
51
TEST(uint32_t,divide_by_n_minus_1)52 TEST(uint32_t, divide_by_n_minus_1) {
53 const uint32_t step = UINT32_C(523);
54 for (uint32_t n = 3; n <= UINT32_MAX - step + 1; n += step) {
55 EXPECT_EQ(UINT32_C(1),
56 fxdiv_quotient_uint32_t(n,
57 fxdiv_init_uint32_t(n - 1)));
58 }
59 }
60
TEST(uint32_t,divide_by_power_of_2)61 TEST(uint32_t, divide_by_power_of_2) {
62 const uint32_t step = UINT32_C(25183);
63 for (uint32_t p = 0; p < 32; p += 1) {
64 const fxdiv_divisor_uint32_t divisor =
65 fxdiv_init_uint32_t(UINT32_C(1) << p);
66 for (uint32_t n = 0; n <= UINT32_MAX - step + 1; n += step) {
67 EXPECT_EQ(n >> p, fxdiv_quotient_uint32_t(n, divisor));
68 }
69 }
70 }
71
TEST(uint32_t,divide_by_power_of_2_plus_1)72 TEST(uint32_t, divide_by_power_of_2_plus_1) {
73 const uint32_t step = UINT32_C(25183);
74 for (uint32_t p = 0; p < 32; p += 1) {
75 const uint32_t d = (UINT32_C(1) << p) + UINT32_C(1);
76 const fxdiv_divisor_uint32_t divisor = fxdiv_init_uint32_t(d);
77 for (uint32_t n = 0; n <= UINT32_MAX - step + 1; n += step) {
78 EXPECT_EQ(n / d, fxdiv_quotient_uint32_t(n, divisor));
79 }
80 }
81 }
82
TEST(uint32_t,divide_by_power_of_2_minus_1)83 TEST(uint32_t, divide_by_power_of_2_minus_1) {
84 const uint32_t step = UINT64_C(25183);
85 for (uint32_t p = 0; p < 32; p += 1) {
86 const uint32_t d = (UINT32_C(2) << p) - UINT32_C(1);
87 const fxdiv_divisor_uint32_t divisor = fxdiv_init_uint32_t(d);
88 for (uint32_t n = 0; n <= UINT32_MAX - step + 1; n += step) {
89 EXPECT_EQ(n / d, fxdiv_quotient_uint32_t(n, divisor));
90 }
91 }
92 }
93
TEST(uint32_t,match_native)94 TEST(uint32_t, match_native) {
95 const uint32_t stepD = UINT32_C(821603);
96 const uint32_t stepN = UINT32_C(821641);
97 for (uint32_t d = UINT32_MAX; d >= stepD; d -= stepD) {
98 const fxdiv_divisor_uint32_t divisor = fxdiv_init_uint32_t(d);
99 for (uint32_t n = UINT32_MAX; n >= stepN; n -= stepN) {
100 EXPECT_EQ(n / d, fxdiv_quotient_uint32_t(n, divisor));
101 }
102 }
103 }
104
TEST(uint64_t,cases)105 TEST(uint64_t, cases) {
106 EXPECT_EQ(UINT64_C(42) / UINT64_C(7),
107 fxdiv_quotient_uint64_t(UINT64_C(42),
108 fxdiv_init_uint64_t(UINT64_C(7))));
109 EXPECT_EQ(UINT64_C(42) / UINT64_C(6),
110 fxdiv_quotient_uint64_t(UINT64_C(42),
111 fxdiv_init_uint64_t(UINT64_C(6))));
112 EXPECT_EQ(UINT64_C(42) / UINT64_C(5),
113 fxdiv_quotient_uint64_t(UINT64_C(42),
114 fxdiv_init_uint64_t(UINT64_C(5))));
115 EXPECT_EQ(UINT64_C(1) / UINT64_C(1),
116 fxdiv_quotient_uint64_t(UINT64_C(1),
117 fxdiv_init_uint64_t(UINT64_C(1))));
118 EXPECT_EQ(UINT64_MAX / UINT64_C(1),
119 fxdiv_quotient_uint64_t(UINT64_MAX,
120 fxdiv_init_uint64_t(UINT64_C(1))));
121 EXPECT_EQ(UINT64_MAX / UINT64_MAX,
122 fxdiv_quotient_uint64_t(UINT64_MAX,
123 fxdiv_init_uint64_t(UINT64_MAX)));
124 EXPECT_EQ((UINT64_MAX - 1) / UINT64_MAX,
125 fxdiv_quotient_uint64_t(UINT64_MAX - 1,
126 fxdiv_init_uint64_t(UINT64_MAX)));
127 EXPECT_EQ(UINT64_MAX / (UINT64_MAX - 1),
128 fxdiv_quotient_uint64_t(UINT64_MAX,
129 fxdiv_init_uint64_t(UINT64_MAX - 1)));
130 EXPECT_EQ(UINT64_C(0) / UINT64_C(1),
131 fxdiv_quotient_uint64_t(UINT64_C(0),
132 fxdiv_init_uint64_t(UINT64_C(1))));
133 }
134
TEST(uint64_t,divide_by_1)135 TEST(uint64_t, divide_by_1) {
136 const fxdiv_divisor_uint64_t d = fxdiv_init_uint64_t(UINT64_C(1));
137 const uint64_t step = UINT64_C(2048116998241);
138 for (uint64_t n = 0; n <= UINT64_MAX - step + 1; n += step) {
139 EXPECT_EQ(n, fxdiv_quotient_uint64_t(n, d));
140 }
141 }
142
TEST(uint64_t,divide_zero)143 TEST(uint64_t, divide_zero) {
144 const uint64_t step = UINT64_C(8624419641811);
145 for (uint64_t d = 1; d <= UINT64_MAX - step + 1; d += step) {
146 EXPECT_EQ(0,
147 fxdiv_quotient_uint64_t(0,
148 fxdiv_init_uint64_t(d)));
149 }
150 }
151
TEST(uint64_t,divide_by_n_minus_1)152 TEST(uint64_t, divide_by_n_minus_1) {
153 const uint64_t step = UINT64_C(8624419641833);
154 for (uint64_t n = 3; n <= UINT64_MAX - step + 1; n += step) {
155 EXPECT_EQ(UINT64_C(1),
156 fxdiv_quotient_uint64_t(n,
157 fxdiv_init_uint64_t(n - 1)));
158 }
159 }
160
TEST(uint64_t,divide_by_power_of_2)161 TEST(uint64_t, divide_by_power_of_2) {
162 const uint64_t step = UINT64_C(93400375993241);
163 for (uint32_t p = 0; p < 64; p += 1) {
164 const fxdiv_divisor_uint64_t divisor =
165 fxdiv_init_uint64_t(UINT64_C(1) << p);
166 for (uint64_t n = 0; n <= UINT64_MAX - step + 1; n += step) {
167 EXPECT_EQ(n >> p, fxdiv_quotient_uint64_t(n, divisor));
168 }
169 }
170 }
171
TEST(uint64_t,divide_by_power_of_2_plus_1)172 TEST(uint64_t, divide_by_power_of_2_plus_1) {
173 const uint64_t step = UINT64_C(93400375993241);
174 for (uint32_t p = 0; p < 64; p += 1) {
175 const uint64_t d = (UINT64_C(1) << p) + UINT64_C(1);
176 const fxdiv_divisor_uint64_t divisor = fxdiv_init_uint64_t(d);
177 for (uint64_t n = 0; n <= UINT64_MAX - step + 1; n += step) {
178 EXPECT_EQ(n / d, fxdiv_quotient_uint64_t(n, divisor));
179 }
180 }
181 }
182
TEST(uint64_t,divide_by_power_of_2_minus_1)183 TEST(uint64_t, divide_by_power_of_2_minus_1) {
184 const uint64_t step = UINT64_C(93400375993241);
185 for (uint32_t p = 0; p < 64; p += 1) {
186 const uint64_t d = (UINT64_C(2) << p) - UINT64_C(1);
187 const fxdiv_divisor_uint64_t divisor = fxdiv_init_uint64_t(d);
188 for (uint64_t n = 0; n <= UINT64_MAX - step + 1; n += step) {
189 EXPECT_EQ(n / d, fxdiv_quotient_uint64_t(n, divisor));
190 }
191 }
192 }
193
TEST(uint64_t,match_native)194 TEST(uint64_t, match_native) {
195 const uint64_t stepD = UINT64_C(7093600525704701);
196 const uint64_t stepN = UINT64_C(7093600525704677);
197 for (uint64_t d = UINT64_MAX; d >= stepD; d -= stepD) {
198 const fxdiv_divisor_uint64_t divisor = fxdiv_init_uint64_t(d);
199 for (uint64_t n = UINT64_MAX; n >= stepN; n -= stepN) {
200 EXPECT_EQ(n / d, fxdiv_quotient_uint64_t(n, divisor));
201 }
202 }
203 }
204
TEST(size_t,cases)205 TEST(size_t, cases) {
206 EXPECT_EQ(42 / 7,
207 fxdiv_quotient_size_t(42,
208 fxdiv_init_size_t(7)));
209 EXPECT_EQ(42 / 6,
210 fxdiv_quotient_size_t(42,
211 fxdiv_init_size_t(6)));
212 EXPECT_EQ(42 / 5,
213 fxdiv_quotient_size_t(42,
214 fxdiv_init_size_t(5)));
215 EXPECT_EQ(1 / 1,
216 fxdiv_quotient_size_t(1,
217 fxdiv_init_size_t(1)));
218 EXPECT_EQ(SIZE_MAX / 1,
219 fxdiv_quotient_size_t(SIZE_MAX,
220 fxdiv_init_size_t(1)));
221 EXPECT_EQ(SIZE_MAX / SIZE_MAX,
222 fxdiv_quotient_size_t(SIZE_MAX,
223 fxdiv_init_size_t(SIZE_MAX)));
224 EXPECT_EQ((SIZE_MAX - 1) / SIZE_MAX,
225 fxdiv_quotient_size_t(SIZE_MAX - 1,
226 fxdiv_init_size_t(SIZE_MAX)));
227 EXPECT_EQ(SIZE_MAX / (SIZE_MAX - 1),
228 fxdiv_quotient_size_t(SIZE_MAX,
229 fxdiv_init_size_t(SIZE_MAX - 1)));
230 EXPECT_EQ(0 / 1,
231 fxdiv_quotient_size_t(0,
232 fxdiv_init_size_t(1)));
233 }
234