comparison scripts/testfun/assert.m @ 6494:76a1a953533d

[project @ 2007-04-05 16:09:03 by jwe]
author jwe
date Thu, 05 Apr 2007 16:09:03 +0000
parents e68b6921b221
children 93c65f2a5668
comparison
equal deleted inserted replaced
6493:5fa513371dde 6494:76a1a953533d
44 44
45 ## TODO: Output throttling: don't print out the entire 100x100 matrix, 45 ## TODO: Output throttling: don't print out the entire 100x100 matrix,
46 ## TODO: but instead give a summary; don't print out the whole list, just 46 ## TODO: but instead give a summary; don't print out the whole list, just
47 ## TODO: say what the first different element is, etc. To do this, make 47 ## TODO: say what the first different element is, etc. To do this, make
48 ## TODO: the message generation type specific. 48 ## TODO: the message generation type specific.
49 function assert(cond, expected, tol) 49
50 function assert (cond, expected, tol)
50 51
51 if (nargin < 1 || nargin > 3) 52 if (nargin < 1 || nargin > 3)
52 print_usage (); 53 print_usage ();
53 endif 54 endif
54 55
55 if (nargin < 3) 56 if (nargin < 3)
56 tol = 0; 57 tol = 0;
57 endif 58 endif
58 59
59 if exist("argn") == 0, argn=" "; endif 60 if (exist ("argn") == 0)
60 in = deblank(argn(1,:)); 61 argn = " ";
61 for i=2:rows(argn) 62 endif
62 in = [in, ",", deblank(argn(i,:))]; 63
64 in = deblank (argn(1,:));
65 for i = 2:rows (argn)
66 in = strcat (in, ",", deblank (argn(i,:)));
63 end 67 end
64 in = ["(",in,")"]; 68 in = strcat ("(", in, ")");
65 69
66 coda = ""; 70 coda = "";
67 iserror = 0; 71 iserror = 0;
68 if (nargin == 1) 72 if (nargin == 1)
69 if (!isnumeric(cond) || !all(cond(:))) 73 if (! isnumeric (cond) || ! all (cond(:)))
70 error ("assert %s failed", in); # say which elements failed? 74 error ("assert %s failed", in); # say which elements failed?
71 endif 75 endif
72 76
73 elseif (is_list(cond)) 77 elseif (is_list (cond))
74 if (!is_list(expected) || length(cond) != length(expected)) 78 if (! is_list (expected) || length (cond) != length (expected))
75 iserror = 1; 79 iserror = 1;
76 else 80 else
77 try 81 try
78 for i=1:length(cond) 82 for i = 1:length (cond)
79 assert(nth(cond,i),nth(expected,i)); 83 assert (nth (cond, i), nth (expected, i));
80 endfor 84 endfor
81 catch 85 catch
82 iserror = 1; 86 iserror = 1;
83 end 87 end
84 endif 88 endif
85 89
86 elseif (ischar (expected)) 90 elseif (ischar (expected))
87 iserror = (!ischar (cond) || !strcmp (cond, expected)); 91 iserror = (! ischar (cond) || ! strcmp (cond, expected));
88 92
89 elseif (iscell(expected)) 93 elseif (iscell (expected))
90 if (!iscell (cond) || any(size(cond)!=size(expected))) 94 if (! iscell (cond) || any (size (cond) != size (expected)))
91 iserror = 1; 95 iserror = 1;
92 else 96 else
93 try 97 try
94 for i=1:length(expected(:)) 98 for i = 1:length (expected(:))
95 assert(cond{i},expected{i},tol); 99 assert (cond{i}, expected{i}, tol);
96 endfor 100 endfor
97 catch 101 catch
98 iserror = 1; 102 iserror = 1;
99 end 103 end
100 endif 104 endif
101 105
102 elseif (isstruct (expected)) 106 elseif (isstruct (expected))
103 if (!isstruct (cond) || any(size(cond) != size(expected)) 107 if (! isstruct (cond) || any (size (cond) != size (expected))
104 || rows(struct_elements(cond)) != rows(struct_elements(expected))) 108 || rows(struct_elements (cond)) != rows (struct_elements (expected)))
105 iserror = 1; 109 iserror = 1;
106 else 110 else
107 try 111 try
108 empty=prod(size(cond))==0; 112 empty = numel (cond) == 0;
109 normal=prod(size(cond))==1; 113 normal = numel (cond) == 1;
110 for [v,k] = cond 114 for [v, k] = cond
111 if !struct_contains(expected,k), error; endif 115 if (! struct_contains (expected, k))
112 if empty, v = cell(1,0); endif 116 error ();
113 if normal, v = {v}; else v = v(:)'; endif 117 endif
114 assert(v,{expected.(k)},tol) 118 if (empty)
119 v = cell (1, 0);
120 endif
121 if (normal)
122 v = {v};
123 else
124 v = v(:)';
125 endif
126 assert (v, {expected.(k)}, tol);
115 endfor 127 endfor
116 catch 128 catch
117 iserror = 1; 129 iserror = 1;
118 end 130 end
119 endif 131 endif
121 elseif (ndims (cond) != ndims (expected) 133 elseif (ndims (cond) != ndims (expected)
122 || any (size (cond) != size (expected))) 134 || any (size (cond) != size (expected)))
123 iserror = 1; 135 iserror = 1;
124 coda = "Dimensions don't match"; 136 coda = "Dimensions don't match";
125 137
126 elseif tol==0 && !strcmp(typeinfo(cond),typeinfo(expected)) 138 elseif (tol == 0 && ! strcmp (typeinfo (cond), typeinfo (expected)))
127 iserror = 1; 139 iserror = 1;
128 coda = ["Type ",typeinfo(cond)," != ",typeinfo(expected)]; 140 coda = strcat ("Type ", typeinfo (cond), " != ", typeinfo (expected));
129 141
130 else # numeric 142 else # numeric
131 A=cond(:); B=expected(:); 143 A = cond(:);
144 B = expected(:);
132 ## Check exceptional values 145 ## Check exceptional values
133 if any(isnan(A) != isnan(B)) 146 if (any (isna (A) != isna (B)))
147 iserror = 1;
148 coda = "NAs don't match";
149 elseif (any (isnan (A) != isnan (B)))
134 iserror = 1; 150 iserror = 1;
135 coda = "NaNs don't match"; 151 coda = "NaNs don't match";
136 elseif any(isna(A) != isna(B)) 152 ### Try to avoid problems comparing strange values like Inf+NaNi.
137 iserror = 1; 153 elseif (any (isinf (A) != isinf (B))
138 coda = "NAs don't match"; 154 || any (A(isinf (A) & ! isnan (A)) != B(isinf (B) & ! isnan (B))))
139 ## Try to avoid problems comparing strange values like Inf+NaNi.
140 elseif (any(isinf(A) != isinf(B))
141 || any(A(isinf(A) & !isnan(A)) != B(isinf(B) & !isnan(B))))
142 iserror = 1; 155 iserror = 1;
143 coda = "Infs don't match"; 156 coda = "Infs don't match";
144 else 157 else
145 ## Check normal values 158 ## Check normal values
146 A = A(finite(A)); B=B(finite(B)); 159 A = A(finite (A));
147 if tol == 0, 160 B = B(finite (B));
148 err = any(A != B); 161 if (tol == 0)
162 err = any (A != B);
149 errtype = "values do not match"; 163 errtype = "values do not match";
150 elseif tol >= 0, 164 elseif (tol >= 0)
151 err = max(abs(A-B)); 165 err = max (abs (A - B));
152 errtype = "maximum absolute error %g exceeds tolerance %g"; 166 errtype = "maximum absolute error %g exceeds tolerance %g";
153 else 167 else
154 abserr = max(abs(A(B==0))); 168 abserr = max (abs (A(B == 0)));
155 A = A(B!=0); B = B(B!=0); 169 A = A(B != 0);
156 relerr = max(abs(A-B)./abs(B)); 170 B = B(B != 0);
157 err = max([abserr;relerr]); 171 relerr = max (abs (A - B) ./ abs (B));
172 err = max ([abserr; relerr]);
158 errtype = "maximum relative error %g exceeds tolerance %g"; 173 errtype = "maximum relative error %g exceeds tolerance %g";
159 endif 174 endif
160 if err > abs(tol) 175 if (err > abs (tol))
161 iserror = 1; 176 iserror = 1;
162 coda = sprintf(errtype,err,abs(tol)); 177 coda = sprintf (errtype, err, abs (tol));
163 endif 178 endif
164 endif 179 endif
165 endif 180 endif
166 181
167 if (!iserror) 182 if (! iserror)
168 return; 183 return;
169 endif 184 endif
170 185
171 ## pretty print the "expected but got" info, 186 ## pretty print the "expected but got" info,
172 ## trimming leading and trailing "\n" 187 ## trimming leading and trailing "\n"
173 str = disp (expected); 188 str = disp (expected);
174 idx = find(str!="\n"); 189 idx = find (str != "\n");
175 if (!isempty(idx)) 190 if (! isempty (idx))
176 str = str(idx(1):idx(length(idx))); 191 str = str(idx(1):idx(end));
177 endif 192 endif
178 str2 = disp (cond); 193 str2 = disp (cond);
179 idx = find(str2!="\n"); 194 idx = find (str2 != "\n");
180 if (!isempty(idx)) 195 if (! isempty (idx))
181 str2 = str2(idx(1):idx(length(idx))); 196 str2 = str2 (idx(1):idx(end));
182 endif 197 endif
183 msg = ["assert ",in," expected\n", str, "\nbut got\n", str2]; 198 msg = strcat ("assert ", in, " expected\n", str, "\nbut got\n", str2);
184 if (!isempty(coda)) 199 if (! isempty (coda))
185 msg = [ msg, "\n", coda ]; 200 msg = strcat (msg, "\n", coda);
186 endif 201 endif
187 error("%s",msg); 202 error ("%s", msg);
188 ## disp(msg); 203 ## disp (msg);
189 ## error("assertion failed"); 204 ## error ("assertion failed");
190 endfunction 205 endfunction
191 206
192 ## empty 207 ## empty
193 %!assert([]) 208 %!assert([])
194 %!assert(zeros(3,0),zeros(3,0)) 209 %!assert(zeros(3,0),zeros(3,0))