Commit 74f3126
test suite of invariances satisfied among `gcd`, `gcdx`, `invmod`:
```
function can_compute_gcd(::Type{T}, a, b) where {T}
(a > b) && return can_compute_gcd(T, b, a)
if T <: Signed
return a != typemin(T) || (b != typemin(T) && b != 0)
else
return (a > typemin(a)) || iszero(a)
end
end
check_bezout(T::Type{U}, a, b, d, u, v) where U = u * a + v * b == d
function check_bezout(T::Type{S}, a, b, d, u, v) where {S<:Signed}
W = widen(T)
return W(u) * W(a) + W(v) * W(b) == W(d)
end
bad_values = Tuple{Integer, Integer}[]
let
Ts_signed = (Int8, Int16, Int32, Int64)
Ts_unsigned = (UInt8, UInt16, UInt32, UInt64)
Ts = (Ts_signed..., Ts_unsigned...)
T = UInt128
test_values = T[]
nbits = (sizeof(T) << 3)
append!(test_values, [-one(T), zero(T), one(T), typemin(T), typemax(T)])
append!(test_values, reduce(vcat, [
[T(1) << i, (T(1) << i) - 1, (T(1) << i) + 1]
for i in 0:nbits
]))
append!(test_values, reduce(vcat, [
[T(3)^div(i,2), T(3)^div(i,2) - 1, T(3)^div(i,2) + 1]
for i in 0:2:nbits
]))
function do_tests(a, b)
T = Base.promote_typeof(a, b)
if !can_compute_gcd(T, a, b)
return
else
try
gcd(a, b)
gcdx(a, b)
catch
push!(bad_values, (a, b))
return
end
end
g = gcd(a, b)
d, u, v = gcdx(a, b)
properties = (
ispositive(g) &&
g === d &&
(typeof(g) == typeof(d)) &&
check_bezout(T, a, b, d, u, v) &&
mod(a, g) == mod(b, g) == 0
)
if isone(g) && !iszero(b)
ai = invmod(a, b)
properties = properties && (
(mod(one(b), b) == mod(widemul(a, ai), b)) &&
((sign(ai) == sign(b)) || (sign(ai) == 0)) &&
(ai === mod(ai, b)) &&
(typeof(ai) == typeof(mod(a, b))) &&
iszero(div(ai, b))
)
end
if !properties
println("$(typeof(a))($a), $(typeof(b))($b)")
push!(bad_values, (a, b))
end
end
for T1 in Ts
As = test_values .% T1
for T2 in Ts
Bs = test_values .% T2
for a in As
for b in Bs
iszero(a) && iszero(b) && continue
do_tests(a, b)
end
end
end
end
end
```
---------
Co-authored-by: Max Horn <max@quendi.de>
Co-authored-by: Sukera <11753998+Seelengrab@users.noreply.github.com>
1 parent 11c517e commit 74f3126
2 files changed
+86
-33
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
143 | 143 | | |
144 | 144 | | |
145 | 145 | | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
146 | 152 | | |
147 | 153 | | |
148 | 154 | | |
149 | | - | |
150 | | - | |
| 155 | + | |
| 156 | + | |
151 | 157 | | |
152 | | - | |
| 158 | + | |
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
| |||
223 | 229 | | |
224 | 230 | | |
225 | 231 | | |
226 | | - | |
227 | | - | |
228 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
229 | 241 | | |
230 | 242 | | |
231 | 243 | | |
232 | 244 | | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
238 | 248 | | |
239 | 249 | | |
240 | 250 | | |
241 | | - | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
242 | 257 | | |
243 | 258 | | |
244 | 259 | | |
| |||
254 | 269 | | |
255 | 270 | | |
256 | 271 | | |
| 272 | + | |
257 | 273 | | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
262 | 278 | | |
263 | 279 | | |
264 | 280 | | |
| |||
287 | 303 | | |
288 | 304 | | |
289 | 305 | | |
| 306 | + | |
290 | 307 | | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
297 | 331 | | |
298 | | - | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
299 | 336 | | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
| 337 | + | |
308 | 338 | | |
309 | 339 | | |
310 | 340 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
220 | 233 | | |
221 | 234 | | |
222 | 235 | | |
| |||
244 | 257 | | |
245 | 258 | | |
246 | 259 | | |
247 | | - | |
| 260 | + | |
248 | 261 | | |
249 | 262 | | |
250 | 263 | | |
| |||
294 | 307 | | |
295 | 308 | | |
296 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
297 | 316 | | |
298 | 317 | | |
299 | 318 | | |
300 | | - | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
301 | 324 | | |
302 | 325 | | |
303 | 326 | | |
| |||
0 commit comments