|
445 | 445 | @test string(big(0), base = rand(2:62), pad = 0) == "" |
446 | 446 | end |
447 | 447 |
|
| 448 | +@testset "Base.GMP.MPZ.export!" begin |
| 449 | + |
| 450 | + function Base_GMP_MPZ_import!(x::BigInt, n::AbstractVector{T}; order::Integer=-1, nails::Integer=0, endian::Integer=0) where {T<:Base.BitInteger} |
| 451 | + ccall((:__gmpz_import, Base.GMP.MPZ.libgmp), |
| 452 | + Cvoid, |
| 453 | + (Base.GMP.MPZ.mpz_t, Csize_t, Cint, Csize_t, Cint, Csize_t, Ptr{Cvoid}), |
| 454 | + x, length(n), order, sizeof(T), endian, nails, n) |
| 455 | + return x |
| 456 | + end |
| 457 | + # test import |
| 458 | + bytes_to_import_from = Vector{UInt8}([1, 0]) |
| 459 | + int_to_import_to = BigInt() |
| 460 | + Base_GMP_MPZ_import!(int_to_import_to, bytes_to_import_from, order=0) |
| 461 | + @test int_to_import_to == BigInt(256) |
| 462 | + |
| 463 | + # test export |
| 464 | + int_to_export_from = BigInt(256) |
| 465 | + bytes_to_export_to = Vector{UInt8}(undef, 2) |
| 466 | + Base.GMP.MPZ.export!(bytes_to_export_to, int_to_export_from, order=0) |
| 467 | + @test all(bytes_to_export_to .== bytes_to_import_from) |
| 468 | + |
| 469 | + # test both composed import(export) is identity |
| 470 | + int_to_export_from = BigInt(256) |
| 471 | + bytes_to_export_to = Vector{UInt8}(undef, 2) |
| 472 | + Base.GMP.MPZ.export!(bytes_to_export_to, int_to_export_from, order=0) |
| 473 | + int_to_import_to = BigInt() |
| 474 | + Base_GMP_MPZ_import!(int_to_import_to, bytes_to_export_to, order=0) |
| 475 | + @test int_to_export_from == int_to_import_to |
| 476 | + |
| 477 | + # test both composed export(import) is identity |
| 478 | + bytes_to_import_from = Vector{UInt8}([1, 0]) |
| 479 | + int_to_import_to = BigInt() |
| 480 | + Base_GMP_MPZ_import!(int_to_import_to, bytes_to_import_from, order=0) |
| 481 | + bytes_to_export_to = Vector{UInt8}(undef, 2) |
| 482 | + Base.GMP.MPZ.export!(bytes_to_export_to, int_to_export_from, order=0) |
| 483 | + @test all(bytes_to_export_to .== bytes_to_import_from) |
| 484 | +end |
| 485 | + |
448 | 486 | @test isqrt(big(4)) == 2 |
449 | 487 | @test isqrt(big(5)) == 2 |
450 | 488 |
|
|
0 commit comments