From c9bceae7892880f1d017481118dde048a6bfffe0 Mon Sep 17 00:00:00 2001 From: ScottPJones Date: Thu, 9 Jul 2015 17:53:01 -0400 Subject: [PATCH] Updated tests to handle ArgumentError instead of BoundsError when passed iterable More specific exceptions for test_throws Fix dangling right parenthesis --- base/abstractarray.jl | 65 ++++++++++++++++++++++++++----------------- test/copy.jl | 14 +++++++--- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index d54ab4b85c33b2..a5704ca07ffa97 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -235,6 +235,7 @@ 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)) @@ -242,11 +243,17 @@ function copy!(dest::AbstractArray, doffs::Integer, src, soffs::Integer) 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)) @@ -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 @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 diff --git a/test/copy.jl b/test/copy.jl index 338d3861375376..1b3f78fcd15c3a 100644 --- a/test/copy.jl +++ b/test/copy.jl @@ -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)