0001 function ok = t_is(got, expected, prec, msg)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
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
0046
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
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