Skip to content

Commit f61a449

Browse files
committed
Switch from use of fprintf() to redirectable mp_printf().
1 parent 24d15f3 commit f61a449

10 files changed

Lines changed: 47 additions & 46 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ since 8.1
66
---------
77

88
#### 7/23/26
9+
- Use recently added `mp_printf()` in place of `fprintf()` everywhere to
10+
allow redirecting of console output.
911
- Add `mp.logger` class, and `mp_printf()` and `mp_disp()` functions
1012
which can be used as drop-in replacements for `fprintf()` and `disp()`.
1113
By default they behave identically, but can use an `mp.logger` object to

lib/mptestver.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
rv = v.Version;
3333
end
3434
else
35-
fprintf('%-22s Version %-10s %-11s\n', v.Name, v.Version, v.Date);
35+
mp_printf('%-22s Version %-10s %-11s\n', v.Name, v.Version, v.Date);
3636
end

lib/t/test_mptest.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
% See also t_run_tests.
1616

1717
% MP-Test
18-
% Copyright (c) 2016-2025, Power Systems Engineering Research Center (PSERC)
18+
% Copyright (c) 2016-2026, Power Systems Engineering Research Center (PSERC)
1919
% by Ray Zimmerman, PSERC Cornell
2020
%
2121
% This file is part of MP-Test.

lib/t_begin.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function t_begin(num_of_tests, quiet)
2828
% See also t_end, t_ok, t_is, t_str_match, t_file_match, t_skip, t_run_tests.
2929

3030
% MP-Test
31-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
31+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
3232
% by Ray Zimmerman, PSERC Cornell
3333
%
3434
% This file is part of MP-Test.
@@ -56,5 +56,5 @@ function t_begin(num_of_tests, quiet)
5656
t_clock = tic;
5757

5858
if ~t_quiet
59-
fprintf('1..%d\n', num_of_tests);
59+
mp_printf('1..%d\n', num_of_tests);
6060
end

lib/t_end.m

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
% See also t_begin, t_ok, t_is, t_str_match, t_file_match, t_skip, t_run_tests.
2121

2222
% MP-Test
23-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
23+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
2424
% by Ray Zimmerman, PSERC Cornell
2525
%
2626
% This file is part of MP-Test.
@@ -47,33 +47,33 @@
4747

