Debian Codegraph @

gcc/testsuite/gdc.test/runnable/constfold.d

raw
1 #! blah
2 // RUNNABLE_PHOBOS_TEST
3 static assert(__LINE__ == 3); // fails as __LINE__ is 2
4
5 import core.stdc.math : signbit;
6
7
8 /************************************/
9
10 static assert(-(1) == -1);
11
12 static assert(!27 == 0);
13 static assert(!0 == 1);
14 static assert(!6.2 == 0);
15 static assert(!0.0 == 1);
16
17 static assert((6 % 4) == 2);
18 static assert((6u % 4u) == 2u);
19
20 static assert((cast(byte)0x109 >> 1) == 4);
21 static assert((cast(byte)-1 >> 1) == -1);
22 static assert((cast(ubyte)0x109 >> 1) == 4);
23
24 static assert((cast(short)0x10009 >> 1) == 4);
25 static assert((cast(short)-1 >> 1) == -1);
26 static assert((cast(ushort)0x10009 >> 1) == 4);
27
28 static assert((cast(long)0x1_0000_0000_0009 >> 1) == 0x8000_0000_0004);
29 static assert((cast(long)-1L >> 1) == -1);
30 static assert((cast(ulong)0x10009 >> 1) == 0x8004);
31
32 static assert((cast(byte)0x109 >>> 1) == 4);
33 static assert((cast(byte)-1 >>> 1) == int.max);
34 static assert((cast(ubyte)0x109 >>> 1) == 4);
35
36 static assert((cast(short)0x10009 >>> 1) == 4);
37 static assert((cast(short)-1 >>> 1) == int.max);
38 static assert((cast(ushort)0x10009 >>> 1) == 4);
39
40 static assert((cast(long)0x1_0000_0000_0009 >>> 1) == 0x8000_0000_0004);
41 static assert((cast(long)-1L >>> 1) == long.max);
42 static assert((cast(ulong)0x10009 >>> 1) == 0x8004);
43
44 static assert((3 ^ 5) == 6);
45
46 static assert((0 && 0) == 0);
47 static assert((0 && 5) == 0);
48 static assert((10 && 0) == 0);
49 static assert((58 && 10000) == 1);
50
51 static assert((0.0 && 0.0) == 0);
52 static assert((0.0 && 5.1) == 0);
53 static assert((10.0 && 0.0) == 0);
54 static assert((58.6 && 10000.7) == 1);
55
56 static assert((0 || 0) == 0);
57 static assert((0 || 5) == 1);
58 static assert((10 || 0) == 1);
59 static assert((58 || 10000) == 1);
60
61 static assert((0.0 || 0.0) == 0);
62 static assert((0.0 || 5.1) == 1);
63 static assert((10.0 || 0.0) == 1);
64 static assert((58.6 || 10000.7) == 1);
65
66 static assert((5 < 3) == 0);
67 static assert((5 < 5) == 0);
68 static assert((5 < 6) == 1);
69 static assert((5 <= 3) == 0);
70 static assert((5 <= 5) == 1);
71 static assert((5 <= 6) == 1);
72 static assert((5 > 3) == 1);
73 static assert((5 > 5) == 0);
74 static assert((5 > 6) == 0);
75 static assert((5 >= 3) == 1);
76 static assert((5 >= 5) == 1);
77 static assert((5 >= 6) == 0);
78
79 static assert((-5 < -3) == 1);
80 static assert((-5 < -5) == 0);
81 static assert((-5 < -6) == 0);
82 static assert((-5 <= -3) == 1);
83 static assert((-5 <= -5) == 1);
84 static assert((-5 <= -6) == 0);
85 static assert((-5 > -3) == 0);
86 static assert((-5 > -5) == 0);
87 static assert((-5 > -6) == 1);
88 static assert((-5 >= -3) == 0);
89 static assert((-5 >= -5) == 1);
90 static assert((-5 >= -6) == 1);
91
92 static assert((5u < 3u) == 0);
93 static assert((5u < 5u) == 0);
94 static assert((5u < 6u) == 1);
95 static assert((5u <= 3u) == 0);
96 static assert((5u <= 5u) == 1);
97 static assert((5u <= 6u) == 1);
98 static assert((5u > 3u) == 1);
99 static assert((5u > 5u) == 0);
100 static assert((5u > 6u) == 0);
101 static assert((5u >= 3u) == 1);
102 static assert((5u >= 5u) == 1);
103 static assert((5u >= 6u) == 0);
104
105 static assert((-5u < 3) == 0);
106 static assert((-5u <= 3) == 0);
107 static assert((-5u > 3) == 1);
108 static assert((-5u >= 3) == 1);
109
110 static assert((-5 < 3u) == 0);
111 static assert((-5 <= 3u) == 0);
112 static assert((-5 > 3u) == 1);
113 static assert((-5 >= 3u) == 1);
114
115 static assert((5.2 < double.nan) == 0);
116 static assert((5.2 <= double.nan) == 0);
117 static assert((5.2 > double.nan) == 0);
118 static assert((5.2 >= double.nan) == 0);
119
120 static assert((double.nan < 6.2) == 0);
121 static assert((double.nan <= 6.2) == 0);
122 static assert((double.nan > 6.2) == 0);
123 static assert((double.nan >= 6.2) == 0);
124
125 static assert((double.nan < double.nan) == 0);
126 static assert((double.nan <= double.nan) == 0);
127 static assert((double.nan > double.nan) == 0);
128 static assert((double.nan >= double.nan) == 0);
129
130 static assert((5.2 < 6.2) == 1);
131 static assert((5.2 <= 6.2) == 1);
132 static assert((5.2 > 6.2) == 0);
133 static assert((5.2 >= 6.2) == 0);
134
135 static assert((5.2 < 5.2) == 0);
136 static assert((5.2 <= 5.2) == 1);
137 static assert((5.2 > 5.2) == 0);
138 static assert((5.2 >= 5.2) == 1);
139
140 static assert((7.2 < 6.2) == 0);
141 static assert((7.2 <= 6.2) == 0);
142 static assert((7.2 > 6.2) == 1);
143 static assert((7.2 >= 6.2) == 1);
144
145 static assert((7.2 == 6.2) == 0);
146 static assert((7.2 != 6.2) == 1);
147
148 static assert((7.2 == 7.2) == 1);
149 static assert((7.2 != 7.2) == 0);
150
151 static assert((7.2 == double.nan) == 0);
152 static assert((7.2 != double.nan) == 1);
153 static assert((double.nan == double.nan) == 0);
154 static assert((double.nan != double.nan) == 1);
155 static assert((double.nan == 7.2) == 0);
156 static assert((double.nan != 7.2) == 1);
157
158 static assert((5 is 5) == 1);
159 static assert((5 is 4) == 0);
160 static assert((5 !is 5) == 0);
161 static assert((5 !is 4) == 1);
162
163 static assert((5.1 is 5.1) == 1);
164 static assert((5.1 is 4.1) == 0);
165 static assert((5.1 !is 5.1) == 0);
166 static assert((5.1 !is 4.1) == 1);
167
168 static assert((5 ? 2 : 3) == 2);
169 static assert((0 ? 2 : 3) == 3);
170 static assert((5.0 ? 2 : 3) == 2);
171 static assert((0.0 ? 2 : 3) == 3);
172
173 static assert("abc" == "abc");
174
175 //static assert("abc"w.sizeof == 6);
176 //static assert("\U00010000bc"w.sizeof == 8);
177
178 static assert([1,2,3][1] == 2);
179 static assert([1,2,3] ~ [4] == [1,2,3,4]);
180 static assert([1,2,3][1..3] == [2,3]);
181
182 static assert(['a','b','c','d'] == "abcd");
183 static assert("efgh" == ['e','f','g','h']);
184 static assert("efgi" != ['e','f','g','h']);
185
186
187 void test1()
188 {
189 int x;
190 int y;
191 int* p;
192
193 p = &x + cast(size_t)&y;
194 p = &x + 2;
195 p = 4 + &y;
196 p = &x - 1;
197
198 assert((&x is &x) == 1);
199 assert((&x is &y) == 0);
200 assert((&x !is &x) == 0);
201 assert((&x !is &y) == 1);
202 }
203
204 /************************************/
205
206 void test2()
207 {
208 // This test only tests undefined, architecture-dependant behavior.
209 // E.g. the result of converting a float whose value doesn't fit into the integer
210 // leads to an undefined result.
211 version (DigitalMars)
212 {
213 float f = float.infinity;
214 int i = cast(int) f;
215 assert(i == cast(int)float.max);
216 assert(i == 0x80000000);
217 }
218 }
219
220 /************************************/
221
222 void test3()
223 {
224 real n = -0.0;
225 const real m = -0.0;
226
227 assert(signbit(n) != 0);
228 assert(signbit(m) != 0);
229 }
230
231 /************************************/
232
233 struct A4 { char [] a; }
234 struct B4 { long x; }
235 struct C4 { int a;
236 static C4 opCall(int b) { C4 q; q.a=b; return q; }
237 }
238 static assert(!is(typeof( (){ A4 s; B4 q = s; })));
239 static assert(!is(typeof( (){ B4 x =1L; })));
240 static assert(is(typeof( (){ C4 g = 7; })));
241 static assert(is(typeof( (){ C4 g = 7; C4 h = g;})));
242
243 /************************************/
244
245 alias uint DWORD;
246 MY_API_FUNCTION lpStartAddress;
247 extern (Windows) alias DWORD function(void*) MY_API_FUNCTION;
248 static assert(MY_API_FUNCTION.stringof == "extern (Windows) uint function(void*)");
249
250 /************************************/
251
252 enum bug6 = cast(void*)0xFEFEFEFE;
253 static assert(bug6 is bug6);
254
255 /************************************/
256
257 struct S7{
258 double z;
259 }
260
261 int bug7(int x) { return x; }
262
263 S7 s7;
264 double e7 = 4;
265 const double d7 = 4;
266
267 static assert(!is(typeof(bug7(cast(long)e7))));
268 static assert(!is(typeof(bug7(cast(long)s7))));
269 version (LDC) {} else // cast in LDC undefined result w/ x > long.max
270 static assert(!is(typeof(bug7(cast(long)3.256679e30))));
271
272 static assert(is(typeof(bug7(cast(long)d7))));
273 static assert(is(typeof(bug7(cast(long)3.256679e4))));
274
275 /************************************/
276
277 class C8 {
278 int x;
279 }
280 alias C8.x F8;
281 static assert(is(typeof(F8) == int));
282 static assert(is(typeof(C8.x) == int));
283
284 /************************************/
285
286 int foo9() {
287 int u = cast(int)(0x1_0000_0000L);
288 while (u) {
289 if (u) {
290 assert(u!=0);
291 }
292 assert(u!=0);
293 }
294 return 2;
295 }
296
297 static assert(foo9()==2);
298
299 /************************************/
300 // https://issues.dlang.org/show_bug.cgi?id=6077
301
302 void test6077() {
303 static string scat(string s1, string s2)
304 {
305 return s1 ~ s2;
306 }
307
308 static string scatass(string s1, string s2)
309 {
310 s1 ~= s2;
311 return s1;
312 }
313
314 static string[] arycats(string[] ary, string s)
315 {
316 return ary ~ s;
317 }
318
319 static string[] scatary(string s, string[] ary)
320 {
321 return s ~ ary;
322 }
323
324 static string[] arycatasss(string[] ary, string s)
325 {
326 ary ~= s;
327 return ary;
328 }
329
330 static assert(scat(null, null) is null);
331 static assert(scatass(null, null) is null);
332 static assert(arycats(null, null) == cast(string[])[null]);
333 static assert(scatary(null, null) == cast(string[])[null]);
334 static assert(arycatasss(null, null) == cast(string[])[null]);
335 }
336
337 /************************************/
338
339 int test4()
340 {
341 int i;
342
343 dchar d;
344 d >>= 1;
345 d >>>= 1;
346 d <<= 1;
347 d = d >> 1;
348 d = d >>> 1;
349 d = d << 1;
350 wchar w;
351 w >>= 1;
352 w >>>= 1;
353 w <<= 1;
354 w = w >> 1;
355 w = w >>> 1;
356 i = w << 1; // promoted to int
357 char c;
358 c >>= 1;
359 c >>>= 1;
360 c <<= 1;
361 c = c >> 1;
362 c = c >>> 1;
363 i = c << 1; // promoted to int
364 return d + w + c + i;
365 }
366
367 static assert(test4() == 24666);
368
369 /************************************/
370 // https://issues.dlang.org/show_bug.cgi?id=8400
371
372 void test8400()
373 {
374 immutable a = [1,2];
375 int[a.length+0] b; // ok
376 int[a.length ] c; // error
377 }
378
379 /************************************/
380 // https://issues.dlang.org/show_bug.cgi?id=8939
381
382 void foo8939(T)(ref T) { } // same for `auto ref`
383 void bar8939(ref const int) { }
384 void bar8939(ref const S8939) { }
385
386 static struct S8939 { int n; }
387
388 const gn8939 = 1; // or `immutable`
389 const gs8939 = S8939(3);
390 static assert(__traits(compiles, foo8939(gn8939), bar8939(gn8939)));
391 static assert(__traits(compiles, foo8939(gs8939), bar8939(gs8939)));
392
393 void test8939()
394 {
395 foo8939(gn8939), bar8939(gn8939);
396 foo8939(gs8939), bar8939(gs8939);
397
398 const ln8939 = 1;
399 const ls8939 = S8939(3);
400 foo8939(ln8939), bar8939(ln8939);
401 foo8939(ls8939), bar8939(ls8939);
402 }
403
404 class C8939regression
405 {
406 const int n1 = 0;
407 const int n2 = 0;
408 const int n3 = 0;
409 const int n4 = 1;
410
411 int refValue(V)(ref V var)
412 {
413 return 0;
414 }
415
416 void foo()
417 {
418 string[2] str;
419 refValue(str[n1]);
420
421 int[] da;
422 refValue(da[n2]);
423
424 int n; int* p = &n;
425 refValue(*cast(int*)(p + n3));
426
427 refValue([1,2,n4].ptr[0]);
428 }
429 }
430
431 /************************************/
432 // https://issues.dlang.org/show_bug.cgi?id=9058
433
434 template TypeTuple9058(TL...) { alias TypeTuple9058 = TL; }
435 template EnumMembers9058(T)
436 {
437 alias EnumMembers9058 = TypeTuple9058!(Foo9058.A, Foo9058.B);
438 }
439 enum Foo9058 { A, B }
440 size_t bar9058(size_t n)
441 {
442 return 0;
443 }
444 void test9058()
445 {
446 Foo9058 x = [EnumMembers9058!Foo9058][bar9058($)];
447 }
448
449 /************************************/
450 // https://issues.dlang.org/show_bug.cgi?id=12306
451
452 void test12306()
453 {
454 struct Point3D { ubyte x, y, z; }
455
456 enum Point3D pt1 = {x:1, y:1, z:1};
457 const Point3D pt2 = {x:1, y:1, z:1};
458 immutable Point3D pt3 = {x:1, y:1, z:1};
459
460 int[pt1.z][pt1.y][pt1.x] a1;
461 int[pt2.z][pt2.y][pt2.x] a2;
462 int[pt3.z][pt3.y][pt3.x] a3;
463
464 ubyte a = 1;
465 const Point3D ptx = {x:a, y:1, z:1};
466
467 static assert(!__traits(compiles, { int[ptx.z][ptx.y][ptx.x] ax; }));
468 }
469
470 /************************************/
471 // https://issues.dlang.org/show_bug.cgi?id=13977
472
473 void test13977()
474 {
475 bool cond(bool b) { return b; }
476 int x = 0;
477 void check(int n = 1) { x = n; }
478
479 cond(true) && check();
480 assert(x == 1); x = 0;
481
482 cond(false) && check();
483 assert(x == 0); x = 0;
484
485 true && check();
486 assert(x == 1); x = 0;
487
488 false && check();
489 assert(x == 0); x = 0;
490 (int[]).init && check();
491 assert(x == 0); x = 0;
492 Object.init && check();
493 assert(x == 0);
494
495 check(2);
496 false && check();
497 assert(x == 2); x = 0;
498 }
499
500 /************************************/
501 // https://issues.dlang.org/show_bug.cgi?id=13978
502
503 void test13978()
504 {
505 bool cond(bool b) { return b; }
506 int x = 0;
507 void check(int n = 1) { x = n; }
508
509 cond(true) || check();
510 assert(x == 0); x = 0;
511
512 cond(false) || check();
513 assert(x == 1); x = 0;
514
515 true || check();
516 assert(x == 0); x = 0;
517
518 false || check();
519 assert(x == 1); x = 0;
520 (int[]).init || check();
521 assert(x == 1); x = 0;
522 Object.init || check();
523 assert(x == 1); x = 0;
524
525 check(2);
526 true || check();
527 assert(x == 2); x = 0;
528 }
529
530 /************************************/
531 // Pull Request 3697
532
533 void test3697and()
534 {
535 enum x = 0;
536 auto y = x && 1 / x;
537 }
538
539 void test3697or()
540 {
541 enum x = 0;
542 enum y = 1;
543 auto z = y || 1 / x;
544 }
545
546 /************************************/
547 // https://issues.dlang.org/show_bug.cgi?id=14459
548
549 void test14459()
550 {
551 const char* s0 = "hi0";
552 const(char)* p0 = s0;
553 assert(p0 == s0);
554
555 const char* s1 = "hi1";
556 const char* s2 = "hi2";
557 const char* s3 = "hi3";
558 const char* s4 = "hi4";
559 const char* s5 = "hi5";
560 const char* s6 = "hi6";
561 const char* s7 = "hi7";
562 const char* s8 = "hi8";
563 const char* s9 = "hi9";
564 const char* s10 = "hi10";
565 const char* s11 = "hi11";
566 const char* s12 = "hi12";
567 const char* s13 = "hi13";
568 const char* s14 = "hi14";
569 const char* s15 = "hi15";
570 assert(p0 == s0); // ok
571 const char* s16 = "hi16";
572 assert(p0 == s0); // ok <- fails
573 }
574
575 /************************************/
576 // https://issues.dlang.org/show_bug.cgi?id=15607
577
578 static immutable char[2][4] code_base = [ "??", 12 ];
579 static assert(code_base[0] == "??");
580 static assert(code_base[1] == [12, 12]);
581 static assert(code_base[2] == typeof(code_base[2]).init);
582
583 /************************************/
584
585 int main()
586 {
587 test1();
588 test2();
589 test3();
590 test3697and();
591 test3697or();
592 test6077();
593 test8400();
594 test8939();
595 test9058();
596 test13977();
597 test13978();
598 test14459();
599
600 return 0;
601 }