Home > matpower7.0 > 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 the global
   variable t_quiet is 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's 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:

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 the global
0008 %   variable t_quiet is true. The input values can be real or complex,
0009 %   and they can be scalar, vector, or 2-d or higher matrices. If GOT
0010 %   is a vector or matrix and EXPECTED is a scalar or NaN, all elements
0011 %   must match the scalar. Intended to be called between calls to
0012 %   T_BEGIN and T_END.
0013 %
0014 %   Optionally returns a true or false value indicating whether or
0015 %   not the test succeeded. NaN's are considered to be equal to each
0016 %   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-2016, 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 t_ok(condition, msg);
0085 if ~condition && ~t_quiet
0086     if max_diff > 0
0087         k = find(~(abs(got_minus_expected(:)) < 10^(-prec)));
0088         [vv, kk] = max(abs(got_minus_expected(k)));
0089         fprintf('    index              got             expected      abs(got - exp)\n');
0090         fprintf('---------------  ----------------  ----------------  ----------------');
0091         for u = 1:length(k)
0092             if isscalar(expected)
0093                 ex = expected;
0094             else
0095                 ex = expected(k(u));
0096             end
0097             if isscalar(got)
0098                 idxstr = '(1)';
0099             else
0100                 idx = cell(1, length(gdims));
0101                 [idx{:}] = ind2sub(gdims, k(u));
0102                 idxstr = sprintf('%d,', idx{1:end-1});
0103                 idxstr = sprintf('(%s%d)', idxstr, idx{end});
0104             end
0105             fprintf('\n%14s  %16g  %16g  %16g', ...
0106                 idxstr, full(got(k(u))), full(ex), full(abs(got_minus_expected(k(u)))));
0107             if u == kk
0108                 fprintf('  *');
0109                 idxstrkk = idxstr;
0110             end
0111         end
0112         fprintf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
0113             idxstrkk, full(max_diff), 10^(-prec));
0114     elseif max_diff == -1
0115         fprintf('    mismatch in locations of NaNs\n');
0116     else
0117         gidxstr = sprintf('%d x ', gdims(1:end-1));
0118         gidxstr = sprintf('%s%d', gidxstr, gdims(end));
0119         eidxstr = sprintf('%d x ', edims(1:end-1));
0120         eidxstr = sprintf('%s%d', eidxstr, edims(end));
0121         fprintf('    dimension mismatch:\n');
0122         fprintf('             got: %s\n', gidxstr);
0123         fprintf('        expected: %s\n\n', eidxstr);
0124     end
0125 end
0126 if nargout
0127     ok = condition;
0128 end

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005