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, see <http://www.gnu.org/licenses/>. |
16 |
*//** |
17 |
@file |
18 |
Unit test functions for FIX and FREE argument lists, especially where |
19 |
there are invalid things in the list. |
20 |
*/ |
21 |
#include <string.h> |
22 |
|
23 |
#include <ascend/general/env.h> |
24 |
#include <ascend/general/platform.h> |
25 |
#include <ascend/utilities/ascEnvVar.h> |
26 |
#include <ascend/utilities/error.h> |
27 |
|
28 |
#include <ascend/compiler/ascCompiler.h> |
29 |
#include <ascend/compiler/module.h> |
30 |
#include <ascend/compiler/parser.h> |
31 |
#include <ascend/compiler/library.h> |
32 |
#include <ascend/compiler/symtab.h> |
33 |
#include <ascend/compiler/simlist.h> |
34 |
#include <ascend/compiler/instquery.h> |
35 |
#include <ascend/compiler/parentchild.h> |
36 |
#include <ascend/compiler/atomvalue.h> |
37 |
|
38 |
#include <ascend/compiler/initialize.h> |
39 |
|
40 |
#include <test/common.h> |
41 |
#include <test/assertimpl.h> |
42 |
|
43 |
static struct Instance *load_model(const char *name){ |
44 |
struct module_t *m; |
45 |
int status; |
46 |
|
47 |
Asc_CompilerInit(1); |
48 |
Asc_PutEnv(ASC_ENV_LIBRARY "=models"); |
49 |
|
50 |
/* load the file */ |
51 |
m = Asc_OpenModule("test/compiler/fixfree.a4c",&status); |
52 |
CU_ASSERT(status == 0); |
53 |
|
54 |
/* parse it */ |
55 |
CU_ASSERT(0 == zz_parse()); |
56 |
|
57 |
/* instantiate it */ |
58 |
struct Instance *sim = SimsCreateInstance(AddSymbol(name), AddSymbol("sim1"), e_normal, NULL); |
59 |
CU_ASSERT_FATAL(sim!=NULL); |
60 |
|
61 |
return sim; |
62 |
} |
63 |
|
64 |
#define GET_FIXED(VAR) \ |
65 |
inst = ChildByChar(root,AddSymbol(VAR)); \ |
66 |
CU_ASSERT_FATAL(inst!=NULL); \ |
67 |
CU_ASSERT((InstanceKind(inst)==REAL_ATOM_INST)); \ |
68 |
inst = ChildByChar(inst,AddSymbol("fixed")); \ |
69 |
CU_ASSERT_FATAL(inst!=NULL); |
70 |
|
71 |
#define CHECK_FIXED(VAR) \ |
72 |
GET_FIXED(VAR);\ |
73 |
CU_ASSERT(GetBooleanAtomValue(inst)); |
74 |
#define CHECK_FREE(VAR) \ |
75 |
GET_FIXED(VAR);\ |
76 |
CU_ASSERT(!GetBooleanAtomValue(inst)); |
77 |
|
78 |
|
79 |
static void test_test1(void){ |
80 |
struct Instance *sim = load_model("test1"); |
81 |
|
82 |
/* check for vars and rels */ |
83 |
struct Instance *root = GetSimulationRoot(sim); |
84 |
struct Instance *inst; |
85 |
|
86 |
CHECK_FREE("x"); |
87 |
CHECK_FREE("y"); |
88 |
CHECK_FREE("z"); |
89 |
|
90 |
/** Call on_load */ |
91 |
struct Name *name = CreateIdName(AddSymbol("on_load")); |
92 |
enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL); |
93 |
CU_ASSERT(pe==Proc_all_ok); |
94 |
|
95 |
CHECK_FREE("x"); |
96 |
CHECK_FIXED("y"); |
97 |
CHECK_FREE("z"); |
98 |
|
99 |
sim_destroy(sim); |
100 |
Asc_CompilerDestroy(); |
101 |
} |
102 |
|
103 |
static void test_test2(void){ |
104 |
struct Instance *sim = load_model("test2"); |
105 |
|
106 |
/* check for vars and rels */ |
107 |
struct Instance *root = GetSimulationRoot(sim); |
108 |
struct Instance *inst; |
109 |
|
110 |
CHECK_FREE("x"); |
111 |
CHECK_FREE("y"); |
112 |
CHECK_FREE("z"); |
113 |
|
114 |
/** Call on_load */ |
115 |
struct Name *name = CreateIdName(AddSymbol("on_load")); |
116 |
enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL); |
117 |
CU_ASSERT(pe!=Proc_all_ok); |
118 |
|
119 |
CHECK_FREE("y"); |
120 |
CHECK_FIXED("x"); |
121 |
CHECK_FREE("z"); /* we expect names after the wrong ones not to have been changed */ |
122 |
|
123 |
sim_destroy(sim); |
124 |
Asc_CompilerDestroy(); |
125 |
} |
126 |
|
127 |
static void test_test3(void){ |
128 |
struct Instance *sim = load_model("test3"); |
129 |
|
130 |
/* check for vars and rels */ |
131 |
struct Instance *root = GetSimulationRoot(sim); |
132 |
struct Instance *inst; |
133 |
|
134 |
CHECK_FREE("x"); |
135 |
CHECK_FREE("z"); |
136 |
|
137 |
/** Call on_load */ |
138 |
struct Name *name = CreateIdName(AddSymbol("on_load")); |
139 |
enum Proc_enum pe = Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL); |
140 |
CU_ASSERT(pe!=Proc_all_ok); |
141 |
|
142 |
CHECK_FIXED("x"); |
143 |
CHECK_FREE("z"); |
144 |
|
145 |
sim_destroy(sim); |
146 |
Asc_CompilerDestroy(); |
147 |
} |
148 |
|
149 |
|
150 |
/*===========================================================================*/ |
151 |
/* Registration information */ |
152 |
|
153 |
/* the list of tests */ |
154 |
|
155 |
#define TESTS(T) \ |
156 |
T(test1) \ |
157 |
T(test2) \ |
158 |
T(test3) |
159 |
|
160 |
REGISTER_TESTS_SIMPLE(compiler_fixfree, TESTS) |
161 |
|