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 int plain(int x) { STEP(x); return (x + 1); }
166 [combination = progn] void aprogn();
167 [combination = sum] int asum();
168 [combination = and] int aand();
169 [combination = max] int amax();
170 [role = around] int base.asum() { return (CALL_NEXT_METHOD); }
172 [combination = custom,
173 empty = { sod_ret = 0; },
174 decls = { struct item **head = &sod_ret; },
175 each = { *head = sod_val; head = &sod_val->next; },
176 after = { *head = 0; }]
177 struct item *alist();
179 [combination = custom,
180 decls = { int *v; size_t i = 0; }, methty = <int>, count = n,
181 empty = { sod_ret.v = 0; sod_ret.n = 0; },
182 before = { v = xmalloc(n*sizeof(int)); },
183 each = { v[i++] = sod_val; },
184 after = { sod_ret.v = v; sod_ret.n = n; }]
188 [link = T1Base, nick = mid]
189 class T1Mid: T1Base {
190 int base.plain(int x) { STEP(x - 1); return (CALL_NEXT_METHOD); }
191 void base.aprogn() { STEP(1); }
192 int base.asum() { return 1; }
193 int base.aand() { return 8; }
194 int base.amax() { return 12; }
195 struct item *base.alist() { return make_item("mid"); }
196 int base.avec() { return 19; }
199 [link = T1Mid, nick = sub]
201 void base.aprogn() { STEP(0); }
202 int base.asum() { return 2; }
203 int base.aand() { return 6; }
204 int base.amax() { return 17; }
205 struct item *base.alist() { return make_item("sub"); }
206 int base.avec() { return 4; }
210 prepare("aggregate, base");
211 { SOD_DECL(T1Base, t1, NO_KWARGS);
214 STEP(0); T1Base_aprogn(t1); STEP(1);
215 if (T1Base_asum(t1) == 0) STEP(2);
216 if (T1Base_aand(t1) == 1) STEP(3);
217 if (!t1->_vt->base.amax) STEP(4);
218 l = T1Base_alist(t1);
222 STEP(T1Base_plain(t1, 7)); /* 7, 8 */
225 prepare("aggregate, mid");
226 { SOD_DECL(T1Mid, t1, NO_KWARGS);
229 STEP(0); T1Base_aprogn(t1); /* 1 */
230 if (T1Base_asum(t1) == 1) STEP(2);
231 if (T1Base_aand(t1) == 8) STEP(3);
232 if (T1Base_amax(t1) == 12) STEP(4);
233 l = T1Base_alist(t1);
234 if (!check_list(l, "mid", (const char *)0)) STEP(5);
237 if (!check_vec(&v, 19, -1)) STEP(6);
239 STEP(T1Base_plain(t1, 8)); /* 7, 8, 9 */
242 prepare("aggregate, sub");
243 { SOD_DECL(T1Sub, t1, NO_KWARGS);
246 T1Base_aprogn(t1); /* 0, 1 */
247 if (T1Base_asum(t1) == 3) STEP(2);
248 if (T1Base_aand(t1) == 8) STEP(3);
249 if (T1Base_amax(t1) == 17) STEP(4);
250 l = T1Base_alist(t1);
251 if (!check_list(l, "sub", "mid", (const char *)0)) STEP(5);
254 if (!check_vec(&v, 4, 19, -1)) STEP(6);
260 /*----- Slot and user initargs --------------------------------------------*/
262 [link = SodObject, nick = t2]
263 class T2: SodObject {
264 [initarg = x] int x = 0;
268 init { if (!y) STEP(0); }
280 prepare("initargs, defaults");
281 { SOD_DECL(T2, t, NO_KWARGS);
282 if (t->t2.x == 0) STEP(0);
285 prepare("initargs, explicit");
286 { SOD_DECL(T2, t, KWARGS(K(x, 42) K(y, 0)));
287 if (t->t2.x == 42) STEP(1);
290 prepare("initargs, inheritance");
291 { SOD_DECL(T2Sub, t, KWARGS(K(c, 1) K(z, 2)));
292 if (t->t2.x == 1) STEP(0);
295 prepare("initargs, ordering");
296 { SOD_DECL(T2Sub, t, KWARGS(K(a, 1) K(b, 2)));
297 if (t->t2.x == 1) STEP(0);
300 prepare("initargs, reprioritizing");
301 { SOD_DECL(T2Sub, t, KWARGS(K(x, 1) K(c, 2)));
302 if (t->t2.x == 1) STEP(0);
307 /*----- Keyword argument propagation --------------------------------------*/
309 [link = SodObject, nick = base]
310 class T3Base: SodObject {
311 void m0(?int x) { STEP(x); }
315 [link = T3Base, nick = mid]
316 class T3Mid: T3Base {
317 void base.m0(?int y) { STEP(y); CALL_NEXT_METHOD; }
318 void base.m1(?) { STEP(4); CALL_NEXT_METHOD; }
321 [link = T3Mid, nick = sub]
323 void base.m0(?int z) { STEP(z); CALL_NEXT_METHOD; }
324 void base.m1(?int z) { STEP(z); CALL_NEXT_METHOD; }
329 { SOD_DECL(T3Sub, t, NO_KWARGS);
330 T3Base_m0(t, KWARGS(K(z, 0) K(y, 1) K(x, 2)));
331 T3Base_m1(t, KWARGS(K(z, 3)));
336 /*----- Metaclass initialization ------------------------------------------*/
338 [link = SodClass, nick = mycls]
339 class MyClass: SodClass {
340 int x = -1, y, z = 2;
343 [nick = myobj, metaclass = MyClass]
344 class MyObject: SodObject {
345 class mycls.x = 0, mycls.y = 1;
349 prepare("metaclass, init");
350 STEP(MyObject__cls_obj->mycls.x);
351 STEP(MyObject__cls_obj->mycls.y);
352 STEP(MyObject__cls_obj->mycls.z);
356 /*----- That's all, folks -------------------------------------------------*/