|
22 | 22 | % -- this function is part of Redbird-m toolbox |
23 | 23 | % |
24 | 24 |
|
25 | | -global RB_RTOL RB_ATOL RB_FAIL RB_TOTAL; |
26 | | -if isempty(RB_RTOL); RB_RTOL = 1e-9; end |
27 | | -if isempty(RB_ATOL); RB_ATOL = 1e-12; end |
28 | | -if isempty(RB_FAIL); RB_FAIL = 0; end |
29 | | -if isempty(RB_TOTAL); RB_TOTAL = 0; end |
| 25 | +global RB_RTOL RB_ATOL RB_FAIL RB_TOTAL |
| 26 | +if isempty(RB_RTOL) |
| 27 | + RB_RTOL = 1e-9; |
| 28 | +end |
| 29 | +if isempty(RB_ATOL) |
| 30 | + RB_ATOL = 1e-12; |
| 31 | +end |
| 32 | +if isempty(RB_FAIL) |
| 33 | + RB_FAIL = 0; |
| 34 | +end |
| 35 | +if isempty(RB_TOTAL) |
| 36 | + RB_TOTAL = 0; |
| 37 | +end |
30 | 38 |
|
31 | 39 | RB_TOTAL = RB_TOTAL + 1; |
32 | 40 |
|
|
39 | 47 | end |
40 | 48 | pass = threw; |
41 | 49 | report(testname, pass, 'expected an error'); |
42 | | - return; |
| 50 | + return |
43 | 51 | end |
44 | 52 |
|
45 | 53 | try |
46 | 54 | if (ischar(expected) && strcmp(expected, 'noerror')) |
47 | 55 | fhandle(varargin{:}); |
48 | 56 | pass = true; |
49 | 57 | report(testname, pass, 'no error'); |
50 | | - return; |
| 58 | + return |
51 | 59 | end |
52 | 60 | out = fhandle(varargin{:}); |
53 | 61 | [pass, msg] = compare(out, expected, RB_RTOL, RB_ATOL); |
|
69 | 77 | catch err |
70 | 78 | why = sprintf('predicate threw: %s', err.message); |
71 | 79 | end |
72 | | - return; |
| 80 | + return |
73 | 81 | end |
74 | 82 | if isnumeric(expected) || islogical(expected) |
75 | 83 | if ~isequal(size(actual), size(expected)) |
76 | 84 | why = sprintf('size mismatch: got [%s], expected [%s]', ... |
77 | 85 | sprintf('%d ', size(actual)), sprintf('%d ', size(expected))); |
78 | | - return; |
| 86 | + return |
79 | 87 | end |
80 | 88 | a = double(actual(:)); |
81 | 89 | e = double(expected(:)); |
82 | 90 | if any(isnan(a) ~= isnan(e)) |
83 | 91 | why = 'NaN pattern mismatch'; |
84 | | - return; |
| 92 | + return |
85 | 93 | end |
86 | 94 | a(isnan(a)) = 0; |
87 | 95 | e(isnan(e)) = 0; |
|
94 | 102 | why = sprintf('numerical mismatch at idx %d: |%g-%g|=%g > %g', ... |
95 | 103 | bad, a(bad), e(bad), diff(bad), tol(max(numel(tol), 1))); |
96 | 104 | end |
97 | | - return; |
| 105 | + return |
98 | 106 | end |
99 | 107 | if ischar(expected) |
100 | 108 | ok = ischar(actual) && strcmp(actual, expected); |
101 | 109 | if ~ok |
102 | 110 | why = 'string mismatch'; |
103 | 111 | end |
104 | | - return; |
| 112 | + return |
105 | 113 | end |
106 | 114 | if iscell(expected) |
107 | 115 | if ~iscell(actual) || ~isequal(size(actual), size(expected)) |
108 | 116 | why = 'cell shape mismatch'; |
109 | | - return; |
| 117 | + return |
110 | 118 | end |
111 | 119 | for k = 1:numel(expected) |
112 | 120 | [ok2, why2] = compare(actual{k}, expected{k}, rtol, atol); |
113 | 121 | if ~ok2 |
114 | 122 | why = sprintf('cell{%d}: %s', k, why2); |
115 | | - return; |
| 123 | + return |
116 | 124 | end |
117 | 125 | end |
118 | 126 | ok = true; |
119 | | - return; |
| 127 | + return |
120 | 128 | end |
121 | 129 | if isstruct(expected) |
122 | 130 | if ~isstruct(actual) |
123 | 131 | why = 'expected struct'; |
124 | | - return; |
| 132 | + return |
125 | 133 | end |
126 | 134 | fn = fieldnames(expected); |
127 | 135 | for k = 1:numel(fn) |
128 | 136 | if ~isfield(actual, fn{k}) |
129 | 137 | why = sprintf('missing field %s', fn{k}); |
130 | | - return; |
| 138 | + return |
131 | 139 | end |
132 | 140 | [ok2, why2] = compare(actual.(fn{k}), expected.(fn{k}), rtol, atol); |
133 | 141 | if ~ok2 |
134 | 142 | why = sprintf('field %s: %s', fn{k}, why2); |
135 | | - return; |
| 143 | + return |
136 | 144 | end |
137 | 145 | end |
138 | 146 | ok = true; |
139 | | - return; |
| 147 | + return |
140 | 148 | end |
141 | 149 | why = sprintf('unsupported expected type %s', class(expected)); |
142 | 150 |
|
143 | 151 | function report(testname, pass, msg) |
144 | | -global RB_FAIL; |
| 152 | +global RB_FAIL |
145 | 153 | if pass |
146 | 154 | fprintf(1, 'Testing %s: ok\n', testname); |
147 | 155 | else |
|
0 commit comments