comparison 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
comparison
equal deleted inserted replaced
20573:e3c0fee87493 20574:dd6345fd8a97
203 %! a(1,:,:,:) = reshape (1:4,[1,1,4]); 203 %! a(1,:,:,:) = reshape (1:4,[1,1,4]);
204 %! assert (a, reshape (1:4,[1,1,1,4])); 204 %! assert (a, reshape (1:4,[1,1,1,4]));
205 205
206 %!error (a(1:2,1:2) = 1:4) 206 %!error (a(1:2,1:2) = 1:4)
207 207
208 %!shared x
209 %! x = 1:5;
210 %!error <attempted to use a complex scalar as an index> x(i)
211 %!error <attempted to use a complex scalar as an index> x(j)
212 %!error <attempted to use a complex scalar as an index> x(1+i)
213
214 ## bug #38357 208 ## bug #38357
215 %!shared d, dd 209 %!shared d, dd
216 %! d = diag ([1, 2, 3]); 210 %! d = diag ([1, 2, 3]);
217 %! dd = diag ([1, 2, 3], 6, 3); 211 %! dd = diag ([1, 2, 3], 6, 3);
218 %!assert (d(1), 1); 212 %!assert (d(1), 1);
499 %!test 493 %!test
500 %! y = ones (2, 2); 494 %! y = ones (2, 2);
501 %! x = ones (2, 2); 495 %! x = ones (2, 2);
502 %! x([], false, :) = []; 496 %! x([], false, :) = [];
503 %! assert (x, y); 497 %! assert (x, y);
498
499
500 ## Test indexing of unnamed constants
501 %!error <index \(0\); subscripts must be> 1(0)
502 %!error <index \(-1\); subscripts must be> 1(-1)
503 %!error <index \(_,0.5\); subscripts> {}(1,0.5)
504 %!error <index \(nan,_\); subscripts> 1(NaN,1)
505 %!error <index \(_,_,<cell....\[x8\]...\); subscripts> [](1,1,{},1,1,1,1,1,1,1,1)
506 %!error <index \(...\[x9\]...-1,_\); subscript> 1(1,1,1,1,1,1,1,1,1,-1,1)
507 %!error <index \(2\); out of bound 1> 1(2)
508 %!error <index \(1\); out of bound 0> [](1)
509 %!error <index \(_,1\); but object has size 5x0> zeros(5,0)(3,1)
510 %!error <index \(3,_\); but object has size 0x5> zeros(0,5)(3,1)
511 %!error <index \(-1\); subscripts> 1(1)(-1)(1)
512 %!
513 %!shared abc
514 %! abc = [1, 2];
515 %! ## Test full matrices in variables
516 %!error <abc\(3\); out of bound 2> abc([false, true, true])
517 %!error <abc\(-1\); subscripts> abc(-1)(1)(1)
518 %! ## xerror <index \(-1\); subscripts> abc(1)(-1)(1) ## why no 'xerror' test?
519
520 %!shared abc
521 %! abc = [1 2; 3 4];
522 %!error <abc\(5\); out of bound 4> abc(5)
523 %!error <abc\(_,3\); but abc has size 2x2> abc(2,3)
524 %!error <abc\(_,_,0.5\); subscripts> exp (abc(2,3,0.5))
525
526 %!shared abc
527 %! abc = [1 2; 3 4]; abc(1,1,2) = 1;
528 %!error <abc\(_,5\); out of bound 4> abc(2,5)
529 %!error <abc\(_,3,_\); but abc has size 2x2x2> abc(2,3,2)
530 %!error <A\(..,I,..\) = \[\]: .* value 3 out of bound 2> abc(3,:) = []
531 %!error <A\(I\) = \[\]: .* value 50 out of bound 8> abc(3:50) = []
532 %!error <a null assignment can only have one non-colon index> abc(3,5) = []
533 %!error <=: nonconformant arguments \(op1 is 1x1, op2 is 1x5\)> abc(3,5) = 1:5
534
535 %! ## Test diagonal matrices, and access of function results
536 %!error <index \(_,_,5\); but object has size 3x3> eye(3)(2,3,5)
537 %!error <index \(-2,_\); subscripts> eye(4)(-2,3)
538
539 %! ## Test cells
540 %!shared abc
541 %! abc = {1, 2; 3, 4};
542 %!error <abc\(_,0.3,_\); subscripts> abc(2,0.3,5)
543 %!error <abc\(_,0.3,_\); subscripts> abc{2,0.3,5}
544 %!error <abc\(-2,_,_,_\); subscripts> abc{-2,1,1,1}
545 %!error <abc\(0,_,_,_\); subscripts> abc(0,1,1,1) = 1
546
547 %! ## Test permutation matrices
548 %!shared abc
549 %! abc = eye(3)([3 1 2],:);
550 %!error <abc\(nan\); subscripts> abc(NA)
551 %!error <abc\(_,_,_,inf,_\); subscripts> abc(1,1,1,Inf,1)
552
553 %! ## Test sparse matrices
554 %!shared abc
555 %! abc = sparse(3,3);
556 %!error <abc\(-1\); subscripts> abc(-1)
557 %!error <abc\(-1\); subscripts> abc(-1) = 1
558 %!error <abc\(-1,_\); subscripts> abc(-1,1)
559 %!error <abc\(-1,_\); subscripts> abc(-1,1) = 1
560 %!error <sparse indexing needs 1 or 2 indices> abc(0,0,0,0)
561 %!error <abc\(4,_\); but abc has size 3x3> abc(4,1)
562
563 %! ## Test ranges
564 %!shared abc
565 %! abc = 1:10;
566 %!error <abc\(-1\); subscripts> abc(-1)
567 %!error <abc\(-1,_\); subscripts> abc(-1,1)
568 %!error <abc\(4,_\); but abc has size 1x10> abc(4,1)
569
570 %! ## Test complex
571 %!shared abc, z
572 %! abc = [1 2];
573 %!error <abc\(0\+1i\); subscripts must be real> abc(i)
574 %! abc = [1 2; 3 4];
575 %!error <abc\(1\+0i\); subscripts must be real> abc(complex(1))
576 %!error <abc\(1\+0.5i,_\); subscripts must be real> abc(1+0.5*i,3)
577 %!error <abc\(_,0-2i\); subscripts must be real> abc(2,0-2*i)
578