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

Code before:
        @test widen(Rational{T}) == Rational{widen(T)}
    end

    @test iszero(typemin(Rational{UInt}))

    @test Rational(Float32(rand_int)) == Rational(rand_int)

    @test Rational(Rational(rand_int)) == Rational(rand_int)

    @test begin
        var = -Rational(UInt32(0))
        var == UInt32(0)
    end

    @test Rational(rand_int, 3)/Complex(3, 2) == Complex(Rational(rand_int, 13), -Rational(rand_int*2, 39))
    @test (true//true) / complex(false, true) === 0//1 - 1//1*im
    @test (false//true) / complex(false, true) === 0//1 + 0//1*im
    @test (false//true) / complex(true, false) === 0//1 + 0//1*im
    @test (false//true) / complex(true, true) === 0//1 + 0//1*im
    @test (true//true) / complex(true, true) === 1//2 - 1//2*im
    @test (false//true) / complex(true//true, true//true) === 0//1 + 0//1*im
    @test (true//true) / complex(true//true, true//true) === 1//2 - 1//2*im
    @test (false//true) / complex(true//false, false//true) === 0//1 + 0//1*im
    @test (true//true) / complex(true//true, true//false) === 0//1 + 0//1*im
    @test_throws DivideError (0//1) / complex(0, 0)
    @test_throws DivideError (1//1) / complex(0, 0)
    @test_throws DivideError (1//0) / complex(0, 0)

    # 1//200 - 1//200*im cannot be represented as Complex{Rational{Int8}}
    @test_throws OverflowError (Int8(1)//Int8(1)) / (Int8(100) + Int8(100)im)

    @test Complex(rand_int, 0) == Rational(rand_int)
    @test Rational(rand_int) == Complex(rand_int, 0)

    @test (Complex(rand_int, 4) == Rational(rand_int)) == false
    @test (Rational(rand_int) == Complex(rand_int, 4)) == false

    @test trunc(Rational(BigInt(rand_int), BigInt(3))) == Rational(trunc(BigInt, Rational(BigInt(rand_int),BigInt(3))))
    @test  ceil(Rational(BigInt(rand_int), BigInt(3))) == Rational( ceil(BigInt, Rational(BigInt(rand_int),BigInt(3))))
    @test round(Rational(BigInt(rand_int), BigInt(3))) == Rational(round(BigInt, Rational(BigInt(rand_int),BigInt(3))))


    for a = -3:3
        @test Rational(Float32(a)) == Rational(a)
        @test Rational(a)//2 == a//2
        @test a//Rational(2) == Rational(a/2)
        @test a.//[-2, -1, 1, 2] == [-a//2, -a//1, a//1, a//2]
        for b=-3:3, c=1:3
            @test b//(a+c*im) == b*a//(a^2+c^2)-(b*c//(a^2+c^2))*im
            for d=-3:3
                @test (a+b*im)//(c+d*im) == (a*c+b*d+(b*c-a*d)*im)//(c^2+d^2)

Review comment (bug):
This is wrong. The correct result is positive infinity, just like here:

```julia-repl
julia> (1 // 1) / 0
1//0
```

Code after:
        @test widen(Rational{T}) == Rational{widen(T)}
    end

    @test iszero(typemin(Rational{UInt}))

    @test Rational(Float32(rand_int)) == Rational(rand_int)

    @test Rational(Rational(rand_int)) == Rational(rand_int)

    @test begin
        var = -Rational(UInt32(0))
        var == UInt32(0)
    end

    @test Rational(rand_int, 3)/Complex(3, 2) == Complex(Rational(rand_int, 13), -Rational(rand_int*2, 39))
    @test (1//1) / complex(0, 1) === 0//1 - 1//1*im
    @test (0//1) / complex(0, 1) === 0//1 + 0//1*im
    @test (0//1) / complex(1, 0) === 0//1 + 0//1*im
    @test (0//1) / complex(1, 1) === 0//1 + 0//1*im
    @test (1//1) / complex(1, 1) === 1//2 - 1//2*im
    @test (0//1) / complex(1//1, 1//1) === 0//1 + 0//1*im
    @test (1//1) / complex(1//1, 1//1) === 1//2 - 1//2*im
    @test (0//1) / complex(1//0, 0//1) === 0//1 + 0//1*im
    @test (1//1) / complex(1//1, 1//0) === 0//1 + 0//1*im
    @test_throws DivideError (0//1) / complex(0, 0)
    @test_throws DivideError (1//1) / complex(0, 0)
    @test_throws DivideError (1//0) / complex(0, 0)

    # 1//200 - 1//200*im cannot be represented as Complex{Rational{Int8}}
    @test_throws OverflowError (Int8(1)//Int8(1)) / (Int8(100) + Int8(100)im)

    @test Complex(rand_int, 0) == Rational(rand_int)
    @test Rational(rand_int) == Complex(rand_int, 0)

    @test (Complex(rand_int, 4) == Rational(rand_int)) == false
    @test (Rational(rand_int) == Complex(rand_int, 4)) == false

    @test trunc(Rational(BigInt(rand_int), BigInt(3))) == Rational(trunc(BigInt, Rational(BigInt(rand_int),BigInt(3))))
    @test  ceil(Rational(BigInt(rand_int), BigInt(3))) == Rational( ceil(BigInt, Rational(BigInt(rand_int),BigInt(3))))
    @test round(Rational(BigInt(rand_int), BigInt(3))) == Rational(round(BigInt, Rational(BigInt(rand_int),BigInt(3))))


    for a = -3:3
        @test Rational(Float32(a)) == Rational(a)
        @test Rational(a)//2 == a//2
        @test a//Rational(2) == Rational(a/2)
        @test a.//[-2, -1, 1, 2] == [-a//2, -a//1, a//1, a//2]
        for b=-3:3, c=1:3
            @test b//(a+c*im) == b*a//(a^2+c^2)-(b*c//(a^2+c^2))*im
            for d=-3:3
                @test (a+b*im)//(c+d*im) == (a*c+b*d+(b*c-a*d)*im)//(c^2+d^2)