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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2336 - (hide annotations) (download) (as text)
Sun Dec 26 03:44:24 2010 UTC (13 years, 3 months ago) by jpye
File MIME type: text/x-csrc
File size: 9472 byte(s)
Suppress some debug output.
In mtx_vector, initialise some members of mtx_vector class, because otherwise they trigger valgrind errors.
1 jpye 1638 /* 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     #include <CUnit/CUnit.h>
24    
25 jpye 2018 #include <ascend/general/env.h>
26 jpye 2323 #include <ascend/general/platform.h>
27 jpye 2018 #include <ascend/utilities/ascEnvVar.h>
28     #include <ascend/utilities/error.h>
29 jpye 1638
30 jpye 2018 #include <ascend/compiler/ascCompiler.h>
31     #include <ascend/compiler/module.h>
32     #include <ascend/compiler/parser.h>
33     #include <ascend/compiler/library.h>
34     #include <ascend/compiler/symtab.h>
35     #include <ascend/compiler/simlist.h>
36     #include <ascend/compiler/instquery.h>
37     #include <ascend/compiler/parentchild.h>
38     #include <ascend/compiler/atomvalue.h>
39 jpye 1638
40 jpye 2017 #include <test/assertimpl.h>
41 jpye 1638
42     static void test_init(void){
43    
44     CU_ASSERT(0 == Asc_CompilerInit(0));
45    
46     Asc_CompilerDestroy();
47     }
48    
49 jpye 1684
50     static void test_fund_types(void){
51     Asc_CompilerInit(1);
52     CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
53     CU_ASSERT(FindType(AddSymbol("boolean_constant"))!=NULL);
54     CU_ASSERT(FindType(AddSymbol("integer"))!=NULL);
55     CU_ASSERT(FindType(AddSymbol("integer_constant"))!=NULL);
56     CU_ASSERT(FindType(AddSymbol("real"))!=NULL);
57     CU_ASSERT(FindType(AddSymbol("real_constant"))!=NULL);
58     CU_ASSERT(FindType(AddSymbol("set"))!=NULL);
59     CU_ASSERT(FindType(AddSymbol("symbol"))!=NULL);
60     CU_ASSERT(FindType(AddSymbol("symbol_constant"))!=NULL);
61     Asc_CompilerDestroy();
62     }
63    
64 jpye 1638 static void test_parse_string_module(void){
65    
66     const char *model = "\n\
67 jpye 1639 DEFINITION relation\
68     included IS_A boolean;\
69     message IS_A symbol;\
70     included := TRUE;\
71     message := 'none';\
72     END relation;\
73 jpye 1638 MODEL test1;\n\
74 jpye 1639 x IS_A real;\n\
75 jpye 1638 x - 1 = 0;\n\
76     END test1;";
77    
78     Asc_CompilerInit(1);
79 jpye 2332 CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
80 jpye 1638
81     struct module_t *m;
82     int status;
83    
84     m = Asc_OpenStringModule(model, &status, ""/* name prefix*/);
85    
86 jpye 2336 #ifdef BASICS_DEBUG
87 jpye 1638 CONSOLE_DEBUG("Asc_OpenStringModule returns status=%d",status);
88 jpye 2336 #endif
89 jpye 1638 CU_ASSERT(status==0); /* if successfully created */
90    
91 jpye 2336 #ifdef BASICS_DEBUG
92 jpye 1638 CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m));
93 jpye 2336 #endif
94 jpye 1638 status = zz_parse();
95    
96 jpye 2336 #ifdef BASICS_DEBUG
97 jpye 1638 CONSOLE_DEBUG("zz_parse returns status=%d",status);
98 jpye 2336 #endif
99 jpye 1638 CU_ASSERT(status==0);
100 jpye 1639
101     struct gl_list_t *l = Asc_TypeByModule(m);
102 jpye 2336 #ifdef BASICS_DEBUG
103 jpye 1639 CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
104 jpye 2336 #endif
105 jpye 1639
106     CU_ASSERT(gl_length(l)==2);
107 jpye 1684 gl_destroy(l);
108 jpye 1640
109 jpye 1684 /* CONSOLE_DEBUG("Asc_OpenStringModule returns status=%d",status); */
110 jpye 1640 Asc_CompilerDestroy();
111 jpye 1638 }
112    
113 jpye 1684 static void test_instantiate_string(void){
114 jpye 1651
115 jpye 1684 const char *model = "(* silly little model *)\n\
116     DEFINITION relation\n\
117     included IS_A boolean;\n\
118     message IS_A symbol;\n\
119     included := TRUE;\n\
120     message := 'none';\n\
121     END relation;\n\
122 jpye 1651 MODEL test1;\n\
123     x IS_A real;\n\
124 jpye 1684 x_rel: x - 1 = 0;\n\
125     METHODS \n\
126     METHOD on_load;\n\
127     END on_load;\n\
128     END test1;\n";
129 jpye 1638
130 jpye 1651 Asc_CompilerInit(1);
131 jpye 2332 CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL);
132 jpye 2336 #ifdef BASICS_DEBUG
133 jpye 2332 CONSOLE_DEBUG("Boolean type found OK");
134 jpye 2336 #endif
135 jpye 1684 /* CONSOLE_DEBUG("MODEL TEXT:\n%s",model); */
136    
137 jpye 1651 struct module_t *m;
138     int status;
139    
140     m = Asc_OpenStringModule(model, &status, ""/* name prefix*/);
141 jpye 1684 CU_ASSERT_FATAL(status==0); /* if successfully created */
142 jpye 1652
143 jpye 1684 status = zz_parse();
144     CU_ASSERT_FATAL(status==0);
145 jpye 1651
146 jpye 1684 CU_ASSERT(FindType(AddSymbol("test1"))!=NULL);
147    
148     struct Instance *sim = SimsCreateInstance(AddSymbol("test1"), AddSymbol("sim1"), e_normal, NULL);
149     CU_ASSERT_FATAL(sim!=NULL);
150    
151     /* check the simulation name */
152 jpye 2336 #ifdef BASICS_DEBUG
153 jpye 1684 CONSOLE_DEBUG("Got simulation, name = %s",SCP(GetSimulationName(sim)));
154 jpye 2336 #endif
155 jpye 1684 CU_ASSERT_FATAL(GetSimulationName(sim)==AddSymbol("sim1"));
156    
157     /* check for the expected instances */
158     struct Instance *root = GetSimulationRoot(sim);
159    
160     CU_ASSERT(ChildByChar(root, AddSymbol("non_existent_var_name")) == NULL);
161     CU_ASSERT(ChildByChar(root, AddSymbol("x")) != NULL);
162     CU_ASSERT_FATAL(ChildByChar(root, AddSymbol("x_rel")) != NULL);
163     CU_ASSERT(NumberChildren(root)==2);
164    
165     /* check instances are of expected types */
166     CU_ASSERT(InstanceKind(ChildByChar(root,AddSymbol("x_rel")))==REL_INST);
167     CU_ASSERT(InstanceKind(ChildByChar(root,AddSymbol("x")))==REAL_ATOM_INST);
168     CU_ASSERT(InstanceKind(ChildByChar(root,AddSymbol("x")))!=REAL_INST);
169    
170     /* check attributes on relation */
171     struct Instance *xrel;
172     xrel = ChildByChar(root,AddSymbol("x_rel"));
173     CU_ASSERT_FATAL(xrel!=NULL);
174     CU_ASSERT(InstanceKind(ChildByChar(xrel,AddSymbol("included")))==BOOLEAN_INST);
175     CU_ASSERT(GetBooleanAtomValue(ChildByChar(xrel,AddSymbol("included")))==TRUE);
176     CU_ASSERT_FATAL(ChildByChar(xrel,AddSymbol("message"))!=NULL);
177     CU_ASSERT(InstanceKind(ChildByChar(xrel,AddSymbol("message")))==SYMBOL_INST);
178    
179     sim_destroy(sim);
180     Asc_CompilerDestroy();
181     }
182    
183     static void test_parse_basemodel(void){
184    
185     struct module_t *m;
186     int status;
187    
188     Asc_CompilerInit(1);
189     Asc_PutEnv(ASC_ENV_LIBRARY "=models");
190    
191     m = Asc_OpenModule("basemodel.a4l",&status);
192     CU_ASSERT(status==0);
193    
194 jpye 2336 #ifdef BASICS_DEBUG
195 jpye 1652 CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m));
196 jpye 2336 #endif
197 jpye 1651 status = zz_parse();
198 jpye 1652
199 jpye 2336 #ifdef BASICS_DEBUG
200 jpye 1652 CONSOLE_DEBUG("zz_parse returns status=%d",status);
201 jpye 2336 #endif
202 jpye 1651 CU_ASSERT(status==0);
203    
204 jpye 1652 struct gl_list_t *l = Asc_TypeByModule(m);
205 jpye 2336 #ifdef BASICS_DEBUG
206 jpye 1652 CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
207 jpye 2336 #endif
208 jpye 1684 gl_destroy(l);
209 jpye 1652
210 jpye 1684 /* there are only 8 things declared in system.a4l: */
211     CU_ASSERT(gl_length(l)==4)
212 jpye 1652
213 jpye 1684 /* but system.a4l also includes basemodel.a4l, which includes... */
214     CU_ASSERT(FindType(AddSymbol("cmumodel"))!=NULL);
215    
216 jpye 1652 Asc_CompilerDestroy();
217     }
218    
219 jpye 1684 static void test_parse_file(void){
220 jpye 1652
221 jpye 1684 struct module_t *m;
222     int status;
223    
224 jpye 1652 Asc_CompilerInit(1);
225     Asc_PutEnv(ASC_ENV_LIBRARY "=models");
226 jpye 1684
227     m = Asc_OpenModule("system.a4l",&status);
228     CU_ASSERT(status==0);
229 jpye 1652
230 jpye 2336 #ifdef BASICS_DEBUG
231 jpye 1684 CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m));
232 jpye 2336 #endif
233 jpye 1684 status = zz_parse();
234 jpye 1652
235 jpye 2336 #ifdef BASICS_DEBUG
236 jpye 1684 CONSOLE_DEBUG("zz_parse returns status=%d",status);
237 jpye 2336 #endif
238 jpye 1684 CU_ASSERT(status==0);
239    
240     struct gl_list_t *l = Asc_TypeByModule(m);
241 jpye 2336 #ifdef BASICS_DEBUG
242 jpye 1684 CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m));
243 jpye 2336 #endif
244 jpye 1684 gl_destroy(l);
245    
246     /* there are only 8 things declared in system.a4l: */
247     CU_ASSERT(gl_length(l)==8)
248    
249     /* here they are... */
250     CU_ASSERT(FindType(AddSymbol("relation"))!=NULL);
251     CU_ASSERT(FindType(AddSymbol("solver_var"))!=NULL);
252     CU_ASSERT(FindType(AddSymbol("logic_relation"))!=NULL);
253     CU_ASSERT(FindType(AddSymbol("solver_int"))!=NULL);
254     CU_ASSERT(FindType(AddSymbol("generic_real"))!=NULL);
255     CU_ASSERT(FindType(AddSymbol("boolean_var"))!=NULL);
256     CU_ASSERT(FindType(AddSymbol("solver_binary"))!=NULL);
257     CU_ASSERT(FindType(AddSymbol("solver_semi"))!=NULL);
258    
259     /* but system.a4l also includes basemodel.a4l, which includes... */
260     CU_ASSERT(FindType(AddSymbol("cmumodel"))!=NULL);
261    
262     Asc_CompilerDestroy();
263     }
264    
265     static void test_instantiate_file(void){
266    
267 jpye 1652 struct module_t *m;
268     int status;
269 jpye 1684
270     Asc_CompilerInit(1);
271     Asc_PutEnv(ASC_ENV_LIBRARY "=models");
272 jpye 1652
273 jpye 1684 /* load the file */
274     m = Asc_OpenModule("johnpye/testlog10.a4c",&status);
275     CU_ASSERT(status == 0);
276 jpye 1652
277 jpye 1684 /* parse it */
278     CU_ASSERT(0 == zz_parse());
279 jpye 1652
280 jpye 1684 /* find the model */
281     CU_ASSERT(FindType(AddSymbol("testlog10"))!=NULL);
282 jpye 1651
283 jpye 1684 /* instantiate it */
284     struct Instance *sim = SimsCreateInstance(AddSymbol("testlog10"), AddSymbol("sim1"), e_normal, NULL);
285     CU_ASSERT_FATAL(sim!=NULL);
286 jpye 1651
287 jpye 1684 /* check for vars and rels */
288     struct Instance *root = GetSimulationRoot(sim);
289     struct Instance *inst;
290    
291     CU_ASSERT(NumberChildren(root)==5);
292     CU_ASSERT((inst = ChildByChar(root,AddSymbol("x"))) && InstanceKind(inst)==REAL_ATOM_INST);
293     CU_ASSERT((inst = ChildByChar(root,AddSymbol("y"))) && InstanceKind(inst)==REAL_ATOM_INST);
294     CU_ASSERT((inst = ChildByChar(root,AddSymbol("z"))) && InstanceKind(inst)==REAL_ATOM_INST);
295    
296     CU_ASSERT((inst = ChildByChar(root,AddSymbol("log_10_expr"))) && InstanceKind(inst)==REL_INST);
297     CU_ASSERT((inst = ChildByChar(root,AddSymbol("log_e_expr"))) && InstanceKind(inst)==REL_INST);
298    
299     sim_destroy(sim);
300 jpye 1651 Asc_CompilerDestroy();
301     }
302    
303 jpye 1638 /*===========================================================================*/
304     /* Registration information */
305    
306     /* the list of tests */
307    
308     #define TESTS(T,X) \
309     T(init) \
310 jpye 1684 X T(fund_types) \
311 jpye 1651 X T(parse_string_module) \
312 jpye 1684 X T(instantiate_string) \
313     X T(parse_basemodel) \
314     X T(parse_file) \
315     X T(instantiate_file)
316 jpye 1638
317     /* you shouldn't need to change the following */
318    
319     #define TESTDECL(TESTFN) {#TESTFN,test_##TESTFN}
320    
321     #define X ,
322    
323 jpye 1651 static CU_TestInfo basics_test_list[] = {
324 jpye 1638 TESTS(TESTDECL,X)
325     X CU_TEST_INFO_NULL
326     };
327    
328     static CU_SuiteInfo suites[] = {
329 jpye 1651 {"compiler_basics", NULL, NULL, basics_test_list},
330 jpye 1638 CU_SUITE_INFO_NULL
331     };
332    
333     /*-------------------------------------------------------------------*/
334 jpye 1644 CU_ErrorCode test_register_compiler_basics(void){
335 jpye 1638 return CU_register_suites(suites);
336     }

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