struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / vaargs.c.in
blob5f8004ec492614179216124bd2035946d263b52a
1 /** Tests argument passing to functions via va_args.
2 Assumes that up to the first two arguments can be passed in registers.
4 type1: va_char, int
5 type2: va_char, int
6 type3: va_char, int
7 */
8 #include <testfwk.h>
9 #include <stdarg.h>
11 /* gcc 3.3 throws a warning, if char is in '...' */
12 #if defined(PORT_HOST) || defined (__SDCC_pdk13) || defined (__SDCC_pdk14) || defined (__SDCC_pdk15) || defined (__SDCC_f8)
13 # define va_char int
14 #else
15 # define va_char char
16 #endif
18 #ifndef __SDCC_pic16
19 static {type1}
20 returnFirstArg(int marker, ...)
22 va_list ap;
23 {type1} i;
25 va_start(ap, marker);
26 i = va_arg(ap, {type1});
28 va_end(ap);
30 LOG(("Returning %d\n", i));
31 return i;
34 static {type2}
35 returnSecondArg(int marker, ...)
37 va_list ap;
38 {type2} i;
40 va_start(ap, marker);
41 UNUSED(va_arg(ap, {type1}));
42 i = va_arg(ap, {type2});
44 va_end(ap);
46 LOG(("Returning %d\n", i));
47 return i;
50 #ifndef __SDCC_pdk14 // Lack of memory
51 static {type2}
52 returnSecondArgCopy(int marker, ...)
54 va_list ap1, ap2;
55 {type2} i;
57 va_start(ap1, marker);
58 UNUSED(va_arg(ap1, {type1}));
59 va_copy(ap2, ap1);
60 i = va_arg(ap2, {type2});
62 va_end(ap1);
63 va_end(ap2);
65 LOG(("Returning %d\n", i));
66 return i;
69 static {type3}
70 returnThirdArg(int marker, ...)
72 va_list ap;
73 {type3} i;
75 va_start(ap, marker);
76 UNUSED(va_arg(ap, {type1}));
77 UNUSED(va_arg(ap, {type2}));
78 i = va_arg(ap, {type3});
80 va_end(ap);
82 LOG(("Returning %d\n", i));
83 return i;
85 #endif
86 #endif
88 void
89 testArgs(void)
91 #ifndef __SDCC_pic16
92 int marker = 12;
94 LOG(("First arg: %u\n", returnFirstArg(marker, ({type1})123, ({type2})45, ({type3})67)));
95 ASSERT(returnFirstArg(marker, ({type1})123, ({type2})45, ({type3})67) == ({type1})123);
96 ASSERT(returnFirstArg(marker, ({type1})-123, ({type2})45, ({type3})67) == ({type1})-123);
97 #ifndef __SDCC_pdk14 // Lack of memory
98 ASSERT(returnSecondArg(marker, ({type1})1, ({type2})-23, ({type3})64) == ({type2})-23);
99 ASSERT(returnSecondArg(marker, ({type1})1, ({type2})8, ({type3})64) == ({type2})8);
101 ASSERT(returnSecondArgCopy(marker, ({type1})1, ({type2})-23, ({type3})64) == ({type2})-23);
102 ASSERT(returnSecondArgCopy(marker, ({type1})1, ({type2})8, ({type3})64) == ({type2})8);
104 ASSERT(returnThirdArg(marker, ({type1})-33, ({type2})-34, ({type3})-35) == ({type3})-35);
105 ASSERT(returnThirdArg(marker, ({type1})-33, ({type2})-34, ({type3})35) == ({type3})35);
106 #endif
107 #endif