File: assert.m

package info (click to toggle)
octave 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,300 kB
  • sloc: cpp: 332,784; ansic: 77,239; fortran: 20,963; objc: 9,396; sh: 8,213; yacc: 4,925; lex: 4,389; perl: 1,544; java: 1,366; awk: 1,259; makefile: 648; xml: 189
file content (799 lines) | stat: -rw-r--r-- 29,018 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
########################################################################
##
## Copyright (C) 2000-2024 The Octave Project Developers
##
## See the file COPYRIGHT.md in the top-level directory of this
## distribution or <https://octave.org/copyright/>.
##
## This file is part of Octave.
##
## Octave is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## Octave is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Octave; see the file COPYING.  If not, see
## <https://www.gnu.org/licenses/>.
##
########################################################################

## -*- texinfo -*-
## @deftypefn  {} {} assert (@var{cond})
## @deftypefnx {} {} assert (@var{cond}, @var{errmsg})
## @deftypefnx {} {} assert (@var{cond}, @var{errmsg}, @dots{})
## @deftypefnx {} {} assert (@var{cond}, @var{msg_id}, @var{errmsg}, @dots{})
## @deftypefnx {} {} assert (@var{observed}, @var{expected})
## @deftypefnx {} {} assert (@var{observed}, @var{expected}, @var{tol})
##
## Produce an error if the specified condition is not met.
##
## @code{assert} can be called in three different ways.
##
## @table @code
## @item  assert (@var{cond})
## @itemx assert (@var{cond}, @var{errmsg})
## @itemx assert (@var{cond}, @var{errmsg}, @dots{})
## @itemx assert (@var{cond}, @var{msg_id}, @var{errmsg}, @dots{})
## Called with a single argument @var{cond}, @code{assert} produces an error if
## @var{cond} is false (numeric zero).
##
## Any additional arguments are passed to the @code{error} function for
## processing.
##
## @item assert (@var{observed}, @var{expected})
## Produce an error if observed is not the same as expected.
##
## Note that @var{observed} and @var{expected} can be scalars, vectors,
## matrices, strings, cell arrays, or structures.
##
## @item assert (@var{observed}, @var{expected}, @var{tol})
## Produce an error if observed is not the same as expected but equality
## comparison for numeric data uses a tolerance @var{tol}.
##
## If @var{tol} is positive then it is an absolute tolerance which will produce
## an error if @code{abs (@var{observed} - @var{expected}) > abs (@var{tol})}.
##
## If @var{tol} is negative then it is a relative tolerance which will produce
## an error if @code{abs (@var{observed} - @var{expected}) >
## abs (@var{tol} * @var{expected})}.
##
## If @var{expected} is zero @var{tol} will always be interpreted as an
## absolute tolerance.
##
## If @var{tol} is not scalar its dimensions must agree with those of
## @var{observed} and @var{expected} and tests are performed on an
## element-by-element basis.
## @end table
## @seealso{fail, test, error, isequal}
## @end deftypefn

