From 2e57e31f62c7e030fb898a962fd9d705d633e77f Mon Sep 17 00:00:00 2001 From: Milan Bouchet-Valat Date: Mon, 29 Feb 2016 19:58:20 +0100 Subject: [PATCH] Implement checked operations on Bool --- base/checked.jl | 2 +- test/checked.jl | 47 +++++++++++++++++++++++++++-------------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/base/checked.jl b/base/checked.jl index a5fef27089e44..3dc39f25f9056 100644 --- a/base/checked.jl +++ b/base/checked.jl @@ -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 diff --git a/test/checked.jl b/test/checked.jl index f685c5c724376..d8563d5d3e9c4 100644 --- a/test/checked.jl +++ b/test/checked.jl @@ -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 @@ -140,41 +140,49 @@ 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) @@ -182,7 +190,6 @@ for T in (UInt8, UInt16, UInt32, UInt64, UInt128) @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))