diff test/index.tst @ 20574:dd6345fd8a97

use exceptions for better invalid index error reporting (bug #45957) * lo-array-gripes.h, lo-array-gripes.cc (index_exception): New base class for indexing errors. (invalid_index, out_of_range): New classes. (gripe_index_out_of_range): New overloaded function. (gripe_invalid_index): New overloaded functions. Delete version with no arguments. (gripe_invalid_assignment_size, gripe_assignment_dimension_mismatch): Delete. Change uses of gripe functions as needed. * Cell.cc (Cell::index, Cell::assign, Cell::delete_elements): Use exceptions to collect error info about and handle indexing errors. * data.cc (Fnth_element, do_accumarray_sum, F__accumarray_sum__, do_accumarray_minmax, do_accumarray_minmax_fun, F__accumdim_sum__): Likewise. * oct-map.cc (octave_map::index, octave_map::assign, octave_map::delete_elements): Likewise. * sparse.cc (Fsparse): Likewise. * sub2ind.cc (Fsub2ind, Find2sub): Likewise. New tests. * utils.cc (dims_to_numel): Likewise. * ov-base-diag.cc (octave_base_diag<DMT, MT>::do_index_op, octave_base_diag<DMT, MT>::subsasgn): Likewise. * ov-base-mat.cc (octave_base_matrix<MT>::subsref, octave_base_matrix<MT>::assign): Likewise. * ov-base-sparse.cc (octave_base_sparse<T>::do_index_op, octave_base_sparse<T>::assign, octave_base_sparse<MT>::delete_elements): Likewise. * ov-classdef.cc (cdef_object_array::subsref, cdef_object_array::subsasgn): Likewise. * ov-java.cc (make_java_index): Likewise. * ov-perm.cc (octave_perm_matrix::do_index_op): Likewise. * ov-range.cc (octave_range::do_index_op): Likewise. * ov-re-diag.cc (octave_diag_matrix::do_index_op): Likewise. * ov-str-mat.cc (octave_char_matrix_str::do_index_op_internal): Likewise. * pt-assign.cc (tree_simple_assignment::rvalue1): Likewise. * pt-idx.cc (tree_index_expression::rvalue, tree_index_expression::lvalue): Likewise. * Array-util.cc (sub2ind): Likewise. * toplev.cc (main_loop): Also catch unhandled index_exception exceptions. * ov-base.cc (octave_base_value::index_vector): Improve error message. * ov-re-sparse.cc (octave_sparse_matrix::index_vector): Likewise. * ov-complex.cc (complex_index): New class. (gripe_complex_index): New function. (octave_complex::index_vector): Use it. * pt-id.h, pt-id.cc (tree_identifier::is_variable, tree_black_hole::is_variable): Now const. * pt-idx.cc (final_index_error): New static function. (tree_index_expression::rvalue, tree_index_expression::lvalue): Use it. * index.tst: New tests.
author Lachlan Andrew <lachlanbis@gmail.com>
date Fri, 02 Oct 2015 15:07:37 -0400
parents 4197fc428c7d
children
line wrap: on
line diff
--- a/test/index.tst	Fri Oct 02 12:25:39 2015 -0400
+++ b/test/index.tst	Fri Oct 02 15:07:37 2015 -0400
@@ -205,12 +205,6 @@
 
 %!error (a(1:2,1:2) = 1:4)
 
-%!shared x
-%! x = 1:5;
-%!error <attempted to use a complex scalar as an index> x(i)
-%!error <attempted to use a complex scalar as an index> x(j)
-%!error <attempted to use a complex scalar as an index> x(1+i)
-
 ## bug #38357
 %!shared d, dd
 %! d = diag ([1, 2, 3]);
@@ -501,3 +495,84 @@
 %! x = ones (2, 2);
 %! x([], false, :) = [];
 %! assert (x, y);
+
+
+  ##  Test indexing of unnamed constants
+%!error <index \(0\); subscripts must be>     1(0)
+%!error <index \(-1\); subscripts must be>    1(-1)
+%!error <index \(_,0.5\); subscripts>                 {}(1,0.5)
+%!error <index \(nan,_\); subscripts>                 1(NaN,1)
+%!error <index \(_,_,<cell....\[x8\]...\); subscripts> [](1,1,{},1,1,1,1,1,1,1,1)
+%!error <index \(...\[x9\]...-1,_\); subscript>      1(1,1,1,1,1,1,1,1,1,-1,1)
+%!error <index \(2\); out of bound 1>                1(2)
+%!error <index \(1\); out of bound 0>                [](1)
+%!error <index \(_,1\); but object has size 5x0>     zeros(5,0)(3,1)
+%!error <index \(3,_\); but object has size 0x5>     zeros(0,5)(3,1)
+%!error <index \(-1\); subscripts>                   1(1)(-1)(1)
+%!
+%!shared abc
+%! abc = [1, 2];
+%! ##  Test full matrices in variables
+%!error <abc\(3\); out of bound 2>      abc([false, true, true])
+%!error <abc\(-1\); subscripts>         abc(-1)(1)(1)
+%! ## xerror <index \(-1\); subscripts> abc(1)(-1)(1)   ## why no 'xerror' test?
+
+%!shared abc
+%! abc = [1 2; 3 4];
+%!error <abc\(5\); out of bound 4>         abc(5)
+%!error <abc\(_,3\); but abc has size 2x2> abc(2,3)
+%!error <abc\(_,_,0.5\); subscripts>       exp (abc(2,3,0.5))
+
+%!shared abc
+%! abc = [1 2; 3 4]; abc(1,1,2) = 1;
+%!error <abc\(_,5\); out of bound 4>                            abc(2,5)
+%!error <abc\(_,3,_\); but abc has size 2x2x2>                  abc(2,3,2)
+%!error <A\(..,I,..\) = \[\]: .* value 3 out of bound 2>        abc(3,:) = []
+%!error <A\(I\) = \[\]: .* value 50 out of bound 8>             abc(3:50) = []
+%!error <a null assignment can only have one non-colon index>   abc(3,5) = []
+%!error <=: nonconformant arguments \(op1 is 1x1, op2 is 1x5\)> abc(3,5) = 1:5
+
+%! ##  Test diagonal matrices, and access of function results
+%!error <index \(_,_,5\); but object has size 3x3> eye(3)(2,3,5)
+%!error <index \(-2,_\); subscripts>               eye(4)(-2,3)
+
+%! ##  Test cells
+%!shared abc
+%! abc = {1, 2; 3, 4};
+%!error <abc\(_,0.3,_\); subscripts>  abc(2,0.3,5)
+%!error <abc\(_,0.3,_\); subscripts>  abc{2,0.3,5}
+%!error <abc\(-2,_,_,_\); subscripts> abc{-2,1,1,1}
+%!error <abc\(0,_,_,_\); subscripts>  abc(0,1,1,1) = 1
+
+%! ##  Test permutation matrices
+%!shared abc
+%! abc = eye(3)([3 1 2],:);
+%!error <abc\(nan\); subscripts>         abc(NA)
+%!error <abc\(_,_,_,inf,_\); subscripts> abc(1,1,1,Inf,1)
+
+%! ##  Test sparse matrices
+%!shared abc
+%! abc = sparse(3,3);
+%!error <abc\(-1\); subscripts>                abc(-1)
+%!error <abc\(-1\); subscripts>                abc(-1) = 1
+%!error <abc\(-1,_\); subscripts>              abc(-1,1)
+%!error <abc\(-1,_\); subscripts>              abc(-1,1) = 1
+%!error <sparse indexing needs 1 or 2 indices> abc(0,0,0,0)
+%!error <abc\(4,_\); but abc has size 3x3>     abc(4,1)
+
+%! ##  Test ranges
+%!shared abc
+%! abc = 1:10;
+%!error <abc\(-1\); subscripts>             abc(-1)
+%!error <abc\(-1,_\); subscripts>           abc(-1,1)
+%!error <abc\(4,_\); but abc has size 1x10> abc(4,1)
+
+%! ##  Test complex
+%!shared abc, z
+%! abc = [1 2];
+%!error <abc\(0\+1i\); subscripts must be real>     abc(i)
+%! abc = [1 2; 3 4];
+%!error <abc\(1\+0i\); subscripts must be real>     abc(complex(1))
+%!error <abc\(1\+0.5i,_\); subscripts must be real> abc(1+0.5*i,3)
+%!error <abc\(_,0-2i\); subscripts must be real>   abc(2,0-2*i)
+