4848
if t_quiet
4949
if all_ok
50-
fprintf('ok');
50+
mp_printf('ok');
5151
if t_skip_cnt
52-
fprintf(' (%d of %d skipped)', t_skip_cnt, t_num_of_tests);
52+
mp_printf(' (%d of %d skipped)', t_skip_cnt, t_num_of_tests);
5353
end
5454
else
55-
fprintf('not ok\n');
56-
fprintf('\t##### Ran %d of %d tests: %d passed, %d failed', ...
55+
mp_printf('not ok\n');
56+
mp_printf('\t##### Ran %d of %d tests: %d passed, %d failed', ...
5757
t_counter, t_num_of_tests, t_ok_cnt, t_not_ok_cnt);
5858
if t_skip_cnt
59-
fprintf(', %d skipped', t_skip_cnt);
59+
mp_printf(', %d skipped', t_skip_cnt);
6060
end
6161
end
62-
fprintf('\n');
62+
mp_printf('\n');
6363
else
6464
if all_ok
6565
if t_skip_cnt
66-
fprintf('All tests successful (%d passed, %d skipped of %d)', ...
66+
mp_printf('All tests successful (%d passed, %d skipped of %d)', ...
6767
t_ok_cnt, t_skip_cnt, t_num_of_tests);
6868
else
69-
fprintf('All tests successful (%d of %d)', t_ok_cnt, t_num_of_tests);
69+
mp_printf('All tests successful (%d of %d)', t_ok_cnt, t_num_of_tests);
7070
end
7171
else
72-
fprintf('Ran %d of %d tests: %d passed, %d failed', ...
72+
mp_printf('Ran %d of %d tests: %d passed, %d failed', ...
7373
t_counter, t_num_of_tests, t_ok_cnt, t_not_ok_cnt);
7474
if t_skip_cnt
75-
fprintf(', %d skipped', t_skip_cnt);
75+
mp_printf(', %d skipped', t_skip_cnt);
7676
end
7777
end
78-
fprintf('\nElapsed time %.2f seconds.\n', toc(t_clock));
78+
mp_printf('\nElapsed time %.2f seconds.\n', toc(t_clock));
7979
end

lib/t_is.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@
129129
if max_diff > 0
130130
k = find(~(abs(got_minus_expected(:)) < 10^(-prec)) & ~isnan(got_minus_expected(:)));
131131
nk = length(k);
132-
fprintf(' index got expected abs(got - exp)\n');
133-
fprintf('--------------- ---------------- ---------------- ----------------');
132+
mp_printf(' index got expected abs(got - exp)\n');
133+
mp_printf('--------------- ---------------- ---------------- ----------------');
134134
if nk > 15
135135
[~, kk] = sort(abs(got_minus_expected(k)), 'descend');
136136
k = k(kk(1:10));
@@ -153,33 +153,33 @@
153153
idxstr = sprintf('(%s%d)', idxstr, idx{end});
154154
end
155155
if cplx
156-
fprintf('\n%14s %16s %16s %16g', ...
156+
mp_printf('\n%14s %16s %16s %16g', ...
157157
idxstr, format_complex(full(got(k(u))), '%g'), ...
158158
format_complex(full(ex), '%g'), full(abs(got(k(u)) - ex)));
159159
else
160-
fprintf('\n%14s %16g %16g %16g', ...
160+
mp_printf('\n%14s %16g %16g %16g', ...
161161
idxstr, full(got(k(u))), full(ex), full(abs(got(k(u)) - ex)));
162162
end
163163
if u == kk
164-
fprintf(' *');
164+
mp_printf(' *');
165165
idxstrkk = idxstr;
166166
end
167167
end
168168
if nk > 15
169-
fprintf('\n ... and %d more ...', nk-10);
169+
mp_printf('\n ... and %d more ...', nk-10);
170170
end
171-
fprintf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
171+
mp_printf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
172172
idxstrkk, full(max_diff), 10^(-prec));
173173
elseif max_diff == -1
174-
fprintf(' mismatch in locations of NaNs\n');
174+
mp_printf(' mismatch in locations of NaNs\n');
175175
else
176176
gidxstr = sprintf('%d x ', gdims(1:end-1));
177177
gidxstr = sprintf('%s%d', gidxstr, gdims(end));
178178
eidxstr = sprintf('%d x ', edims(1:end-1));
179179
eidxstr = sprintf('%s%d', eidxstr, edims(end));
180-
fprintf(' dimension mismatch:\n');
181-
fprintf(' got: %s\n', gidxstr);
182-
fprintf(' expected: %s\n\n', eidxstr);
180+
mp_printf(' dimension mismatch:\n');
181+
mp_printf(' got: %s\n', gidxstr);
182+
mp_printf(' expected: %s\n\n', eidxstr);
183183
end
184184
end
185185
if nargout

lib/t_ok.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
% See also t_is, t_file_match, t_str_match, t_skip, t_begin, t_end, t_run_tests.
3434

3535
% MP-Test
36-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
36+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
3737
% by Ray Zimmerman, PSERC Cornell
3838
%
3939
% This file is part of MP-Test.
@@ -55,11 +55,11 @@
5555
else
5656
t_not_ok_cnt = t_not_ok_cnt + 1;
5757
if ~t_quiet
58-
fprintf('not ');
58+
mp_printf('not ');
5959
end
6060
end
6161
if ~t_quiet
62-
fprintf('ok %d%s\n', t_counter, msg);
62+
mp_printf('ok %d%s\n', t_counter, msg);
6363
end
6464
t_counter = t_counter + 1;
6565
if nargout

lib/t_run_tests.m

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
% See also t_begin, t_end.
2929

3030
% MP-Test
31-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
31+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
3232
% by Ray Zimmerman, PSERC Cornell
3333
%
3434
% This file is part of MP-Test.
@@ -64,11 +64,10 @@
6464
t0 = tic;
6565
for k = 1:length(test_names)
6666
if verbose
67-
fprintf('\n---------- %s ----------\n', test_names{k});
67+
mp_printf('\n---------- %s ----------\n', test_names{k});
6868
else
6969
pad = maxlen + 4 - length(test_names{k});
70-
fprintf('%s', test_names{k});
71-
for m = 1:pad, fprintf('.'); end
70+
mp_printf('%s%s', test_names{k}, repmat('.', [1 pad]));
7271
end
7372
feval( test_names{k}, ~verbose );
7473

@@ -80,25 +79,25 @@
8079
end
8180

8281
if verbose
83-
fprintf('\n\n---------- Summary ----------\n');
82+
mp_printf('\n\n---------- Summary ----------\n');
8483
end
8584
if counter == num_of_tests && counter == ok_cnt + skip_cnt && not_ok_cnt == 0
8685
all_ok = 1;
8786
if skip_cnt
88-
fprintf('All tests successful (%d passed, %d skipped of %d)', ...
87+
mp_printf('All tests successful (%d passed, %d skipped of %d)', ...
8988
ok_cnt, skip_cnt, num_of_tests);
9089
else
91-
fprintf('All tests successful (%d of %d)', ok_cnt, num_of_tests);
90+
mp_printf('All tests successful (%d of %d)', ok_cnt, num_of_tests);
9291
end
9392
else
9493
all_ok = 0;
95-
fprintf('Ran %d of %d tests: %d passed, %d failed', ...
94+
mp_printf('Ran %d of %d tests: %d passed, %d failed', ...
9695
counter, num_of_tests, ok_cnt, not_ok_cnt);
9796
if skip_cnt
98-
fprintf(', %d skipped', skip_cnt);
97+
mp_printf(', %d skipped', skip_cnt);
9998
end
10099
end
101-
fprintf('\nElapsed time %.2f seconds.\n', toc(t0));
100+
mp_printf('\nElapsed time %.2f seconds.\n', toc(t0));
102101

103102
if nargout
104103
all_ok_ = all_ok; %% copy to optional output arg

lib/t_skip.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function t_skip(cnt, msg)
2626
% See also t_ok, t_is, t_file_match, t_str_match, t_begin, t_end, t_run_tests.
2727

2828
% MP-Test
29-
% Copyright (c) 2004-2024, Power Systems Engineering Research Center (PSERC)
29+
% Copyright (c) 2004-2026, Power Systems Engineering Research Center (PSERC)
3030
% by Ray Zimmerman, PSERC Cornell
3131
%
3232
% This file is part of MP-Test.
@@ -46,9 +46,9 @@ function t_skip(cnt, msg)
4646
t_skip_cnt = t_skip_cnt + cnt;
4747
if ~t_quiet
4848
if cnt == 1
49-
fprintf('skipped %d%s\n', t_counter, msg);
49+
mp_printf('skipped %d%s\n', t_counter, msg);
5050
else
51-
fprintf('skipped %d..%d%s\n', t_counter, t_counter+cnt-1, msg);
51+
mp_printf('skipped %d..%d%s\n', t_counter, t_counter+cnt-1, msg);
5252
end
5353
end
5454
t_counter = t_counter + cnt;

lib/toggle_debug_mode.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function toggle_debug_mode(on_off, quiet)
1616
%
1717
% See also debug_assert.
1818

19-
% Copyright (c) 2024-2025, Ray Zimmerman
19+
% Copyright (c) 2024-2026, Ray Zimmerman
2020

2121
global DEBUG_MODE
2222
if nargin && ~isempty(on_off)
@@ -32,8 +32,8 @@ function toggle_debug_mode(on_off, quiet)
3232
end
3333
if nargin < 2 || ~quiet
3434
if DEBUG_MODE
35-
fprintf('DEBUG_MODE is ON\n');
35+
mp_printf('DEBUG_MODE is ON\n');
3636
else
37-
fprintf('DEBUG_MODE is OFF\n');
37+
mp_printf('DEBUG_MODE is OFF\n');
3838
end
3939
end

0 commit comments

Comments
 (0)