3 * Test program for the keyword-argument machinery
5 * (c) 2015 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.
34 static void show_kwval(const char *what
, int f
, const char *fmt
, ...)
38 printf(" %s = ", what
);
49 #define SHOW(mem, fmt) show_kwval(#mem, kw.mem##_suppliedp, fmt, kw.mem)
54 _(const char *, z, "default")
56 static KWSET_PARSEFN(t1
)
57 static KWCALL
void t1(const char *what
, KWTAIL
)
61 printf("t1: %s\n", what
);
67 static KWCALL
void t2(const char *what
, ...)
71 printf("t2: %s\n", what
);
73 t1("via t2", KWARGS(K_VALIST(ap
)));
81 static KWSET_PARSEFN(t3
)
82 static KWCALL
void t3(const char *what
, KWTAIL
)
84 struct kwval v
[KW_COUNT(t1
)];
88 printf("t3: %s\n", what
);
92 KW_COPY(t3
, t1
, kw
, v
, n
);
93 t1("via t3", KWARGS(K_TAB(v
, n
)
94 K(x
, kw
.x_suppliedp ? kw
.x
+ 1 : 42)));
97 /* The @KW_TEST@ machinery from the manpage... */
98 #define KWARGS_TEST(k, val) KWARGS(K(k, val) K(kw.unknown, 0))
100 static jmp_buf kw_test_jmp
;
102 static void kw_test_unknown(const char *set
, const char *kw
)
104 if (strcmp(kw
, "kw.unknown") == 0) longjmp(kw_test_jmp
, 1);
105 else longjmp(kw_test_jmp
, 2);
108 #define KW_TEST(flag, set, call) do { \
109 kw_unkhookfn *oldunk = kw_unkhook; \
110 kw_unkhook = kw_test_unknown; \
111 switch (setjmp(kw_test_jmp)) { \
112 case 0: call; abort(); \
113 case 1: flag = 1; break; \
114 case 2: flag = 0; break; \
117 kw_unkhook = oldunk; \
123 t1("no args", NO_KWARGS
);
124 t1("some args", KWARGS(K(z
, "set") K(x
, 42)));
126 t2("indirection", KWARGS(K(y
, 6.283)));
128 t3("no args", NO_KWARGS
);
129 t3("x initially 19", KWARGS(K(q
, 't') K(x
, 19) K(z
, "boing")));
131 #define TEST_KWTEST(kw, val) do { \
133 KW_TEST(f_, t3, t3(0, KWARGS_TEST(kw, val))); \
134 printf("t3 arg `%s' %s\n", #kw, f_ ? "present" : "absent"); \
137 TEST_KWTEST(z
, "splat");
138 TEST_KWTEST(nope
, 0);
143 /*----- That's all, folks -------------------------------------------------*/