function assert (cond, varargin)

  if (nargin == 0)
    print_usage ();
  endif

  persistent call_depth = -1;
  persistent errmsg;

  unwind_protect

    call_depth += 1;

    if (call_depth == 0)
      errmsg = "";
    endif

    if (nargin == 1 || (nargin > 1 && islogical (cond) && ischar (varargin{1})))
      if ((! isnumeric (cond) && ! islogical (cond))
          || isempty (cond) || ! all (cond(:)))

        ## We don't want to start the debugger here if debug_on_error is
        ## true so we set it to false and make the change local.  Then
        ## debug_on_error will be reset to true after this function
        ## returns and the debugger will start at the location of the
        ## call to print_usage.
        debug_on_error (false, "local");

        if (nargin == 1)
          ## Perhaps, say which elements failed?
          argin = ["(" inputname(1, false) ")"];
          error ("assert %s failed", argin);
        else
          error (varargin{:});
        endif
      endif
    else
      expected = varargin{1};
      if (nargin < 3)
        tol = 0;
      elseif (nargin == 3)
        tol = varargin{2};
      else
        print_usage ();
      endif

      ## Add to list as the errors accumulate.  If empty at end then no errors.
      err.index = {};
      err.observed = {};
      err.expected = {};
      err.reason = {};

      if (ischar (expected))
        if (! ischar (cond))
          err.index{end+1} = ".";
          err.expected{end+1} = ["'" expected "'"];
          if (isnumeric (cond))
            err.observed{end+1} = num2str (cond);
            err.reason{end+1} = "Expected string, but observed number";
          else
            err.observed{end+1} = "O";
            err.reason{end+1} = ["Expected string, but observed " class(cond)];
          endif
        elseif (! strcmp (cond, expected))
          err.index{end+1} = "[]";
          err.observed{end+1} = ["'" cond(:).' "'"];
          err.expected{end+1} = ["'" expected(:).' "'"];
          err.reason{end+1} = "Strings don't match";
        endif

      elseif (iscell (expected))
        if (! iscell (cond))
          err.index{end+1} = ".";
          err.observed{end+1} = "O";
          err.expected{end+1} = "E";
          err.reason{end+1} = ["Expected cell, but observed " class(cond)];
        elseif (ndims (cond) != ndims (expected)
                || any (size (cond) != size (expected)))
          err.index{end+1} = ".";
          err.observed{end+1} = ["O(" sprintf("%dx", size(cond))(1:end-1) ")"];
          err.expected{end+1} = ["E(" sprintf("%dx", size(expected))(1:end-1) ")"];
          err.reason{end+1} = "Dimensions don't match";
        else
          try
            ## Recursively compare cell arrays
            for i = 1:length (expected(:))
              assert (cond{i}, expected{i}, tol);
            endfor
          catch
            err.index{end+1} = "{}";
            err.observed{end+1} = "O";
            err.expected{end+1} = "E";
            err.reason{end+1} = "Cell configuration error";
          end_try_catch
        endif

      elseif (is_function_handle (expected))
        if (! is_function_handle (cond))
          err.index{end+1} = "@";
          err.observed{end+1} = "O";
          err.expected{end+1} = "E";
          err.reason{end+1} = ["Expected function handle, but observed " class(cond)];
        elseif (! isequal (cond, expected))
          err.index{end+1} = "@";
          err.observed{end+1} = "O";
          err.expected{end+1} = "E";
          err.reason{end+1} = "Function handles don't match";
        endif

      elseif (isstruct (expected))
        if (! isstruct (cond))
          err.index{end+1} = ".";
          err.observed{end+1} = "O";
          err.expected{end+1} = "E";
          err.reason{end+1} = ["Expected struct, but observed " class(cond)];
        elseif (ndims (cond) != ndims (expected)
                || any (size (cond) != size (expected))
                || numfields (cond) != numfields (expected))

          err.index{end+1} = ".";
          err.observed{end+1} = ["O(" sprintf("%dx", size(cond))(1:end-1) ")"];
          err.expected{end+1} = ["E(" sprintf("%dx", size(expected))(1:end-1) ")"];
          err.reason{end+1} = "Structure sizes don't match";
        elseif (! strcmp (sort (fieldnames (cond)),
                          sort (fieldnames (expected))))
          err.index{end+1} = ".";
          err.observed{end+1} = "O";
          err.expected{end+1} = "E";
          err.reason{end+1} = "Structure fieldname mismatch";
        else
          try
            ## Test if both structs are empty, i.e. 0x0, Nx0, or Nx0 structs.
            ## In this case the values cannot be extracted for comparison as
            ## they are not assignable and not defined.
            assert (isempty (cond), isempty (expected));

            if (! isempty (cond))
              for [v, k] = cond
                if (numel (cond) == 1)
                  v = {v};
                else
                  v = v(:)';
                endif
                ## Recursively call assert for struct array values
                assert (v, {expected.(k)}, tol);
              endfor
            endif
          catch
            err.index{end+1} = ".";
            err.observed{end+1} = "O";
            err.expected{end+1} = "E";
            err.reason{end+1} = "Structure configuration error";
          end_try_catch
        endif

      elseif (ndims (cond) != ndims (expected)
              || any (size (cond) != size (expected)))
        err.index{end+1} = ".";
        err.observed{end+1} = ["O(" sprintf("%dx", size(cond))(1:end-1) ")"];
        err.expected{end+1} = ["E(" sprintf("%dx", size(expected))(1:end-1) ")"];
        err.reason{end+1} = "Dimensions don't match";

      else  # Numeric comparison
        if (nargin < 3)
          ## Without explicit tolerance, be more strict.
          if (! strcmp (class (cond), class (expected)))
            err.index{end+1} = "()";
            err.observed{end+1} = "O";
            err.expected{end+1} = "E";
            err.reason{end+1} = ["Class " class(cond) " != " class(expected)];
          elseif (isnumeric (cond) || islogical (cond))
            if (issparse (cond) != issparse (expected))
              err.index{end+1} = "()";
              err.observed{end+1} = "O";
              err.expected{end+1} = "E";
              if (issparse (cond))
                err.reason{end+1} = "sparse != non-sparse";
              else
                err.reason{end+1} = "non-sparse != sparse";
              endif
            elseif (iscomplex (cond) != iscomplex (expected))
              err.index{end+1} = "()";
              err.observed{end+1} = "O";
              err.expected{end+1} = "E";
             if (iscomplex (cond))
                err.reason{end+1} = "complex != real";
              else
                err.reason{end+1} = "real != complex";
              endif
            endif
          endif
        endif

        if (isempty (err.index))

          A = cond;
          B = expected;

          if (isinteger (A) && isinteger (B))
            ## Non-floating point numbers can't have exceptional or complex
            ## values so skip tests.
            A_null = A;
            B_null = B;
          else
            ## Check exceptional values.
            is_real = (isreal (A) && isreal (B));
            if (is_real)
              errvec = (isna (A) != isna (B));
            else
              errvec = (  isna (real (A)) != isna (real (B))
                        | isna (imag (A)) != isna (imag (B)));
            endif
            erridx = find (errvec);
            if (! isempty (erridx))
              err.index(end+1:end+length (erridx)) = ...
                ind2tuple (size (A), erridx);
              err.observed(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (A(erridx) (:))));
              err.expected(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (B(erridx) (:))));
              err.reason(end+1:end+length (erridx)) = ...
                repmat ({"'NA' mismatch"}, length (erridx), 1);
            endif
            errseen = errvec;

            if (is_real)
              errvec = (isnan (A) != isnan (B));
            else
              errvec = (  isnan (real (A)) != isnan (real (B))
                        | isnan (imag (A)) != isnan (imag (B)));
            endif
            erridx = find (errvec & ! errseen);
            if (! isempty (erridx))
              err.index(end+1:end+length (erridx)) = ...
                ind2tuple (size (A), erridx);
              err.observed(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (A(erridx) (:))));
              err.expected(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (B(erridx) (:))));
              err.reason(end+1:end+length (erridx)) = ...
                repmat ({"'NaN' mismatch"}, length (erridx), 1);
            endif
            errseen |= errvec;

            if (is_real)
              errvec = ((isinf (A) | isinf (B)) & (real (A) != real (B)));
            else
              errvec =   ((isinf (real (A)) | isinf (real (B))) ...
                          & (real (A) != real (B)))             ...
                       | ((isinf (imag (A)) | isinf (imag (B))) ...
                          & (imag (A) != imag (B)));
            endif
            erridx = find (errvec & ! errseen);
            if (! isempty (erridx))
              err.index(end+1:end+length (erridx)) = ...
                ind2tuple (size (A), erridx);
              err.observed(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (A(erridx) (:))));
              err.expected(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (B(erridx) (:))));
              err.reason(end+1:end+length (erridx)) = ...
                repmat ({"'Inf' mismatch"}, length (erridx), 1);
            endif
            errseen |= errvec;

            ## Check normal values.
            ## Replace exceptional values already checked above by zero.
            if (is_real)
              A_null_real = A;
              B_null_real = B;
            else
              A_null_real = real (A);
              B_null_real = real (B);
            endif
            if (issparse (errseen))
              exclude = errseen ...
                        | isnan (A_null_real) | isinf (A_null_real) ...
                        | isnan (B_null_real) | isinf (B_null_real);
            else
              exclude = errseen ...
                        | ! isfinite (A_null_real) & ! isfinite (B_null_real);
            endif
            A_null_real(exclude) = 0;
            B_null_real(exclude) = 0;

            if (is_real)
              A_null = A_null_real;
              B_null = B_null_real;
            else
              A_null_imag = imag (A);
              B_null_imag = imag (B);
              if (issparse (errseen))
                exclude = errseen ...
                          | isnan (A_null_imag) | isinf (A_null_imag) ...
                          | isnan (B_null_imag) | isinf (B_null_imag);
              else
                exclude = errseen ...
                          | ! isfinite (A_null_imag) & ! isfinite (B_null_imag);
              endif

              A_null_imag(exclude) = 0;
              B_null_imag(exclude) = 0;
              A_null = complex (A_null_real, A_null_imag);
              B_null = complex (B_null_real, B_null_imag);
            endif

            clear A_null_real B_null_real;
            clear A_null_imag B_null_imag;
          endif

          if (isscalar (tol))
            mtol = tol * ones (size (A));
          else
            mtol = tol;
          endif

          k = (mtol == 0);
          erridx = find ((A_null != B_null) & k);
          if (! isempty (erridx))
            err.index(end+1:end+length (erridx)) = ...
              ind2tuple (size (A), erridx);
            err.observed(end+1:end+length (erridx)) = ...
              strtrim (cellstr (num2str (A(erridx) (:))));
            err.expected(end+1:end+length (erridx)) = ...
              strtrim (cellstr (num2str (B(erridx) (:))));
            err.reason(end+1:end+length (erridx)) = ...
              ostrsplit (deblank (
                         sprintf ("Abs err %.5g exceeds tol %.5g by %.1g\n",
                [abs(A_null(erridx) - B_null(erridx))(:), mtol(erridx)(:), ...
                 abs(A_null(erridx) - B_null(erridx))(:)-mtol(erridx)(:)].')),
                         "\n");
          endif

          k = (mtol > 0);
          erridx = find ((abs (A_null - B_null) > mtol) & k);
          if (! isempty (erridx))
            err.index(end+1:end+length (erridx)) = ...
              ind2tuple (size (A), erridx);
            err.observed(end+1:end+length (erridx)) = ...
              strtrim (cellstr (num2str (A(erridx) (:))));
            err.expected(end+1:end+length (erridx)) = ...
              strtrim (cellstr (num2str (B(erridx) (:))));
            err.reason(end+1:end+length (erridx)) = ...
              ostrsplit (deblank (
                         sprintf ("Abs err %.5g exceeds tol %.5g by %.1g\n",
                [abs(A_null(erridx) - B_null(erridx))(:), mtol(erridx)(:), ...
                 abs(A_null(erridx) - B_null(erridx))(:)-mtol(erridx)(:)].')),
                         "\n");
          endif

          k = (mtol < 0);
          if (any (k(:)))
            ## Test for absolute error where relative error can't be calculated.
            erridx = find ((B_null == 0) & abs (A_null) > abs (mtol) & k);
            if (! isempty (erridx))
              err.index(end+1:end+length (erridx)) = ...
                ind2tuple (size (A), erridx);
              err.observed(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (A(erridx) (:))));
              err.expected(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (B(erridx) (:))));
              err.reason(end+1:end+length (erridx)) = ...
                ostrsplit (deblank (
                           sprintf ("Abs err %.5g exceeds tol %.5g by %.1g\n",
                      [abs(A_null(erridx) - B_null(erridx)), -mtol(erridx), ...
                       abs(A_null(erridx) - B_null(erridx))+mtol(erridx)].')),
                           "\n");
            endif
            ## Test for relative error
            Bdiv = Inf (size (B_null));
            Bdiv(k & (B_null != 0)) = B_null(k & (B_null != 0));
            relerr = abs ((A_null - B_null) ./ abs (Bdiv));
            erridx = find ((relerr > abs (mtol)) & k);
            if (! isempty (erridx))
              err.index(end+1:end+length (erridx)) = ...
                ind2tuple (size (A), erridx);
              err.observed(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (A(erridx) (:))));
              err.expected(end+1:end+length (erridx)) = ...
                strtrim (cellstr (num2str (B(erridx) (:))));
              err.reason(end+1:end+length (erridx)) = ...
                ostrsplit (deblank (
                           sprintf ("Rel err %.5g exceeds tol %.5g by %.1g\n",
                                    [relerr(erridx)(:), -mtol(erridx)(:), ...
                                     relerr(erridx)(:)+mtol(erridx)(:)].')),
                           "\n");
            endif
          endif
        endif

      endif

      ## Print any errors
      if (! isempty (err.index))
        arg_names = cell (nargin, 1);
        for i = 1:nargin
          arg_names{i} = inputname (i, false);
        endfor
        argin = ["(" strjoin(arg_names, ",") ")"];
        if (! isempty (errmsg))
          errmsg = [errmsg "\n"];
        endif
        errmsg = [errmsg, pprint(argin, err)];
      endif

    endif

  unwind_protect_cleanup
    call_depth -= 1;
  end_unwind_protect

  if (call_depth == -1)
    ## Last time through.  If there were any errors on any pass, raise a flag.
    if (! isempty (errmsg))

      ## We don't want to start the debugger here if debug_on_error is
      ## true so we set it to false and make the change local.  Then
      ## debug_on_error will be reset to true after this function
      ## returns and the debugger will start at the location of the call
      ## to print_usage.
      debug_on_error (false, "local");

      error (errmsg);
    endif
  endif

endfunction


## empty input
%!error assert ([])
%!error assert ("")
%!error assert ({})
%!error assert (struct ([]))
%!assert (zeros (3,0), zeros (3,0))
%!error <O\(3x0\)\s+E\(0x2\)> assert (zeros (3,0), zeros (0,2))
%!error <Dimensions don't match> assert (zeros (3,0), [])
%!error <Dimensions don't match> assert (zeros (2,0,2), zeros (2,0))

## conditions
%!assert (isempty ([]))
%!assert (1)
%!error assert (0)
%!assert (ones (3,1))
%!assert (ones (1,3))
%!assert (ones (3,4))
%!error assert ([1,0,1])
%!error assert ([1;1;0])
%!error assert ([1,0;1,1])
%!error <2-part error> assert (false, "%s %s", "2-part", "error")
%!error <2-part error> assert (false, "TST:msg_id", "%s %s", "2-part", "error")

## scalars
%!error <Dimensions don't match> assert (3, [3,3])
%!error <Dimensions don't match> assert (3, [3,3; 3,3])
%!error <Dimensions don't match> assert ([3,3; 3,3], 3)
%!assert (3, 3)
%!error <Abs err 1 exceeds tol> assert (3, 4)
%!assert (3+eps, 3, eps)
%!assert (3, 3+eps, eps)
%!error <Abs err 4.4409e-0?16 exceeds tol> assert (3+2*eps, 3, eps)
%!error <Abs err 4.4409e-0?16 exceeds tol> assert (3, 3+2*eps, eps)

## vectors
%!assert ([1,2,3],[1,2,3])
%!assert ([1;2;3],[1;2;3])
%!error <Abs err 1 exceeds tol 0> assert ([2,2,3,3],[1,2,3,4])
%!error <Abs err 1 exceeds tol 0.5> assert ([2,2,3,3],[1,2,3,4],0.5)
%!error <Rel err 1 exceeds tol 0.1> assert ([2,2,3,5],[1,2,3,4],-0.1)
%!error <Abs err 1 exceeds tol 0> assert ([6;6;7;7],[5;6;7;8])
%!error <Abs err 1 exceeds tol 0.5> assert ([6;6;7;7],[5;6;7;8],0.5)
%!error <Rel err .* exceeds tol 0.1> assert ([6;6;7;7],[5;6;7;8],-0.1)
%!error <Dimensions don't match> assert ([1,2,3],[1;2;3])
%!error <Dimensions don't match> assert ([1,2],[1,2,3])
%!error <Dimensions don't match> assert ([1;2;3],[1;2])

## matrices
%!assert ([1,2;3,4],[1,2;3,4])
%!error <\(1,2\)\s+4\s+2> assert ([1,4;3,4],[1,2;3,4])
%!error <Dimensions don't match> assert ([1,3;2,4;3,5],[1,2;3,4])
%!test  # 2-D matrix
%! A = [1 2 3]'*[1,2];
%! assert (A, A);
%! fail ("assert (A.*(A!=2),A)");
%!test  # N-D matrix
%! X = zeros (2,2,3);
%! Y = X;
%! Y(1,2,3) = 1.5;
%! fail ("assert (X,Y)", "\(1,2,3\).*Abs err 1.5 exceeds tol 0");

## must give a small tolerance for floating point errors on relative
%!assert (100+100*eps, 100, -2*eps)
%!assert (100, 100+100*eps, -2*eps)
%!error <Rel err .* exceeds tol> assert (100+300*eps, 100, -2*eps)
%!error <Rel err .* exceeds tol> assert (100, 100+300*eps, -2*eps)

## test relative vs. absolute tolerances
%!test  assert (0.1+eps, 0.1, 2*eps);
%!error <Rel err 2.2204e-0?15 exceeds tol> assert (0.1+eps, 0.1, -2*eps)
%!test  assert (100+100*eps, 100, -2*eps);
%!error <Abs err 2.8422e-0?14 exceeds tol> assert (100+100*eps, 100, 2*eps)

## Corner case of relative tolerance with 0 divider
%!error <Abs err 2 exceeds tol 0.1> assert (2, 0, -0.1)

## Extra checking of inputs when tolerance unspecified.
%!error <Class single != double> assert (single (1), 1)
%!error <Class uint8 != uint16> assert (uint8 (1), uint16 (1))
%!error <sparse != non-sparse> assert (sparse([1]), [1])
%!error <non-sparse != sparse> assert ([1], sparse([1]))
%!error <complex != real> assert (1+i, 1)
%!error <real != complex> assert (1, 1+i)

## exceptional values
%!assert ([NaN, NA, Inf, -Inf, 1+eps, eps], [NaN, NA, Inf, -Inf, 1, 0], eps)

%!error <'NaN' mismatch> assert (NaN, 1)
%!error <'NaN' mismatch> assert ([NaN 1], [1 NaN])
%!test
%! try
%!   assert ([NaN 1], [1 NaN]);
%! catch
%!   errmsg = lasterr ();
%!   if (sum (errmsg () == "\n") != 4)
%!     error ("Too many errors reported for NaN assert");
%!   elseif (strfind (errmsg, "NA"))
%!     error ("NA reported for NaN assert");
%!   elseif (strfind (errmsg, "Abs err NaN exceeds tol 0"))
%!     error ("Abs err reported for NaN assert");
%!   endif
%! end_try_catch

%!error <'NA' mismatch> assert (NA, 1)
%!error assert ([NA 1]', [1 NA]')
%!test
%! try
%!   assert ([NA 1]', [1 NA]');
%! catch
%!   errmsg = lasterr ();
%!   if (sum (errmsg () == "\n") != 4)
%!     error ("Too many errors reported for NA assert");
%!   elseif (strfind (errmsg, "NaN"))
%!     error ("NaN reported for NA assert");
%!   elseif (strfind (errmsg, "Abs err NA exceeds tol 0"))
%!     error ("Abs err reported for NA assert");
%!   endif
%! end_try_catch
%!error assert ([(complex (NA, 1)) (complex (2, NA))], [(complex (NA, 2)) 2])

%!error <'Inf' mismatch> assert (-Inf, Inf)
%!error <'Inf' mismatch> assert ([-Inf Inf], [Inf -Inf])
%!test
%! try
%!   assert (complex (Inf, 0.2), complex (-Inf, 0.2 + 2*eps), eps);
%! catch
%!   errmsg = lasterr ();
%!   if (sum (errmsg () == "\n") != 3)
%!     error ("Too many errors reported for Inf assert");
%!   elseif (strfind (errmsg, "Abs err"))
%!     error ("Abs err reported for Inf assert");
%!   endif
%! end_try_catch
%!error <Abs err> assert (complex (Inf, 0.2), complex (Inf, 0.2 + 2*eps), eps)

## strings
%!assert ("dog", "dog")
%!error <Strings don't match> assert ("dog", "cat")
%!error <Expected string, but observed number> assert (3, "dog")
%!error <Class char != double> assert ("dog", [3 3 3])
%!error <Expected string, but observed cell> assert ({"dog"}, "dog")
%!error <Expected string, but observed struct> assert (struct ("dog", 3), "dog")

## cell arrays
%!error <Expected cell, but observed double> assert (1, {1})
%!error <Dimensions don't match> assert (cell (1,2,3), cell (3,2,1))
%!test
%! x = {{{1}}, 2};  # cell with multiple levels
%! y = x;
%! assert (x,y);
%! y{1}{1}{1} = 3;
%! fail ("assert (x,y)", "Abs err 2 exceeds tol 0");

## function handles
%!assert (@sin, @sin)
%!error <Function handles don't match> assert (@sin, @cos)
%!error <Expected function handle, but observed double> assert (pi, @cos)
%!error <Class function_handle != double> assert (@sin, pi)

%!test
%! x = {[3], [1,2,3]; 100+100*eps, "dog"};
%! y = x;
%! assert (x, y);
%! y = x; y(1,1) = [2];
%! fail ("assert (x, y)");
%! y = x; y(1,2) = [0, 2, 3];
%! fail ("assert (x, y)");
%! y = x; y(2,1) = 101;
%! fail ("assert (x, y)");
%! y = x; y(2,2) = "cat";
%! fail ("assert (x, y)");
%! y = x; y(1,1) = [2];  y(1,2) = [0, 2, 3]; y(2,1) = 101; y(2,2) = "cat";
%! fail ("assert (x, y)");

## structures
%!error <Expected struct, but observed double> assert (1, struct ("a", 1))
%!error <Structure sizes don't match>
%! x(1,2,3).a = 1;
%! y(1,2).a = 1;
%! assert (x,y);
%!error <Structure sizes don't match>
%! x(1,2,3).a = 1;
%! y(3,2,2).a = 1;
%! assert (x,y);
%!error <Structure sizes don't match>
%! x.a = 1;
%! x.b = 1;
%! y.a = 1;
%! assert (x,y);
%!error <Structure fieldname mismatch>
%! x.b = 1;
%! y.a = 1;
%! assert (x,y);

%!test
%! x.a = 1; x.b=[2, 2];
%! y.a = 1; y.b=[2, 2];
%! assert (x, y);
%! y.b=3;
%! fail ("assert (x, y)");
%! fail ("assert (3, x)");
%! fail ("assert (x, 3)");
%! ## Empty structures
%! x = resize (x, 0, 1);
%! y = resize (y, 0, 1);
%! assert (x, y);

## vector of tolerances
%!test
%! x = [-40:0];
%! y1 = (10.^x).*(10.^x);
%! y2 = 10.^(2*x);
%! ## Increase tolerance from eps (y1) to 4*eps (y1) because of an upstream bug
%! ## in mingw-w64: https://sourceforge.net/p/mingw-w64/bugs/466/
%! assert (y1, y2, 4*eps (y1));
%! fail ("assert (y1, y2 + eps*1e-70, eps (y1))");

## Multiple tolerances
%!test
%! x = [1 2; 3 4];
%! y = [0 -1; 1 2];
%! tol = [-0.1 0; -0.2 0.3];
%! try
%!   assert (x, y, tol);
%! catch
%!   errmsg = lasterr ();
%!   if (sum (errmsg () == "\n") != 6)
%!     error ("Incorrect number of errors reported");
%!   endif
%!   assert (! isempty (regexp (errmsg, '\(1,2\).*Abs err 3 exceeds tol 0\>')));
%!   assert (! isempty (regexp (errmsg, '\(2,2\).*Abs err 2 exceeds tol 0.3')));
%!   assert (! isempty (regexp (errmsg, '\(1,1\).*Abs err 1 exceeds tol 0.1')));
%!   assert (! isempty (regexp (errmsg, '\(2,1\).*Rel err 2 exceeds tol 0.2')));
%! end_try_catch

%!test <*57615>
%! try
%!   assert (complex (pi*1e-17,2*pi), 0, 1e-1);
%! catch
%!   errmsg = lasterr ();
%!   assert (isempty (strfind (errmsg, "sprintf: invalid field width")));
%! end_try_catch

%!test <*63988>
%! A = ["ab"; "cd"];
%! B = ["ad"; "cb"];
%! try
%!   assert (A, B);
%! catch
%!   errmsg = lasterr ();
%!   if (regexp (errmsg, 'horizontal dimensions mismatch'))
%!     error ("assert failed for char arrays with multiple rows");
%!   endif
%! end_try_catch

## test input validation
%!error <Invalid call> assert ()
%!error <Invalid call> assert (1,2,3,4)


## Convert all error indices into tuple format
function cout = ind2tuple (matsize, erridx)

  cout = cell (numel (erridx), 1);
  tmp = cell (1, numel (matsize));
  [tmp{:}] = ind2sub (matsize, erridx(:));
  subs = [tmp{:}];
  if (numel (matsize) == 2)
    subs = subs(:, matsize != 1);
  endif
  for i = 1:numel (erridx)
    loc = sprintf ("%d,", subs(i,:));
    cout{i} = ["(" loc(1:end-1) ")"];
  endfor

endfunction


## Pretty print the various errors in a condensed tabular format.
function str = pprint (argin, err)

  str = ["ASSERT errors for:  assert " argin "\n"];
  str = [str, "\n  Location  |  Observed  |  Expected  |  Reason\n"];
  for i = 1:length (err.index)
    leni = length (err.index{i});
    leno = length (err.observed{i});
    lene = length (err.expected{i});
    str = [str, sprintf("%*s%*s %*s%*s %*s%*s   %s\n",
                  6+fix(leni/2), err.index{i}   , max (6-fix(leni/2), 0), "",
                  6+fix(leno/2), err.observed{i}, max (6-fix(leno/2), 0), "",
                  6+fix(lene/2), err.expected{i}, max (6-fix(lene/2), 0), "",
                  err.reason{i})];
  endfor

endfunction