Skip to content
This repository was archived by the owner on May 4, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the semantics here (beyond just the rename)… but I'm pretty sure that this is actually what you want.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this is what's wanted.

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