Skip to content

Commit

Permalink
Implement checked operations on Bool
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Feb 29, 2016
1 parent d7b04be commit 2e57e31
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
2 changes: 1 addition & 1 deletion base/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ checked_mod{T<:Integer}(x::T, y::T) = no_op_err("checked_mod", T)
checked_cld{T<:Integer}(x::T, y::T) = no_op_err("checked_cld", T)

typealias SignedInt Union{Int8,Int16,Int32,Int64,Int128}
typealias UnsignedInt Union{UInt8,UInt16,UInt32,UInt64,UInt128}
typealias UnsignedInt Union{UInt8,UInt16,UInt32,UInt64,UInt128, Bool}

# LLVM has several code generation bugs for checked integer arithmetic (see e.g.
# #4905). We thus distinguish between operations that can be implemented via
Expand Down
47 changes: 27 additions & 20 deletions test/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Base: checked_abs, checked_neg, checked_add, checked_sub, checked_mul,
checked_div, checked_rem, checked_fld, checked_mod, checked_cld

# Test return types
for T in (Int8,Int16,Int32,Int64,Int128, UInt8,UInt16,UInt32,UInt64,UInt128)
for T in (Int8,Int16,Int32,Int64,Int128, UInt8,UInt16,UInt32,UInt64,UInt128,Bool)
z, o = T(0), T(1)
@test typeof(checked_neg(z)) === T
@test typeof(checked_abs(z)) === T
Expand Down Expand Up @@ -140,49 +140,56 @@ for T in (Int8, Int16, Int32, Int64, Int128)
@test_throws DivideError checked_cld(typemin(T), T(-1))
end

for T in (UInt8, UInt16, UInt32, UInt64, UInt128)
for T in (UInt8, UInt16, UInt32, UInt64, UInt128, Bool)
# regular cases
@test checked_abs(T(0)) === T(0)
@test checked_neg(T(0)) === T(0)
@test checked_abs(T(3)) === T(3)
@test_throws OverflowError checked_neg(T(3))

# regular cases
@test checked_add(T(4), T(3)) === T(7)
@test checked_sub(T(4), T(3)) === T(1)
@test checked_mul(T(4), T(3)) === T(12)

# corner cases
halfmax = T(typemax(T)÷2)
halfmax_plus1 = T(halfmax+1)
sqrtmax = T(1) << T(sizeof(T)*4)
if T != Bool
# regular cases
@test checked_abs(T(3)) === T(3)
@test_throws OverflowError checked_neg(T(3))

# regular cases
@test checked_add(T(4), T(3)) === T(7)
@test checked_sub(T(4), T(3)) === T(1)
@test checked_mul(T(4), T(3)) === T(12)

# corner cases
halfmax = T(typemax(T)÷2)
halfmax_plus1 = T(halfmax+1)
sqrtmax = T(1) << T(sizeof(T)*4)

@test_throws OverflowError checked_add(typemax(T), T(1))
@test_throws OverflowError checked_add(T(1), typemax(T))
@test_throws OverflowError checked_add(halfmax_plus1, halfmax_plus1)
@test_throws OverflowError checked_add(halfmax_plus1, halfmax_plus1)

@test_throws OverflowError checked_sub(T(0), T(1))
@test_throws OverflowError checked_sub(T(0), typemax(T))
@test_throws OverflowError checked_sub(T(1), typemax(T))
@test_throws OverflowError checked_mul(sqrtmax, sqrtmax)
end

@test checked_add(typemax(T), T(0)) === typemax(T)
@test checked_add(T(0), T(0)) === T(0)
@test_throws OverflowError checked_add(typemax(T), T(1))
@test checked_add(T(0), T(1)) === T(T(0) + 1)
@test checked_add(T(0), typemax(T)) === typemax(T)
@test checked_add(T(0), T(0)) === T(0)
@test_throws OverflowError checked_add(T(1), typemax(T))
@test checked_add(T(1), T(0)) === T(T(0) + 1)
@test checked_add(typemax(T), T(0)) === typemax(T)
@test checked_add(T(0), typemax(T)) === typemax(T)
@test_throws OverflowError checked_add(halfmax_plus1, halfmax_plus1)

@test checked_sub(typemax(T), T(0)) === typemax(T)
@test checked_sub(typemax(T), T(1)) === T(typemax(T) - 1)
@test checked_sub(T(0), T(0)) === T(0)
@test_throws OverflowError checked_sub(T(0), T(1))
@test_throws OverflowError checked_sub(T(0), typemax(T))
@test_throws OverflowError checked_sub(T(1), typemax(T))
@test checked_sub(T(0), T(0)) === T(0)
@test checked_sub(typemax(T), typemax(T)) === T(0)

@test checked_mul(typemax(T), T(0)) === T(0)
@test checked_mul(T(0), T(0)) === T(0)
@test checked_mul(typemax(T), T(1)) === typemax(T)
@test checked_mul(T(0), T(1)) === T(0)
@test_throws OverflowError checked_mul(sqrtmax, sqrtmax)

@test checked_div(typemax(T), T(1)) === typemax(T)
@test_throws DivideError checked_div(typemax(T), T(0))
Expand Down

0 comments on commit 2e57e31

Please sign in to comment.