/[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 2244 - (show annotations) (download) (as text)
Mon Aug 2 01:05:11 2010 UTC (10 years, 5 months ago) by jpye
File MIME type: text/x-csrc
File size: 4946 byte(s)
Fix bug with 'RUN default', need to use Initialize(GetSimulationRoot(sim),...) for running methods!
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
46 static struct Instance *load_and_initialise(const char *fname, const char *modelname){
47
48 struct module_t *m;
49 int status;
50
51 CONSOLE_DEBUG("Loading model '%s'...",fname);
52
53 Asc_CompilerInit(1);
54 Asc_PutEnv(ASC_ENV_LIBRARY "=models");
55
56 /* load the file */
57 m = Asc_OpenModule(fname,&status);
58 CU_ASSERT(status == 0);
59
60 /* parse it */
61 CU_ASSERT(0 == zz_parse());
62
63 /* find the model */
64 CU_ASSERT(FindType(AddSymbol(modelname))!=NULL);
65
66 /* instantiate it */
67 struct Instance *sim = SimsCreateInstance(AddSymbol(modelname), AddSymbol("sim1"), e_normal, NULL);
68 CU_ASSERT_FATAL(sim!=NULL);
69
70 CONSOLE_DEBUG("Running on_load...");
71
72 symchar *onload = AddSymbol("on_load");
73 enum Proc_enum pe;
74 pe = Initialize(GetSimulationRoot(sim),CreateIdName(onload),SCP(onload), ASCERR, WP_STOPONERR, NULL, NULL);
75 CU_ASSERT_FATAL(pe == Proc_all_ok);
76
77 return sim;
78 }
79
80 static void test_default1(void){
81
82 struct Instance *sim = load_and_initialise("test/defaultall/test1.a4c", "test1");
83
84 /* check for vars and rels */
85 struct Instance *root = GetSimulationRoot(sim);
86 struct Instance *inst, *a, *b;
87
88 CU_ASSERT_FATAL((inst = ChildByChar(root,AddSymbol("s1"))) && InstanceKind(inst)==MODEL_INST);
89 CU_ASSERT_FATAL((a = ChildByChar(inst,AddSymbol("a"))) && InstanceKind(a)==REAL_ATOM_INST);
90 CU_ASSERT_FATAL((b = ChildByChar(inst,AddSymbol("b"))) && InstanceKind(b)==REAL_ATOM_INST);
91
92 CONSOLE_DEBUG("Checking values...");
93
94 /* check that values have been correctly initialised */
95 double va = RealAtomValue(a);
96 double vb = RealAtomValue(b);
97 CONSOLE_DEBUG("Value of 'a' = %f",va);
98 CU_ASSERT(va==4.);
99 CONSOLE_DEBUG("Value of 'b' = %f",vb);
100 CU_ASSERT(vb==8.);
101
102 CONSOLE_DEBUG("Cleaning up...");
103 /* clean up */
104 sim_destroy(sim);
105 Asc_CompilerDestroy();
106 }
107
108
109
110 static void test_default2(void){
111
112 struct Instance *sim = load_and_initialise("test/defaultall/test2.a4c", "test2");
113
114 /* check for vars and rels */
115 struct Instance *root, *inst1, *inst2, *a, *b;
116 root = GetSimulationRoot(sim);
117
118 CU_ASSERT_FATAL((inst1 = ChildByChar(root,AddSymbol("s2"))) && InstanceKind(inst1)==MODEL_INST);
119 CU_ASSERT_FATAL((inst2 = ChildByChar(inst1,AddSymbol("s1a"))) && InstanceKind(inst2)==MODEL_INST);
120 CU_ASSERT_FATAL((a = ChildByChar(inst2,AddSymbol("a"))) && InstanceKind(a)==REAL_ATOM_INST);
121 CU_ASSERT_FATAL((b = ChildByChar(inst2,AddSymbol("b"))) && InstanceKind(b)==REAL_ATOM_INST);
122
123 CONSOLE_DEBUG("Checking values...");
124
125 /* check that values have been correctly initialised */
126 double va = RealAtomValue(a);
127 double vb = RealAtomValue(b);
128 CONSOLE_DEBUG("Value of 'a' = %f",va);
129 CU_ASSERT(va==4.);
130 CONSOLE_DEBUG("Value of 'b' = %f",vb);
131 CU_ASSERT(vb==8.);
132
133 CONSOLE_DEBUG("Cleaning up...");
134 /* clean up */
135 sim_destroy(sim);
136 Asc_CompilerDestroy();
137 }
138
139
140 /*===========================================================================*/
141 /* Registration information */
142
143 /* the list of tests */
144
145 #define TESTS(T,X) \
146 T(default1) \
147 X T(default2)
148
149 /* you shouldn't need to change the following */
150
151 #define TESTDECL(TESTFN) {#TESTFN,test_##TESTFN}
152
153 #define X ,
154
155 static CU_TestInfo defaultall_test_list[] = {
156 TESTS(TESTDECL,X)
157 X CU_TEST_INFO_NULL
158 };
159
160 static CU_SuiteInfo suites[] = {
161 {"packages_defaultall", NULL, NULL, defaultall_test_list},
162 CU_SUITE_INFO_NULL
163 };
164
165 /*-------------------------------------------------------------------*/
166 CU_ErrorCode test_register_packages_defaultall(void){
167 return CU_register_suites(suites);
168 }
169

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