Repository: JuliaLang/julia
File: test/intfuncs.jl (Julia)

Code before:

    @test powermod(2, 0, 5) == 1
    @test powermod(2, 0, -5) == -4

    @test powermod(2, -1, 5) == 3
    @test powermod(2, -2, 5) == 4
    @test powermod(2, -1, -5) == -2
    @test powermod(2, -2, -5) == -1

    @test powermod(2, typemin(Int128), 5) == 1
    @test powermod(2, typemin(Int128), -5) == -4

    @test powermod(2, big(3), 5) == 3
    @test powermod(2, big(3), -5) == -2
    @inferred  powermod(2, -2, -5)
    @inferred  powermod(big(2), -2, UInt(5))
end

@testset "nextpow/prevpow" begin
    @testset "small BigInt: $f $R" for f ∈ (prevpow, nextpow),
                                       P ∈ (Int8, Int16, Int32, Int64, Int128),
                                       R ∈ (Int8, Int16, Int32, Int64, Int128),
                                       p ∈ 1:64,
                                       r ∈ 2:5
        q = P(p)
        n = R(r)
        for a ∈ (n, unsigned(n)), b ∈ (q, unsigned(q))
            @test f(a, b) == f(a, big(b))
        end
    end

    @test nextpow(2, 3) == 4
    @test nextpow(2, 4) == 4
    @test nextpow(2, 7) == 8
    @test_throws DomainError nextpow(0, 3)
    @test_throws DomainError nextpow(3, 0)

    @test prevpow(2, 3) == 2
    @test prevpow(2, 4) == 4
    @test prevpow(2, 5) == 4
    @test prevpow(Int64(10), Int64(1234567890123456789)) === Int64(1000000000000000000)
    @test prevpow(10, 101.0) === 100
    @test prevpow(10.0, 101) === 100.0
    @test_throws DomainError prevpow(0, 3)
    @test_throws DomainError prevpow(3, 0)

    # "argument is beyond the range of type of the base"
    @test_throws DomainError prevpow(Int8(3), 243)
    @test_throws DomainError nextpow(Int8(3), 243)

    # "result is beyond the range of type of the base"

Review comment (suggestion):
```suggestion
    fs = (prevpow, nextpow)
    types = (Int8, BigInt, BigFloat)
    for f ∈ fs, P ∈ types, R ∈ types, p ∈ 1:20, r ∈ 2:5
        q = P(p)
        n = R(r)
        @test f(r, p) == f(n, q)
    end
```

Code after:

    @test powermod(2, 0, 5) == 1
    @test powermod(2, 0, -5) == -4

    @test powermod(2, -1, 5) == 3
    @test powermod(2, -2, 5) == 4
    @test powermod(2, -1, -5) == -2
    @test powermod(2, -2, -5) == -1

    @test powermod(2, typemin(Int128), 5) == 1
    @test powermod(2, typemin(Int128), -5) == -4

    @test powermod(2, big(3), 5) == 3
    @test powermod(2, big(3), -5) == -2
    @inferred  powermod(2, -2, -5)
    @inferred  powermod(big(2), -2, UInt(5))
end

@testset "nextpow/prevpow" begin
    fs = (prevpow, nextpow)
    types = (Int8, BigInt, BigFloat)
    for f ∈ fs, P ∈ types, R ∈ types, p ∈ 1:20, r ∈ 2:5
        q = P(p)
        n = R(r)
        @test f(r, p) == f(n, q)
    end

    @test nextpow(2, 3) == 4
    @test nextpow(2, 4) == 4
    @test nextpow(2, 7) == 8
    @test_throws DomainError nextpow(0, 3)
    @test_throws DomainError nextpow(3, 0)

    @test prevpow(2, 3) == 2
    @test prevpow(2, 4) == 4
    @test prevpow(2, 5) == 4
    @test prevpow(Int64(10), Int64(1234567890123456789)) === Int64(1000000000000000000)
    @test prevpow(10, 101.0) === 100
    @test prevpow(10.0, 101) === 100.0
    @test_throws DomainError prevpow(0, 3)
    @test_throws DomainError prevpow(3, 0)

    # "argument is beyond the range of type of the base"
    @test_throws DomainError prevpow(Int8(3), 243)
    @test_throws DomainError nextpow(Int8(3), 243)

    # "result is beyond the range of type of the base"
    @test_throws OverflowError nextpow(Int8(3), 82)
end

@testset "ndigits/ndigits0z" begin