Skip to content

Commit

Permalink
Make Aqua.test_all(FixedPointDecimal) pass (#104)
Browse files Browse the repository at this point in the history
* Remove ambiguity for Bool(::FD)

* Remove ambiguity for trunc floor ceil

* Aqua.test_all(FixedPointDecimals) passes

* Update Project.toml

Co-authored-by: Nick Robinson <[email protected]>

* Add aqua tests

* Also test with Aqua 0.8.9

* Unit tests for the methods added to resolve Aqua-detected ambiguities

* Go back to aqua 0.7

* Typo in test

* Get the aqua tests to actually run.  Also compat with aqua 0.8

* Add comment

* Fix up aqua test

* Aqua test cleanup

---------

Co-authored-by: Nick Robinson <[email protected]>
  • Loading branch information
kuszmaul and nickrobinson251 authored Jan 23, 2025
1 parent e36a42f commit 20c21c5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
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, 0.8"
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 @@ __precompile__()

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 @@ function Base.widemul(x::FD{T, f}, y::Integer) where {T, f}
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 @@ for fn in [:trunc, :floor, :ceil]
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 @@ end
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})))
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})))
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 @@ function Base.convert(::Type{TR}, x::FD{T, f}) where {TR <: Rational, T, f}
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 @@
@testset "Aqua" begin
using Aqua
Aqua.test_all(FixedPointDecimals)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ include(joinpath(pkg_path, "test", "utils.jl"))

@testset "FixedPointDecimals" begin
include("FixedDecimal.jl")
include("aqua_test.jl")
end # global testset

2 comments on commit 20c21c5

@nickrobinson251
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/123574

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.5 -m "<description of version>" 20c21c5239babddce722745f9f52065d23df58be
git push origin v0.5.5

Please sign in to comment.