Skip to content

Commit

Permalink
get dates tests passing with checked signed<->unsigned conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Sep 19, 2014
1 parent e1690fc commit c67837c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion base/dates/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,4 @@ Base.promote_rule(::Type{Date},x::Type{DateTime}) = DateTime
Base.isless(x::Date,y::Date) = isless(value(x),value(y))
Base.isless(x::DateTime,y::DateTime) = isless(value(x),value(y))
Base.isless(x::TimeType,y::TimeType) = isless(promote(x,y)...)
==(x::TimeType,y::TimeType) = ===(promote(x,y)...)
==(x::TimeType,y::TimeType) = ===(promote(x,y)...)
11 changes: 7 additions & 4 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ abs(x::Signed) = flipsign(x,x)

~(n::Integer) = -n-1

asunsigned(x::Integer) = reinterpret(typeof(unsigned(zero(x))), x)
asunsigned(x) = unsigned(x)
assigned(x::Integer) = reinterpret(typeof(signed(zero(x))), x)
assigned(x) = signed(x)

div(x::Signed, y::Unsigned) = flipsign(assigned(div(asunsigned(abs(x)),y)),x)
div(x::Unsigned, y::Signed) = asunsigned(flipsign(assigned(div(x,asunsigned(abs(y)))),y))

Expand Down Expand Up @@ -132,9 +137,6 @@ for T in IntTypes
end
end

asunsigned(x) = reinterpret(typeof(unsigned(zero(x))), x)
assigned(x) = reinterpret(typeof(signed(zero(x))), x)

==(x::Signed, y::Unsigned) = (x >= 0) & (asunsigned(x) == y)
==(x::Unsigned, y::Signed ) = (y >= 0) & (x == asunsigned(y))
< (x::Signed, y::Unsigned) = (x < 0) | (asunsigned(x) < y)
Expand Down Expand Up @@ -227,7 +229,8 @@ int32(x) = convert(Int32,x)
int64(x) = convert(Int64,x)
int128(x) = convert(Int128,x)

uint8(x) = itrunc(Uint8,x)
uint8(x) = convert(Uint8, x)
uint8(x::Integer) = itrunc(Uint8,x)
uint8(x::Int8) = box(Uint8,unbox(Int8,x))
uint16(x) = convert(Uint16,x)
uint32(x) = convert(Uint32,x)
Expand Down
4 changes: 2 additions & 2 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ immutable StepRange{T,S} <: OrdinalRange{T,S}
if T<:Signed && (diff > zero(diff)) != (stop > start)
# handle overflowed subtraction with unsigned rem
if diff > zero(diff)
remain = -oftype(T, unsigned(-diff) % step)
remain = -oftype(T, asunsigned(-diff) % step)
else
remain = oftype(T, unsigned(diff) % step)
remain = oftype(T, asunsigned(diff) % step)
end
else
remain = steprem(start,stop,step)
Expand Down
3 changes: 3 additions & 0 deletions src/intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ static Value *generic_box(jl_value_t *targ, jl_value_t *x, jl_codectx_t *ctx)
if (vxt != llvmt) {
if (vxt == T_void)
return vx;
if (!vxt->isSingleValueType()) {
jl_error("box: argument not of a primitive type");
}
if (llvmt == T_int1) {
vx = builder.CreateTrunc(vx, llvmt);
}
Expand Down
6 changes: 3 additions & 3 deletions test/dates/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ test = Dates.Date(1,1,1)
@test Dates.Date(false,true,true) == test - Dates.Year(1)
@test_throws ArgumentError Dates.Date(true,true,false)
@test Dates.Date(uint64(1),uint64(1),uint64(1)) == test
@test Dates.Date(0xffffffffffffffff,uint64(1),uint64(1)) == test - Dates.Year(2)
@test Dates.Date(-1,uint64(1),uint64(1)) == test - Dates.Year(2)
@test Dates.Date(int128(1),int128(1),int128(1)) == test
@test Dates.Date(170141183460469231731687303715884105727,int128(1),int128(1)) == test - Dates.Year(2)
@test_throws InexactError Dates.Date(170141183460469231731687303715884105727,int128(1),int128(1))

This comment has been minimized.

Copy link
@quinnj

quinnj Oct 13, 2014

Member

This is due to convert(Int64,x) now throwing an InexactError if x > typemax(Int64); correct?

@test Dates.Date(uint128(1),uint128(1),uint128(1)) == test
@test Dates.Date(big(1),big(1),big(1)) == test
@test Dates.Date(big(1),big(1),big(1)) == test
Expand Down Expand Up @@ -171,4 +171,4 @@ ms = Dates.Millisecond(1)
@test Dates.Date(d,y) == Dates.Date(1,1,1)
@test Dates.Date(d,m) == Dates.Date(1,1,1)
@test Dates.Date(m,y) == Dates.Date(1,1,1)
=#
=#

0 comments on commit c67837c

Please sign in to comment.