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 if isreal(got) && isreal(expected)
0085 cplx = 0;
0086 else
0087 cplx = 1;
0088 end
0089
0090 t_ok(condition, msg);
0091 if ~condition && ~t_quiet
0092 if max_diff > 0
0093 k = find(~(abs(got_minus_expected(:)) < 10^(-prec)));
0094 [vv, kk] = max(abs(got_minus_expected(k)));
0095 fprintf(' index got expected abs(got - exp)\n');
0096 fprintf('--------------- ---------------- ---------------- ----------------');
0097 for u = 1:length(k)
0098 if isscalar(expected)
0099 ex = expected;
0100 else
0101 ex = expected(k(u));
0102 end
0103 if isscalar(got)
0104 idxstr = '(1)';
0105 else
0106 idx = cell(1, length(gdims));
0107 [idx{:}] = ind2sub(gdims, k(u));
0108 idxstr = sprintf('%d,', idx{1:end-1});
0109 idxstr = sprintf('(%s%d)', idxstr, idx{end});
0110 end
0111 if cplx
0112 fprintf('\n%14s %16s %16s %16g', ...
0113 idxstr, format_complex(full(got(k(u))), '%g'), ...
0114 format_complex(full(ex), '%g'), full(abs(got_minus_expected(k(u)))));
0115 else
0116 fprintf('\n%14s %16g %16g %16g', ...
0117 idxstr, full(got(k(u))), full(ex), full(abs(got_minus_expected(k(u)))));
0118 end
0119 if u == kk
0120 fprintf(' *');
0121 idxstrkk = idxstr;
0122 end
0123 end
0124 fprintf('\nmax diff @ %s = %g > allowed tol of %g\n\n', ...
0125 idxstrkk, full(max_diff), 10^(-prec));
0126 elseif max_diff == -1
0127 fprintf(' mismatch in locations of NaNs\n');
0128 else
0129 gidxstr = sprintf('%d x ', gdims(1:end-1));
0130 gidxstr = sprintf('%s%d', gidxstr, gdims(end));
0131 eidxstr = sprintf('%d x ', edims(1:end-1));
0132 eidxstr = sprintf('%s%d', eidxstr, edims(end));
0133 fprintf(' dimension mismatch:\n');
0134 fprintf(' got: %s\n', gidxstr);
0135 fprintf(' expected: %s\n\n', eidxstr);
0136 end
0137 end
0138 if nargout
0139 ok = condition;
0140 end
0141
0142 function s = format_complex(v, fmt)
0143 if imag(v) < 0
0144 sgn = ' - ';
0145 else
0146 sgn = ' + ';
0147 end
0148 s = sprintf([fmt sgn fmt 'i'], real(v), abs(imag(v)));