Skip to content

Commit 20c21c5

Browse files
Make Aqua.test_all(FixedPointDecimal) pass (#104)
* Remove ambiguity for Bool(::FD) * Remove ambiguity for trunc floor ceil * Aqua.test_all(FixedPointDecimals) passes * Update Project.toml Co-authored-by: Nick Robinson <npr251@gmail.com> * 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 <npr251@gmail.com>
1 parent e36a42f commit 20c21c5

File tree

6 files changed

+45
-4
lines changed

6 files changed

+45
-4
lines changed

Project.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
name = "FixedPointDecimals"
22
uuid = "fb4d412d-6eee-574d-9565-ede6634db7b0"
33
authors = ["Fengyang Wang <fengyang.wang.0@gmail.com>", "Curtis Vogt <curtis.vogt@gmail.com>"]
4-
version = "0.5.4"
4+
version = "0.5.5"
55

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

1010
[compat]
11-
Parsers = "2.7"
11+
Aqua = "0.7, 0.8"
1212
BitIntegers = "0.3.1"
13+
Parsers = "2.7"
14+
Printf = "1.6"
15+
Test = "1.6"
1316
julia = "1.6"
1417

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

1923
[targets]
20-
test = ["Printf", "Test"]
24+
test = ["Aqua", "Printf", "Test"]

src/FixedPointDecimals.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ __precompile__()
2525

2626
module FixedPointDecimals
2727

28+
using Base: checked_abs, checked_add, checked_cld, checked_div, checked_fld,
29+
checked_mod, checked_mul, checked_neg, checked_rem, checked_sub
30+
2831
export FixedDecimal, RoundThrows
2932

3033
# (Re)export checked_* arithmetic functions
@@ -158,6 +161,10 @@ function Base.widemul(x::FD{T, f}, y::Integer) where {T, f}
158161
reinterpret(FD{typeof(i), f}, i)
159162
end
160163
Base.widemul(x::Integer, y::FD) = widemul(y, x)
164+
# Need to avoid ambiguity:
165+
Base.widemul(x::Bool, y::FD) = widemul(y, x)
166+
# Need to avoid ambiguity:
167+
Base.widemul(x::FD{T, f}, y::Bool) where {T, f} = widemul(x, Int(y))
161168

162169
"""
163170
_round_to_nearest(quotient, remainder, divisor, ::RoundingMode{M})
@@ -289,6 +296,10 @@ for fn in [:trunc, :floor, :ceil]
289296
val = _apply_exact_float($(Symbol(fn, "mul")), T, x, powt)
290297
reinterpret(FD{T, f}, val)
291298
end
299+
# needed to avoid ambiguity
300+
@eval function Base.$fn(::Type{FD{T, f}}, x::Rational) where {T, f}
301+
reinterpret(FD{T, f}, $fn(T, x * coefficient(FD{T, f})))
302+
end
292303
end
293304
function Base.round(::Type{TI}, x::FD, m::RoundingMode=RoundNearest) where {TI <: Integer}
294305
convert(TI, round(x,m))::TI
@@ -301,6 +312,13 @@ end
301312
function Base.round(::Type{FD{T, f}}, x::Rational, ::RoundingMode{:Nearest}=RoundNearest) where {T, f}
302313
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
303314
end
315+
function Base.round(::Type{FD{T, f}}, x::Rational{Bool}, ::RoundingMode{:Nearest}=RoundNearest) where {T, f}
316+
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
317+
end
318+
function Base.round(::Type{FD{T, f}}, x::Rational{Tr}, ::RoundingMode{:Nearest}=RoundNearest) where {T, f, Tr}
319+
reinterpret(FD{T, f}, round(T, x * coefficient(FD{T, f})))
320+
end
321+
304322

305323
# conversions and promotions
306324
Base.convert(::Type{FD{T, f}}, x::FD{T, f}) where {T, f} = x # Converting an FD to itself is a no-op
@@ -562,6 +580,8 @@ function Base.convert(::Type{TR}, x::FD{T, f}) where {TR <: Rational, T, f}
562580
end
563581

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

566586
Base.promote_rule(::Type{FD{T, f}}, ::Type{<:Integer}) where {T, f} = FD{T, f}
567587
Base.promote_rule(::Type{<:FD}, ::Type{TF}) where {TF <: AbstractFloat} = TF

src/parse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ end
172172
# the scaling means padding the backing integer with zeros or rounding them as necessary.
173173
# We overloaded the "scale" and "noscale" methods to produce backing integers for FixedDecimals.
174174
# We return a value of T -- i.e. the _integer_ backing the FixedDecimal, the reintrpret needs to happen later
175-
@inline function Parsers.typeparser(conf::FixedDecimalConf{T}, source, pos, len, b, code, pl, options) where {T<:Integer}
175+
@inline function Parsers.typeparser(conf::FixedDecimalConf{T}, source, pos, len, b, code, pl, options::Parsers.Options) where {T<:Integer}
176176
if !(options.rounding in (nothing, RoundNearest, RoundToZero, RoundThrows))
177177
throw(ArgumentError("Unhandled rounding mode $(options.rounding)"))
178178
end

test/FixedDecimal.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1484,3 +1484,15 @@ end
14841484
@test hash(FD2(1//10)) hash(0.1)
14851485
end
14861486

1487+
1488+
@testset "ambiguities" begin
1489+
# Unit tests for the methods added to resolve Aqua-detected ambiguities.
1490+
@test widemul(true, FD3(1.5)) == FD3(1.5)
1491+
@test widemul(FD3(1.5), true) == FD3(1.5)
1492+
@test trunc(FD3, 4//3) == FD3(1.333)
1493+
@test floor(FD3, 4//3) == FD3(1.333)
1494+
@test ceil(FD3, 4//3) == FD3(1.334)
1495+
@test round(FD3, true) == FD3(1.000)
1496+
@test round(FD3, 4//3) == FD3(1.333)
1497+
@test Bool(FixedDecimal{Int,4}(1))
1498+
end

test/aqua_test.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@testset "Aqua" begin
2+
using Aqua
3+
Aqua.test_all(FixedPointDecimals)
4+
end

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ include(joinpath(pkg_path, "test", "utils.jl"))
1010

1111
@testset "FixedPointDecimals" begin
1212
include("FixedDecimal.jl")
13+
include("aqua_test.jl")
1314
end # global testset

0 commit comments

Comments
 (0)