16 /*----- Preliminary definitions -------------------------------------------*/
18 /* Confuse the fragment scanner... */
22 static const char *test_name;
25 static void prepare(const char *name) { test_name = name; test_seq = 0; }
27 static void step(int q, const char *where)
30 fprintf(stderr, "test %s (%s): step %d instead of %d\n",
31 test_name, where, q, test_seq);
37 static void done(int q, const char *where) { step(q, where); }
39 #define STRINGIFY_(x) #x
40 #define STRINGIFY(x) STRINGIFY_(x)
41 #define WHERE __FILE__ ":" STRINGIFY(__LINE__)
42 #define STEP(q) step((q), WHERE)
43 #define DONE(q) done((q), WHERE)
48 [user (tests head) tests (tests tail) main (user end)]
50 /*----- Test machinery ----------------------------------------------------*/
52 static void tests(void)
55 code c: (tests tail) {
61 /*----- Main program ------------------------------------------------------*/
71 /*----- Various kinds of method combinations ------------------------------*/
82 static void *xmalloc(size_t n)
85 if (!p) { perror("malloc"); exit(EXIT_FAILURE); }
89 static struct item *make_item(const char *p)
90 { struct item *q = xmalloc(sizeof(*q)); q->p = p; return (q); }
92 static void free_list(struct item *i)
93 { struct item *ii; while (i) { ii = i->next; free(i); i = ii; } }
95 static int check_list(struct item *i, ...)
103 p = va_arg(ap, const char *);
105 if (!i || strcmp(i->p, p) != 0) break;
113 struct vec { int *v; size_t n; };
115 static void free_vec(struct vec *v) { free(v->v); }
117 static int check_vec(struct vec *v, ...)
128 if (i == v->n || j != v->v[i]) break;
131 if (i == v->n) rc = 0;
138 [link = SodObject, nick = base]
139 class T1Base: SodObject {
140 [combination = progn] void aprogn();
141 [combination = sum] int asum();
142 [combination = and] int aand();
143 [combination = max] int amax();
145 [combination = custom,
146 empty = { sod_ret = 0; },
147 decls = { struct item **head = &sod_ret; },
148 each = { *head = sod_val; head = &sod_val->next; },
149 after = { *head = 0; }]
150 struct item *alist();
152 [combination = custom,
153 decls = { int *v; size_t i = 0; }, methty = <int>, count = n,
154 empty = { sod_ret.v = 0; sod_ret.n = 0; },
155 before = { v = xmalloc(n*sizeof(int)); },
156 each = { v[i++] = sod_val; },
157 after = { sod_ret.v = v; sod_ret.n = n; }]
161 [link = T1Base, nick = mid]
162 class T1Mid: T1Base {
163 void base.aprogn() { STEP(1); }
164 int base.asum() { return 1; }
165 int base.aand() { return 8; }
166 int base.amax() { return 12; }
167 struct item *base.alist() { return make_item("mid"); }
168 int base.avec() { return 19; }
171 [link = T1Mid, nick = sub]
173 void base.aprogn() { STEP(0); }
174 int base.asum() { return 2; }
175 int base.aand() { return 6; }
176 int base.amax() { return 17; }
177 struct item *base.alist() { return make_item("sub"); }
178 int base.avec() { return 4; }
182 prepare("aggregate, base");
183 { SOD_DECL(T1Base, t1, NO_KWARGS);
186 STEP(0); T1Base_aprogn(t1); STEP(1);
187 if (T1Base_asum(t1) == 0) STEP(2);
188 if (T1Base_aand(t1) == 1) STEP(3);
189 if (!t1->_vt->base.amax) STEP(4);
190 l = T1Base_alist(t1);
196 prepare("aggregate, mid");
197 { SOD_DECL(T1Mid, t1, NO_KWARGS);
200 STEP(0); T1Base_aprogn(t1); /* 1 */
201 if (T1Base_asum(t1) == 1) STEP(2);
202 if (T1Base_aand(t1) == 8) STEP(3);
203 if (T1Base_amax(t1) == 12) STEP(4);
204 l = T1Base_alist(t1);
205 if (!check_list(l, "mid", (const char *)0)) STEP(5);
208 if (!check_vec(&v, 19, -1)) STEP(6);
212 prepare("aggregate, sub");
213 { SOD_DECL(T1Sub, t1, NO_KWARGS);
216 T1Base_aprogn(t1); /* 0, 1 */
217 if (T1Base_asum(t1) == 3) STEP(2);
218 if (T1Base_aand(t1) == 8) STEP(3);
219 if (T1Base_amax(t1) == 17) STEP(4);
220 l = T1Base_alist(t1);
221 if (!check_list(l, "sub", "mid", (const char *)0)) STEP(5);
224 if (!check_vec(&v, 4, 19, -1)) STEP(6);
230 /*----- Slot and user initargs --------------------------------------------*/
232 [link = SodObject, nick = t2]
233 class T2: SodObject {
234 [initarg = x] int x = 0;
237 init { if (!y) STEP(0); }
241 prepare("initargs, defaults");
242 { SOD_DECL(T2, t, NO_KWARGS);
243 if (t->t2.x == 0) STEP(0);
246 prepare("initargs, explicit");
247 { SOD_DECL(T2, t, KWARGS(K(x, 42) K(y, 0)));
248 if (t->t2.x == 42) STEP(1);
253 /*----- Keyword argument propagation --------------------------------------*/
255 [link = SodObject, nick = base]
256 class T3Base: SodObject {
257 void m0(?int x) { STEP(x); }
261 [link = T3Base, nick = mid]
262 class T3Mid: T3Base {
263 void base.m0(?int y) { STEP(y); CALL_NEXT_METHOD; }
264 void base.m1(?) { STEP(4); CALL_NEXT_METHOD; }
267 [link = T3Mid, nick = sub]
269 void base.m0(?int z) { STEP(z); CALL_NEXT_METHOD; }
270 void base.m1(?int z) { STEP(z); CALL_NEXT_METHOD; }
275 { SOD_DECL(T3Sub, t, NO_KWARGS);
276 T3Base_m0(t, KWARGS(K(z, 0) K(y, 1) K(x, 2)));
277 T3Base_m1(t, KWARGS(K(z, 3)));
282 /*----- That's all, folks -------------------------------------------------*/