/[ascend]/branches/relerrorlist/ascend/utilities/test/test_error.c
ViewVC logotype

Contents of /branches/relerrorlist/ascend/utilities/test/test_error.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3197 - (show annotations) (download) (as text)
Sun Apr 23 03:30:25 2017 UTC (14 months ago) by jpye
File MIME type: text/x-csrc
File size: 11925 byte(s)
test suite almost done, one memory leak still to be fixed.

1 /* ASCEND modelling environment
2 Copyright (C) 2017 John Pye
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <stdio.h>
18 #define ASC_BUILDING_INTERFACE
19 #include <ascend/general/platform.h>
20 #ifdef __WIN32__
21 #include <io.h>
22 #endif
23 #include <ascend/utilities/error.h>
24 #include <ascend/general/ascMalloc.h>
25 #include <test/common.h>
26
27 //#define TESTERROR_DEBUG
28 #ifdef TESTERROR_DEBUG
29 # define MSG CONSOLE_DEBUG
30 #else
31 # define MSG(ARGS...) ((void)0)
32 #endif
33
34 #define MESSAGEMAX 1000
35 #define NMESSAGES 1000
36 char messages[NMESSAGES][MESSAGEMAX];
37 const char *filenames[NMESSAGES];
38 const char *funcnames[NMESSAGES];
39 error_severity_t sevs[NMESSAGES];
40 int lines[NMESSAGES];
41 int currmessage = 0;
42
43 int reporter_test(const error_severity_t sev, const char *filename
44 , const int line, const char *funcname, const char *fmt, va_list args
45 ){
46 assert(currmessage >= 0);
47 assert(currmessage < NMESSAGES);
48 int n = snprintf(messages[currmessage],MESSAGEMAX,fmt,args);
49 MSG(fmt,args);
50 if(n > MESSAGEMAX - 10){
51 assert(0);
52 }
53 filenames[currmessage] = filename;
54 lines[currmessage] = line;
55 funcnames[currmessage] = funcname;
56 sevs[currmessage] = sev;
57 currmessage++;
58 return currmessage;
59 }
60
61 error_reporter_tree_t *TREECURRENT(){
62 return error_reporter_get_tree_current();
63 }
64
65 static void test_error(void){
66 unsigned long prior_meminuse;
67 prior_meminuse = ascmeminuse();
68
69 currmessage = 0;
70 error_reporter_set_callback(reporter_test);
71 CU_TEST(currmessage == 0);
72
73 error_reporter(ASC_USER_ERROR,"thisfile",987,NULL,"hello");
74
75 CU_TEST(currmessage == 1);
76 CU_TEST(strcmp(filenames[0],"thisfile")==0);
77 CU_TEST(lines[0] == 987);
78 CU_TEST(strcmp(messages[0],"hello")==0);
79 CU_TEST(sevs[0] == ASC_USER_ERROR);
80
81 error_reporter(ASC_USER_NOTE,"otherfile",123,"funcname","salaam");
82
83 CU_TEST(currmessage == 2);
84 CU_TEST(strcmp(filenames[0],"thisfile")==0);
85 CU_TEST(lines[0] == 987);
86 CU_TEST(strcmp(messages[0],"hello")==0);
87 CU_TEST(sevs[0] == ASC_USER_ERROR);
88 CU_TEST(strcmp(filenames[1], "otherfile")==0);
89 CU_TEST(lines[1] == 123);
90 CU_TEST(strcmp(messages[1],"salaam")==0);
91 CU_TEST(sevs[1] == ASC_USER_NOTE);
92
93 ERROR_REPORTER_HERE(ASC_PROG_ERR,"bonjour");
94 CU_TEST(currmessage == 3);
95 CU_TEST(strcmp(filenames[1], "otherfile")==0);
96 CU_TEST(lines[1] == 123);
97 CU_TEST(sevs[1] == ASC_USER_NOTE);
98 CU_TEST(strcmp(messages[1],"salaam")==0);
99 CU_TEST(strcmp(messages[2],"bonjour")==0);
100 CU_TEST(sevs[2] == ASC_PROG_ERR);
101
102 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
103 }
104
105 static void test_errortree1(void){
106 unsigned long prior_meminuse;
107 prior_meminuse = ascmeminuse();
108 CU_TEST(NULL == TREECURRENT());
109 error_reporter_set_callback(reporter_test);
110
111 // test a non-caching tree with one message
112 currmessage = 0;
113 error_reporter_tree_t *T1 = error_reporter_tree_start(0);
114
115 error_reporter(ASC_USER_NOTE,"otherfile",123,"funcname","salaam");
116 CU_TEST(currmessage == 1);
117
118 CU_TEST(NULL != TREECURRENT());
119
120 CU_TEST(T1 == TREECURRENT());
121 error_reporter_tree_end(T1);
122
123 CU_TEST(NULL == TREECURRENT());
124
125 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
126 }
127
128
129 static void test_errortree2(void){
130 unsigned long prior_meminuse;
131 prior_meminuse = ascmeminuse();
132 CU_TEST(NULL == TREECURRENT());
133 error_reporter_set_callback(reporter_test);
134
135 // test a caching tree with three messages
136 currmessage = 0;
137 error_reporter_tree_t *T2 = error_reporter_tree_start(1);
138 error_reporter(ASC_USER_NOTE,"otherfile",123,"funcname","salaam1");
139 error_reporter(ASC_USER_NOTE,"otherfile",111,"funcname","salaam2");
140 error_reporter(ASC_USER_NOTE,"otherfile",333,"funcname","salaam3");
141 CU_TEST(currmessage == 0);
142 CU_TEST(T2 == TREECURRENT());
143 error_reporter_tree_end(T2);
144 CU_TEST(currmessage == 3);
145 CU_TEST(strcmp(messages[0],"salaam1")==0);
146 CU_TEST(strcmp(messages[1],"salaam2")==0);
147 CU_TEST(strcmp(messages[2],"salaam3")==0);
148 CU_TEST(TREECURRENT()==NULL);
149
150 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
151 }
152
153 static void test_errortree3(void){
154 unsigned long prior_meminuse;
155 prior_meminuse = ascmeminuse();
156 CU_TEST(NULL == TREECURRENT());
157 error_reporter_set_callback(reporter_test);
158
159 // test a non-caching tree with three messages
160 currmessage = 0;
161 error_reporter_tree_t *T3 = error_reporter_tree_start(0);
162 error_reporter(ASC_USER_NOTE,"otherfile",123,"funcname","hola1");
163 error_reporter(ASC_USER_NOTE,"otherfile",111,"funcname","hola2");
164 error_reporter(ASC_USER_NOTE,"otherfile",333,"funcname","hola3");
165 CU_TEST(currmessage == 3);
166 CU_TEST(strcmp(messages[0],"hola1")==0);
167 CU_TEST(strcmp(messages[1],"hola2")==0);
168 CU_TEST(strcmp(messages[2],"hola3")==0);
169
170 CU_TEST(T3 == TREECURRENT());
171 error_reporter_tree_end(T3);
172 CU_TEST(currmessage == 3);
173
174 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
175 }
176
177 static void test_errortree4(void){
178 unsigned long prior_meminuse;
179 prior_meminuse = ascmeminuse();
180 CU_TEST(NULL == TREECURRENT());
181 error_reporter_set_callback(reporter_test);
182
183 // test a message, then a non caching tree with a non-caching subtree
184 currmessage = 0;
185 error_reporter(ASC_USER_NOTE,"f1",111,NULL,"allo0");
186 CU_TEST(currmessage == 1)
187 CU_TEST(strcmp(messages[0],"allo0")==0);
188 error_reporter_tree_t *T4 = error_reporter_tree_start(0);
189 error_reporter(ASC_USER_NOTE,"f2",222,"fn1","allo1");
190 error_reporter(ASC_USER_NOTE,"f2",333,"fn2","allo2");
191 error_reporter(ASC_USER_NOTE,"f2",444,"fn3","allo3");
192 CU_TEST(strcmp(messages[1],"allo1")==0);
193 CU_TEST(strcmp(messages[2],"allo2")==0);
194 CU_TEST(strcmp(messages[3],"allo3")==0);
195 CU_TEST(lines[3]==444);
196 CU_TEST(lines[2]==333);
197 CU_TEST(lines[1]==222);
198 CU_TEST(currmessage == 4);
199 CU_TEST(T4 == TREECURRENT());
200 error_reporter_tree_t *t1 = TREECURRENT();
201 error_reporter_tree_t *T5 = error_reporter_tree_start(0);
202 CU_TEST(T5 == TREECURRENT());
203 CU_TEST(currmessage == 4);
204 CU_TEST(TREECURRENT()!=t1);
205 error_reporter(ASC_USER_NOTE,"f3",555,"fn4","buongiorno");
206 CU_TEST(currmessage == 5);
207 error_reporter_tree_end(T5);
208 CU_TEST(T4 == TREECURRENT());
209 CU_TEST(currmessage == 5);
210 CU_TEST(TREECURRENT()==t1);
211 CU_TEST(!error_reporter_tree_has_error(T4));
212 error_reporter_tree_end(T4);
213 CU_TEST(currmessage == 5);
214 CU_TEST(TREECURRENT()==NULL);
215
216 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
217 }
218
219 static void test_errortree5(void){
220 unsigned long prior_meminuse;
221 prior_meminuse = ascmeminuse();
222 CU_TEST(NULL == TREECURRENT());
223 error_reporter_set_callback(reporter_test);
224
225 // test a message, then a caching tree with a non-caching subtree
226 currmessage = 0;
227 error_reporter(ASC_USER_NOTE,"f1",111,NULL,"allo0");
228 CU_TEST(currmessage == 1)
229 CU_TEST(strcmp(messages[0],"allo0")==0);
230 error_reporter_tree_t *T6 = error_reporter_tree_start(1); // caching
231 error_reporter(ASC_USER_NOTE,"f2",222,"fn1","allo1");
232 error_reporter(ASC_USER_NOTE,"f2",333,"fn2","allo2");
233 error_reporter(ASC_USER_NOTE,"f2",444,"fn3","allo3");
234 CU_TEST(currmessage == 1);
235 error_reporter_tree_t *t1 = TREECURRENT();
236 error_reporter_tree_t *T7 = error_reporter_tree_start(0);
237 CU_TEST(currmessage == 1);
238 CU_TEST(TREECURRENT()!=t1);
239 error_reporter(ASC_PROG_ERR,"f3",555,"fn4","buongiorno");
240 CU_TEST(currmessage == 1);
241 error_reporter_tree_end(T7);
242 CU_TEST(T6 == TREECURRENT());
243 CU_TEST(currmessage == 1);
244 CU_TEST(TREECURRENT()==t1);
245 CU_TEST(error_reporter_tree_has_error(T6));
246 error_reporter_tree_end(T6);
247 CU_TEST(lines[1]==222);
248 CU_TEST(lines[2]==333);
249 CU_TEST(lines[3]==444);
250 CU_TEST(lines[4]==555);
251 CU_TEST(strcmp(messages[1],"allo1")==0);
252 CU_TEST(strcmp(messages[2],"allo2")==0);
253 CU_TEST(strcmp(messages[3],"allo3")==0);
254 CU_TEST(strcmp(messages[4],"buongiorno")==0);
255 CU_TEST(currmessage == 5);
256 CU_TEST(TREECURRENT()==NULL);
257
258 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
259 }
260
261 static void test_errortree6(void){
262 unsigned long prior_meminuse;
263 prior_meminuse = ascmeminuse();
264 CU_TEST(NULL == TREECURRENT());
265 error_reporter_set_callback(reporter_test);
266
267 // test a message, then a non-caching tree with a caching subtree
268 currmessage = 0;
269 error_reporter(ASC_USER_NOTE,"f1",111,NULL,"allo0");
270 CU_TEST(currmessage == 1)
271 CU_TEST(strcmp(messages[0],"allo0")==0);
272 error_reporter_tree_t *T8 = error_reporter_tree_start(0); // non-caching
273 error_reporter(ASC_USER_NOTE,"f2",222,"fn1","allo1");
274 error_reporter(ASC_USER_NOTE,"f2",333,"fn2","allo2");
275 error_reporter(ASC_USER_NOTE,"f2",444,"fn3","allo3");
276 CU_TEST(currmessage == 4);
277 error_reporter_tree_t *t1 = TREECURRENT();
278 error_reporter_tree_t *T9 = error_reporter_tree_start(1); // caching
279 CU_TEST(currmessage == 4);
280 CU_TEST(TREECURRENT()!=t1);
281 error_reporter(ASC_USER_ERROR,"f3",555,"fn4","buongiorno");
282 CU_TEST(currmessage == 4);
283 CU_TEST(T9 == TREECURRENT());
284 error_reporter_tree_end(T9);
285 CU_TEST(T8 == TREECURRENT());
286 CU_TEST(currmessage == 5);
287 CU_TEST(TREECURRENT()==t1);
288 CU_TEST(error_reporter_tree_has_error(T8));
289 error_reporter_tree_end(T8);
290 CU_TEST(lines[1]==222);
291 CU_TEST(lines[2]==333);
292 CU_TEST(lines[3]==444);
293 CU_TEST(lines[4]==555);
294 CU_TEST(strcmp(messages[0],"allo0")==0);
295 CU_TEST(strcmp(messages[1],"allo1")==0);
296 CU_TEST(strcmp(messages[2],"allo2")==0);
297 CU_TEST(strcmp(messages[3],"allo3")==0);
298 CU_TEST(strcmp(messages[4],"buongiorno")==0);
299 CU_TEST(currmessage == 5);
300 CU_TEST(TREECURRENT()==NULL);
301
302 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
303 }
304
305 static void test_errortree7(void){
306 unsigned long prior_meminuse;
307 prior_meminuse = ascmeminuse();
308 CU_TEST(NULL == TREECURRENT());
309 error_reporter_set_callback(reporter_test);
310
311 // test clearing away a nested error
312 currmessage = 0;
313 error_reporter(ASC_USER_NOTE,"f1",111,NULL,"allo0");
314 CU_TEST(currmessage == 1)
315 CU_TEST(strcmp(messages[0],"allo0")==0);
316 error_reporter_tree_t *T10 = error_reporter_tree_start(0); // non-caching
317 error_reporter(ASC_USER_NOTE,"f2",222,"fn1","allo1");
318 error_reporter(ASC_USER_NOTE,"f2",333,"fn2","allo2");
319 error_reporter(ASC_USER_NOTE,"f2",444,"fn3","allo3");
320 CU_TEST(currmessage == 4);
321 error_reporter_tree_t *t1 = TREECURRENT();
322
323 error_reporter_tree_t *T11 = error_reporter_tree_start(1); // caching
324 CU_TEST(currmessage == 4);
325 CU_TEST(TREECURRENT()!=t1);
326 error_reporter(ASC_USER_ERROR,"f3",555,"fn4","buongiorno");
327 CU_TEST(currmessage == 4);
328 CU_TEST(T11 == TREECURRENT());
329 error_reporter_tree_end_clear(T11);
330
331 CU_TEST(T10 == TREECURRENT());
332 CU_TEST(T11 != T10);
333 CU_TEST(currmessage == 4);
334 CU_TEST(TREECURRENT()==t1);
335 CU_TEST(!error_reporter_tree_has_error(T10));
336 error_reporter_tree_end(T10);
337 CU_TEST(lines[0]==111);
338 CU_TEST(lines[1]==222);
339 CU_TEST(lines[2]==333);
340 CU_TEST(lines[3]==444);
341 CU_TEST(strcmp(messages[0],"allo0")==0);
342 CU_TEST(strcmp(messages[1],"allo1")==0);
343 CU_TEST(strcmp(messages[2],"allo2")==0);
344 CU_TEST(strcmp(messages[3],"allo3")==0);
345 CU_TEST(currmessage == 4);
346 CU_TEST(TREECURRENT()==NULL);
347
348 CU_TEST(prior_meminuse == ascmeminuse()); /* make sure we cleaned up after ourselves */
349 }
350
351 /*===========================================================================*/
352 /* Registration information */
353
354 #define TESTS(T) \
355 T(error) \
356 T(errortree1) \
357 T(errortree2) \
358 T(errortree3) \
359 T(errortree4) \
360 T(errortree5) \
361 T(errortree6) \
362 T(errortree7) \
363
364 REGISTER_TESTS_SIMPLE(utilities_error, TESTS)
365

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22