OSDN Git Service

* c-decl.c (declspecs_add_type): Don't pedwarn for _Complex in
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / gcc.dg / c99-bool-1.c
1 /* Test for _Bool and <stdbool.h> in C99.  */
2 /* Origin: Joseph Myers <jsm28@cam.ac.uk> */
3 /* { dg-do run } */
4 /* { dg-options "-std=iso9899:1999 -pedantic-errors" } */
5
6 /* _Bool must be a builtin type.  */
7
8 _Bool foo;
9
10 #include <stdbool.h>
11
12 /* Three macros must be integer constant expressions suitable for use
13    in #if.
14 */
15
16 #if !defined(true) || (true != 1)
17 #error "bad stdbool true" /* { dg-bogus "#error" "bad stdbool.h" } */
18 #endif
19
20 #if !defined(false) || (false != 0)
21 #error "bad stdbool false" /* { dg-bogus "#error" "bad stdbool.h" } */
22 #endif
23
24 #if !defined(__bool_true_false_are_defined) || (__bool_true_false_are_defined != 1)
25 #error "bad stdbool __bool_true_false_are_defined" /* { dg-bogus "#error" "bad stdbool.h" } */
26 #endif
27
28 int a = true;
29 int b = false;
30 int c = __bool_true_false_are_defined;
31
32 struct foo
33 {
34   _Bool a : 1;
35   _Bool b : 2;
36   _Bool c : 7;
37 } sf;
38
39 #define str(x) xstr(x)
40 #define xstr(x) #x
41
42
43 extern void abort (void);
44 extern void exit (int);
45 extern int strcmp (const char *, const char *);
46
47 int
48 main (void)
49 {
50   /* The macro `bool' must expand to _Bool.  */
51   const char *t = str (bool);
52   _Bool u, v;
53   if (strcmp (t, "_Bool"))
54     abort ();
55   if (a != 1 || b != 0 || c != 1)
56     abort ();
57   /* Casts to _Bool have a specified behavior.  */
58   if ((int)(_Bool)2 != 1)
59     abort ();
60   if ((int)(_Bool)0.2 != 1)
61     abort ();
62   /* Pointers may be assigned to _Bool.  */
63   if ((u = t) != 1)
64     abort ();
65   /* _Bool may be used to subscript arrays.  */
66   u = 0;
67   if (t[u] != '_')
68     abort ();
69   if (u[t] != '_')
70     abort ();
71   u = 1;
72   if (t[u] != 'B')
73     abort ();
74   if (u[t] != 'B')
75     abort ();
76   /* Test increment and decrement operators.  */
77   u = 0;
78   if (u++ != 0)
79     abort ();
80   if (u != 1)
81     abort ();
82   if (u++ != 1)
83     abort ();
84   if (u != 1)
85     abort ();
86   u = 0;
87   if (++u != 1)
88     abort ();
89   if (u != 1)
90     abort ();
91   if (++u != 1)
92     abort ();
93   if (u != 1)
94     abort ();
95   u = 0;
96   if (u-- != 0)
97     abort ();
98   if (u != 1)
99     abort ();
100   if (u-- != 1)
101     abort ();
102   if (u != 0)
103     abort ();
104   u = 0;
105   if (--u != 1)
106     abort ();
107   if (u != 1)
108     abort ();
109   if (--u != 0)
110     abort ();
111   if (u != 0)
112     abort ();
113   /* Test unary + - ~ !.  */
114   u = 0;
115   if (+u != 0)
116     abort ();
117   if (-u != 0)
118     abort ();
119   u = 1;
120   if (+u != 1)
121     abort ();
122   if (-u != -1)
123     abort ();
124   u = 2;
125   if (+u != 1)
126     abort ();
127   if (-u != -1)
128     abort ();
129   u = 0;
130   if (~u != ~(int)0)
131     abort ();
132   u = 1;
133   if (~u != ~(int)1)
134     abort ();
135   u = 0;
136   if (!u != 1)
137     abort ();
138   u = 1;
139   if (!u != 0)
140     abort ();
141   /* Test arithmetic * / % + - (which all apply promotions).  */
142   u = 0;
143   if (u + 2 != 2)
144     abort ();
145   u = 1;
146   if (u * 4 != 4)
147     abort ();
148   if (u % 3 != 1)
149     abort ();
150   if (u / 1 != 1)
151     abort ();
152   if (4 / u != 4)
153     abort ();
154   if (u - 7 != -6)
155     abort ();
156   /* Test bitwise shift << >>.  */
157   u = 1;
158   if (u << 1 != 2)
159     abort ();
160   if (u >> 1 != 0)
161     abort ();
162   /* Test relational and equality operators < > <= >= == !=.  */
163   u = 0;
164   v = 0;
165   if (u < v || u > v || !(u <= v) || !(u >= v) || !(u == v) || u != v)
166     abort ();
167   u = 0;
168   v = 1;
169   if (!(u < v) || u > v || !(u <= v) || u >= v || u == v || !(u != v))
170     abort ();
171   /* Test bitwise operators & ^ |.  */
172   u = 1;
173   if ((u | 2) != 3)
174     abort ();
175   if ((u ^ 3) != 2)
176     abort ();
177   if ((u & 1) != 1)
178     abort ();
179   if ((u & 0) != 0)
180     abort ();
181   /* Test logical && ||.  */
182   u = 0;
183   v = 1;
184   if (!(u || v))
185     abort ();
186   if (!(v || u))
187     abort ();
188   if (u && v)
189     abort ();
190   if (v && u)
191     abort ();
192   u = 1;
193   v = 1;
194   if (!(u && v))
195     abort ();
196   /* Test conditional ? :.  */
197   u = 0;
198   if ((u ? 4 : 7) != 7)
199     abort ();
200   u = 1;
201   v = 0;
202   if ((1 ? u : v) != 1)
203     abort ();
204   if ((1 ? 4 : u) != 4)
205     abort ();
206   /* Test assignment operators = *= /= %= += -= <<= >>= &= ^= |=.  */
207   if ((u = 2) != 1)
208     abort ();
209   if (u != 1)
210     abort ();
211   if ((u *= -1) != 1)
212     abort ();
213   if (u != 1)
214     abort ();
215   if ((u /= 2) != 0)
216     abort ();
217   if ((u += 3) != 1)
218     abort ();
219   if ((u -= 1) != 0)
220     abort ();
221   u = 1;
222   if ((u <<= 4) != 1)
223     abort ();
224   if ((u >>= 1) != 0)
225     abort ();
226   u = 1;
227   if ((u &= 0) != 0)
228     abort ();
229   if ((u |= 2) != 1)
230     abort ();
231   if ((u ^= 3) != 1)
232     abort ();
233   /* Test comma expressions.  */
234   u = 1;
235   if ((4, u) != 1)
236     abort ();
237   /* Test bitfields.  */
238   {
239     int i;
240     for (i = 0; i < sizeof (struct foo); i++)
241       *((unsigned char *)&sf + i) = (unsigned char) -1;
242     sf.a = 1;
243     if (sf.a != 1)
244       abort ();
245     sf.b = 1;
246     if (sf.b != 1)
247       abort ();
248     sf.c = 1;
249     if (sf.c != 1)
250       abort ();
251     sf.a = 0;
252     if (sf.a != 0)
253       abort ();
254     sf.b = 0;
255     if (sf.b != 0)
256       abort ();
257     sf.c = 0;
258     if (sf.c != 0)
259       abort ();
260   }
261   exit (0);
262 }