Skip to content

Commit

Permalink
Implement checked operations on Bool
Browse files Browse the repository at this point in the history
Bool behaves almost like unsigned integers, but with special cases
for checked_add() and checked_sub() which require adding methods
and testing separately.
  • Loading branch information
nalimilan committed Feb 29, 2016
1 parent d7b04be commit 58bdac3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 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 Expand Up @@ -166,6 +166,8 @@ function checked_add{T<:BrokenUnsignedInt}(x::T, y::T)
x + y
end
end
checked_add(x::Bool, y::Bool) = x + y
checked_add(x::Bool) = +x

# Handle multiple arguments
checked_add(x) = x
Expand Down Expand Up @@ -212,6 +214,8 @@ function checked_sub{T<:BrokenUnsignedInt}(x::T, y::T)
x - y
end
end
checked_sub(x::Bool, y::Bool) = x - y
checked_sub(x::Bool) = -x

"""
Base.checked_mul(x, y)
Expand Down
44 changes: 44 additions & 0 deletions test/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,50 @@ for T in (UInt8, UInt16, UInt32, UInt64, UInt128)
@test_throws DivideError checked_cld(typemax(T), T(0))
end

# Boolean
@test checked_add(false) === 0
@test checked_add(true) === 1
@test checked_sub(false) === 0
@test checked_sub(true) === -1
@test checked_neg(false) === 0
@test checked_neg(true) === -1
@test checked_abs(true) === true
@test checked_abs(false) === false
@test checked_mul(false) === false
@test checked_mul(true) === true

@test checked_add(true, true) === 2
@test checked_add(true, false) === 1
@test checked_add(false, false) === 0
@test checked_add(false, true) === 1

@test checked_sub(true, true) === 0
@test checked_sub(true, false) === 1
@test checked_sub(false, false) === 0
@test checked_sub(false, true) === -1

@test checked_mul(true, false) === false
@test checked_mul(false, false) === false
@test checked_mul(true, true) === true
@test checked_mul(false, true) === false

@test checked_div(true, true) === true
@test checked_div(false, true) === false
@test_throws DivideError checked_div(true, false)
@test checked_rem(true, true) === false
@test checked_rem(false, true) === false
@test_throws DivideError checked_rem(true, false)
@test checked_fld(true, true) === true
@test checked_fld(false, true) === false
@test_throws DivideError checked_fld(true, false)
@test checked_mod(true, true) === false
@test checked_mod(false, true) === false
@test_throws DivideError checked_mod(true, false)
@test checked_cld(true, true) === true
@test checked_cld(false, true) === false
@test_throws DivideError checked_cld(true, false)

# BigInt
@test checked_abs(BigInt(-1)) == BigInt(1)
@test checked_abs(BigInt(1)) == BigInt(1)
@test checked_neg(BigInt(-1)) == BigInt(1)
Expand Down

0 comments on commit 58bdac3

Please sign in to comment.