3 * Test program for Sod functionality
5 * (c) 2016 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Sensible Object Design, an object system for C.
12 * SOD is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * SOD is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with SOD; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 /*----- Preliminary definitions -------------------------------------------*/
42 /* Confuse the fragment scanner... */
46 static const char *test_name;
49 static void prepare(const char *name) { test_name = name; test_seq = 0; }
51 static void step(int q, const char *where)
54 fprintf(stderr, "test %s (%s): step %d instead of %d\n",
55 test_name, where, q, test_seq);
61 static void done(int q, const char *where) { step(q, where); }
63 #define STRINGIFY_(x) #x
64 #define STRINGIFY(x) STRINGIFY_(x)
65 #define WHERE __FILE__ ":" STRINGIFY(__LINE__)
66 #define STEP(q) step((q), WHERE)
67 #define DONE(q) done((q), WHERE)
72 [user (tests head) tests (tests tail) main (user end)]
74 /*----- Test machinery ----------------------------------------------------*/
76 static void tests(void)
79 code c: (tests tail) {
85 /*----- Main program ------------------------------------------------------*/
95 /*----- Various kinds of method combinations ------------------------------*/
106 static void *xmalloc(size_t n)
109 if (!p) { perror("malloc"); exit(EXIT_FAILURE); }
113 static struct item *make_item(const char *p)
114 { struct item *q = xmalloc(sizeof(*q)); q->p = p; return (q); }
116 static void free_list(struct item *i)
117 { struct item *ii; while (i) { ii = i->next; free(i); i = ii; } }
119 static int check_list(struct item *i, ...)
127 p = va_arg(ap, const char *);
129 if (!i || strcmp(i->p, p) != 0) break;
137 struct vec { int *v; size_t n; };
139 static void free_vec(struct vec *v) { free(v->v); }
141 static int check_vec(struct vec *v, ...)
152 if (i == v->n || j != v->v[i]) break;
155 if (i == v->n) rc = 0;
162 [link = SodObject, nick = base]
163 class T1Base: SodObject {
164 [combination = progn] void aprogn();
165 [combination = sum] int asum();
166 [combination = and] int aand();
167 [combination = max] int amax();
169 [combination = custom,
170 empty = { sod_ret = 0; },
171 decls = { struct item **head = &sod_ret; },
172 each = { *head = sod_val; head = &sod_val->next; },
173 after = { *head = 0; }]
174 struct item *alist();
176 [combination = custom,
177 decls = { int *v; size_t i = 0; }, methty = <int>, count = n,
178 empty = { sod_ret.v = 0; sod_ret.n = 0; },
179 before = { v = xmalloc(n*sizeof(int)); },
180 each = { v[i++] = sod_val; },
181 after = { sod_ret.v = v; sod_ret.n = n; }]
185 [link = T1Base, nick = mid]
186 class T1Mid: T1Base {
187 void base.aprogn() { STEP(1); }
188 int base.asum() { return 1; }
189 int base.aand() { return 8; }
190 int base.amax() { return 12; }
191 struct item *base.alist() { return make_item("mid"); }
192 int base.avec() { return 19; }
195 [link = T1Mid, nick = sub]
197 void base.aprogn() { STEP(0); }
198 int base.asum() { return 2; }
199 int base.aand() { return 6; }
200 int base.amax() { return 17; }
201 struct item *base.alist() { return make_item("sub"); }
202 int base.avec() { return 4; }
206 prepare("aggregate, base");
207 { SOD_DECL(T1Base, t1, NO_KWARGS);
210 STEP(0); T1Base_aprogn(t1); STEP(1);
211 if (T1Base_asum(t1) == 0) STEP(2);
212 if (T1Base_aand(t1) == 1) STEP(3);
213 if (!t1->_vt->base.amax) STEP(4);
214 l = T1Base_alist(t1);
220 prepare("aggregate, mid");
221 { SOD_DECL(T1Mid, t1, NO_KWARGS);
224 STEP(0); T1Base_aprogn(t1); /* 1 */
225 if (T1Base_asum(t1) == 1) STEP(2);
226 if (T1Base_aand(t1) == 8) STEP(3);
227 if (T1Base_amax(t1) == 12) STEP(4);
228 l = T1Base_alist(t1);
229 if (!check_list(l, "mid", (const char *)0)) STEP(5);
232 if (!check_vec(&v, 19, -1)) STEP(6);
236 prepare("aggregate, sub");
237 { SOD_DECL(T1Sub, t1, NO_KWARGS);
240 T1Base_aprogn(t1); /* 0, 1 */
241 if (T1Base_asum(t1) == 3) STEP(2);
242 if (T1Base_aand(t1) == 8) STEP(3);
243 if (T1Base_amax(t1) == 17) STEP(4);
244 l = T1Base_alist(t1);
245 if (!check_list(l, "sub", "mid", (const char *)0)) STEP(5);
248 if (!check_vec(&v, 4, 19, -1)) STEP(6);
254 /*----- Slot and user initargs --------------------------------------------*/
256 [link = SodObject, nick = t2]
257 class T2: SodObject {
258 [initarg = x] int x = 0;
262 init { if (!y) STEP(0); }
274 prepare("initargs, defaults");
275 { SOD_DECL(T2, t, NO_KWARGS);
276 if (t->t2.x == 0) STEP(0);
279 prepare("initargs, explicit");
280 { SOD_DECL(T2, t, KWARGS(K(x, 42) K(y, 0)));
281 if (t->t2.x == 42) STEP(1);
284 prepare("initargs, inheritance");
285 { SOD_DECL(T2Sub, t, KWARGS(K(c, 1) K(z, 2)));
286 if (t->t2.x == 1) STEP(0);
289 prepare("initargs, ordering");
290 { SOD_DECL(T2Sub, t, KWARGS(K(a, 1) K(b, 2)));
291 if (t->t2.x == 1) STEP(0);
294 prepare("initargs, reprioritizing");
295 { SOD_DECL(T2Sub, t, KWARGS(K(x, 1) K(c, 2)));
296 if (t->t2.x == 1) STEP(0);
301 /*----- Keyword argument propagation --------------------------------------*/
303 [link = SodObject, nick = base]
304 class T3Base: SodObject {
305 void m0(?int x) { STEP(x); }
309 [link = T3Base, nick = mid]
310 class T3Mid: T3Base {
311 void base.m0(?int y) { STEP(y); CALL_NEXT_METHOD; }
312 void base.m1(?) { STEP(4); CALL_NEXT_METHOD; }
315 [link = T3Mid, nick = sub]
317 void base.m0(?int z) { STEP(z); CALL_NEXT_METHOD; }
318 void base.m1(?int z) { STEP(z); CALL_NEXT_METHOD; }
323 { SOD_DECL(T3Sub, t, NO_KWARGS);
324 T3Base_m0(t, KWARGS(K(z, 0) K(y, 1) K(x, 2)));
325 T3Base_m1(t, KWARGS(K(z, 3)));
330 /*----- Metaclass initialization ------------------------------------------*/
332 [link = SodClass, nick = mycls]
333 class MyClass: SodClass {
334 int x = -1, y, z = 2;
337 [link = SodObject, nick = myobj, metaclass = MyClass]
338 class MyObject: SodObject {
339 class mycls.x = 0, mycls.y = 1;
343 prepare("metaclass, init");
344 STEP(MyObject__cls_obj->mycls.x);
345 STEP(MyObject__cls_obj->mycls.y);
346 STEP(MyObject__cls_obj->mycls.z);
350 /*----- That's all, folks -------------------------------------------------*/