Skip to content

Commit

Permalink
Updated tests to handle ArgumentError instead of BoundsError when pas…
Browse files Browse the repository at this point in the history
…sed iterable

More specific exceptions for test_throws

Fix dangling right parenthesis
  • Loading branch information
ScottPJones committed Jul 10, 2015
1 parent 1d97054 commit 2a1105c
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
65 changes: 40 additions & 25 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,25 @@ function copy!(dest::AbstractArray, doffs::Integer, src)
return dest
end

# copy from an some iterable object into an AbstractArray
function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer)
if (doffs < 1) | (soffs < 1)
doffs < 1 && throw(BoundsError(dest, doffs))
throw(BoundsError(src, soffs))
end
st = start(src)
for j = 1:(soffs-1)
done(src, st) && throw(BoundsError(src, j))
if done(src, st)
throw(ArgumentError(string("source has fewer elements than required, ",
"expected at least ",soffs,", got ",j-1)))
end
_, st = next(src, st)
end
dn = done(src, st)
dn && throw(BoundsError(src))
if dn
throw(ArgumentError(string("source has fewer elements than required, ",
"expected at least ",soffs,", got ",soffs-1)))
end
i, dmax = doffs, length(dest)
@inbounds while !dn
i > dmax && throw(BoundsError(dest, i))
Expand All @@ -270,7 +277,10 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer, n::Inte
end
st = start(src)
for j = 1:(soffs-1)
done(src, st) && throw(BoundsError(src, j))
if done(src, st)
throw(ArgumentError(string("source has fewer elements than required, ",
"expected at least ",soffs,", got ",j-1)))
end
_, st = next(src, st)
end
i = doffs
Expand Down Expand Up @@ -307,14 +317,12 @@ end
function copy!(dest::AbstractArray, doffs::Integer,
src::AbstractArray, soffs::Integer,
n::Integer)
n < 0 && throw(BoundsError(src, n))
n == 0 && return dest
if soffs+n-1 > length(src) || doffs+n-1 > length(dest) || doffs < 1 || soffs < 1
soffs+n-1 > length(src) && throw(BoundsError(src, soffs+n-1))
doffs+n-1 > length(dest) && throw(BoundsError(dest, doffs+n-1))
doffs < 1 && throw(BoundsError(dest, doffs))
throw(BoundsError(src, soffs))
end
n < 0 && throw(BoundsError(src, n))
soffs+n-1 > length(src) && throw(BoundsError(src, soffs+n-1))
doffs+n-1 > length(dest) && throw(BoundsError(dest, doffs+n-1))
doffs < 1 && throw(BoundsError(dest, doffs))
soffs < 1 && throw(BoundsError(src, soffs))
@inbounds for i = 0:(n-1)
dest[doffs+i] = src[soffs+i]
end
Expand All @@ -325,10 +333,13 @@ copy(a::AbstractArray) = copy!(similar(a), a)

function copy!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_dest::Range{Int},
A::AbstractVecOrMat{S}, ir_src::Range{Int}, jr_src::Range{Int})
if length(ir_dest) != length(ir_src) || length(jr_dest) != length(jr_src)
length(ir_dest) != length(ir_src) &&
throw(ArgumentError("source and destination must have same size (got $(length(ir_src)) and $(length(ir_dest)))"))
throw(ArgumentError("source and destination must have same size (got $(length(jr_src)) and $(length(jr_dest)))"))
if length(ir_dest) != length(ir_src)
throw(ArgumentError(string("source and destination must have same size (got ",
length(ir_src)," and ",length(ir_dest),")")))
end
if length(jr_dest) != length(jr_src)
throw(ArgumentError(string("source and destination must have same size (got ",
length(jr_src)," and ",length(jr_dest),")")))
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
Expand All @@ -346,10 +357,13 @@ end

