/[ascend]/trunk/ascend/compiler/test/test_basics.c
ViewVC logotype

Contents of /trunk/ascend/compiler/test/test_basics.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2629 - (show annotations) (download) (as text)
Tue May 22 16:54:22 2012 UTC (10 years, 6 months ago) by jpye
File MIME type: text/x-csrc
File size: 13924 byte(s)
Fixed lack of cleanup in type_info test (test not really useful, but leave it there for now)
1 /* ASCEND modelling environment
2 Copyright (C) 2007 Carnegie Mellon University
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, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 *//**
19 @file
20 Unit test functions for compiler. Nothing here yet.
21 */
22 #include <string.h>
23
24 #include <ascend/general/env.h>
25 #include <ascend/general/platform.h>
26 #include <ascend/utilities/ascEnvVar.h>
27 #include <ascend/utilities/error.h>
28
29 #include <ascend/compiler/ascCompiler.h>
30 #include <ascend/compiler/module.h>
31 #include <ascend/compiler/parser.h>
32 #include <ascend/compiler/library.h>
33 #include <ascend/compiler/symtab.h>
34 #include <ascend/compiler/simlist.h>
35 #include <ascend/compiler/instquery.h>
36 #include <ascend/compiler/parentchild.h>
37 #include <ascend/compiler/atomvalue.h>
38
39 #include <ascend/compiler/initialize.h>
40
41 #include <test/common.h>
42 #include <test/assertimpl.h>
43
44 static void test_init(void){
45
46 CU_ASSERT(0 == Asc_CompilerInit(0));
47
48 Asc_CompilerDestroy();
49 }
50
51
52 static void test_fund_types(void){
53 Asc_CompilerInit(1);
54 CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
55 CU_ASSERT(FindType(AddSymbol("boolean_constant"))!=NULL);
56 CU_ASSERT(FindType(AddSymbol("integer"))!=NULL);
57 CU_ASSERT(FindType(AddSymbol("integer_constant"))!=NULL);
58 CU_ASSERT(FindType(AddSymbol("real"))!=NULL);
59 CU_ASSERT(FindType(AddSymbol("real_constant"))!=NULL);
60 CU_ASSERT(FindType(AddSymbol("set"))!=NULL);
61 CU_ASSERT(FindType(AddSymbol("symbol"))!=NULL);
62 CU_ASSERT(FindType(AddSymbol("symbol_constant"))!=NULL);
63 Asc_CompilerDestroy();
64 }
65
66 static void test_parse_string_module(void){
67
68 const char *model = "\n\
69 DEFINITION relation\
70 included IS_A boolean;\
71 message IS_A symbol;\
72 included := TRUE;\
73 message := 'none';\
74 END relation;\
75 MODEL test1;\n\
76 x IS_A real;\n\
77 x - 1 = 0;\n\
78 END test1;";
79
80 Asc_CompilerInit(1);
81 CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
82
83 struct module_t *m;
84 int status;
85
86 m = Asc_OpenStringModule(model, &status, ""/* name prefix*/);
87
88 #ifdef BASICS_DEBUG
89 CONSOLE_DEBUG("Asc_OpenStringModule returns status=%d",status);
90 #endif
91 CU_ASSERT(status==0); /* if successfully created */
92
93 #ifdef BASICS_DEBUG
94 CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m));
95 #endif
96 status = zz_parse();
97
98 #ifdef BASICS_DEBUG
99 CONSOLE_DEBUG("zz_parse returns status=%d",status);
100 #endif
101 CU_ASSERT(status==0);
102
103 struct gl_list_t *l = Asc_TypeByModule(m);
104 #ifdef BASICS_DEBUG
105 CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
106 #endif
107
108 CU_ASSERT(gl_length(l)==2);
109 gl_destroy(l);
110
111 /* CONSOLE_DEBUG("Asc_OpenStringModule returns status=%d",status); */
112 Asc_CompilerDestroy();
113 }
114
115 static void test_instantiate_string(void){
116
117 const char *model = "(* silly little model *)\n\
118 DEFINITION relation\n\
119 included IS_A boolean;\n\
120 message IS_A symbol;\n\
121 included := TRUE;\n\
122 message := 'none';\n\
123 END relation;\n\
124 MODEL test1;\n\
125 x IS_A real;\n\
126 x_rel: x - 1 = 0;\n\
127 METHODS \n\
128 METHOD on_load;\n\
129 END on_load;\n\
130 END test1;\n";
131
132 Asc_CompilerInit(1);
133 CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
134 #ifdef BASICS_DEBUG
135 CONSOLE_DEBUG("Boolean type found OK");
136 #endif
137 /* CONSOLE_DEBUG("MODEL TEXT:\n%s",model); */
138
139 struct module_t *m;
140 int status;
141
142 m = Asc_OpenStringModule(model, &status, ""/* name prefix*/);
143 CU_ASSERT_FATAL(status==0); /* if successfully created */
144
145 status = zz_parse();
146 CU_ASSERT_FATAL(status==0);
147
148 CU_ASSERT(FindType(AddSymbol("test1"))!=NULL);
149
150 struct Instance *sim = SimsCreateInstance(AddSymbol("test1"), AddSymbol("sim1"), e_normal, NULL);
151 CU_ASSERT_FATAL(sim!=NULL);
152
153 /* check the simulation name */
154 #ifdef BASICS_DEBUG
155 CONSOLE_DEBUG("Got simulation, name = %s",SCP(GetSimulationName(sim)));
156 #endif
157 CU_ASSERT_FATAL(GetSimulationName(sim)==AddSymbol("sim1"));
158
159 /* check for the expected instances */
160 struct Instance *root = GetSimulationRoot(sim);
161
162 CU_ASSERT(ChildByChar(root, AddSymbol("non_existent_var_name")) == NULL);
163 CU_ASSERT(ChildByChar(root, AddSymbol("x")) != NULL);
164 CU_ASSERT_FATAL(ChildByChar(root, AddSymbol("x_rel")) != NULL);
165 CU_ASSERT(NumberChildren(root)==2);
166
167 /* check instances are of expected types */
168 CU_ASSERT(InstanceKind(ChildByChar(root,AddSymbol("x_rel")))==REL_INST);
169 CU_ASSERT(InstanceKind(ChildByChar(root,AddSymbol("x")))==REAL_ATOM_INST);
170 CU_ASSERT(InstanceKind(ChildByChar(root,AddSymbol("x")))!=REAL_INST);
171
172 /* check attributes on relation */
173 struct Instance *xrel;
174 xrel = ChildByChar(root,AddSymbol("x_rel"));
175 CU_ASSERT_FATAL(xrel!=NULL);
176 CU_ASSERT(InstanceKind(ChildByChar(xrel,AddSymbol("included")))==BOOLEAN_INST);
177 CU_ASSERT(GetBooleanAtomValue(ChildByChar(xrel,AddSymbol("included")))==TRUE);
178 CU_ASSERT_FATAL(ChildByChar(xrel,AddSymbol("message"))!=NULL);
179 CU_ASSERT(InstanceKind(ChildByChar(xrel,AddSymbol("message")))==SYMBOL_INST);
180
181 sim_destroy(sim);
182 Asc_CompilerDestroy();
183 }
184
185 static void test_parse_basemodel(void){
186
187 struct module_t *m;
188 int status;
189
190 Asc_CompilerInit(1);
191 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
192
193 m = Asc_OpenModule("basemodel.a4l",&status);
194 CU_ASSERT(status==0);
195
196 #ifdef BASICS_DEBUG
197 CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m));
198 #endif
199 status = zz_parse();
200
201 #ifdef BASICS_DEBUG
202 CONSOLE_DEBUG("zz_parse returns status=%d",status);
203 #endif
204 CU_ASSERT(status==0);
205
206 struct gl_list_t *l = Asc_TypeByModule(m);
207 #ifdef BASICS_DEBUG
208 CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
209 #endif
210 gl_destroy(l);
211
212 /* there are only 8 things declared in system.a4l: */
213 CU_ASSERT(gl_length(l)==4)
214
215 /* but system.a4l also includes basemodel.a4l, which includes... */
216 CU_ASSERT(FindType(AddSymbol("cmumodel"))!=NULL);
217
218 Asc_CompilerDestroy();
219 }
220
221 static void test_parse_file(void){
222
223 struct module_t *m;
224 int status;
225
226 Asc_CompilerInit(1);
227 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
228
229 m = Asc_OpenModule("system.a4l",&status);
230 CU_ASSERT(status==0);
231
232 #ifdef BASICS_DEBUG
233 CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m));
234 #endif
235 status = zz_parse();
236
237 #ifdef BASICS_DEBUG
238 CONSOLE_DEBUG("zz_parse returns status=%d",status);
239 #endif
240 CU_ASSERT(status==0);
241
242 struct gl_list_t *l = Asc_TypeByModule(m);
243 #ifdef BASICS_DEBUG
244 CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
245 #endif
246 gl_destroy(l);
247
248 /* there are only 8 things declared in system.a4l: */
249 CU_ASSERT(gl_length(l)==8)
250
251 /* here they are... */
252 CU_ASSERT(FindType(AddSymbol("relation"))!=NULL);
253 CU_ASSERT(FindType(AddSymbol("solver_var"))!=NULL);
254 CU_ASSERT(FindType(AddSymbol("logic_relation"))!=NULL);
255 CU_ASSERT(FindType(AddSymbol("solver_int"))!=NULL);
256 CU_ASSERT(FindType(AddSymbol("generic_real"))!=NULL);
257 CU_ASSERT(FindType(AddSymbol("boolean_var"))!=NULL);
258 CU_ASSERT(FindType(AddSymbol("solver_binary"))!=NULL);
259 CU_ASSERT(FindType(AddSymbol("solver_semi"))!=NULL);
260
261 /* but system.a4l also includes basemodel.a4l, which includes... */
262 CU_ASSERT(FindType(AddSymbol("cmumodel"))!=NULL);
263
264 Asc_CompilerDestroy();
265 }
266
267 static void test_instantiate_file(void){
268
269 struct module_t *m;
270 int status;
271
272 Asc_CompilerInit(1);
273 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
274
275 /* load the file */
276 m = Asc_OpenModule("johnpye/testlog10.a4c",&status);
277 CU_ASSERT(status == 0);
278
279 /* parse it */
280 CU_ASSERT(0 == zz_parse());
281
282 /* find the model */
283 CU_ASSERT(FindType(AddSymbol("testlog10"))!=NULL);
284
285 /* instantiate it */
286 struct Instance *sim = SimsCreateInstance(AddSymbol("testlog10"), AddSymbol("sim1"), e_normal, NULL);
287 CU_ASSERT_FATAL(sim!=NULL);
288
289 /* check for vars and rels */
290 struct Instance *root = GetSimulationRoot(sim);
291 struct Instance *inst;
292
293 CU_ASSERT(NumberChildren(root)==5);
294 CU_ASSERT((inst = ChildByChar(root,AddSymbol("x"))) && InstanceKind(inst)==REAL_ATOM_INST);
295 CU_ASSERT((inst = ChildByChar(root,AddSymbol("y"))) && InstanceKind(inst)==REAL_ATOM_INST);
296 CU_ASSERT((inst = ChildByChar(root,AddSymbol("z"))) && InstanceKind(inst)==REAL_ATOM_INST);
297
298 CU_ASSERT((inst = ChildByChar(root,AddSymbol("log_10_expr"))) && InstanceKind(inst)==REL_INST);
299 CU_ASSERT((inst = ChildByChar(root,AddSymbol("log_e_expr"))) && InstanceKind(inst)==REL_INST);
300
301 sim_destroy(sim);
302 Asc_CompilerDestroy();
303 }
304
305 static void test_initialize(void){
306 struct module_t *m;
307 int status;
308
309 Asc_CompilerInit(1);
310 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
311
312 /* load the file */
313 #define TESTFILE "testinit"
314 m = Asc_OpenModule("test/compiler/" TESTFILE ".a4c",&status);
315 CU_ASSERT(status == 0);
316
317 /* parse it */
318 CU_ASSERT(0 == zz_parse());
319
320 /* find the model */
321 CU_ASSERT(FindType(AddSymbol(TESTFILE))!=NULL);
322
323 /* instantiate it */
324 struct Instance *sim = SimsCreateInstance(AddSymbol(TESTFILE), AddSymbol("sim1"), e_normal, NULL);
325 CU_ASSERT_FATAL(sim!=NULL);
326
327 /* check for vars and rels */
328 struct Instance *root = GetSimulationRoot(sim);
329 struct Instance *inst;
330
331 CU_ASSERT(NumberChildren(root)==3);
332 CU_ASSERT((inst = ChildByChar(root,AddSymbol("x"))) && InstanceKind(inst)==REAL_ATOM_INST);
333 CU_ASSERT((inst = ChildByChar(root,AddSymbol("y"))) && InstanceKind(inst)==REAL_ATOM_INST);
334
335 CU_ASSERT((inst = ChildByChar(root,AddSymbol("expr1"))) && InstanceKind(inst)==REL_INST);
336
337
338 /** Call on_load */
339 struct Name *name = CreateIdName(AddSymbol("on_load"));
340 //CONSOLE_DEBUG("RUNNING ON_LOAD");
341 enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
342 CU_ASSERT(pe==Proc_all_ok);
343
344 sim_destroy(sim);
345 Asc_CompilerDestroy();
346 #undef TESTFILE
347 }
348
349 static void test_stop(void){
350 struct module_t *m;
351 int status;
352
353 Asc_CompilerInit(1);
354 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
355
356 /* load the file */
357 #define TESTFILE "stop"
358 m = Asc_OpenModule("test/compiler/" TESTFILE ".a4c",&status);
359 CU_ASSERT(status == 0);
360
361 /* parse it */
362 CU_ASSERT(0 == zz_parse());
363
364 /* find the model */
365 CU_ASSERT(FindType(AddSymbol(TESTFILE))!=NULL);
366
367 /* instantiate it */
368 struct Instance *sim = SimsCreateInstance(AddSymbol(TESTFILE), AddSymbol("sim1"), e_normal, NULL);
369 CU_ASSERT_FATAL(sim!=NULL);
370
371 /** Call on_load */
372 struct Name *name = CreateIdName(AddSymbol("on_load"));
373
374 enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
375 CU_ASSERT(pe!=Proc_all_ok);
376
377 struct Instance *inst;
378 CU_ASSERT((inst = ChildByChar(GetSimulationRoot(sim),AddSymbol("x"))) && InstanceKind(inst)==REAL_ATOM_INST);
379 CU_ASSERT(RealAtomValue(inst)==2.0);
380
381 sim_destroy(sim);
382 Asc_CompilerDestroy();
383 #undef TESTFILE
384 }
385
386
387 static void test_stoponfailedassert(void){
388 struct module_t *m;
389 int status;
390
391 Asc_CompilerInit(1);
392 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
393
394 /* load the file */
395 #define TESTFILE "stoponerror"
396 m = Asc_OpenModule("test/compiler/" TESTFILE ".a4c",&status);
397 CU_ASSERT(status == 0);
398
399 /* parse it */
400 CU_ASSERT(0 == zz_parse());
401
402 /* find the model */
403 CU_ASSERT(FindType(AddSymbol(TESTFILE))!=NULL);
404
405 /* instantiate it */
406 struct Instance *sim = SimsCreateInstance(AddSymbol(TESTFILE), AddSymbol("sim1"), e_normal, NULL);
407 CU_ASSERT_FATAL(sim!=NULL);
408
409 /** Call on_load */
410 struct Name *name = CreateIdName(AddSymbol("on_load"));
411
412 enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
413 CU_ASSERT(pe!=Proc_all_ok);
414
415 sim_destroy(sim);
416 Asc_CompilerDestroy();
417 #undef TESTFILE
418 }
419
420 /*
421 This is a test to check ascend bug #87.
422 */
423 static void test_badassign(void){
424 struct module_t *m;
425 int status;
426
427 Asc_CompilerInit(1);
428 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
429
430 /* load the file */
431 #define TESTFILE "badassign"
432 m = Asc_OpenModule("test/compiler/" TESTFILE ".a4c",&status);
433 CU_ASSERT(status == 0);
434
435 /* parse it */
436 CU_ASSERT(0 == zz_parse());
437
438 /* find the model */
439 CU_ASSERT(FindType(AddSymbol(TESTFILE))!=NULL);
440
441 /* instantiate it */
442 struct Instance *sim = SimsCreateInstance(AddSymbol(TESTFILE), AddSymbol("sim1"), e_normal, NULL);
443 CU_ASSERT_FATAL(sim!=NULL);
444
445 /* Call on_load */
446 struct Name *name = CreateIdName(AddSymbol("on_load"));
447 enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL);
448 CU_ASSERT(pe!=Proc_all_ok); /* on_load should have returned error */
449
450 /* Check that x := 2 was NOT executed (after error statement) */
451 struct Instance *inst;
452 CU_ASSERT((inst = ChildByChar(GetSimulationRoot(sim),AddSymbol("x"))) && InstanceKind(inst)==REAL_ATOM_INST);
453 CU_ASSERT(RealAtomValue(inst)==1.0);
454
455 /* clean up */
456 sim_destroy(sim);
457 Asc_CompilerDestroy();
458 #undef TESTFILE
459 }
460
461
462 static void test_type_info(void){
463 struct module_t *m;
464 int status;
465
466 Asc_CompilerInit(1);
467 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
468
469 /* load the file */
470 m = Asc_OpenModule("test/canvas/simple_recycle.a4c",&status);
471 CU_ASSERT(status == 0);
472
473 /* parse it */
474 CU_ASSERT(0 == zz_parse());
475
476 /* find the model */
477 struct TypeDescription *T;
478 T = FindType(AddSymbol("ammonia_flash"));
479
480 CU_ASSERT(T != NULL);
481
482 ChildListPtr CL;
483 CL = GetChildList(T);
484
485 WriteChildList(ASCERR,CL);
486
487 Asc_CompilerDestroy();
488 }
489
490
491 /*===========================================================================*/
492 /* Registration information */
493
494 /* the list of tests */
495
496 #define TESTS(T) \
497 T(init) \
498 T(fund_types) \
499 T(parse_string_module) \
500 T(instantiate_string) \
501 T(parse_basemodel) \
502 T(parse_file) \
503 T(instantiate_file) \
504 T(initialize) \
505 T(stop) \
506 T(stoponfailedassert) \
507 T(badassign) \
508 T(type_info)
509
510 REGISTER_TESTS_SIMPLE(compiler_basics, TESTS)
511

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