Home > matpower5.1 > t > 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. 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. Intended to be called between calls to
0009 %   T_BEGIN and T_END.
0010 %
0011 %   Optionally returns a true or false value indicating whether or
0012 %   not the test succeeded. NaN's are considered to be equal to each
0013 %   other.
0014 %
0015 %   Example:
0016 %       quiet = 0;
0017 %       t_begin(5, quiet);
0018 %       t_ok(pi > 3, 'size of pi');
0019 %       t_skip(3, 'not yet written');
0020 %       t_is(2+2, 4, 12, '2+2 still equals 4');
0021 %       t_end;
0022 %
0023 %   See also T_OK, T_SKIP, T_BEGIN, T_END, T_RUN_TESTS.
0024 
0025 %   MATPOWER
0026 %   Copyright (c) 2004-2015 by Power System Engineering Research Center (PSERC)
0027 %   by Ray Zimmerman, PSERC Cornell
0028 %
0029 %   $Id: t_is.m 2644 2015-03-11 19:34:22Z ray $
0030 %
0031 %   This file is part of MATPOWER.
0032 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0033 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0034 
0035 global t_quiet;
0036 
0037 if nargin < 4
0038     msg = '';
0039 end
0040 if nargin < 3 || isempty(prec)
0041     prec = 5;
0042 end
0043 [m, n] = size(expected);
0044 if all(size(got) == [m, n]) || all([m, n] == [1 1])
0045     if m == 0 || n == 0
0046         condition = true;
0047     else
0048         %% check for NaNs!
0049         gNaN = find(isnan(got(:)));
0050         eNaN = find(isnan(expected(:)));
0051         if (~isscalar(expected) && ...
0052                 (length(gNaN) ~= length(eNaN) || sum(gNaN-eNaN) ~= 0)) || ...
0053             (isscalar(expected) && ...
0054                     (( isnan(expected) && ~all(isnan(got))) || ...
0055                      (~isnan(expected) && any(isnan(got)))) )
0056             condition = false;
0057             max_diff = -1;
0058         elseif all(all(isnan(got))) && all(all(isnan(got)))
0059             condition = true;
0060         else
0061             got_minus_expected = got - expected;
0062             max_diff = max(max(abs(got_minus_expected)));
0063             condition = ( max_diff < 10^(-prec) );
0064         end
0065     end
0066 else
0067     condition = false;
0068     max_diff = 0;
0069 end
0070 
0071 t_ok(condition, msg);
0072 if ~condition && ~t_quiet
0073     if max_diff > 0
0074         [i, j, v] = find(~(abs(got_minus_expected) < 10^(-prec)));
0075         k = i+(j-1)*m;
0076         [vv, kk] = max(abs(got_minus_expected(k)));
0077         fprintf('  row     col          got             expected          got - exp\n');
0078         fprintf('-------  ------  ----------------  ----------------  ----------------');
0079         for u = 1:length(i)
0080             if isscalar(expected)
0081                 ex = expected;
0082             else
0083                 ex = expected(k(u));
0084             end
0085             fprintf('\n%6d  %6d  %16g  %16g  %16g', ...
0086                 [i(u) j(u) got(k(u)) ex got_minus_expected(k(u))]');
0087             if u == kk
0088                 fprintf('  *');
0089             end
0090         end
0091         fprintf('\nmax diff @ (%d,%d) = %g > allowed tol of %g\n\n', ...
0092             i(kk), j(kk), max_diff, 10^(-prec));
0093     elseif max_diff == -1
0094         fprintf('    mismatch in locations of NaNs\n');
0095     else
0096         fprintf('    dimension mismatch:\n');
0097         fprintf('             got: %d x %d\n', size(got));
0098         fprintf('        expected: %d x %d\n\n', size(expected));
0099     end
0100 end
0101 if nargout
0102     ok = condition;
0103 end

Generated on Fri 20-Mar-2015 18:23:34 by m2html © 2005