Skip to content

Commit a367d88

Browse files
committed
Enhance t_is() to handle Inf values appropriately.
1 parent b1d57a6 commit a367d88

File tree

3 files changed

+122
-9
lines changed

3 files changed

+122
-9
lines changed

CHANGES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ Change history for MP-Test
22
==========================
33

44

5+
Since version 8.0
6+
-----------------
7+
8+
#### 3/21/25
9+
- Enhance `t_is()` to handle Inf values appropriately.
10+
11+
512
Version 8.0 - *May 10, 2024*
613
----------------------------
714

lib/t/t_test_fcns.m

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ function t_test_fcns(quiet)
2121
end
2222

2323
ntests = 5;
24-
npass = 29;
25-
nfail = 18;
24+
npass = 34;
25+
nfail = 27;
2626

2727
g = [];
2828
e = [];
@@ -62,36 +62,90 @@ function t_test_fcns(quiet)
6262
got = 3;
6363
g(k) = t_is(got, expected, tol, t);
6464

65+
k = k + 1; e(k) = 1;
66+
t = sprintf('%s : t_is(Inf, Inf, ...)', f(e(k)));
67+
expected = Inf;
68+
got = Inf;
69+
g(k) = t_is(got, expected, tol, t);
70+
71+
k = k + 1; e(k) = 1;
72+
t = sprintf('%s : t_is(-Inf, -Inf, ...)', f(e(k)));
73+
expected = -Inf;
74+
got = -Inf;
75+
g(k) = t_is(got, expected, tol, t);
76+
6577
k = k + 1; e(k) = 0;
6678
t = sprintf('%s : t_is(1, 3, ...)', f(e(k)));
6779
expected = 3;
6880
got = 1;
6981
g(k) = t_is(got, expected, tol, t);
7082

83+
k = k + 1; e(k) = 0;
84+
t = sprintf('%s : t_is(Inf, 3, ...)', f(e(k)));
85+
expected = 3;
86+
got = Inf;
87+
g(k) = t_is(got, expected, tol, t);
88+
89+
k = k + 1; e(k) = 0;
90+
t = sprintf('%s : t_is(1, -Inf, ...)', f(e(k)));
91+
expected = -Inf;
92+
got = 1;
93+
g(k) = t_is(got, expected, tol, t);
94+
7195
k = k + 1; e(k) = 1;
7296
t = sprintf('%s : t_is([3 3; 3 3], 3, ...)', f(e(k)));
7397
expected = 3;
7498
got = [3 3; 3 3];
7599
g(k) = t_is(got, expected, tol, t);
76100

101+
k = k + 1; e(k) = 1;
102+
t = sprintf('%s : t_is([Inf Inf; Inf Inf], Inf, ...)', f(e(k)));
103+
expected = Inf;
104+
got = [Inf Inf; Inf Inf];
105+
g(k) = t_is(got, expected, tol, t);
106+
77107
k = k + 1; e(k) = 0;
78108
t = sprintf('%s : t_is([3 4; 3 3], 3, ...)', f(e(k)));
79109
expected = 3;
80110
got = [3 4; 3 3];
81111
g(k) = t_is(got, expected, tol, t);
82112

113+
k = k + 1; e(k) = 0;
114+
t = sprintf('%s : t_is([Inf 4; Inf Inf], Inf, ...)', f(e(k)));
115+
expected = Inf;
116+
got = [Inf 4; Inf Inf];
117+
g(k) = t_is(got, expected, tol, t);
118+
83119
k = k + 1; e(k) = 1;
84120
t = sprintf('%s : t_is([3 3; 4 3], [3 3; 4 3], ...)', f(e(k)));
85121
expected = [3 3; 4 3];
86122
got = [3 3; 4 3];
87123
g(k) = t_is(got, expected, tol, t);
88124

125+
k = k + 1; e(k) = 1;
126+
t = sprintf('%s : t_is([3 -Inf; 4 3], [3 -Inf; 4 3], ...)', f(e(k)));
127+
expected = [3 -Inf; 4 3];
128+
got = [3 -Inf; 4 3];
129+
g(k) = t_is(got, expected, tol, t);
130+
89131
k = k + 1; e(k) = 0;
90132
t = sprintf('%s : t_is([3 4; 3 3], [3 3; 4 3], ...)', f(e(k)));
91133
expected = [3 3; 4 3];
92134
got = [3 4; 3 3];
93135
g(k) = t_is(got, expected, tol, t);
94136

137+
k = k + 1; e(k) = 0;
138+
t = sprintf('%s : t_is([3 4; 3 -Inf], [3 3; 4 3], ...)', f(e(k)));
139+
expected = [3 3; 4 3];
140+
got = [3 4; 3 -Inf];
141+
g(k) = t_is(got, expected, tol, t);
142+
143+
k = k + 1; e(k) = 0;
144+
t = sprintf('%s : t_is([3 3; 4 -Inf], [3 3; 4 Inf], ...)', f(e(k)));
145+
expected = [3 3; 4 Inf];
146+
got = [3 3; 4 -Inf];
147+
g(k) = t_is(got, expected, tol, t);
148+
95149
k = k + 1; e(k) = 1;
96150
t = sprintf('%s : t_is(ones(2,4,3), ones(2,4,3), ...)', f(e(k)));
97151
expected = ones(2,4,3);
@@ -148,12 +202,42 @@ function t_test_fcns(quiet)
148202
got = NaN;
149203
g(k) = t_is(got, expected, tol, t);
150204

