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 edims = size(expected);
0045 gdims = size(got);
0046 if (length(edims) == length(gdims) && all(edims == gdims)) || ...
0047 all(edims == 1) && ~any(gdims == 0)
0048 if all(edims == 0)
0049 condition = true;
0050 else
0051
0052 gNaN = find(isnan(got(:)));
0053 eNaN = find(isnan(expected(:)));
0054 if (~isscalar(expected) && ...
0055 (length(gNaN) ~= length(eNaN) || sum(gNaN-eNaN) ~= 0)) || ...
0056 (isscalar(expected) && ...
0057 (( isnan(expected) && ~all(isnan(got(:)))) || ...
0058 (~isnan(expected) && any(isnan(got(:))))) )
0059 condition = false;
0060 max_diff = -1;
0061 elseif all(isnan(expected(:))) && all(isnan(got(:)))
0062 condition = true;
0063 else
0064 got_minus_expected = got - expected;
0065 max_diff = max(abs(got_minus_expected(:)));
0066 condition = ( max_diff < 10^(-prec) );
0067 end
0068 end
0069 else
0070 condition = false;
0071 max_diff = 0;
0072 end
0073
0074 t_ok(condition, msg);
0075 if ~condition && ~t_quiet
0076 if max_diff > 0
0077 k = find(~(abs(got_minus_expected(:)) < 10^(-prec)));
0078 [vv, kk] = max(abs(got_minus_expected(k)));
0079 fprintf(' index got expected got - exp\n');
0080 fprintf('--------------- ---------------- ---------------- ----------------');
0081 for u = 1:length(k)
0082 if isscalar(expected)
0083 ex = expected;
0084 else
0085 ex = expected(k(u));
0086 end
0087 if isscalar(got)
0088 idxstr = '(1)';
0089 else
0090 idx = cell(1, length(gdims));
0091 [idx{:}] = ind2sub(gdims, k(u));
0092 idxstr = sprintf('%d,', idx{1:end-1});
0093 idxstr = sprintf('(%s%d)', idxstr, idx{end});
0094 end
0095 fprintf('\n%14s %16g %16g %16g', ...
0096 idxstr, got(k(u)), ex, got_minus_expected(k(u)));
0097 if u == kk
0098 fprintf(' *');
0099 idxstrkk = idxstr;
0100 end
0101 end
0102 fprintf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
0103 idxstrkk, max_diff, 10^(-prec));
0104 elseif max_diff == -1
0105 fprintf(' mismatch in locations of NaNs\n');
0106 else
0107 gidxstr = sprintf('%d x ', gdims(1:end-1));
0108 gidxstr = sprintf('%s%d', gidxstr, gdims(end));
0109 eidxstr = sprintf('%d x ', edims(1:end-1));
0110 eidxstr = sprintf('%s%d', eidxstr, edims(end));
0111 fprintf(' dimension mismatch:\n');
0112 fprintf(' got: %s\n', gidxstr);
0113 fprintf(' expected: %s\n\n', eidxstr);
0114 end
0115 end
0116 if nargout
0117 ok = condition;
0118 end