src/method-proto.lisp: Collect arguments in vanilla delegation trampolines.
[sod] / test / test.sod
CommitLineData
5de79fa0
MW
1/* -*-sod-*- *
2 *
3 * Test program for Sod functionality
4 *
5 * (c) 2016 Straylight/Edgeware
6 */
7
8/*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Sensible Object Design, an object system for C.
11 *
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.
16 *
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.
21 *
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.
25 */
4a83289a 26
fd040f06 27code h: includes {
4a83289a
MW
28#include "sod.h"
29}
30
fd040f06 31code c: includes {
4a83289a
MW
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35
36#include "test.h"
37}
38
fd040f06 39code c: early_user {
4a83289a
MW
40/*----- Preliminary definitions -------------------------------------------*/
41
42/* Confuse the fragment scanner... */
43#define LBRACE {
44#define RBRACE }
45
46static const char *test_name;
47static int test_seq;
48
49static void prepare(const char *name) { test_name = name; test_seq = 0; }
50
51static void step(int q, const char *where)
52{
53 if (test_seq != q) {
54 fprintf(stderr, "test %s (%s): step %d instead of %d\n",
55 test_name, where, q, test_seq);
56 abort();
57 }
58 test_seq++;
59}
60
61static void done(int q, const char *where) { step(q, where); }
62
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)
68
69}
70
fd040f06 71code c: (tests head)
4a83289a
MW
72 [user (tests head) tests (tests tail) main (user end)]
73{
74/*----- Test machinery ----------------------------------------------------*/
75
76static void tests(void)
77LBRACE
78}
fd040f06 79code c: (tests tail) {
4a83289a
MW
80RBRACE
81
82}
83
fd040f06 84code c: main {
4a83289a
MW
85/*----- Main program ------------------------------------------------------*/
86
87int main(void)
88{
89 tests();
90 return (0);
91}
92
93}
94
95/*----- Various kinds of method combinations ------------------------------*/
96
fd040f06 97code h: early_user {
4a83289a
MW
98struct item {
99 struct item *next;
100 const char *p;
101};
102
103}
104
fd040f06 105code c: early_user {
4a83289a
MW
106static void *xmalloc(size_t n)
107{
108 void *p = malloc(n);
109 if (!p) { perror("malloc"); exit(EXIT_FAILURE); }
110 return (p);
111}
112
113static struct item *make_item(const char *p)
114 { struct item *q = xmalloc(sizeof(*q)); q->p = p; return (q); }
115
116static void free_list(struct item *i)
117 { struct item *ii; while (i) { ii = i->next; free(i); i = ii; } }
118
119static int check_list(struct item *i, ...)
120{
121 va_list ap;
122 const char *p;
123 int rc = -1;
124
125 va_start(ap, i);
126 for (;;) {
127 p = va_arg(ap, const char *);
128 if (!p) break;
129 if (!i || strcmp(i->p, p) != 0) break;
130 i = i->next;
131 }
132 if (!i) rc = 0;
133 va_end(ap);
134 return (rc);
135}
136
137struct vec { int *v; size_t n; };
138
139static void free_vec(struct vec *v) { free(v->v); }
140
141static int check_vec(struct vec *v, ...)
142{
143 va_list ap;
144 int j;
145 size_t i = 0;
146 int rc = -1;
147
148 va_start(ap, v);
149 for (;;) {
150 j = va_arg(ap, int);
151 if (j < 0) break;
152 if (i == v->n || j != v->v[i]) break;
153 i++;
154 }
155 if (i == v->n) rc = 0;
156 va_end(ap);
157 return (rc);
158}
159
160}
161
1e99dff0 162[link = SodObject, nick = base]
fd040f06 163class T1Base: SodObject {
a469422e
MW
164 int plain(int x) { STEP(x); return (x + 1); }
165
b07535d8
MW
166 [combination = progn] void aprogn();
167 [combination = sum] int asum();
168 [combination = and] int aand();
169 [combination = max] int amax();
4a83289a
MW
170
171 [combination = custom,
b07535d8 172 empty = { sod_ret = 0; },
4a83289a
MW
173 decls = { struct item **head = &sod_ret; },
174 each = { *head = sod_val; head = &sod_val->next; },
175 after = { *head = 0; }]
b07535d8 176 struct item *alist();
4a83289a
MW
177
178 [combination = custom,
179 decls = { int *v; size_t i = 0; }, methty = <int>, count = n,
b07535d8 180 empty = { sod_ret.v = 0; sod_ret.n = 0; },
4a83289a
MW
181 before = { v = xmalloc(n*sizeof(int)); },
182 each = { v[i++] = sod_val; },
183 after = { sod_ret.v = v; sod_ret.n = n; }]
184 struct vec avec();
b07535d8
MW
185}
186
1e99dff0 187[link = T1Base, nick = mid]
b07535d8 188class T1Mid: T1Base {
a469422e 189 int base.plain(int x) { STEP(x - 1); return (CALL_NEXT_METHOD); }
1e99dff0
MW
190 void base.aprogn() { STEP(1); }
191 int base.asum() { return 1; }
192 int base.aand() { return 8; }
193 int base.amax() { return 12; }
194 struct item *base.alist() { return make_item("mid"); }
195 int base.avec() { return 19; }
4a83289a
MW
196}
197
1e99dff0 198[link = T1Mid, nick = sub]
b07535d8 199class T1Sub: T1Mid {
1e99dff0
MW
200 void base.aprogn() { STEP(0); }
201 int base.asum() { return 2; }
202 int base.aand() { return 6; }
203 int base.amax() { return 17; }
204 struct item *base.alist() { return make_item("sub"); }
205 int base.avec() { return 4; }
4a83289a
MW
206}
207
fd040f06 208code c: tests {
4a83289a 209 prepare("aggregate, base");
a142609c 210 { SOD_DECL(T1Base, t1, NO_KWARGS);
4a83289a
MW
211 struct item *l;
212 struct vec v;
b07535d8
MW
213 STEP(0); T1Base_aprogn(t1); STEP(1);
214 if (T1Base_asum(t1) == 0) STEP(2);
215 if (T1Base_aand(t1) == 1) STEP(3);
1e99dff0 216 if (!t1->_vt->base.amax) STEP(4);
b07535d8
MW
217 l = T1Base_alist(t1);
218 if (!l) STEP(5);
219 v = T1Base_avec(t1);
220 if (!v.n) STEP(6);
a469422e
MW
221 STEP(T1Base_plain(t1, 7)); /* 7, 8 */
222 DONE(9);
b07535d8
MW
223 }
224 prepare("aggregate, mid");
225 { SOD_DECL(T1Mid, t1, NO_KWARGS);
226 struct item *l;
227 struct vec v;
4a83289a
MW
228 STEP(0); T1Base_aprogn(t1); /* 1 */
229 if (T1Base_asum(t1) == 1) STEP(2);
230 if (T1Base_aand(t1) == 8) STEP(3);
231 if (T1Base_amax(t1) == 12) STEP(4);
232 l = T1Base_alist(t1);
b07535d8 233 if (!check_list(l, "mid", (const char *)0)) STEP(5);
4a83289a
MW
234 free_list(l);
235 v = T1Base_avec(t1);
236 if (!check_vec(&v, 19, -1)) STEP(6);
237 free_vec(&v);
a469422e
MW
238 STEP(T1Base_plain(t1, 8)); /* 7, 8, 9 */
239 DONE(10);
4a83289a
MW
240 }
241 prepare("aggregate, sub");
a142609c 242 { SOD_DECL(T1Sub, t1, NO_KWARGS);
4a83289a
MW
243 struct item *l;
244 struct vec v;
245 T1Base_aprogn(t1); /* 0, 1 */
246 if (T1Base_asum(t1) == 3) STEP(2);
247 if (T1Base_aand(t1) == 8) STEP(3);
248 if (T1Base_amax(t1) == 17) STEP(4);
249 l = T1Base_alist(t1);
b07535d8 250 if (!check_list(l, "sub", "mid", (const char *)0)) STEP(5);
4a83289a
MW
251 free_list(l);
252 v = T1Base_avec(t1);
253 if (!check_vec(&v, 4, 19, -1)) STEP(6);
254 free_vec(&v);
255 DONE(7);
256 }
257}
258
b2983f35
MW
259/*----- Slot and user initargs --------------------------------------------*/
260
261[link = SodObject, nick = t2]
fd040f06 262class T2: SodObject {
b2983f35 263 [initarg = x] int x = 0;
dea6ee94 264 [initarg = z] t2.x;
b2983f35
MW
265
266 initarg int y = 1;
267 init { if (!y) STEP(0); }
268}
269
dea6ee94
MW
270[link = T2]
271class T2Sub: T2 {
272 [initarg = a] t2.x;
273 [initarg = b] t2.x;
274 [initarg = x] t2.x;
275 [initarg = c] t2.x;
276}
277
fd040f06 278code c: tests {
b2983f35
MW
279 prepare("initargs, defaults");
280 { SOD_DECL(T2, t, NO_KWARGS);
281 if (t->t2.x == 0) STEP(0);
282 DONE(1);
283 }
284 prepare("initargs, explicit");
285 { SOD_DECL(T2, t, KWARGS(K(x, 42) K(y, 0)));
286 if (t->t2.x == 42) STEP(1);
287 DONE(2);
288 }
dea6ee94
MW
289 prepare("initargs, inheritance");
290 { SOD_DECL(T2Sub, t, KWARGS(K(c, 1) K(z, 2)));
291 if (t->t2.x == 1) STEP(0);
292 DONE(1);
293 }
294 prepare("initargs, ordering");
295 { SOD_DECL(T2Sub, t, KWARGS(K(a, 1) K(b, 2)));
296 if (t->t2.x == 1) STEP(0);
297 DONE(1);
298 }
299 prepare("initargs, reprioritizing");
300 { SOD_DECL(T2Sub, t, KWARGS(K(x, 1) K(c, 2)));
301 if (t->t2.x == 1) STEP(0);
302 DONE(1);
303 }
b2983f35
MW
304}
305
bce58d37
MW
306/*----- Keyword argument propagation --------------------------------------*/
307
308[link = SodObject, nick = base]
fd040f06 309class T3Base: SodObject {
bce58d37
MW
310 void m0(?int x) { STEP(x); }
311 void m1(?) { }
312}
313
12386a26 314[link = T3Base, nick = mid]
fd040f06 315class T3Mid: T3Base {
12386a26
MW
316 void base.m0(?int y) { STEP(y); CALL_NEXT_METHOD; }
317 void base.m1(?) { STEP(4); CALL_NEXT_METHOD; }
318}
319
320[link = T3Mid, nick = sub]
fd040f06 321class T3Sub: T3Mid {
bce58d37
MW
322 void base.m0(?int z) { STEP(z); CALL_NEXT_METHOD; }
323 void base.m1(?int z) { STEP(z); CALL_NEXT_METHOD; }
324}
325
fd040f06 326code c: tests {
bce58d37
MW
327 prepare("kwargs");
328 { SOD_DECL(T3Sub, t, NO_KWARGS);
12386a26
MW
329 T3Base_m0(t, KWARGS(K(z, 0) K(y, 1) K(x, 2)));
330 T3Base_m1(t, KWARGS(K(z, 3)));
331 DONE(5);
bce58d37
MW
332 }
333}
334
4aea4c60
MW
335/*----- Metaclass initialization ------------------------------------------*/
336
337[link = SodClass, nick = mycls]
338class MyClass: SodClass {
339 int x = -1, y, z = 2;
340}
341
94fd46f5 342[nick = myobj, metaclass = MyClass]
4aea4c60
MW
343class MyObject: SodObject {
344 class mycls.x = 0, mycls.y = 1;
345}
346
347code c: tests {
348 prepare("metaclass, init");
349 STEP(MyObject__cls_obj->mycls.x);
350 STEP(MyObject__cls_obj->mycls.y);
351 STEP(MyObject__cls_obj->mycls.z);
352 DONE(3);
353}
354
4a83289a 355/*----- That's all, folks -------------------------------------------------*/