/[ascend]/trunk/models/test/defaultmethods.a4c
ViewVC logotype

Contents of /trunk/models/test/defaultmethods.a4c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1304 - (show annotations) (download) (as text)
Thu Mar 1 06:34:04 2007 UTC (15 years, 4 months ago) by johnpye
File MIME type: text/x-ascend
File size: 6725 byte(s)
Added test for empty sets with new default behaviour.
1 REQUIRE "system.a4l";
2
3 ATOM var_default_one REFINES solver_var
4 DIMENSIONLESS
5 DEFAULT 1.0;
6 nominal := 100;
7 lower_bound := 0.0;
8 upper_bound := 10000;
9 END var_default_one;
10
11 ATOM var_default_two REFINES solver_var
12 DIMENSIONLESS
13 DEFAULT 2.0;
14 nominal := 100;
15 lower_bound := 0.0;
16 upper_bound := 10000;
17 END var_default_two;
18
19 (* check that the on_load method sets 'x' to 1.0 *)
20 MODEL testdefault1;
21 x IS_A var_default_one;
22 METHODS
23 METHOD self_test;
24 ASSERT x == 1.0;
25 END self_test;
26 END testdefault1;
27
28 (*
29 check that the 'default_self' default method sets x DEFAULT to 2.0,
30 even if we override on_load
31 *)
32 MODEL testdefault2;
33 x IS_A var_default_two;
34 METHODS
35 METHOD on_load;
36 RUN default_self;
37 END on_load;
38 METHOD self_test;
39 ASSERT x == 2.0;
40 END self_test;
41 END testdefault2;
42
43 (* check the 'default' method gets called by on_load *)
44 MODEL testdefault3;
45 x IS_A var_default_one;
46 y IS_A var_default_one;
47 METHODS
48 METHOD default;
49 x := 3.0;
50 END default;
51 METHOD self_test;
52 ASSERT x == 3.0;
53 END self_test;
54 END testdefault3;
55
56 (* test that 'default' gets called even if defined in a parent model *)
57 MODEL testdefault4 REFINES testdefault3;
58 METHODS
59 METHOD self_test;
60 ASSERT x == 3.0;
61 ASSERT y == 1.0;
62 END self_test;
63 END testdefault4;
64
65 (* test that we can override a 'default' method defined in a parent model *)
66 MODEL testdefault5 REFINES testdefault4;
67 METHODS
68 METHOD default;
69 x := 4.0;
70 END default;
71 METHOD self_test;
72 ASSERT x == 4.0;
73 ASSERT y == 1.0;
74 END self_test;
75 END testdefault5;
76
77 (* test that a new 'default_self' method will kill the call to 'default' *)
78 MODEL testdefault6 REFINES testdefault4;
79 METHODS
80 METHOD default_self;
81 x := 5.0;
82 y := 6.0;
83 END default_self;
84 METHOD self_test;
85 ASSERT x == 5.0;
86 ASSERT y == 6.0;
87 END self_test;
88 END testdefault6;
89
90 (* test that 'default_self' goes inside arrays as well *)
91 MODEL testdefault7;
92 x[1..3] IS_A var_default_two;
93 y[1..2] IS_A var_default_one;
94 METHODS
95 METHOD self_test;
96 ASSERT x[1] == 2.0;
97 ASSERT x[2] == 2.0;
98 ASSERT x[3] == 2.0;
99 ASSERT y[1] == 1.0;
100 ASSERT y[2] == 1.0;
101 END self_test;
102 END testdefault7;
103
104 (* test that 'default_self' goes inside arrays inside parent types *)
105 MODEL testdefault8 REFINES testdefault7;
106 METHODS
107 METHOD self_test;
108 ASSERT x[1] == 2.0;
109 ASSERT x[2] == 2.0;
110 ASSERT x[3] == 2.0;
111 ASSERT y[1] == 1.0;
112 ASSERT y[2] == 1.0;
113 END self_test;
114 END testdefault8;
115
116 (* test that default_all recurses into submodels *)
117 MODEL ModelA;
118 x IS_A var_default_one;
119 y IS_A var_default_two;
120 END ModelA;
121
122 MODEL ModelB;
123 z IS_A var_default_one;
124 w IS_A var_default_two;
125 METHODS
126 METHOD default;
127 z := 3.0;
128 w := 4.0;
129 END default;
130 END ModelB;
131
132 MODEL testdefault9;
133 A IS_A ModelA;
134 B IS_A ModelB;
135 METHODS
136 METHOD self_test;
137 ASSERT A.x == 1.0;
138 ASSERT A.y == 2.0;
139 ASSERT B.z == 3.0;
140 ASSERT B.w == 4.0;
141 END self_test;
142 END testdefault9;
143
144 (* test that default_all recurses into arrays containing submodels *)
145 MODEL testdefault10;
146 A[1..2] IS_A ModelA;
147 B[1..2] IS_A ModelB;
148 METHODS
149 METHOD self_test;
150 ASSERT A[1].x == 1.0;
151 ASSERT A[1].y == 2.0;
152 ASSERT A[2].x == 1.0;
153 ASSERT A[2].y == 2.0;
154 ASSERT B[1].z == 3.0;
155 ASSERT B[1].w == 4.0;
156 ASSERT B[2].z == 3.0;
157 ASSERT B[2].w == 4.0;
158 END self_test;
159 END testdefault10;
160
161 (* test that an override of default_all in a submodel gets found *)
162 MODEL ModelC REFINES ModelA;
163 METHODS
164 METHOD default_all;
165 x := 5.0;
166 y := 6.0;
167 END default_all;
168 END ModelC;
169
170 MODEL testdefault11;
171 A[1..2] IS_A ModelC;
172 B[1..2] IS_A ModelB;
173 METHODS
174 METHOD self_test;
175 ASSERT A[1].x == 5.0;
176 ASSERT A[1].y == 6.0;
177 ASSERT A[2].x == 5.0;
178 ASSERT A[2].y == 6.0;
179 ASSERT B[1].z == 3.0;
180 ASSERT B[1].w == 4.0;
181 ASSERT B[2].z == 3.0;
182 ASSERT B[2].w == 4.0;
183 END self_test;
184 END testdefault11;
185
186 (* test what happens when two variables are ARE_THE_SAMEd *)
187 MODEL testdefault12;
188 x IS_A var_default_one;
189 y IS_A var_default_one;
190 x,y ARE_THE_SAME;
191 METHODS
192 METHOD default;
193 x := 2.0;
194 END default;
195 METHOD self_test;
196 ASSERT x == 2.0;
197 ASSERT y == 2.0;
198 END self_test;
199 END testdefault12;
200
201 (* when two models are ARE_THE_SAMEd, the default_all is kept *)
202 MODEL testdefault13;
203 A IS_A ModelA;
204 C IS_A ModelC;
205 A,C ARE_THE_SAME;
206 METHODS
207 METHOD self_test;
208 ASSERT A.x == 5.0;
209 ASSERT A.y == 6.0;
210 ASSERT C.x == 5.0;
211 ASSERT C.y == 6.0;
212 END self_test;
213 END testdefault13;
214
215 (* override default value for one var inside one of two are-the-same submodels *)
216 MODEL ModelD;
217 A IS_A ModelA;
218 END ModelD;
219
220 MODEL testdefault14;
221 A IS_A ModelA;
222 D IS_A ModelD;
223 A,D.A ARE_THE_SAME;
224 METHODS
225 METHOD default;
226 A.x := 7.0;
227 END default;
228 METHOD self_test;
229 ASSERT A.x == 7.0;
230 ASSERT A.y == 2.0;
231 ASSERT D.A.x == 7.0;
232 ASSERT D.A.y == 2.0;
233 END self_test;
234 END testdefault14;
235
236 (* override 'default' method inside one of two are-the-same submodels *)
237 MODEL ModelD1;
238 A IS_A ModelA;
239 METHODS
240 METHOD default;
241 A.y := 8.0;
242 END default;
243 END ModelD1;
244
245 MODEL testdefault15;
246 A IS_A ModelA;
247 D IS_A ModelD1;
248 A,D.A ARE_THE_SAME;
249 METHODS
250 METHOD default;
251 A.x := 7.0;
252 END default;
253 METHOD self_test;
254 ASSERT A.x == 7.0;
255 ASSERT A.y == 8.0;
256 END self_test;
257 END testdefault15;
258
259 (* behaviour in this case is undefined, @TODO warn user about use of ARE_THE_SAME *)
260 MODEL ModelE REFINES ModelA;
261 METHODS
262 METHOD default;
263 y := 9.0;
264 END default;
265 END ModelE;
266
267 MODEL ModelF;
268 E IS_A ModelE;
269 METHODS
270 METHOD default;
271 E.y := 8.0;
272 END default;
273 END ModelF;
274
275 MODEL testdefault16;
276 E IS_A ModelE;
277 F IS_A ModelF;
278 F.E, E ARE_THE_SAME;
279 METHODS
280 METHOD default;
281 E.x := 7.0;
282 END default;
283 METHOD self_test;
284 ASSERT E.x == 7.0;
285 ASSERT E.y == 8.0;
286 END self_test;
287 END testdefault16;
288
289 (* test with some tricky set notation (see following, related) *)
290 MODEL testdefault17;
291 nodes,head,tail IS_A set OF integer_constant;
292 x IS_A var_default_one;
293 n IS_A integer_constant;
294 n :== 3;
295 nodes :== [1..n];
296 head :== [1];
297 tail :== nodes - head;
298 y[1..n] IS_A var_default_two;
299 FOR i IN head CREATE
300 y_eq[i]: y[i] = x;
301 END FOR;
302 FOR i IN tail CREATE
303 y_eq2[i]: y[i] = y[i-1];
304 END FOR;
305 METHODS
306 METHOD self_test;
307 ASSERT x == 1.0;
308 ASSERT y[1] == 2.0;
309 ASSERT y[1] == 2.0;
310 ASSERT y[1] == 2.0;
311 END self_test;
312 END testdefault17;
313
314 (* test with NULL children *)
315 MODEL testdefault18;
316 nodes,head,tail IS_A set OF integer_constant;
317 x IS_A var_default_one;
318 n IS_A integer_constant;
319 n :== 3;
320 nodes :== [1..n];
321 head :== [1,2,3];
322 tail :== nodes - head;
323 y[1..n] IS_A var_default_two;
324 FOR i IN head CREATE
325 y_eq[i]: y[i] = x;
326 END FOR;
327 FOR i IN tail CREATE (* empty list *)
328 y_eq2[i]: y[i] = y[i-1];
329 END FOR;
330 METHODS
331 METHOD self_test;
332 ASSERT x == 1.0;
333 ASSERT y[1] == 2.0;
334 ASSERT y[1] == 2.0;
335 ASSERT y[1] == 2.0;
336 END self_test;
337 END testdefault18;
338
339

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