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