205+
k = k + 1; e(k) = 0;
206+
t = sprintf('%s : t_is(-Inf, NaN, ...)', f(e(k)));
207+
expected = NaN;
208+
got = -Inf;
209+
g(k) = t_is(got, expected, tol, t);
210+
211+
k = k + 1; e(k) = 0;
212+
t = sprintf('%s : t_is(NaN, Inf, ...)', f(e(k)));
213+
expected = Inf;
214+
got = NaN;
215+
g(k) = t_is(got, expected, tol, t);
216+
151217
k = k + 1; e(k) = 1;
152218
t = sprintf('%s : t_is(NaN(3,2), NaN, ...)', f(e(k)));
153219
expected = NaN;
154220
got = NaN(3,2);
155221
g(k) = t_is(got, expected, tol, t);
156222

223+
k = k + 1; e(k) = 1;
224+
t = sprintf('%s : t_is([3 4; NaN Inf], [3 4; NaN Inf], ...)', f(e(k)));
225+
expected = [3 4; NaN Inf];
226+
got = [3 4; NaN Inf];
227+
g(k) = t_is(got, expected, tol, t);
228+
229+
k = k + 1; e(k) = 0;
230+
t = sprintf('%s : t_is([3 4; NaN Inf], [3 4; Inf NaN], ...)', f(e(k)));
231+
expected = [3 4; Inf NaN];
232+
got = [3 4; NaN Inf];
233+
g(k) = t_is(got, expected, tol, t);
234+
235+
k = k + 1; e(k) = 0;
236+
t = sprintf('%s : t_is([3 4.001; NaN Inf], [3 4; NaN Inf], ...)', f(e(k)));
237+
expected = [3 4; NaN Inf];
238+
got = [3 4.001; NaN Inf];
239+
g(k) = t_is(got, expected, 4, t);
240+
157241
k = k + 1; e(k) = 0;
158242
t = sprintf('%s : t_is(int32(0.6), 1.0, ...)', f(e(k)));
159243
expected = 0.6;

lib/t_is.m

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
% The input values can be real or complex, and they can be scalar, vector,
2626
% or 2-d or higher matrices. If ``got`` is a vector or matrix and
2727
% ``expected`` is a scalar or *NaN*, all elements must match the scalar.
28-
% *NaN* values are considered to be equal to each other.
28+
% *NaN* values are considered to be equal to each other, as are Inf values.
2929
%
3030
% Intended to be called between calls to t_begin and t_end.
3131
%
@@ -41,7 +41,7 @@
4141
% See also t_ok, t_file_match, t_str_match, t_skip, t_begin, t_end, t_run_tests.
4242

4343
% MP-Test
44-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
44+
% Copyright (c) 2004-2025, Power Systems Engineering Research Center (PSERC)
4545
% by Ray Zimmerman, PSERC Cornell
4646
%
4747
% This file is part of MP-Test.
@@ -86,14 +86,36 @@
8686
elseif all(isnan(expected(:))) && all(isnan(got(:)))
8787
condition = true;
8888
else
89+
have_inf = any(isinf(got(:))) || any(isinf(expected(:)));
90+
if have_inf
91+
pi_got = find(got(:) == Inf);
92+
ni_got = find(got(:) == -Inf);
93+
pi_expected = find(expected(:) == Inf);
94+
ni_expected = find(expected(:) == -Inf);
95+
got(pi_got) = 1e10;
96+
got(ni_got) = -1e10;
97+
expected(pi_expected) = 1e10;
98+
expected(ni_expected) = -1e10;
99+
end
89100
got_minus_expected = got - expected;
90-
max_diff = max(abs(got_minus_expected(:)));
101+
[max_diff, i] = max(abs(got_minus_expected(:)));
91102
condition = ( max_diff < 10^(-prec) );
103+
104+
if ~condition && have_inf
105+
if ismember(i, [pi_got; ni_got; pi_expected; ni_expected]) || ...
106+
(isscalar(expected) && ismember(1, [pi_expected; ni_expected]))
107+
max_diff = Inf;
108+
end
109+
got(pi_got) = Inf;
110+
got(ni_got) = -Inf;
111+
expected(pi_expected) = Inf;
112+
expected(ni_expected) = -Inf;
113+
end
92114
end
93115
end
94116
else
95117
condition = false;
96-
max_diff = 0;
118+
max_diff = 0; %% dimension mismatch
97119
end
98120

99121
if isreal(got) && isreal(expected)
@@ -105,7 +127,7 @@
105127
t_ok(condition, msg);
106128
if ~condition && ~t_quiet
107129
if max_diff > 0
108-
k = find(~(abs(got_minus_expected(:)) < 10^(-prec)));
130+
k = find(~(abs(got_minus_expected(:)) < 10^(-prec)) & ~isnan(got_minus_expected(:)));
109131
[vv, kk] = max(abs(got_minus_expected(k)));
110132
fprintf(' index got expected abs(got - exp)\n');
111133
fprintf('--------------- ---------------- ---------------- ----------------');
@@ -126,10 +148,10 @@
126148
if cplx
127149
fprintf('\n%14s %16s %16s %16g', ...
128150
idxstr, format_complex(full(got(k(u))), '%g'), ...
129-
format_complex(full(ex), '%g'), full(abs(got_minus_expected(k(u)))));
151+
format_complex(full(ex), '%g'), full(abs(got(k(u)) - ex)));
130152
else
131153
fprintf('\n%14s %16g %16g %16g', ...
132-
idxstr, full(got(k(u))), full(ex), full(abs(got_minus_expected(k(u)))));
154+
idxstr, full(got(k(u))), full(ex), full(abs(got(k(u)) - ex)));
133155
end
134156
if u == kk
135157
fprintf(' *');

0 commit comments

Comments
 (0)