scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

0009-stdarg.c (1125B)


      1 #include <assert.h>
      2 #include <stdarg.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 
      6 /*
      7 output:
      8 testing
      9 test 1
     10 test 2
     11 done
     12 end:
     13 */
     14 
     15 void
     16 vtest(char *fmt, va_list va)
     17 {
     18 	int cnt = 1;
     19 	long long *p;
     20 
     21 	while (*fmt) {
     22 		switch (*fmt++) {
     23 		case 'c':
     24 			assert(va_arg(va, int) == cnt++);
     25 			break;
     26 		case 's':
     27 			assert(va_arg(va, int) == cnt++);
     28 			break;
     29 		case 'i':
     30 			assert(va_arg(va, int) == cnt++);
     31 			break;
     32 		case 'l':
     33 			assert(va_arg(va, long) == cnt++);
     34 			break;
     35 		case 'q':
     36 			assert(va_arg(va, long long) == cnt++);
     37 			break;
     38 		case 'p':
     39 			p = va_arg(va, void *);
     40 			assert(*p == cnt++);
     41 			break;
     42 		case 'f':
     43 			assert(va_arg(va, double) == cnt++);
     44 			break;
     45 		default:
     46 			abort();
     47 		}
     48 	}
     49 }
     50 
     51 void
     52 test(char *s, char *fmt, ...)
     53 {
     54 	va_list va, vc;
     55 
     56 	puts(s);
     57 	va_start(va, fmt);
     58 	va_copy(vc, va);
     59 	vtest(fmt, vc);
     60 	va_end(vc);
     61 	va_end(va);
     62 }
     63 
     64 int
     65 main()
     66 {
     67 	char c;
     68 	short s;
     69 	int i;
     70 	long l;
     71 	long long ll;
     72 	float f;
     73 
     74 	puts("testing");
     75 	c = 1;
     76 	i = 2;
     77 	l = 3;
     78 	ll = 4;
     79 	test("test 1", "cilp", c, i, l, (void *) &ll);
     80 
     81 	c = 1;
     82 	s = 2;
     83 	ll = 3;
     84 	f = 4.0;
     85 	test("test 2", "csqf", c, s, ll, f);
     86 	puts("done");
     87 
     88 	return 0;
     89 }