Home > matpower7.1 > mptest > lib > t_is.m

t_is

PURPOSE ^

T_IS Tests if two matrices are identical to some tolerance.

SYNOPSIS ^

function ok = t_is(got, expected, prec, msg)

DESCRIPTION ^

T_IS  Tests if two matrices are identical to some tolerance.
   T_IS(GOT, EXPECTED, PREC, MSG) increments the global test count
   and if the maximum difference between corresponding elements of
   GOT and EXPECTED is less than 10^(-PREC) then it increments the
   passed tests count, otherwise increments the failed tests count.
   Prints 'ok' or 'not ok' followed by the MSG, unless T_BEGIN was
   called with input QUIET equal true. The input values can be real or
   complex, and they can be scalar, vector, or 2-d or higher matrices.
   If GOT is a vector or matrix and EXPECTED is a scalar or NaN, all
   elements must match the scalar. Intended to be called between calls
   to T_BEGIN and T_END.

   Optionally returns a true or false value indicating whether or
   not the test succeeded. NaN values are considered to be equal to
   each other.

   Example:
       quiet = 0;
       t_begin(5, quiet);
       t_ok(pi > 3, 'size of pi');
       t_skip(3, 'not yet written');
       t_is(2+2, 4, 12, '2+2 still equals 4');
       t_end;

   See also T_OK, T_SKIP, T_BEGIN, T_END, T_RUN_TESTS.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function ok = t_is(got, expected, prec, msg)
0002 %T_IS  Tests if two matrices are identical to some tolerance.
0003 %   T_IS(GOT, EXPECTED, PREC, MSG) increments the global test count
0004 %   and if the maximum difference between corresponding elements of
0005 %   GOT and EXPECTED is less than 10^(-PREC) then it increments the
0006 %   passed tests count, otherwise increments the failed tests count.
0007 %   Prints 'ok' or 'not ok' followed by the MSG, unless T_BEGIN was
0008 %   called with input QUIET equal true. The input values can be real or
0009 %   complex, and they can be scalar, vector, or 2-d or higher matrices.
0010 %   If GOT is a vector or matrix and EXPECTED is a scalar or NaN, all
0011 %   elements must match the scalar. Intended to be called between calls
0012 %   to T_BEGIN and T_END.
0013 %
0014 %   Optionally returns a true or false value indicating whether or
0015 %   not the test succeeded. NaN values are considered to be equal to
0016 %   each other.
0017 %
0018 %   Example:
0019 %       quiet = 0;
0020 %       t_begin(5, quiet);
0021 %       t_ok(pi > 3, 'size of pi');
0022 %       t_skip(3, 'not yet written');
0023 %       t_is(2+2, 4, 12, '2+2 still equals 4');
0024 %       t_end;
0025 %
0026 %   See also T_OK, T_SKIP, T_BEGIN, T_END, T_RUN_TESTS.
0027 
0028 %   MP-Test
0029 %   Copyright (c) 2004-2020, Power Systems Engineering Research Center (PSERC)
0030 %   by Ray Zimmerman, PSERC Cornell
0031 %
0032 %   This file is part of MP-Test.
0033 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0034 %   See https://github.com/MATPOWER/mptest for more info.
0035 
0036 global t_quiet;
0037 
0038 if nargin < 4
0039     msg = '';
0040 end
0041 if nargin < 3 || isempty(prec)
0042     prec = 5;
0043 end
0044 
0045 %% make sure we don't try to compare a double with an int
0046 %% (difference can appear to be zero when it's actually not)
0047 if isinteger(got) && ~isinteger(expected)
0048     got = double(got);
0049 end
0050 if ~isinteger(got) && isinteger(expected)
0051     expected = double(expected);
0052 end
0053 
0054 edims = size(expected);
0055 gdims = size(got);
0056 if (length(edims) == length(gdims) && all(edims == gdims)) || ...
0057         all(edims == 1) && ~any(gdims == 0)
0058     if all(edims == 0)
0059         condition = true;
0060     else
0061         %% check for NaNs!
0062         gNaN = find(isnan(got(:)));
0063         eNaN = find(isnan(expected(:)));
0064         if (~isscalar(expected) && ...
0065                 (length(gNaN) ~= length(eNaN) || sum(gNaN-eNaN) ~= 0)) || ...
0066             (isscalar(expected) && ...
0067                     (( isnan(expected) && ~all(isnan(got(:)))) || ...
0068                      (~isnan(expected) && any(isnan(got(:))))) )
0069             condition = false;
0070             max_diff = -1;
0071         elseif all(isnan(expected(:))) && all(isnan(got(:)))
0072             condition = true;
0073         else
0074             got_minus_expected = got - expected;
0075             max_diff = max(abs(got_minus_expected(:)));
0076             condition = ( max_diff < 10^(-prec) );
0077         end
0078     end
0079 else
0080     condition = false;
0081     max_diff = 0;
0082 end
0083 
0084 if isreal(got) && isreal(expected)
0085     cplx = 0;
0086 else
0087     cplx = 1;
0088 end
0089 
0090 t_ok(condition, msg);
0091 if ~condition && ~t_quiet
0092     if max_diff > 0
0093         k = find(~(abs(got_minus_expected(:)) < 10^(-prec)));
0094         [vv, kk] = max(abs(got_minus_expected(k)));
0095         fprintf('    index              got             expected      abs(got - exp)\n');
0096         fprintf('---------------  ----------------  ----------------  ----------------');
0097         for u = 1:length(k)
0098             if isscalar(expected)
0099                 ex = expected;
0100             else
0101                 ex = expected(k(u));
0102             end
0103             if isscalar(got)
0104                 idxstr = '(1)';
0105             else
0106                 idx = cell(1, length(gdims));
0107                 [idx{:}] = ind2sub(gdims, k(u));
0108                 idxstr = sprintf('%d,', idx{1:end-1});
0109                 idxstr = sprintf('(%s%d)', idxstr, idx{end});
0110             end
0111             if cplx
0112                 fprintf('\n%14s  %16s  %16s  %16g', ...
0113                     idxstr, format_complex(full(got(k(u))), '%g'), ...
0114                             format_complex(full(ex), '%g'), full(abs(got_minus_expected(k(u)))));
0115             else
0116                 fprintf('\n%14s  %16g  %16g  %16g', ...
0117                     idxstr, full(got(k(u))), full(ex), full(abs(got_minus_expected(k(u)))));
0118             end
0119             if u == kk
0120                 fprintf('  *');
0121                 idxstrkk = idxstr;
0122             end
0123         end
0124         fprintf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
0125             idxstrkk, full(max_diff), 10^(-prec));
0126     elseif max_diff == -1
0127         fprintf('    mismatch in locations of NaNs\n');
0128     else
0129         gidxstr = sprintf('%d x ', gdims(1:end-1));
0130         gidxstr = sprintf('%s%d', gidxstr, gdims(end));
0131         eidxstr = sprintf('%d x ', edims(1:end-1));
0132         eidxstr = sprintf('%s%d', eidxstr, edims(end));
0133         fprintf('    dimension mismatch:\n');
0134         fprintf('             got: %s\n', gidxstr);
0135         fprintf('        expected: %s\n\n', eidxstr);
0136     end
0137 end
0138 if nargout
0139     ok = condition;
0140 end
0141 
0142 function s = format_complex(v, fmt)
0143 if imag(v) < 0
0144     sgn = ' - ';
0145 else
0146     sgn = ' + ';
0147 end
0148 s = sprintf([fmt sgn fmt 'i'], real(v), abs(imag(v)));

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005