X-Git-Url: https://pd.if.org/git/?p=lice;a=blobdiff_plain;f=tests%2Fvarargs.c;fp=tests%2Fvarargs.c;h=7cc88d98c61611a3e335c3e5974fdbcf5e3b664a;hp=0000000000000000000000000000000000000000;hb=b6a50b8be3d6a2e2d6624983f6bf1bf0c9f6802a;hpb=946bdbe1d5dd89ab671391fbe429a1c2c48ecaa7 diff --git a/tests/varargs.c b/tests/varargs.c new file mode 100644 index 0000000..7cc88d9 --- /dev/null +++ b/tests/varargs.c @@ -0,0 +1,67 @@ +// stdarg + +#include +char buffer[1024]; + +void testi(int a, ...) { + va_list ap; + va_start(ap, a); + expecti(a, 1); + expecti(va_arg(ap, int), 2); + expecti(va_arg(ap, int), 3); + expecti(va_arg(ap, int), 4); + expecti(va_arg(ap, int), 5); + + va_end(ap); +} + +void testf(float a, ...) { + va_list ap; + va_start(ap, a); + + expectf(a, 1.0f); + expectf(va_arg(ap, float), 2.0f); + expectf(va_arg(ap, float), 4.0f); + expectf(va_arg(ap, float), 8.0f); + + va_end(ap); +} + +void testm(char *p, ...) { + va_list ap; + va_start(ap, p); + + expectstr(p, "hello world"); + expectf (va_arg(ap, float), 3.14); + expecti (va_arg(ap, int), 1024); + expectstr(va_arg(ap, char *), "good bye world"); + expecti (va_arg(ap, int), 2048); + + va_end(ap); +} + +char *format(char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + vsprintf(buffer, fmt, ap); // fuck yeah + va_end(ap); + return buffer; +} + +void testt(void) { + expectstr(format(""), ""); // nothing + expectstr(format("%d", 10), "10"); + expectstr( + format("%d,%.1f,%d,%.1f,%s", 1024, 3.14, 2048, 6.28, "hello world"), + "1024,3.1,2048,6.3,hello world" + ); +} + +int main(void) { + testi(1, 2, 3, 4, 5); + testf(1.0f, 2.0f, 4.0f, 8.0f); + testm("hello world", 3.14, 1024, "good bye world", 2048); + testt(); + + return 0; +}