Skip to content

Compose SimpleRatio and SaferIntegers #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Ratios.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ function __init__()
SimpleRatio(x::FixedPoint) = SimpleRatio(reinterpret(x), rawone_noerr(x))
Base.convert(::Type{S}, x::FixedPoint) where S<:SimpleRatio = S(x)
end
@require SaferIntegers = "88634af6-177f-5301-88b8-7819386cfa38" begin
using .SaferIntegers: SafeSigned, SafeUnsigned
-(x::SimpleRatio{T}) where {T<:SafeSigned} = SimpleRatio(-x.num, x.den)
-(x::SimpleRatio{T}) where {T<:SafeUnsigned} = throw(VERSION < v"0.7.0-DEV.1269" ? OverflowError() : OverflowError("cannot negate unsigned number"))
end
end

end
32 changes: 32 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Ratios, Test
using FixedPointNumbers
using SaferIntegers

@testset "SimpleRatio" begin
r = SimpleRatio(1,2)
Expand Down Expand Up @@ -42,4 +43,35 @@ using FixedPointNumbers
@test SimpleRatio(5,3) * -0.03Q0f7 == SimpleRatio{Int}(rationalize((5.0*(-0.03Q0f7))/3))
r = @inferred(SimpleRatio(0.75Q0f7))
@test r == 3//4 && r isa SimpleRatio{Int16}

@testset "SimpleRatio and SaferIntegers" begin
@test_throws OverflowError Ratios.SimpleRatio{SafeInt64}(99980001, 99980001) + Ratios.SimpleRatio{SafeInt64}(999800010000, 99980002)
@test -Ratios.SimpleRatio{SafeInt}(1,5) == Ratios.SimpleRatio{SafeInt}(-1,5)
@test_throws OverflowError -Ratios.SimpleRatio{SafeUInt}(1,5) == Ratios.SimpleRatio{SafeInt}(-1,5)
let a_den = 5, b_den = 255
@test b_den % a_den == 0
correct_numerator = b_den ÷ a_den + 1

# The addition overflows and the sum is wrong
T = UInt8
ST = SaferIntegers.safeint(T)
@test SimpleRatio{T}(1, a_den) + SimpleRatio{T}(1, b_den) == SimpleRatio{T}(correct_numerator, b_den)
@test convert(Float64, SimpleRatio{T}(1, a_den) + SimpleRatio{T}(1, b_den) ) != convert(Float64, SimpleRatio{T}(correct_numerator, b_den))
@test_throws OverflowError SimpleRatio{ST}(1, a_den) + SimpleRatio{ST}(1, b_den) == SimpleRatio{ST}(correct_numerator, b_den)

# The addition works, but checking equality overflows
T = UInt16
ST = SaferIntegers.safeint(T)
@test SimpleRatio{T}(1, a_den) + SimpleRatio{T}(1, b_den) == SimpleRatio{T}(correct_numerator, b_den)
@test convert(Float64, SimpleRatio{T}(1, a_den) + SimpleRatio{T}(1, b_den) ) == convert(Float64, SimpleRatio{T}(correct_numerator, b_den))
@test_throws OverflowError SimpleRatio{ST}(1, a_den) + SimpleRatio{ST}(1, b_den) == SimpleRatio{ST}(correct_numerator, b_den)

# No overflow, everything works
T = UInt32
ST = SaferIntegers.safeint(T)
@test SimpleRatio{T}(1, a_den) + SimpleRatio{T}(1, b_den) == SimpleRatio{T}(correct_numerator, b_den)
@test convert(Float64, SimpleRatio{T}(1, a_den) + SimpleRatio{T}(1, b_den) ) == convert(Float64, SimpleRatio{T}(correct_numerator, b_den))
@test SimpleRatio{ST}(1, a_den) + SimpleRatio{ST}(1, b_den) == SimpleRatio{ST}(correct_numerator, b_den)
end
end
end