]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/testswitch.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / testswitch.d
1 // PERMUTE_ARGS: -w
2
3 extern(C) int printf(const char*, ...);
4
5 int testswitch(string h)
6 {
7 int x;
8
9 switch (h)
10 {
11 case "abc":
12 printf("abc\n");
13 x = 4;
14 break;
15 case "foo":
16 printf("foo\n");
17 x = 1;
18 break;
19 case "bar":
20 printf("bar\n");
21 x = 2;
22 break;
23 default:
24 printf("default\n");
25 x = 3;
26 break;
27 }
28 return x;
29 }
30
31 void test1()
32 {
33 int i;
34
35 i = testswitch("foo");
36 printf("i = %d\n", i);
37 assert(i == 1);
38 assert(testswitch("abc") == 4);
39 assert(testswitch("bar") == 2);
40 assert(testswitch("hello") == 3);
41 printf("Success\n");
42 }
43
44 /*****************************************/
45
46 void test2()
47 {
48 int i;
49
50 switch (5)
51 {
52 case 3,4,5,6:
53 i = 20;
54 break;
55
56 case 7:
57 default:
58 assert(0);
59 }
60 assert(i == 20);
61 }
62
63
64 /*****************************************/
65
66 void test3()
67 {
68 int i;
69
70 switch (5)
71 {
72 case 7:
73 i = 6;
74 goto default;
75 default:
76 i = 8;
77 break;
78
79 case 3,4,5,6:
80 i = 20;
81 goto default;
82 }
83 assert(i == 8);
84 }
85
86
87 /*****************************************/
88
89 void test4()
90 { int i;
91
92 switch (5)
93 {
94 case 3,4,5,6:
95 i = 20;
96 goto default;
97
98 case 7:
99 i = 6;
100 goto default;
101
102 default:
103 i = 8;
104 break;
105 }
106 assert(i == 8);
107 }
108
109
110 /*****************************************/
111
112 void test5()
113 { int i;
114
115 switch (5)
116 {
117 case 7:
118 i = 6;
119 goto case;
120 default:
121 i = 8;
122 break;
123
124 case 3,4,5,6:
125 i = 20;
126 break;
127 }
128 assert(i == 20);
129 }
130
131
132 /*****************************************/
133
134 void test6()
135 {
136 int i;
137
138 switch (5)
139 {
140 case 7:
141 i = 6;
142 goto case 4;
143 default:
144 i = 8;
145 break;
146
147 case 3,4,5,6:
148 i = 20;
149 break;
150 }
151 assert(i == 20);
152 }
153
154
155 /*****************************************/
156
157 void test7()
158 {
159 int i;
160
161 switch (5)
162 {
163 case 3,4,5,6:
164 i = 20;
165 break;
166
167 case 7:
168 i = 6;
169 goto case 4;
170 default:
171 i = 8;
172 break;
173 }
174 assert(i == 20);
175 }
176
177
178 /*****************************************/
179
180 void test8()
181 {
182 dstring str = "xyz";
183 switch (str)
184 {
185 case "xyz":
186 printf("correct\n");
187 return;
188
189 case "abc":
190 break;
191
192 default:
193 assert(0);
194 }
195 assert(0);
196 }
197
198 /*****************************************/
199
200 void test9()
201 {
202 int i = 1;
203
204 switch(i)
205 {
206 case 2:
207 return;
208 case 1:
209 switch(i)
210 {
211 case 1:
212 goto case 2;
213 default:
214 assert(0);
215 }
216 default:
217 assert(0);
218 }
219 assert(0);
220 }
221
222 /*****************************************/
223
224 void test10()
225 {
226 int id1 = 0;
227 int id;
228 switch (id1)
229 {
230 case 0: ++id; goto case;
231 case 7: ++id; goto case;
232 case 6: ++id; goto case;
233 case 5: ++id; goto case;
234 case 4: ++id; goto case;
235 case 3: ++id; goto case;
236 case 2: ++id; goto case;
237 case 1: ++id; goto default;
238 default:
239 break;
240 }
241 assert(id == 8);
242 }
243
244 /*****************************************/
245
246 void test11()
247 {
248 long foo = 4;
249 switch (foo)
250 {
251 case 2: assert (false);
252 case 3: break;
253 case 4: break;
254 case 5: break;
255 default: assert(0);
256 }
257 }
258
259 /*****************************************/
260
261 void test12()
262 {
263 switch("#!")
264 {
265 case "#!": printf("----Found #!\n"); break;
266 case "\xFF\xFE"c: break;
267 default:
268 assert(0);
269 }
270 }
271
272 /*****************************************/
273
274 void test13()
275 {
276 switch("#!")
277 {
278 case "#!": printf("----Found #!\n"); break;
279 case "#\xFE"c: break;
280 default:
281 assert(0);
282 }
283 }
284
285 /*****************************************/
286
287 void foo14(A...)(int i)
288 {
289 switch (i)
290 {
291 foreach(a; A)
292 {
293 goto case;
294 case a:
295 printf("%d\n", a);
296 }
297 break;
298 default:
299 assert(0);
300 }
301 }
302
303 void bar14(A...)(int i)
304 {
305 switch (i)
306 {
307 foreach(j, a; A)
308 {
309 goto case;
310 case A[j]:
311 printf("a = %d, A[%zd] = %d\n", a, j, A[j]);
312 }
313 break;
314 default:
315 assert(0);
316 }
317 }
318
319 void test14()
320 {
321 foo14!(1,2,3,4,5)(1);
322 bar14!(1,2,3,4,5)(1);
323 }
324
325 /*****************************************/
326
327 const int X15;
328 immutable int Y15;
329 const int Z15;
330
331 int foo15(int i)
332 {
333 auto y = 1;
334 switch (i)
335 {
336 case X15:
337 y += 1;
338 goto case;
339 case 3:
340 y += 2;
341 break;
342 case Y15:
343 y += 20;
344 goto case;
345 case Z15:
346 y += 10;
347 break;
348 default:
349 y += 4;
350 break;
351 }
352 printf("y = %d\n", y);
353 return y;
354 }
355
356 static this()
357 {
358 X15 = 4;
359 Z15 = 5;
360 }
361
362 shared static this()
363 {
364 Y15 = 4;
365 }
366
367 void test15()
368 {
369 auto i = foo15(3);
370 assert(i == 3);
371 i = foo15(4);
372 assert(i == 4);
373 i = foo15(7);
374 assert(i == 5);
375 i = foo15(5);
376 assert(i == 11);
377 }
378
379 /*****************************************/
380
381 enum E16
382 {
383 A,B,C
384 }
385
386 void test16()
387 {
388 E16 e = E16.A;
389 final switch (e)
390 {
391 case E16.A:
392 case E16.B:
393 case E16.C:
394 {}
395 }
396 }
397
398 /*****************************************/
399
400 void test17()
401 {
402 int i = 2;
403 switch (i)
404 {
405 case 1: .. case 3:
406 i = 5;
407 break;
408 default:
409 assert(0);
410 }
411 if (i != 5)
412 assert(0);
413
414 switch (i)
415 {
416 case 1: .. case 3:
417 i = 4;
418 break;
419 case 5:
420 i = 6;
421 break;
422 default:
423 assert(0);
424 }
425 if (i != 6)
426 assert(0);
427 }
428
429 /*****************************************/
430
431 int test19()
432 {
433 enum foo{ bar }
434 foo x;
435 final switch(x){ case foo.bar: return 0; }
436 }
437
438 /*****************************************/
439
440 void test20()
441 {
442 switch(1)
443 {
444 mixin("case 0:{}");
445 case 1:
446 case 2:
447 default:
448 }
449 }
450
451 /*****************************************/
452
453 void hang3139(int x)
454 {
455 switch(x) {
456 case -9: .. case -1:
457 default:
458 }
459 }
460
461 int wrongcode3139(int x)
462 {
463 switch(x) {
464 case -9: .. case 2: return 3;
465 default:
466 return 4;
467 }
468 }
469
470 static assert(wrongcode3139(-5)==3);
471
472 // https://issues.dlang.org/show_bug.cgi?id=3139
473 static assert(!is(typeof(
474 (long x) { switch(x) { case long.max: .. case -long.max:
475 default:} return 4; }(3)
476 )));
477
478
479 /*****************************************/
480
481 void test7358()
482 {
483 static void test7358a()
484 {
485 enum X { A = 1, B = 2 }
486
487 auto x = X.A | X.B;
488
489 final switch(x)
490 {
491 case X.A:
492 case X.B:
493 break;
494 }
495 }
496
497 bool exception;
498 try
499 test7358a();
500 catch (Error)
501 exception = true;
502 assert(exception);
503 }
504
505 /*****************************************/
506 // https://issues.dlang.org/show_bug.cgi?id=9263
507
508 void test9263()
509 {
510 enum Foo { A }
511
512 Foo f;
513 final switch (f) with(Foo)
514 {
515 case A:
516 return;
517 }
518 }
519
520 /*****************************************/
521
522 int bar21(int i)
523 {
524 switch (i)
525 {
526 // case 1: return 11;
527 case 2: return 12;
528 case 3: return 13;
529 case 4: return 14;
530 case 5: return 15;
531 case 6: return 16;
532 case 7: return 17;
533 case 8: return 18;
534 case 9: return 19;
535 case 10: return 20;
536 default: break;
537 }
538
539 switch (i)
540 {
541 case 11: return 21;
542 case 12: return 22;
543 case 13: return 23;
544 case 14: return 24;
545 case 15: return 25;
546 case 16: return 26;
547 case 17: return 27;
548 case 18: return 28;
549 case 19: return 29;
550 case 20: return 30;
551 default: return 31;
552 }
553 }
554
555 void test21()
556 {
557 // int j = bar(12);
558 // printf("j = %d\n", j);
559
560 for (int i = 2; i < 21; i++)
561 {
562 int j = bar21(i);
563 //printf("j = %d\n", j);
564 assert(j == i + 10);
565 }
566 }
567
568 /*****************************************/
569
570 int bar22(int i)
571 {
572 switch (i)
573 {
574 case 1: return i + 1;
575 case 10: return i + 2;
576 case 20: return i + 3;
577 case 50: return i + 4;
578 case 1000: return i + 5;
579 default: return 28;
580 }
581 }
582
583 void test22()
584 {
585 assert(bar22(1) == 2);
586 assert(bar22(10) == 12);
587 assert(bar22(20) == 23);
588 assert(bar22(50) == 54);
589 assert(bar22(1000) == 1005);
590 assert(bar22(0) == 28);
591 assert(bar22(5) == 28);
592 assert(bar22(15) == 28);
593 assert(bar22(25) == 28);
594 assert(bar22(58) == 28);
595 assert(bar22(2000) == 28);
596 }
597
598 /*****************************************/
599
600 long bar23(long i)
601 {
602 switch (i)
603 {
604 case 1: return i + 1;
605 case 0x10_0000_0000L: return i + 2;
606 case 0x20_0070_0000L: return i + 3;
607 case 0x50_0000_0000L: return i + 4;
608 case 0x1000_0000_8000L: return i + 5;
609 default: return 28;
610 }
611 }
612
613 void test23()
614 {
615 assert(bar23(1) == 2);
616 assert(bar23(0x10_0000_0000L) == 0x10_0000_0000L + 2);
617 assert(bar23(0x20_0070_0000L) == 0x20_0070_0000L + 3);
618 assert(bar23(0x50_0000_0000L) == 0x50_0000_0000L + 4);
619 assert(bar23(0x1000_0000_8000L) == 0x1000_0000_8000L + 5);
620 assert(bar23(0) == 28);
621 assert(bar23(58) == 28);
622 assert(bar23(0x10_0000_0000L+1) == 28);
623 assert(bar23(0x20_0070_0000L+5) == 28);
624 assert(bar23(0x50_0000_0000L+25) == 28);
625 assert(bar23(0x1000_0000_8000L+1) == 28);
626 }
627
628 /*****************************************/
629 // https://issues.dlang.org/show_bug.cgi?id=14352
630
631 int transmogrify14352(int input)
632 {
633 int output = 0;
634 switch (input)
635 {
636 case 0, 1:
637 if (input == 0)
638 goto case;
639 else
640 output++;
641 goto case;
642 case 2:
643 output += 5;
644 goto case;
645 case 3:
646 output += 5;
647 break;
648 case 4, 5, 6:
649 goto default;
650 case 7:
651 case 8:
652 output += 20;
653 break;
654 default:
655 return -1;
656 }
657 return output;
658 }
659
660 void test14352()
661 {
662 assert(transmogrify14352(0) == 10);
663 }
664
665 /*****************************************/
666 // https://issues.dlang.org/show_bug.cgi?id=14587
667 // DMD 2.067.1 generating crashing binary on OSX
668
669 struct Card {
670 int value;
671 int suit;
672 }
673
674 void foo14587(Card card) {
675 switch(card.value) {
676 case 4: case 5: case 6: case 11:
677 break;
678 default:
679 }
680 }
681
682 void test14587() {
683 auto card = Card(11, 1);
684 foo14587(card);
685 }
686
687 /*****************************************/
688 // https://issues.dlang.org/show_bug.cgi?id=15396
689 // static immutable not recognized as constant within switch statement
690
691 void test15396()
692 {
693 static immutable Foo = "Foobar";
694 static const Bar = "BarFoo";
695
696 foreach (var; [ "Foobar", "BarFoo" ])
697 {
698 switch (var)
699 {
700 case Foo:
701 break;
702 case Bar:
703 break;
704 default:
705 assert(0, "test15396 failed");
706 }
707 }
708 }
709
710 /*****************************************/
711 // https://issues.dlang.org/show_bug.cgi?id=15538
712
713 struct S15538
714 {
715 int a = 0;
716 int b = 1;
717 }
718
719 int f15538(S15538 s)
720 {
721 switch (s.a)
722 {
723 case 0: return 10;
724 case 1: return 20;
725 case 2: return 30;
726 case 3: return 40;
727 default: return 99;
728 }
729 }
730
731 void test15538()
732 {
733 S15538 s;
734 assert(f15538(s) == 10); /* fails */
735 }
736
737 /*****************************************/
738
739 int main()
740 {
741 test1();
742 test2();
743 test3();
744 test4();
745 test5();
746 test6();
747 test7();
748 test8();
749 test9();
750 test10();
751 test11();
752 test12();
753 test13();
754 test14();
755 test15();
756 test16();
757 test17();
758 test19();
759 test20();
760 test7358();
761 test9263();
762 test21();
763 test22();
764 test23();
765 test14352();
766 test14587();
767 test15396();
768 test15538();
769
770 printf("Success\n");
771 return 0;
772 }