Skip to content

Commit

Permalink
Update index checkbounds API
Browse files Browse the repository at this point in the history
Now formally supported and documented due to JuliaLang/julia#11895. Note: this does not compat-ably version the change.
  • Loading branch information
mbauman committed Aug 26, 2015
1 parent 6e48730 commit a55ea88
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
13 changes: 7 additions & 6 deletions src/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,21 @@ end

# ----- Base._checkbounds ----------------------------------------------------#

function Base._checkbounds{T<:Real}(sz::Int, x::Nullable{T})
isnull(x) ? throw(NullException()) : Base._checkbounds(sz, get(x))
function Base.checkbounds{T<:Real}(::Type{Bool}, sz::Int, x::Nullable{T})
isnull(x) ? throw(NullException()) : checkbounds(Bool, sz, get(x))
end

function Base._checkbounds(sz::Int, I::NullableVector{Bool})
length(I) == sz || throw(BoundsError())
function Base.checkbounds(::Type{Bool}, sz::Int, I::NullableVector{Bool})
anynull(I) && throw(NullException())
length(I) == sz
end

function Base._checkbounds{T<:Real}(sz::Int, I::NullableArray{T})
function Base.checkbounds{T<:Real}(::Type{Bool}, sz::Int, I::NullableArray{T})
inbounds = true
anynull(I) && throw(NullException())
for i in 1:length(I)
@inbounds v = unsafe_getvalue_notnull(I, i)
inbounds &= Base._checkbounds(sz, v)
inbounds &= checkbounds(Bool, sz, v)
end
return inbounds
end
Expand Down
24 changes: 9 additions & 15 deletions test/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module TestIndexing
using NullableArrays
import NullableArrays: unsafe_getindex_notnull,
unsafe_getvalue_notnull
import Base: _checkbounds

x = NullableArray(Int, (5, 2))

Expand Down Expand Up @@ -170,28 +169,23 @@ module TestIndexing
@test isequal(unsafe_getvalue_notnull(X, 1), 1)
@test isequal(unsafe_getvalue_notnull(X, 2), 2)

#----- test Base._checkbounds -----#
#----- test Base.checkbounds -----#

X = NullableArray([1:10...])
b = vcat(false, fill(true, 9))

# Base._checkbounds{T<:Real}(sz::Int, x::Nullable{T})
@test_throws NullException _checkbounds(1, Nullable(1, true))
@test _checkbounds(10, Nullable(1)) == true
# Base.checkbounds{T<:Real}(::Type{Bool}, sz::Int, x::Nullable{T})
@test_throws NullException checkbounds(Bool, 1, Nullable(1, true))
@test checkbounds(Bool, 10, Nullable(1)) == true
@test isequal(X[Nullable(1)], Nullable(1))

# Base._checkbounds(sz::Int, X::NullableVector{Bool})
@test _checkbounds(5, NullableArray([true, false, true, false, true]))
@test_throws(BoundsError,
_checkbounds(5,
NullableArray([true, false, true, true])
)
)
# Base.checkbounds(::Type{Bool}, sz::Int, X::NullableVector{Bool})
@test checkbounds(Bool, 5, NullableArray([true, false, true, false, true]))
@test isequal(X[b], NullableArray([2:10...]))

# Base._checkbounds{T<:Real}(sz::Int, I::NullableArray{T})
@test _checkbounds(10, NullableArray([1:10...]))
@test _checkbounds(10, NullableArray([10, 11])) == false
# Base.checkbounds{T<:Real}(::Type{Bool}, sz::Int, I::NullableArray{T})
@test checkbounds(Bool, 10, NullableArray([1:10...]))
@test checkbounds(Bool, 10, NullableArray([10, 11])) == false
@test_throws BoundsError checkbounds(X, NullableArray([10, 11]))

#---- test Base.to_index -----#
Expand Down

0 comments on commit a55ea88

Please sign in to comment.