-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathtest_resolve.c
More file actions
314 lines (233 loc) · 8.72 KB
/
test_resolve.c
File metadata and controls
314 lines (233 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include <assert.h>
#include "express/resolve.h"
/* core */
#include "express/hash.h"
#include "express/linklist.h"
/* non-core */
#include "express/symbol.h"
#include "express/schema.h"
#include "express/expr.h"
#include "express/type.h"
#include "driver.h"
#include "fff.h"
/*
* mock globals
*/
char * EXPRESSprogram_name;
int yylineno;
int __SCOPE_search_id;
int EXPRESSpass;
struct Scope_ * FUNC_NVL;
struct Scope_ * FUNC_USEDIN;
struct EXPop_entry EXPop_table[OP_LAST];
int tag_count;
/*
* mock functions
*/
DEFINE_FFF_GLOBALS
FAKE_VALUE_FUNC(void *, SCOPEfind, Scope, char *, int)
FAKE_VALUE_FUNC(Variable, VARfind, Scope, char *, int)
FAKE_VALUE_FUNC(char *, VARget_simple_name, Variable)
FAKE_VALUE_FUNC(struct Scope_ *, ENTITYfind_inherited_entity, struct Scope_ *, char *, int)
FAKE_VALUE_FUNC(Variable, ENTITYget_named_attribute, Entity, char *)
FAKE_VALUE_FUNC(Variable, ENTITYresolve_attr_ref, Entity, Symbol *, Symbol *)
FAKE_VALUE_FUNC(int, ENTITYdeclares_variable, Entity, Variable)
FAKE_VALUE_FUNC(int, EXPRESS_fail, Express)
void setup() {
RESOLVEinitialize();
RESET_FAKE(SCOPEfind);
RESET_FAKE(VARfind);
RESET_FAKE(VARget_simple_name);
RESET_FAKE(ENTITYfind_inherited_entity);
RESET_FAKE(ENTITYresolve_attr_ref);
RESET_FAKE(ENTITYget_named_attribute);
RESET_FAKE(ENTITYdeclares_variable);
RESET_FAKE(EXPRESS_fail);
}
void * SCOPEfind_handler(Scope scope, char * name, int type) {
(void) type;
return DICTlookup(scope->symbol_table, name);
}
int test_exp_resolve_bad_func_call() {
Schema scope;
Symbol *func_id;
Expression func_call;
scope = SCHEMAcreate();
func_id = SYMBOLcreate("func1", 1, "test1");
func_call = EXPcreate_from_symbol(Type_Funcall, func_id);
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
EXP_resolve(func_call, scope, Type_Dont_Care);
assert(func_call->symbol.resolved != RESOLVED);
return 0;
}
int test_exp_resolve_func_call() {
Schema scope;
Symbol *func_id;
Expression func_call;
Function func_def;
scope = SCHEMAcreate();
func_id = SYMBOLcreate("func1", 1, "test1");
func_call = EXPcreate_from_symbol(Type_Funcall, func_id);
func_def = TYPEcreate_nostab(func_id, scope, OBJ_FUNCTION);
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
EXP_resolve(func_call, scope, Type_Dont_Care);
assert(func_call->symbol.resolved == RESOLVED);
assert(func_call->u.funcall.function == func_def);
return 0;
}
Variable VARfind_handler(Scope scope, char *name, int strict) {
(void) strict;
return DICTlookup(scope->symbol_table, name);
}
int test_exp_resolve_local_identifier() {
Schema scope;
Entity ent;
Expression ent_attr, ent_attr_ref;
Symbol *attr_id, *attr_ref, *ent_id;
Variable v_attr;
Type attr_typ;
scope = SCHEMAcreate();
ent_id = SYMBOLcreate("entity1", 1, "test_2");
ent = ENTITYcreate(ent_id);
DICT_define(scope->symbol_table, ent_id->name, ent, ent_id, OBJ_ENTITY);
attr_id = SYMBOLcreate("attr1", 1, "test_2");
ent_attr = EXPcreate_from_symbol(Type_Attribute, attr_id);
attr_typ = TYPEcreate(real_);
v_attr = VARcreate(ent_attr, attr_typ);
v_attr->flags.attribute = true;
DICT_define(ent->symbol_table, attr_id->name, v_attr, attr_id, OBJ_VARIABLE);
attr_ref = SYMBOLcreate("attr1", 1, "test_2");
ent_attr_ref = EXPcreate_from_symbol(Type_Identifier, attr_ref);
VARfind_fake.custom_fake = VARfind_handler;
EXP_resolve(ent_attr_ref, ent, Type_Dont_Care);
assert(ent_attr_ref->u.variable == v_attr);
assert(ent_attr_ref->symbol.resolved == RESOLVED);
return 0;
}
int test_entity_resolve_subtype_expr_entity() {
Schema scope;
Entity ent1, ent2;
Expression subtype_exp;
Symbol *ent1_id, *ent2_id, *ent2_ref;
int chk;
scope = SCHEMAcreate();
ent1_id = SYMBOLcreate("ent1", 1, "test_3");
ent2_id = SYMBOLcreate("ent2", 1, "test_3");
ent2_ref = SYMBOLcreate("ent2", 1, "test_3");
ent1 = ENTITYcreate(ent1_id);
ent2 = ENTITYcreate(ent2_id);
DICTdefine(scope->symbol_table, ent1_id->name, ent1, ent1_id, OBJ_ENTITY);
DICTdefine(scope->symbol_table, ent2_id->name, ent2, ent2_id, OBJ_ENTITY);
subtype_exp = EXPcreate_from_symbol(Type_Identifier, ent2_ref);
ent1->superscope = scope;
ent1->u.entity->subtypes = LISTcreate();
ent1->u.entity->subtype_expression = subtype_exp;
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
chk = ENTITYresolve_subtype_expression(subtype_exp, ent1, &ent1->u.entity->subtypes);
assert(chk == RESOLVED);
return 0;
}
int test_type_resolve_entity() {
Schema scope;
Type sel, ent_base;
Entity ent;
Symbol *ent_id, *sel_id;
scope = SCHEMAcreate();
ent_id = SYMBOLcreate("ent", 1, "test_4");
sel_id = SYMBOLcreate("sel_typ", 1, "test_4");
ent_base = TYPEcreate_name(ent_id);
ent_base->superscope = scope;
ent = ENTITYcreate(ent_id);
ent->superscope = scope;
sel = TYPEcreate(select_);
sel->symbol = *sel_id;
sel->u.type->body->list = LISTcreate();
sel->superscope = scope;
LISTadd_last(sel->u.type->body->list, ent_base);
DICTdefine(scope->symbol_table, ent_id->name, ent, ent_id, OBJ_ENTITY);
DICTdefine(scope->symbol_table, sel_id->name, sel, sel_id, OBJ_TYPE);
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
TYPE_resolve(&sel);
assert(sel->symbol.resolved == RESOLVED);
return 0;
}
int test_stmt_resolve_pcall_proc() {
Schema scope;
Function f;
Procedure p;
Statement s;
Symbol *func_id, *proc_id, *proc_ref;
scope = SCHEMAcreate();
func_id = SYMBOLcreate("func1", 1, "test_5");
proc_id = SYMBOLcreate("proc1", 1, "test_5");
proc_ref = SYMBOLcreate("proc1", 1, "test_5");
f = ALGcreate(OBJ_FUNCTION);
DICTdefine(scope->symbol_table, func_id->name, f, func_id, OBJ_FUNCTION);
p = ALGcreate(OBJ_PROCEDURE);
DICTdefine(f->symbol_table, proc_id->name, p, proc_id, OBJ_PROCEDURE);
s = PCALLcreate(NULL);
s->symbol = *proc_ref;
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
STMTresolve(s, f);
assert(s->u.proc->procedure == p);
return 0;
}
int test_scope_resolve_named_types() {
Schema scope;
Type sel, ent_base;
Entity ent;
Symbol *ent_id, *sel_id;
scope = SCHEMAcreate();
sel_id = SYMBOLcreate("sel_typ", 1, "test_4");
ent_id = SYMBOLcreate("ent", 1, "test_4");
ent_base = TYPEcreate(entity_);
ent_base->symbol = *ent_id;
ent_base->superscope = scope;
ent = ENTITYcreate(ent_id);
ent->superscope = scope;
sel = TYPEcreate(select_);
sel->symbol = *sel_id;
sel->u.type->body->list = LISTcreate();
sel->superscope = scope;
LISTadd_last(sel->u.type->body->list, ent_base);
DICTdefine(scope->symbol_table, ent_id->name, ent, ent_id, OBJ_ENTITY);
DICTdefine(scope->symbol_table, sel_id->name, sel, sel_id, OBJ_TYPE);
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
SCOPEresolve_types(scope);
assert(!(ent->symbol.resolved & RESOLVE_FAILED));
assert(!(sel->symbol.resolved & RESOLVE_FAILED));
assert(!(scope->symbol.resolved & RESOLVE_FAILED));
return 0;
}
int test_entity_resolve_supertypes() {
Schema scope;
Entity ent1, ent2;
Symbol *ent1_id, *ent2_id, *ent1_ref;
scope = SCHEMAcreate();
ent1_id = SYMBOLcreate("ent1", 1, "test_3");
ent2_id = SYMBOLcreate("ent2", 1, "test_3");
ent1_ref = SYMBOLcreate("ent1", 1, "test_3");
ent1 = ENTITYcreate(ent1_id);
ent2 = ENTITYcreate(ent2_id);
ent1->superscope = scope;
ent2->superscope = scope;
DICTdefine(scope->symbol_table, ent1_id->name, ent1, ent1_id, OBJ_ENTITY);
DICTdefine(scope->symbol_table, ent2_id->name, ent2, ent2_id, OBJ_ENTITY);
ent2->u.entity->supertype_symbols = LISTcreate();
LISTadd_last(ent2->u.entity->supertype_symbols, ent1_ref);
SCOPEfind_fake.custom_fake = SCOPEfind_handler;
ENTITYresolve_supertypes(ent2);
assert(!(ent2->symbol.resolved & RESOLVE_FAILED));
return 0;
}
struct test_def tests[] = {
{"exp_resolve_bad_func_call", test_exp_resolve_bad_func_call},
{"exp_resolve_func_call", test_exp_resolve_func_call},
{"exp_resolve_local_identifier", test_exp_resolve_local_identifier},
{"entity_resolve_subtype_expr_entity", test_entity_resolve_subtype_expr_entity},
{"type_resolve_entity", test_type_resolve_entity},
{"stmt_resolve_pcall_proc", test_stmt_resolve_pcall_proc},
{"scope_resolve_named_types", test_scope_resolve_named_types},
{"entity_resolve_supertypes_entity", test_entity_resolve_supertypes},
{NULL}
};