Skip to content

Check and fix the sumsquares demo #2

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

Merged
merged 2 commits into from
Nov 26, 2019
Merged
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
23 changes: 14 additions & 9 deletions src/CheckedArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ macro check(expr)
for i = 2:length(expr.args)
safeexpr.args[i] = Expr(:call, :(CheckedArithmetic.safearg), expr.args[i])
end
return esc(quote
val = $expr
valcmp = CheckedArithmetic.safeconvert(typeof(val), $safeexpr)
@test val == valcmp
return val
end)
return quote
local val = $(esc(expr))
local valcmp = CheckedArithmetic.safeconvert(typeof(val), $(esc(safeexpr)))
if ismissing(val) && ismissing(valcmp)
val
else
val == valcmp || error(val, " is not equal to ", valcmp)
val
end
end
end

"""
Expand Down Expand Up @@ -172,12 +176,12 @@ safearg(ref::Ref) = Ref(safearg(ref[]))
safearg(d::Dict) = Dict(safearg(p) for p in d)
safearg(d::Base.EnvDict) = d
safearg(d::Base.ImmutableDict) = Base.ImmutableDict(safearg(p) for p in d)
safearg(d::Iterators.Pairs) = Iterators.Pairs(safearg(p) for p in d)
safearg(d::Iterators.Pairs) = Iterators.Pairs(safearg(d.data), d.itr)
safearg(d::IdDict) = IdDict(safearg(p) for p in d)
safearg(d::WeakKeyDict) = WeakKeyDict(k=>safearg(v) for (k, v) in d) # do not convert keys
## AbstractSets
safearg(s::Set) = Set(f(x) for x in s)
safearg(s::Base.IdSet) = Base.IdSet(f(x) for x in s)
safearg(s::Set) = Set(safearg(x) for x in s)
safearg(s::Base.IdSet) = Base.IdSet(safearg(x) for x in s)
safearg(s::BitSet) = s

# Other common types
Expand Down Expand Up @@ -243,6 +247,7 @@ Base.@pure accumulatortype(op::Function, T1::Type, T2::Type, T3::Type...) =
accumulatortype(op, promote_type(T1, T2, T3...))
Base.@pure accumulatortype(T1::Type, T2::Type, T3::Type...) =
accumulatortype(*, T1, T2, T3...)
accumulatortype(::Type{T}) where T = accumulatortype(*, T)

const SignPreserving = Union{typeof(+), typeof(*)}
const ArithmeticOp = Union{SignPreserving,typeof(-)}
Expand Down
18 changes: 14 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using CheckedArithmetic
using Test
using Test, Dates

@test isempty(detect_ambiguities(CheckedArithmetic, Base, Core))

Expand All @@ -8,6 +8,14 @@ using Test
minus(x, y) = x - y
end

function sumsquares(A::AbstractArray)
s = zero(accumulatortype(eltype(A)))
for a in A
s += acc(a)^2
end
return s
end

@testset "CheckedArithmetic.jl" begin
@testset "@checked" begin
@test @checked(abs(Int8(-2))) === Int8(2)
Expand Down Expand Up @@ -41,7 +49,7 @@ end
@test_throws OverflowError minus(0x20, 0x30)
end

@testset "check" begin
@testset "@check" begin
@test @check(3+5) == 8
@test_throws InexactError @check(0xf0+0x15)
@test @check([3]+[5]) == [8]
Expand All @@ -54,10 +62,10 @@ end
@test @check(times2(Dict("a"=>7))) == Dict("a"=>14)
@test_throws InexactError @check(times2(Dict("a"=>0xf0)))
for item in Any["hi", :hi, (3,), (silly="hi",), trues(3), [true], 1:3, 1:2:5,
LinRange(1, 3, 3), StepRangeLen(1.0, 3.0, 3), 0x01:0x03, Ref(2),
LinRange(1, 3, 3), StepRangeLen(1.0, 3.0, 3), 0x01:0x03,
pairs((silly="hi",)), Set([1,3]), BitSet(7), nothing, missing,
Some(nothing), 'c', MIME("text/plain"), IOBuffer(), r"\d+",
Channel(), CartesianIndex(1, 3), Base.UUID(0), `ls`,
Channel(7), CartesianIndex(1, 3), Base.UUID(0), `ls`,
sum, Base, Val(3), Task(()->1), Base.Order.Forward, Timer(0.1),
now()]
@test @check(identity(item)) === item
Expand All @@ -69,5 +77,7 @@ end
@test acc(-, 0x02) === 2
@test accumulatortype(-, UInt8) === Int
@test accumulatortype(*, Int16, Float16) === Float64

@test sumsquares(0x00:0xff) == sumsquares(0:255)
end
end