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 |
#include <CUnit/CUnit.h> |
24 |
|
25 |
#include <ascend/general/env.h> |
26 |
#include <ascend/general/platform.h> |
27 |
#include <ascend/utilities/ascEnvVar.h> |
28 |
#include <ascend/utilities/error.h> |
29 |
|
30 |
#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 |
|
40 |
#include <test/assertimpl.h> |
41 |
|
42 |
static void test_init(void){ |
43 |
|
44 |
CU_ASSERT(0 == Asc_CompilerInit(0)); |
45 |
|
46 |
Asc_CompilerDestroy(); |
47 |
} |
48 |
|
49 |
|
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 |
static void test_parse_string_module(void){ |
65 |
|
66 |
const char *model = "\n\ |
67 |
DEFINITION relation\ |
68 |
included IS_A boolean;\ |
69 |
message IS_A symbol;\ |
70 |
included := TRUE;\ |
71 |
message := 'none';\ |
72 |
END relation;\ |
73 |
MODEL test1;\n\ |
74 |
x IS_A real;\n\ |
75 |
x - 1 = 0;\n\ |
76 |
END test1;"; |
77 |
|
78 |
Asc_CompilerInit(1); |
79 |
CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL); |
80 |
|
81 |
struct module_t *m; |
82 |
int status; |
83 |
|
84 |
m = Asc_OpenStringModule(model, &status, ""/* name prefix*/); |
85 |
|
86 |
#ifdef BASICS_DEBUG |
87 |
CONSOLE_DEBUG("Asc_OpenStringModule returns status=%d",status); |
88 |
#endif |
89 |
CU_ASSERT(status==0); /* if successfully created */ |
90 |
|
91 |
#ifdef BASICS_DEBUG |
92 |
CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m)); |
93 |
#endif |
94 |
status = zz_parse(); |
95 |
|
96 |
#ifdef BASICS_DEBUG |
97 |
CONSOLE_DEBUG("zz_parse returns status=%d",status); |
98 |
#endif |
99 |
CU_ASSERT(status==0); |
100 |
|
101 |
struct gl_list_t *l = Asc_TypeByModule(m); |
102 |
#ifdef BASICS_DEBUG |
103 |
CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m)); |
104 |
#endif |
105 |
|
106 |
CU_ASSERT(gl_length(l)==2); |
107 |
gl_destroy(l); |
108 |
|
109 |
/* CONSOLE_DEBUG("Asc_OpenStringModule returns status=%d",status); */ |
110 |
Asc_CompilerDestroy(); |
111 |
} |
112 |
|
113 |
static void test_instantiate_string(void){ |
114 |
|
115 |
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 |
MODEL test1;\n\ |
123 |
x IS_A real;\n\ |
124 |
x_rel: x - 1 = 0;\n\ |
125 |
METHODS \n\ |
126 |
METHOD on_load;\n\ |
127 |
END on_load;\n\ |
128 |
END test1;\n"; |
129 |
|
130 |
Asc_CompilerInit(1); |
131 |
CU_ASSERT(FindType(AddSymbol("boolean"))!=NULL); |
132 |
#ifdef BASICS_DEBUG |
133 |
CONSOLE_DEBUG("Boolean type found OK"); |
134 |
#endif |
135 |
/* CONSOLE_DEBUG("MODEL TEXT:\n%s",model); */ |
136 |
|
137 |
struct module_t *m; |
138 |
int status; |
139 |
|
140 |
m = Asc_OpenStringModule(model, &status, ""/* name prefix*/); |
141 |
CU_ASSERT_FATAL(status==0); /* if successfully created */ |
142 |
|
143 |
status = zz_parse(); |
144 |
CU_ASSERT_FATAL(status==0); |
145 |
|
146 |
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 |
#ifdef BASICS_DEBUG |
153 |
CONSOLE_DEBUG("Got simulation, name = %s",SCP(GetSimulationName(sim))); |
154 |
#endif |
155 |
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 |
#ifdef BASICS_DEBUG |
195 |
CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m)); |
196 |
#endif |
197 |
status = zz_parse(); |
198 |
|
199 |
#ifdef BASICS_DEBUG |
200 |
CONSOLE_DEBUG("zz_parse returns status=%d",status); |
201 |
#endif |
202 |
CU_ASSERT(status==0); |
203 |
|
204 |
struct gl_list_t *l = Asc_TypeByModule(m); |
205 |
#ifdef BASICS_DEBUG |
206 |
CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m)); |
207 |
#endif |
208 |
gl_destroy(l); |
209 |
|
210 |
/* there are only 8 things declared in system.a4l: */ |
211 |
CU_ASSERT(gl_length(l)==4) |
212 |
|
213 |
/* but system.a4l also includes basemodel.a4l, which includes... */ |
214 |
CU_ASSERT(FindType(AddSymbol("cmumodel"))!=NULL); |
215 |
|
216 |
Asc_CompilerDestroy(); |
217 |
} |
218 |
|
219 |
static void test_parse_file(void){ |
220 |
|
221 |
struct module_t *m; |
222 |
int status; |
223 |
|
224 |
Asc_CompilerInit(1); |
225 |
Asc_PutEnv(ASC_ENV_LIBRARY "=models"); |
226 |
|
227 |
m = Asc_OpenModule("system.a4l",&status); |
228 |
CU_ASSERT(status==0); |
229 |
|
230 |
#ifdef BASICS_DEBUG |
231 |
CONSOLE_DEBUG("Beginning parse of %s",Asc_ModuleName(m)); |
232 |
#endif |
233 |
status = zz_parse(); |
234 |
|
235 |
#ifdef BASICS_DEBUG |
236 |
CONSOLE_DEBUG("zz_parse returns status=%d",status); |
237 |
#endif |
238 |
CU_ASSERT(status==0); |
239 |
|
240 |
struct gl_list_t *l = Asc_TypeByModule(m); |
241 |
#ifdef BASICS_DEBUG |
242 |
CONSOLE_DEBUG("%lu library entries loaded from %s",gl_length(l),Asc_ModuleName(m)); |
243 |
#endif |
244 |
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 |
struct module_t *m; |
268 |
int status; |
269 |
|
270 |
Asc_CompilerInit(1); |
271 |
Asc_PutEnv(ASC_ENV_LIBRARY "=models"); |
272 |
|
273 |
/* load the file */ |
274 |
m = Asc_OpenModule("johnpye/testlog10.a4c",&status); |
275 |
CU_ASSERT(status == 0); |
276 |
|
277 |
/* parse it */ |
278 |
CU_ASSERT(0 == zz_parse()); |
279 |
|
280 |
/* find the model */ |
281 |
CU_ASSERT(FindType(AddSymbol("testlog10"))!=NULL); |
282 |
|
283 |
/* instantiate it */ |
284 |
struct Instance *sim = SimsCreateInstance(AddSymbol("testlog10"), AddSymbol("sim1"), e_normal, NULL); |
285 |
CU_ASSERT_FATAL(sim!=NULL); |
286 |
|
287 |
/* 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 |
Asc_CompilerDestroy(); |
301 |
} |
302 |
|
303 |
/*===========================================================================*/ |
304 |
/* Registration information */ |
305 |
|
306 |
/* the list of tests */ |
307 |
|
308 |
#define TESTS(T,X) \ |
309 |
T(init) \ |
310 |
X T(fund_types) \ |
311 |
X T(parse_string_module) \ |
312 |
X T(instantiate_string) \ |
313 |
X T(parse_basemodel) \ |
314 |
X T(parse_file) \ |
315 |
X T(instantiate_file) |
316 |
|
317 |
/* you shouldn't need to change the following */ |
318 |
|
319 |
#define TESTDECL(TESTFN) {#TESTFN,test_##TESTFN} |
320 |
|
321 |
#define X , |
322 |
|
323 |
static CU_TestInfo basics_test_list[] = { |
324 |
TESTS(TESTDECL,X) |
325 |
X CU_TEST_INFO_NULL |
326 |
}; |
327 |
|
328 |
static CU_SuiteInfo suites[] = { |
329 |
{"compiler_basics", NULL, NULL, basics_test_list}, |
330 |
CU_SUITE_INFO_NULL |
331 |
}; |
332 |
|
333 |
/*-------------------------------------------------------------------*/ |
334 |
CU_ErrorCode test_register_compiler_basics(void){ |
335 |
return CU_register_suites(suites); |
336 |
} |