2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / gcc.dg / cpp / vararg2.c
blob2b2b0335c790709a5c85c4143c62e27da8f6faac
1 /* { dg-do preprocess } */
2 /* { dg-options "-std=gnu99" } */
4 /* Jamie's varargs macros from hell. Not for the faint of heart.
5 Great tests that C99 and GNU varargs give identical results.
6 Adapted to the testsuite by Neil Booth, 1 Nov 2000. */
8 /* Permission is granted to use, copy, modify and distribute this file
9 freely for any purpose whatsoever. This file is free software, and
10 there's no warranty.
12 -- Jamie Lokier <jamie.lokier@cern.ch>, 25/Sep/2000. */
14 #define dup3(x) x,x,x
16 /* Count elements in a list (0 to 10 max). */
17 #define gnu_count(y...) _gnu_count1 ( , ##y)
18 #define _gnu_count1(y...) _gnu_count2 (y,10,9,8,7,6,5,4,3,2,1,0)
19 #define _gnu_count2(_,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,n,ys...) n
21 /* Tail of a list. */
22 #define gnu_tail(y...) _gnu_tail (y)
23 #define _gnu_tail(x,y...) y
25 /* Repeat N times. */
26 #define gnu_repeat(n, x) gnu_tail (_gnu_repeat (n, x))
27 #define _gnu_repeat(n, x) _gnu_repeat_##n (x)
28 #define _gnu_repeat_0(x)
29 #define _gnu_repeat_1(x) ,x
30 #define _gnu_repeat_2(x) ,x,x
31 #define _gnu_repeat_3(x) ,x,x,x
32 #define _gnu_repeat_4(x) ,x,x,x,x
33 #define _gnu_repeat_5(x) ,x,x,x,x,x
35 #define _gnu_keep(xs...) xs
36 #define _gnu_discard(xs...)
37 #define _gnu_split_r(n,xs...) _gnu_split_rd (n,_gnu_keep,_gnu_discard xs)
38 #define _gnu_split_d(n,xs...) _gnu_split_rd (n,_gnu_discard,_gnu_keep xs)
39 #define _gnu_split_rd(n,xs...) _gnu_split_##n (xs)
40 #define _gnu_split_0(a,b,xs...) a() b(xs)
41 #define _gnu_split_1(a,b,x0,xs...) a(x0) b(xs)
42 #define _gnu_split_2(a,b,x0,x1,xs...) a(x0,x1) b(xs)
43 #define _gnu_split_3(a,b,x0,x1,x2,xs...) a(x0,x1,x2) b(xs)
44 #define _gnu_split_4(a,b,x0,x1,x2,x3,xs...) a(x0,x1,x2,x3) b(xs)
45 #define _gnu_split_5(a,b,x0,x1,x2,x3,x4,xs...) a(x0,x1,x2,x3,x4) b(xs)
47 /* List manipulations. Surprise: index zero is the rightmost element. */
48 #define gnu_take(n, xs...) \
49 _gnu_split_d (_gnu_count1 ( , ## xs), _gnu_repeat (n, _gnu_error) , ## xs)
50 #define gnu_drop(n, xs...) \
51 _gnu_split_d (n,,_gnu_split_r (_gnu_count1 ( , ## xs), _gnu_repeat (n, _gnu_error) , ## xs))
52 #define gnu_index(pos, xs...) gnu_take (1, gnu_drop (pos , ## xs))
54 /* C99 __VA_ARGS__ versions */
55 #define c99_count(...) _c99_count1 ( , ##__VA_ARGS__)/* If only ## worked.*/
56 #define _c99_count1(...) _c99_count2 (__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0)
57 #define _c99_count2(_,x0,x1,x2,x3,x4,x5,x6,x7,x8,x9,n,...) n
59 #define c99_tail(...) _c99_tail (__VA_ARGS__)
60 #define _c99_tail(x,...) __VA_ARGS__
62 /* Repeat N times. */
63 #define c99_repeat(n, x) c99_tail (_c99_repeat (n, x))
64 #define _c99_repeat(n, x) _c99_repeat_##n (x)
65 #define _c99_repeat_0(x)
66 #define _c99_repeat_1(x) ,x
67 #define _c99_repeat_2(x) ,x,x
68 #define _c99_repeat_3(x) ,x,x,x
69 #define _c99_repeat_4(x) ,x,x,x,x
70 #define _c99_repeat_5(x) ,x,x,x,x,x
72 #define _c99_keep(...) __VA_ARGS__
73 #define _c99_discard(...)
74 #define _c99_split_r(n,...) _c99_split_rd(n,_c99_keep,_c99_discard __VA_ARGS__)
75 #define _c99_split_d(n,...) _c99_split_rd(n,_c99_discard,_c99_keep __VA_ARGS__)
76 #define _c99_split_rd(n,...) _c99_split_##n (__VA_ARGS__)
77 #define _c99_split_0(a,b,...) a() b(__VA_ARGS__)
78 #define _c99_split_1(a,b,x0,...) a(x0) b(__VA_ARGS__)
79 #define _c99_split_2(a,b,x0,x1,...) a(x0,x1) b(__VA_ARGS__)
80 #define _c99_split_3(a,b,x0,x1,x2,...) a(x0,x1,x2) b(__VA_ARGS__)
81 #define _c99_split_4(a,b,x0,x1,x2,x3,...) a(x0,x1,x2,x3) b(__VA_ARGS__)
82 #define _c99_split_5(a,b,x0,x1,x2,x3,x4,...) a(x0,x1,x2,x3,x4) b(__VA_ARGS__)
84 /* List manipulations. Surprise: index zero is the rightmost element. */
85 #define c99_take(n, ...) \
86 _c99_split_d (_c99_count1 ( , ## __VA_ARGS__), _c99_repeat (n, _c99_error) , ## __VA_ARGS__)
87 #define c99_drop(n, ...) \
88 _c99_split_d (n,,_c99_split_r (_c99_count1 ( , ## __VA_ARGS__), _c99_repeat (n, _c99_error) , ## __VA_ARGS__))
89 #define c99_index(pos, ...) c99_take (1, c99_drop (pos , ## __VA_ARGS__))
91 /************** Expansions **************/
93 /* Correct answers are 0, 0, 1, 2, 10. */
94 #if _gnu_count1 () != 0 || gnu_count () != 0 || gnu_count (A) != 1 \
95 || gnu_count (,) != 2 || gnu_count (A, B, C, D, E, F, G, H, I, J) != 10
96 #error gnu_count
97 #endif
99 /* Correct answers are empty, 'x'. */
100 #if gnu_repeat (0, 'x') gnu_repeat (1, 'x') != 'x'
101 #error gnu_repeat
102 #endif
104 /* Correct answers are "e", "b", "a", empty. */
105 #if gnu_index (0, 'a', 'b', 'c', 'd', 'e') != 'e' \
106 || gnu_index (3, 'a', 'b', 'c', 'd', 'e') != 'b' \
107 || gnu_index (4, 'a', 'b', 'c', 'd', 'e') != 'a' \
108 gnu_index (5, 'a', 'b', 'c', 'd', 'e')
109 #error gnu_index
110 #endif
112 /************* C99 tests *************/
114 /* The answers are 0, 0, 1, 2, 10 as for the non-C99 version. */
115 #if _c99_count1 () != 0 || c99_count () != 0 || c99_count (A) != 1 \
116 || c99_count (,) != 2 || c99_count (A, B, C, D, E, F, G, H, I, J) != 10
117 #error c99_count
118 #endif
120 /* Correct answers are empty, 'x'. */
121 #if c99_repeat (0, 'x') c99_repeat (1, 'x') != 'x'
122 #error c99_repeat
123 #endif
125 /* Correct answers are "e", "b", "a", empty. */
126 #if c99_index (0, 'a', 'b', 'c', 'd', 'e') != 'e' \
127 || c99_index (3, 'a', 'b', 'c', 'd', 'e') != 'b' \
128 || c99_index (4, 'a', 'b', 'c', 'd', 'e') != 'a' \
129 c99_index (5, 'a', 'b', 'c', 'd', 'e')
130 #error gnu_index
131 #endif