Debian Codegraph @

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

raw
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 shared static this()
357 {
358 X15 = 4;
359 Y15 = 4;
360 Z15 = 5;
361 }
362
363 void test15()
364 {
365 auto i = foo15(3);
366 assert(i == 3);
367 i = foo15(4);
368 assert(i == 4);
369 i = foo15(7);
370 assert(i == 5);
371 i = foo15(5);
372 assert(i == 11);
373 }
374
375 /*****************************************/
376
377 enum E16
378 {
379 A,B,C
380 }
381
382 void test16()
383 {
384 E16 e = E16.A;
385 final switch (e)
386 {
387 case E16.A:
388 case E16.B:
389 case E16.C:
390 {}
391 }
392 }
393
394 /*****************************************/
395
396 void test17()
397 {
398 int i = 2;
399 switch (i)
400 {
401 case 1: .. case 3:
402 i = 5;
403 break;
404 default:
405 assert(0);
406 }
407 if (i != 5)
408 assert(0);
409
410 switch (i)
411 {
412 case 1: .. case 3:
413 i = 4;
414 break;
415 case 5:
416 i = 6;
417 break;
418 default:
419 assert(0);
420 }
421 if (i != 6)
422 assert(0);
423 }
424
425 /*****************************************/
426
427 int test19()
428 {
429 enum foo{ bar }
430 foo x;
431 final switch(x){ case foo.bar: return 0; }
432 }
433
434 /*****************************************/
435
436 void test20()
437 {
438 switch(1)
439 {
440 mixin("case 0:{}");
441 case 1:
442 case 2:
443 default:
444 }
445 }
446
447 /*****************************************/
448
449 void hang3139(int x)
450 {
451 switch(x) {
452 case -9: .. case -1:
453 default:
454 }
455 }
456
457 int wrongcode3139(int x)
458 {
459 switch(x) {
460 case -9: .. case 2: return 3;
461 default:
462 return 4;
463 }
464 }
465
466 static assert(wrongcode3139(-5)==3);
467
468 // https://issues.dlang.org/show_bug.cgi?id=3139
469 static assert(!is(typeof(
470 (long x) { switch(x) { case long.max: .. case -long.max:
471 default:} return 4; }(3)
472 )));
473
474
475 /*****************************************/
476
477 void test7358()
478 {
479 static void test7358a()
480 {
481 enum X { A = 1, B = 2 }
482
483 auto x = X.A | X.B;
484
485 final switch(x)
486 {
487 case X.A:
488 case X.B:
489 break;
490 }
491 }
492
493 bool exception;
494 try
495 test7358a();
496 catch (Error)
497 exception = true;
498 assert(exception);
499 }
500
501 /*****************************************/
502 // https://issues.dlang.org/show_bug.cgi?id=9263
503
504 void test9263()
505 {
506 enum Foo { A }
507
508 Foo f;
509 final switch (f) with(Foo)
510 {
511 case A:
512 return;
513 }
514 }
515
516 /*****************************************/
517
518 int bar21(int i)
519 {
520 switch (i)
521 {
522 // case 1: return 11;
523 case 2: return 12;
524 case 3: return 13;
525 case 4: return 14;
526 case 5: return 15;
527 case 6: return 16;
528 case 7: return 17;
529 case 8: return 18;
530 case 9: return 19;
531 case 10: return 20;
532 default: break;
533 }
534
535 switch (i)
536 {
537 case 11: return 21;
538 case 12: return 22;
539 case 13: return 23;
540 case 14: return 24;
541 case 15: return 25;
542 case 16: return 26;
543 case 17: return 27;
544 case 18: return 28;
545 case 19: return 29;
546 case 20: return 30;
547 default: return 31;
548 }
549 }
550
551 void test21()
552 {
553 // int j = bar(12);
554 // printf("j = %d\n", j);
555
556 for (int i = 2; i < 21; i++)
557 {
558 int j = bar21(i);
559 //printf("j = %d\n", j);
560 assert(j == i + 10);
561 }
562 }
563
564 /*****************************************/
565
566 int bar22(int i)
567 {
568 switch (i)
569 {
570 case 1: return i + 1;
571 case 10: return i + 2;
572 case 20: return i + 3;
573 case 50: return i + 4;
574 case 1000: return i + 5;
575 default: return 28;
576 }
577 }
578
579 void test22()
580 {
581 assert(bar22(1) == 2);
582 assert(bar22(10) == 12);
583 assert(bar22(20) == 23);
584 assert(bar22(50) == 54);
585 assert(bar22(1000) == 1005);
586 assert(bar22(0) == 28);
587 assert(bar22(5) == 28);
588 assert(bar22(15) == 28);
589 assert(bar22(25) == 28);
590 assert(bar22(58) == 28);
591 assert(bar22(2000) == 28);
592 }
593
594 /*****************************************/
595
596 long bar23(long i)
597 {
598 switch (i)
599 {
600 case 1: return i + 1;
601 case 0x10_0000_0000L: return i + 2;
602 case 0x20_0070_0000L: return i + 3;
603 case 0x50_0000_0000L: return i + 4;
604 case 0x1000_0000_8000L: return i + 5;
605 default: return 28;
606 }
607 }
608
609 void test23()
610 {
611 assert(bar23(1) == 2);
612 assert(bar23(0x10_0000_0000L) == 0x10_0000_0000L + 2);
613 assert(bar23(0x20_0070_0000L) == 0x20_0070_0000L + 3);
614 assert(bar23(0x50_0000_0000L) == 0x50_0000_0000L + 4);
615 assert(bar23(0x1000_0000_8000L) == 0x1000_0000_8000L + 5);
616 assert(bar23(0) == 28);
617 assert(bar23(58) == 28);
618 assert(bar23(0x10_0000_0000L+1) == 28);
619 assert(bar23(0x20_0070_0000L+5) == 28);
620 assert(bar23(0x50_0000_0000L+25) == 28);
621 assert(bar23(0x1000_0000_8000L+1) == 28);
622 }
623
624 /*****************************************/
625 // https://issues.dlang.org/show_bug.cgi?id=14352
626
627 int transmogrify14352(int input)
628 {
629 int output = 0;
630 switch (input)
631 {
632 case 0, 1:
633 if (input == 0)
634 goto case;
635 else
636 output++;
637 goto case;
638 case 2:
639 output += 5;
640 goto case;
641 case 3:
642 output += 5;
643 break;
644 case 4, 5, 6:
645 goto default;
646 case 7:
647 case 8:
648 output += 20;
649 break;
650 default:
651 return -1;
652 }
653 return output;
654 }
655
656 void test14352()
657 {
658 assert(transmogrify14352(0) == 10);
659 }
660
661 /*****************************************/
662 // https://issues.dlang.org/show_bug.cgi?id=14587
663 // DMD 2.067.1 generating crashing binary on OSX
664
665 struct Card {
666 int value;
667 int suit;
668 }
669
670 void foo14587(Card card) {
671 switch(card.value) {
672 case 4: case 5: case 6: case 11:
673 break;
674 default:
675 }
676 }
677
678 void test14587() {
679 auto card = Card(11, 1);
680 foo14587(card);
681 }
682
683 /*****************************************/
684 // https://issues.dlang.org/show_bug.cgi?id=15396
685 // static immutable not recognized as constant within switch statement
686
687 void test15396()
688 {
689 static immutable Foo = "Foobar";
690 static const Bar = "BarFoo";
691
692 foreach (var; [ "Foobar", "BarFoo" ])
693 {
694 switch (var)
695 {
696 case Foo:
697 break;
698 case Bar:
699 break;
700 default:
701 assert(0, "test15396 failed");
702 }
703 }
704 }
705
706 /*****************************************/
707 // https://issues.dlang.org/show_bug.cgi?id=15538
708
709 struct S15538
710 {
711 int a = 0;
712 int b = 1;
713 }
714
715 int f15538(S15538 s)
716 {
717 switch (s.a)
718 {
719 case 0: return 10;
720 case 1: return 20;
721 case 2: return 30;
722 case 3: return 40;
723 default: return 99;
724 }
725 }
726
727 void test15538()
728 {
729 S15538 s;
730 assert(f15538(s) == 10); /* fails */
731 }
732
733 /*****************************************/
734
735 int main()
736 {
737 test1();
738 test2();
739 test3();
740 test4();
741 test5();
742 test6();
743 test7();
744 test8();
745 test9();
746 test10();
747 test11();
748 test12();
749 test13();
750 test14();
751 test15();
752 test16();
753 test17();
754 test19();
755 test20();
756 test7358();
757 test9263();
758 test21();
759 test22();
760 test23();
761 test14352();
762 test14587();
763 test15396();
764 test15538();
765
766 printf("Success\n");
767 return 0;
768 }