function copy_transpose!{R,S}(B::AbstractVecOrMat{R}, ir_dest::Range{Int}, jr_dest::Range{Int},
A::AbstractVecOrMat{S}, ir_src::Range{Int}, jr_src::Range{Int})
if length(ir_dest) != length(jr_src) || length(jr_dest) != length(ir_src)
length(ir_dest) != length(jr_src) &&
throw(ArgumentError("source and destination must have same size (got $(length(jr_src)) and $(length(ir_dest)))"))
throw(ArgumentError("source and destination must have same size (got $(length(ir_src)) and $(length(jr_dest)))"))
if length(ir_dest) != length(jr_src)
throw(ArgumentError(string("source and destination must have same size (got ",
length(jr_src)," and ",length(ir_dest),")")))
end
if length(jr_dest) != length(ir_src)
throw(ArgumentError(string("source and destination must have same size (got ",
length(ir_src)," and ",length(jr_dest),")")))
end
checkbounds(B, ir_dest, jr_dest)
checkbounds(A, ir_src, jr_src)
Expand Down Expand Up @@ -711,7 +725,7 @@ function hcat{T}(A::AbstractVecOrMat{T}...)
for j = 1:nargs
Aj = A[j]
if size(Aj, 1) != nrows
throw(ArgumentError("number of rows of arrays must match (got $(map(x->size(x,1), A)))"))
throw(ArgumentError("number of rows of each array must match (got $(map(x->size(x,1), A)))"))
end
dense &= isa(Aj,Array)
nd = ndims(Aj)
Expand Down Expand Up @@ -743,7 +757,7 @@ function vcat{T}(A::AbstractMatrix{T}...)
ncols = size(A[1], 2)
for j = 2:nargs
if size(A[j], 2) != ncols
throw(ArgumentError("number of columns of arrays must match (got $(map(x->size(x,2), A)))"))
throw(ArgumentError("number of columns of each array must match (got $(map(x->size(x,2), A)))"))
end
end
B = similar(full(A[1]), nrows, ncols)
Expand Down Expand Up @@ -782,12 +796,13 @@ function cat_t(catdims, typeC::Type, X...)
for i = 2:nargs
for d = 1:ndimsC
currentdim = (d <= ndimsX[i] ? size(X[i],d) : 1)
if dims2cat[d] == 0
dimsC[d] == currentdim ||
throw(DimensionMismatch("mismatch in dimension $(d) (expected $currentdim, got $(dimsC[d]))"))
else
if dims2cat[d] != 0
dimsC[d] += currentdim
catsizes[i,dims2cat[d]] = currentdim
elseif dimsC[d] != currentdim
throw(DimensionMismatch(string("mismatch in dimension ",d,
" (expected ",dimsC[d],
" got ",currentdim,")")))
end
end
end
Expand Down Expand Up @@ -1053,7 +1068,7 @@ function sub2ind{T<:Integer}(dims::Tuple{Vararg{Integer}}, I::AbstractVector{T}.
for i=1:M
indices[i] += s*(Ij[i]-1)
end
s*= (j <= N ? dims[j] : 1)
s *= (j <= N ? dims[j] : 1)
end
return indices
end
Expand Down
14 changes: 10 additions & 4 deletions test/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@ for (dest, src, bigsrc, emptysrc, res) in [
@test copy!(copy(dest), 99, src(), 99, 0) == dest

@test copy!(copy(dest), 1, emptysrc()) == dest
@test_throws BoundsError copy!(dest, 1, emptysrc(), 1)
x = emptysrc()
exc = isa(x, AbstractArray) ? BoundsError : ArgumentError
@test_throws exc copy!(dest, 1, emptysrc(), 1)

for idx in [0, 4]
for idx in (0, 4)
@test_throws BoundsError copy!(dest, idx, src())
@test_throws BoundsError copy!(dest, idx, src(), 1)
@test_throws BoundsError copy!(dest, idx, src(), 1, 1)
@test_throws BoundsError copy!(dest, 1, src(), idx)
@test_throws BoundsError copy!(dest, 1, src(), idx, 1)
x = src()
exc = (idx == 4 && isa(x, Task)) ? ArgumentError : BoundsError
@test_throws exc copy!(dest, 1, x, idx)
x = src()
exc = (idx == 4 && isa(x, Task)) ? ArgumentError : BoundsError
@test_throws exc copy!(dest, 1, x, idx, 1)
end

@test_throws BoundsError copy!(dest, 1, src(), 1, -1)
Expand Down

0 comments on commit 2a1105c

Please sign in to comment.