/[ascend]/trunk/ascend/packages/test/test_defaultall.c
ViewVC logotype

Contents of /trunk/ascend/packages/test/test_defaultall.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2245 - (show annotations) (download) (as text)
Mon Aug 2 04:20:18 2010 UTC (10 years, 3 months ago) by jpye
File MIME type: text/x-csrc
File size: 7474 byte(s)
Added test 3b to defaultall tests, all looks to be working fine 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 default_all automatic recursive methods.
21 */
22 #include <string.h>
23 #include <CUnit/CUnit.h>
24
25 #include <ascend/general/env.h>
26 #include <ascend/utilities/ascConfig.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/simlist.h>
35 #include <ascend/compiler/instquery.h>
36 #include <ascend/compiler/parentchild.h>
37 #include <ascend/compiler/atomvalue.h>
38 #include <ascend/compiler/instance_io.h>
39 #include <ascend/compiler/instantiate.h>
40 #include <ascend/compiler/initialize.h>
41 #include <ascend/compiler/watchpt.h>
42
43 #include <test/assertimpl.h>
44
45 static enum Proc_enum run_method(struct Instance *sim, const char *methodname){
46 CONSOLE_DEBUG("Running '%s'...",methodname);
47
48 symchar *onload = AddSymbol(methodname);
49 enum Proc_enum pe;
50 pe = Initialize(GetSimulationRoot(sim),CreateIdName(onload),SCP(onload), ASCERR, WP_STOPONERR, NULL, NULL);
51 return pe;
52 }
53
54 static struct Instance *load_and_initialise(const char *fname, const char *modelname){
55
56 struct module_t *m;
57 int status;
58
59 CONSOLE_DEBUG("Loading model '%s'...",fname);
60
61 Asc_CompilerInit(1);
62 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
63
64 /* load the file */
65 m = Asc_OpenModule(fname,&status);
66 CU_ASSERT(status == 0);
67
68 /* parse it */
69 CU_ASSERT(0 == zz_parse());
70
71 /* find the model */
72 CU_ASSERT(FindType(AddSymbol(modelname))!=NULL);
73
74 /* instantiate it */
75 struct Instance *sim = SimsCreateInstance(AddSymbol(modelname), AddSymbol("sim1"), e_normal, NULL);
76 CU_ASSERT_FATAL(sim!=NULL);
77
78 CU_ASSERT_FATAL(Proc_all_ok == run_method(sim, "on_load"));
79
80 return sim;
81 }
82
83 static void test_default1(void){
84
85 struct Instance *sim = load_and_initialise("test/defaultall/test1.a4c", "test1");
86
87 /* check for vars and rels */
88 struct Instance *root = GetSimulationRoot(sim);
89 struct Instance *inst, *a, *b;
90
91 CU_ASSERT_FATAL((inst = ChildByChar(root,AddSymbol("s1"))) && InstanceKind(inst)==MODEL_INST);
92 CU_ASSERT_FATAL((a = ChildByChar(inst,AddSymbol("a"))) && InstanceKind(a)==REAL_ATOM_INST);
93 CU_ASSERT_FATAL((b = ChildByChar(inst,AddSymbol("b"))) && InstanceKind(b)==REAL_ATOM_INST);
94
95 CONSOLE_DEBUG("Checking values...");
96
97 /* check that values have been correctly initialised */
98 double va = RealAtomValue(a);
99 double vb = RealAtomValue(b);
100 CONSOLE_DEBUG("Value of 'a' = %f",va);
101 CU_ASSERT(va==4.);
102 CONSOLE_DEBUG("Value of 'b' = %f",vb);
103 CU_ASSERT(vb==8.);
104
105 CONSOLE_DEBUG("Cleaning up...");
106 /* clean up */
107 sim_destroy(sim);
108 Asc_CompilerDestroy();
109 }
110
111
112
113 static void test_default2(void){
114
115 struct Instance *sim = load_and_initialise("test/defaultall/test2.a4c", "test2");
116
117 /* check for vars and rels */
118 struct Instance *root, *inst1, *inst2, *a, *b;
119 root = GetSimulationRoot(sim);
120
121 CU_ASSERT_FATAL((inst1 = ChildByChar(root,AddSymbol("s2"))) && InstanceKind(inst1)==MODEL_INST);
122 CU_ASSERT_FATAL((inst2 = ChildByChar(inst1,AddSymbol("s1a"))) && InstanceKind(inst2)==MODEL_INST);
123 CU_ASSERT_FATAL((a = ChildByChar(inst2,AddSymbol("a"))) && InstanceKind(a)==REAL_ATOM_INST);
124 CU_ASSERT_FATAL((b = ChildByChar(inst2,AddSymbol("b"))) && InstanceKind(b)==REAL_ATOM_INST);
125
126 CONSOLE_DEBUG("Checking values...");
127
128 /* check that values have been correctly initialised */
129 double va = RealAtomValue(a);
130 double vb = RealAtomValue(b);
131 CONSOLE_DEBUG("Value of 'a' = %f",va);
132 CU_ASSERT(va==4.);
133 CONSOLE_DEBUG("Value of 'b' = %f",vb);
134 CU_ASSERT(vb==8.);
135
136 CONSOLE_DEBUG("Cleaning up...");
137 /* clean up */
138 sim_destroy(sim);
139 Asc_CompilerDestroy();
140 }
141
142
143 static void test_default3(void){
144
145 struct Instance *sim = load_and_initialise("test/defaultall/test3.a4c", "test3");
146
147 /* check for vars and rels */
148 struct Instance *root, *inst1, *inst2, *a, *b, *c, *d;
149 root = GetSimulationRoot(sim);
150
151 CU_ASSERT_FATAL((inst1 = ChildByChar(root,AddSymbol("s2"))) && InstanceKind(inst1)==MODEL_INST);
152 CU_ASSERT_FATAL((inst2 = ChildByChar(inst1,AddSymbol("s1a"))) && InstanceKind(inst2)==MODEL_INST);
153 CU_ASSERT_FATAL((a = ChildByChar(inst2,AddSymbol("a"))) && InstanceKind(a)==REAL_ATOM_INST);
154 CU_ASSERT_FATAL((b = ChildByChar(inst2,AddSymbol("b"))) && InstanceKind(b)==REAL_ATOM_INST);
155 CU_ASSERT_FATAL((c = ChildByChar(inst2,AddSymbol("c"))) && InstanceKind(c)==REAL_ATOM_INST);
156 CU_ASSERT_FATAL((d = ChildByChar(inst1,AddSymbol("d"))) && InstanceKind(d)==REAL_ATOM_INST);
157
158 CONSOLE_DEBUG("Checking values...");
159
160 CU_ASSERT(RealAtomValue(a)==4.);
161 CU_ASSERT(RealAtomValue(c)==3);
162 CU_ASSERT(RealAtomValue(b)==8.);
163 CU_ASSERT(RealAtomValue(d)==5);
164
165 CONSOLE_DEBUG("Cleaning up...");
166 /* clean up */
167 sim_destroy(sim);
168 Asc_CompilerDestroy();
169 }
170
171
172
173 static void test_default3b(void){
174
175 struct Instance *sim = load_and_initialise("test/defaultall/test3.a4c", "test3");
176
177 /* check for vars and rels */
178 struct Instance *root, *inst1, *inst2, *a, *b, *c, *d;
179
180 root = GetSimulationRoot(sim);
181 CU_ASSERT_FATAL((inst1 = ChildByChar(root,AddSymbol("s2"))) && InstanceKind(inst1)==MODEL_INST);
182 CU_ASSERT_FATAL((inst2 = ChildByChar(inst1,AddSymbol("s1a"))) && InstanceKind(inst2)==MODEL_INST);
183 CU_ASSERT_FATAL((a = ChildByChar(inst2,AddSymbol("a"))) && InstanceKind(a)==REAL_ATOM_INST);
184 CU_ASSERT_FATAL((b = ChildByChar(inst2,AddSymbol("b"))) && InstanceKind(b)==REAL_ATOM_INST);
185 CU_ASSERT_FATAL((c = ChildByChar(inst2,AddSymbol("c"))) && InstanceKind(c)==REAL_ATOM_INST);
186 CU_ASSERT_FATAL((d = ChildByChar(inst1,AddSymbol("d"))) && InstanceKind(d)==REAL_ATOM_INST);
187
188 CU_ASSERT_FATAL(Proc_all_ok == run_method(sim, "mess_up_values"));
189
190 CU_ASSERT(RealAtomValue(a)==0.);
191 CU_ASSERT(RealAtomValue(b)==0.);
192 CU_ASSERT(RealAtomValue(c)==0.);
193 CU_ASSERT(RealAtomValue(d)==0.);
194
195 CU_ASSERT_FATAL(Proc_all_ok == run_method(sim, "on_load"));
196
197 CU_ASSERT(RealAtomValue(a)==4.);
198 CU_ASSERT(RealAtomValue(c)==3);
199 CU_ASSERT(RealAtomValue(b)==8.);
200 CU_ASSERT(RealAtomValue(d)==5);
201
202 /* clean up */
203 sim_destroy(sim);
204 Asc_CompilerDestroy();
205 }
206
207 /*===========================================================================*/
208 /* Registration information */
209
210 /* the list of tests */
211
212 #define TESTS(T,X) \
213 T(default1) \
214 X T(default2) \
215 X T(default3) \
216 X T(default3b)
217
218 /* you shouldn't need to change the following */
219
220 #define TESTDECL(TESTFN) {#TESTFN,test_##TESTFN}
221
222 #define X ,
223
224 static CU_TestInfo defaultall_test_list[] = {
225 TESTS(TESTDECL,X)
226 X CU_TEST_INFO_NULL
227 };
228
229 static CU_SuiteInfo suites[] = {
230 {"packages_defaultall", NULL, NULL, defaultall_test_list},
231 CU_SUITE_INFO_NULL
232 };
233
234 /*-------------------------------------------------------------------*/
235 CU_ErrorCode test_register_packages_defaultall(void){
236 return CU_register_suites(suites);
237 }
238

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