Home > matpower5.0 > 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 %   $Id: t_is.m 2447 2014-12-05 20:32:22Z ray $
0027 %   by Ray Zimmerman, PSERC Cornell
0028 %   Copyright (c) 2004-2010 by Power System Engineering Research Center (PSERC)
0029 %
0030 %   This file is part of MATPOWER.
0031 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0032 %
0033 %   MATPOWER is free software: you can redistribute it and/or modify
0034 %   it under the terms of the GNU General Public License as published
0035 %   by the Free Software Foundation, either version 3 of the License,
0036 %   or (at your option) any later version.
0037 %
0038 %   MATPOWER is distributed in the hope that it will be useful,
0039 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0040 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0041 %   GNU General Public License for more details.
0042 %
0043 %   You should have received a copy of the GNU General Public License
0044 %   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
0045 %
0046 %   Additional permission under GNU GPL version 3 section 7
0047 %
0048 %   If you modify MATPOWER, or any covered work, to interface with
0049 %   other modules (such as MATLAB code and MEX-files) available in a
0050 %   MATLAB(R) or comparable environment containing parts covered
0051 %   under other licensing terms, the licensors of MATPOWER grant
0052 %   you additional permission to convey the resulting work.
0053 
0054 global t_quiet;
0055 
0056 if nargin < 4
0057     msg = '';
0058 end
0059 if nargin < 3 || isempty(prec)
0060     prec = 5;
0061 end
0062 [m, n] = size(expected);
0063 if all(size(got) == [m, n]) || all([m, n] == [1 1])
0064     if m == 0 || n == 0
0065         condition = true;
0066     else
0067         %% check for NaNs!
0068         gNaN = find(isnan(got(:)));
0069         eNaN = find(isnan(expected(:)));
0070         if (~isscalar(expected) && ...
0071                 (length(gNaN) ~= length(eNaN) || sum(gNaN-eNaN) ~= 0)) || ...
0072             (isscalar(expected) && ...
0073                     (( isnan(expected) && ~all(isnan(got))) || ...
0074                      (~isnan(expected) && any(isnan(got)))) )
0075             condition = false;
0076             max_diff = -1;
0077         elseif all(all(isnan(got))) && all(all(isnan(got)))
0078             condition = true;
0079         else
0080             got_minus_expected = got - expected;
0081             max_diff = max(max(abs(got_minus_expected)));
0082             condition = ( max_diff < 10^(-prec) );
0083         end
0084     end
0085 else
0086     condition = false;
0087     max_diff = 0;
0088 end
0089 
0090 t_ok(condition, msg);
0091 if ~condition && ~t_quiet
0092     if max_diff > 0
0093         [i, j, v] = find(~(abs(got_minus_expected) < 10^(-prec)));
0094         k = i+(j-1)*m;
0095         [vv, kk] = max(abs(got_minus_expected(k)));
0096         fprintf('  row     col          got             expected          got - exp\n');
0097         fprintf('-------  ------  ----------------  ----------------  ----------------');
0098         for u = 1:length(i)
0099             if isscalar(expected)
0100                 ex = expected;
0101             else
0102                 ex = expected(k(u));
0103             end
0104             fprintf('\n%6d  %6d  %16g  %16g  %16g', ...
0105                 [i(u) j(u) got(k(u)) ex got_minus_expected(k(u))]');
0106             if u == kk
0107                 fprintf('  *');
0108             end
0109         end
0110         fprintf('\nmax diff @ (%d,%d) = %g > allowed tol of %g\n\n', ...
0111             i(kk), j(kk), max_diff, 10^(-prec));
0112     elseif max_diff == -1
0113         fprintf('    mismatch in locations of NaNs\n');
0114     else
0115         fprintf('    dimension mismatch:\n');
0116         fprintf('             got: %d x %d\n', size(got));
0117         fprintf('        expected: %d x %d\n\n', size(expected));
0118     end
0119 end
0120 if nargout
0121     ok = condition;
0122 end

Generated on Mon 26-Jan-2015 15:21:31 by m2html © 2005