Skip to content
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

Make Aqua.test_all(FixedPointDecimal) pass #104

Merged
merged 13 commits into from
Jan 23, 2025
10 changes: 7 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
name = "FixedPointDecimals"
uuid = "fb4d412d-6eee-574d-9565-ede6634db7b0"
authors = ["Fengyang Wang <[email protected]>", "Curtis Vogt <[email protected]>"]
version = "0.5.4"
version = "0.5.5"

[deps]
BitIntegers = "c3b6d118-76ef-56ca-8cc7-ebb389d030a1"
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[compat]
Parsers = "2.7"
Aqua = "0.7"
BitIntegers = "0.3.1"
Parsers = "2.7"
Printf = "1.6"
Test = "1.6"
julia = "1.6"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Printf", "Test"]
test = ["Aqua", "Printf", "Test"]
20 changes: 20 additions & 0 deletions src/FixedPointDecimals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

module FixedPointDecimals

using Base: checked_abs, checked_add, checked_cld, checked_div, checked_fld,
checked_mod, checked_mul, checked_neg, checked_rem, checked_sub

export FixedDecimal, RoundThrows

# (Re)export checked_* arithmetic functions
Expand Down Expand Up @@ -158,6 +161,10 @@
reinterpret(FD{typeof(i), f}, i)
end
Base.widemul(x::Integer, y::FD) = widemul(y, x)
# Need to avoid ambiguity:
Base.widemul(x::Bool, y::FD) = widemul(y, x)
# Need to avoid ambiguity:
Base.widemul(x::FD{T, f}, y::Bool) where {T, f} = widemul(x, Int(y))

"""
_round_to_nearest(quotient, remainder, divisor, ::RoundingMode{M})
Expand Down Expand Up @@ -289,6 +296,10 @@
val = _apply_exact_float($(Symbol(fn, "mul")), T, x, powt)
reinterpret(FD{T, f}, val)
end
# needed to avoid ambiguity
@eval function Base.$fn(::Type{FD{T, f}}, x::Rational) where {T, f}
reinterpret(FD{T, f}, $fn(T, x * coefficient(FD{T, f})))
end
end
function Base.round(::Type{TI}, x::FD, m::RoundingMode=RoundNearest) where {TI <: Integer}
convert(TI, round(x,m))::TI
Expand All @@ -301,6 +312,13 @@
function Base.round(::Type{FD{T, f}}, x::Rational, ::RoundingMode{:Nearest}=RoundNearest) where {T, f}
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
end
function Base.round(::Type{FD{T, f}}, x::Rational{Bool}, ::RoundingMode{:Nearest}=RoundNearest) where {T, f}
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))

Check warning on line 316 in src/FixedPointDecimals.jl

View check run for this annotation

Codecov / codecov/patch

src/FixedPointDecimals.jl#L315-L316

Added lines #L315 - L316 were not covered by tests
end
function Base.round(::Type{FD{T, f}}, x::Rational{Tr}, ::RoundingMode{:Nearest}=RoundNearest) where {T, f, Tr}
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))

Check warning on line 319 in src/FixedPointDecimals.jl

View check run for this annotation

Codecov / codecov/patch

src/FixedPointDecimals.jl#L318-L319

Added lines #L318 - L319 were not covered by tests
end


# conversions and promotions
Base.convert(::Type{FD{T, f}}, x::FD{T, f}) where {T, f} = x # Converting an FD to itself is a no-op
Expand Down Expand Up @@ -562,6 +580,8 @@
end

(::Type{T})(x::FD) where {T<:Union{AbstractFloat,Integer,Rational}} = convert(T, x)
# Need to avoid ambiguity:
Bool(x::FD) = convert(Bool, x)

Base.promote_rule(::Type{FD{T, f}}, ::Type{<:Integer}) where {T, f} = FD{T, f}
Base.promote_rule(::Type{<:FD}, ::Type{TF}) where {TF <: AbstractFloat} = TF
Expand Down
2 changes: 1 addition & 1 deletion src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ end
# the scaling means padding the backing integer with zeros or rounding them as necessary.
# We overloaded the "scale" and "noscale" methods to produce backing integers for FixedDecimals.
# We return a value of T -- i.e. the _integer_ backing the FixedDecimal, the reintrpret needs to happen later
@inline function Parsers.typeparser(conf::FixedDecimalConf{T}, source, pos, len, b, code, pl, options) where {T<:Integer}
@inline function Parsers.typeparser(conf::FixedDecimalConf{T}, source, pos, len, b, code, pl, options::Parsers.Options) where {T<:Integer}
if !(options.rounding in (nothing, RoundNearest, RoundToZero, RoundThrows))
throw(ArgumentError("Unhandled rounding mode $(options.rounding)"))
end
Expand Down
12 changes: 12 additions & 0 deletions test/FixedDecimal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1484,3 +1484,15 @@ end
@test hash(FD2(1//10)) ≠ hash(0.1)
end


@testset "ambiguities" begin
# Unit tests for the methods added to resolve Aqua-detected ambiguities.
@test widemul(true, FD3(1.5)) == FD3(1.5)
@test widemul(FD3(1.5), true) == FD3(1.5)
@test trunc(FD3, 4//3) == FD3(1.333)
@test floor(FD3, 4//3) == FD3(1.333)
@test ceil(FD3, 4//3) == FD3(1.334)
@test round(FD3, true) == FD3(1.000)
@test round(FD3, 4//3) == FD3(1.333)
@test Bool(FixedDecimal{Int,4}(1))
end
4 changes: 4 additions & 0 deletions test/aqua_test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@testitem "Aqua" begin
kuszmaul marked this conversation as resolved.
Show resolved Hide resolved
using Aqua
Aqua.test_all(FixedPointDecimal)
end
kuszmaul marked this conversation as resolved.
Show resolved Hide resolved
Loading