]> pd.if.org Git - lice/blob - tests/varargs.c
autocommit for files dated 2014-11-17 20:15:26
[lice] / tests / varargs.c
1 // stdarg
2
3 #include <stdarg.h>
4 char buffer[1024];
5
6 void testi(int a, ...) {
7     va_list ap;
8     va_start(ap, a);
9     expecti(a,               1);
10     expecti(va_arg(ap, int), 2);
11     expecti(va_arg(ap, int), 3);
12     expecti(va_arg(ap, int), 4);
13     expecti(va_arg(ap, int), 5);
14
15     va_end(ap);
16 }
17
18 void testf(float a, ...) {
19     va_list ap;
20     va_start(ap, a);
21
22     expectf(a,                 1.0f);
23     expectf(va_arg(ap, float), 2.0f);
24     expectf(va_arg(ap, float), 4.0f);
25     expectf(va_arg(ap, float), 8.0f);
26
27     va_end(ap);
28 }
29
30 void testm(char *p, ...) {
31     va_list ap;
32     va_start(ap, p);
33
34     expectstr(p,                 "hello world");
35     expectf  (va_arg(ap, float),  3.14);
36     expecti  (va_arg(ap, int),    1024);
37     expectstr(va_arg(ap, char *), "good bye world");
38     expecti  (va_arg(ap, int),    2048);
39
40     va_end(ap);
41 }
42
43 char *format(char *fmt, ...) {
44     va_list ap;
45     va_start(ap, fmt);
46     vsprintf(buffer, fmt, ap); // fuck yeah
47     va_end(ap);
48     return buffer;
49 }
50
51 void testt(void) {
52     expectstr(format(""),        ""); // nothing
53     expectstr(format("%d", 10),  "10");
54     expectstr(
55         format("%d,%.1f,%d,%.1f,%s", 1024, 3.14, 2048, 6.28, "hello world"),
56         "1024,3.1,2048,6.3,hello world"
57     );
58 }
59
60 int main(void) {
61     testi(1, 2, 3, 4, 5);
62     testf(1.0f, 2.0f, 4.0f, 8.0f);
63     testm("hello world", 3.14, 1024, "good bye world", 2048);
64     testt();
65
66     return 0;
67 }