0001 function unit_test_cmp(txt,a,b,tol)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 persistent ntotal;
0014 persistent test_start_time;
0015 persistent npass;
0016 if strcmp(txt,'RESET_COUNTER');
0017 ntotal=0; npass=0; test_start_time=cputime(); return;
0018 end
0019 if strcmp(txt,'SHOW_COUNTER');
0020 if ntotal == 0;
0021 eidors_msg('%s: pass %d/%d (t=%6.2fs)',a, ...
0022 npass, ntotal, cputime() - test_start_time, 0 );
0023 else
0024 eidors_msg('%s: pass %d/%d = %5.1f%% (t=%6.2fs)', a, ...
0025 npass, ntotal, 100*npass/ntotal, ...
0026 cputime() - test_start_time, 0 );
0027 end
0028 return;
0029 end
0030 if strcmp(txt,'UNIT_TEST_FCN');
0031 unit_test_cmp('RESET_COUNTER');
0032 feval(a,'UNIT_TEST');
0033 unit_test_cmp( 'SHOW_COUNTER',a);
0034 return
0035 end
0036
0037
0038 if strcmp(txt,'UNIT_TEST'); do_unit_test; return; end
0039
0040
0041 if nargin < 4; tol = 0; end
0042 if tol<0;
0043 expect_fail = 1;
0044 if tol==-inf; tol= 0; end
0045 else
0046 expect_fail= 0;
0047 end
0048 tolstr='';
0049
0050 fprintf('TEST: %20s = ',txt);
0051 ok='Fail';
0052 if (isnumeric(a) || islogical(a)) && ...
0053 (isnumeric(b) || islogical(b))
0054 sza = size(a); szb= size(b);
0055 eqsz= isequal( size(a), size(b));
0056 sza1 = all(sza==1); szb1 = all(szb==1);
0057 if ~eqsz && ~sza1 && ~szb1
0058 ok='Fail (size change)';
0059 else
0060 if isnan(a) == isnan(b);
0061 a(isnan(a))=0; b(isnan(b))=0;
0062 end;
0063 if all(abs(double(a) - double(b)) <= tol);
0064 ok='OK';
0065 end;
0066
0067 if abs(tol)>0
0068 tolstr= sprintf('(%1.3f x tol)', full(max(abs(a(:)-b(:))/tol)));
0069 end
0070 end
0071 else
0072 if strcmp(eidors_var_id(a), eidors_var_id(b)); ok='OK';end
0073 end
0074
0075 if expect_fail
0076 ok = 'OK (fail as expected)';
0077 end
0078
0079 fprintf('%4s %s\n', ok, tolstr);
0080 if strcmp(ok(1:2),'OK'); npass= npass+1; end
0081 ntotal= ntotal+1;
0082
0083 function do_unit_test
0084 unit_test_cmp('Expect OK' ,3,3);
0085
0086 unit_test_cmp('Expect Fail',1,1.01, -inf);
0087 unit_test_cmp('Expect OK' ,1,.99,.02);
0088 unit_test_cmp('Expect Fail',1,.99,-.002);
0089
0090 a= rand(10); b = a;
0091 unit_test_cmp('Expect OK' ,a,b);
0092 unit_test_cmp('Expect Fail',a,b+.001, -inf);
0093 unit_test_cmp('Expect OK ',a,b+.001, .002);
0094
0095 a(1,1) = NaN; b=a;
0096 unit_test_cmp('Expect OK' ,a,b);
0097 unit_test_cmp('Expect Fail',a,b+.001, -inf);
0098 unit_test_cmp('Expect OK ',a,b+.001, .002);
0099
0100 unit_test_cmp('Expect Fail',a,'boo', -inf);
0101 unit_test_cmp('Expect OK','boo','boo');
0102
0103 t.a= a; s.a=b;
0104 unit_test_cmp('Expect OK' ,t,s);
0105 s2.b= b;
0106 unit_test_cmp('Expect Fail' ,t,s2, -inf);
0107
0108 unit_test_cmp('Expect Fail', ones(3,3), ones(3,3,3), -inf);
0109 unit_test_cmp('Expect Fail', ones(3,1), ones(1,3), -inf);
0110 unit_test_cmp('Expect OK' ,3,[3,3,3,3]);
0111 unit_test_cmp('Expect OK' ,3,[3,3,3,3]);