]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/foreach4.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / foreach4.d
1
2 import core.stdc.stdio;
3 import std.stdio;
4
5 alias bool bit;
6
7 /************************************************/
8
9 class Foo
10 {
11 uint array[2];
12
13 int opApply(int delegate(ref uint) dg)
14 {
15 int result;
16 for (int i = 0; i < array.length; i++)
17 {
18 result = dg(array[i]);
19 if (result)
20 break;
21 }
22 return result;
23 }
24 }
25
26
27 /**************************************************/
28
29 void test1()
30 {
31 Foo a = new Foo();
32 int i;
33
34 a.array[0] = 73;
35 a.array[1] = 82;
36
37 foreach (uint u; a)
38 {
39 i++;
40 u++;
41 }
42 assert(i == 2);
43 assert(a.array[0] == 73);
44 assert(a.array[1] == 82);
45 }
46
47 /**************************************************/
48
49 void test2()
50 {
51 Foo a = new Foo();
52 int i;
53
54 a.array[0] = 73;
55 a.array[1] = 82;
56
57 foreach (ref uint u; a)
58 {
59 i++;
60 u++;
61 }
62 assert(i == 2);
63 assert(a.array[0] == 74);
64 assert(a.array[1] == 83);
65 }
66
67 /**************************************************/
68
69 void test3()
70 {
71 Foo a = new Foo();
72 int i;
73
74 a.array[0] = 73;
75 a.array[1] = 82;
76
77 foreach (ref uint u; a)
78 {
79 i++;
80 if (i)
81 break;
82 u++;
83 }
84 assert(i == 1);
85 assert(a.array[0] == 73);
86 assert(a.array[1] == 82);
87 }
88
89 /**************************************************/
90
91 void test4()
92 {
93 Foo a = new Foo();
94 int i;
95
96 a.array[0] = 73;
97 a.array[1] = 82;
98
99 foreach (ref uint u; a)
100 {
101 i++;
102 if (i == 1)
103 continue;
104 u++;
105 }
106 assert(i == 2);
107 assert(a.array[0] == 73 && a.array[1] == 83);
108 }
109
110 /**************************************************/
111
112 void test5()
113 {
114 Foo a = new Foo();
115 int i;
116
117 a.array[0] = 73;
118 a.array[1] = 82;
119
120 Loop:
121 while (1)
122 {
123 foreach (ref uint u; a)
124 {
125 i++;
126 if (i)
127 break Loop;
128 u++;
129 }
130 }
131 assert(i == 1);
132 assert(a.array[0] == 73);
133 assert(a.array[1] == 82);
134 }
135
136 /**************************************************/
137
138 void test6()
139 {
140 Foo a = new Foo();
141 int i;
142
143 a.array[0] = 73;
144 a.array[1] = 82;
145
146 Loop:
147 while (1)
148 {
149 foreach (ref uint u; a)
150 {
151 i++;
152 if (i == 1)
153 continue Loop;
154 u++;
155 }
156 break;
157 }
158 assert(i == 3);
159 assert(a.array[0] == 74);
160 assert(a.array[1] == 83);
161 }
162
163 /**************************************************/
164
165 void test7()
166 {
167 Foo a = new Foo();
168 int i;
169
170 a.array[0] = 73;
171 a.array[1] = 82;
172
173 foreach (ref uint u; a)
174 {
175 i++;
176 if (i)
177 goto Label;
178 u++;
179 }
180 assert(0);
181 Label:
182 assert(i == 1);
183 assert(a.array[0] == 73);
184 assert(a.array[1] == 82);
185 }
186
187 /**************************************************/
188
189 void test8_x(Foo a)
190 {
191 int i;
192 foreach (ref uint u; a)
193 {
194 i++;
195 if (i)
196 return;
197 u++;
198 }
199 }
200
201 void test8()
202 {
203 Foo a = new Foo();
204 int i;
205
206 a.array[0] = 73;
207 a.array[1] = 82;
208
209 test8_x(a);
210 assert(i == 0);
211 assert(a.array[0] == 73);
212 assert(a.array[1] == 82);
213 }
214
215 /**************************************************/
216
217 int test9_x(Foo a)
218 {
219 int i;
220 foreach (ref uint u; a)
221 {
222 i++;
223 if (i)
224 return 67;
225 u++;
226 }
227 return 23;
228 }
229
230 void test9()
231 {
232 Foo a = new Foo();
233 int i;
234
235 a.array[0] = 73;
236 a.array[1] = 82;
237
238 i = test9_x(a);
239 assert(i == 67);
240 assert(a.array[0] == 73);
241 assert(a.array[1] == 82);
242 }
243
244 /**************************************************/
245
246 int test10_x(Foo a)
247 {
248 int i;
249 foreach (ref uint u; a)
250 {
251 i++;
252 if (i)
253 return i;
254 u++;
255 }
256 return 23;
257 }
258
259 void test10()
260 {
261 Foo a = new Foo();
262 int i;
263
264 a.array[0] = 73;
265 a.array[1] = 82;
266
267 i = test10_x(a);
268 assert(i == 1);
269 assert(a.array[0] == 73);
270 assert(a.array[1] == 82);
271 }
272
273 /**************************************************/
274 /+
275 the expected output:
276
277 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0 1 1 0 0 0
278 1
279 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 0
280 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 0
281 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 0 1 0 1 1 1 1 0
282 +/
283
284 void test11()
285 {
286 bit[25] data;
287 int j;
288
289 for (int i = 0; i < 25; i += 5) {
290 data[i+0] = data[i+1] = true;
291 }
292
293 for (int i = 0; i < 25; i++) {
294 printf("%d ", data[i]);
295 if ((i % 5) < 2)
296 assert(data[i] == true);
297 else
298 assert(data[i] == false);
299 }
300
301 printf("\n%d\n", data[22] = true);
302 j = data[22] = true;
303 assert(j == true);
304
305 for (int i = 0; i < 25; i += 5) {
306 data[i+1] = data[i+3] = true;
307 j = i % 5;
308 if (j == 0 || j == 1 || j == 3)
309 assert(data[i] == true);
310 else
311 assert(data[i] == false);
312 }
313
314 for (int i = 0; i < 25; i++) {
315 printf("%d ", data[i]);
316 }
317
318 printf("\n");
319
320 int k;
321 foreach (bit b; data) {
322 printf("%d ", b);
323 j = k % 5;
324 if (j == 0 || j == 1 || j == 3 || k == 22)
325 assert(data[k] == true);
326 else
327 assert(data[k] == false);
328 k++;
329 }
330 printf("\n");
331
332 foreach (int l, bit b; data) {
333 printf("%d ", b);
334 j = l % 5;
335 if (j == 0 || j == 1 || j == 3 || l == 22)
336 assert(data[l] == true);
337 else
338 assert(data[l] == false);
339 }
340 printf("\n");
341 }
342
343
344 /**************************************************/
345
346 void test12()
347 {
348 int j;
349
350 j = 0;
351 foreach (dchar d; "hello")
352 {
353 printf("d = x%x\n", d);
354 if (j == 0) assert(d == 'h');
355 if (j == 1) assert(d == 'e');
356 if (j == 2) assert(d == 'l');
357 if (j == 3) assert(d == 'l');
358 if (j == 4) assert(d == 'o');
359 j++;
360 }
361 assert(j == 5);
362
363 j = 0;
364 foreach (size_t i, dchar d; "hello")
365 {
366 printf("i = %d, d = x%x\n", i, d);
367 if (j == 0) assert(d == 'h');
368 if (j == 1) assert(d == 'e');
369 if (j == 2) assert(d == 'l');
370 if (j == 3) assert(d == 'l');
371 if (j == 4) assert(d == 'o');
372 assert(i == j);
373 j++;
374 }
375 assert(j == 5);
376 }
377
378 /**************************************************/
379
380 void test13()
381 {
382 int j;
383
384 j = 0;
385 foreach (wchar d; "hello")
386 {
387 printf("d = x%x\n", d);
388 if (j == 0) assert(d == 'h');
389 if (j == 1) assert(d == 'e');
390 if (j == 2) assert(d == 'l');
391 if (j == 3) assert(d == 'l');
392 if (j == 4) assert(d == 'o');
393 j++;
394 }
395 assert(j == 5);
396
397 j = 0;
398 foreach (size_t i, wchar d; "hello")
399 {
400 printf("i = %d, d = x%x\n", i, d);
401 if (j == 0) assert(d == 'h');
402 if (j == 1) assert(d == 'e');
403 if (j == 2) assert(d == 'l');
404 if (j == 3) assert(d == 'l');
405 if (j == 4) assert(d == 'o');
406 assert(i == j);
407 j++;
408 }
409 assert(j == 5);
410 }
411
412 /**************************************************/
413
414 void test14()
415 {
416 int j;
417
418 j = 0;
419 foreach (char d; cast(wstring)"hello")
420 {
421 printf("d = x%x\n", d);
422 if (j == 0) assert(d == 'h');
423 if (j == 1) assert(d == 'e');
424 if (j == 2) assert(d == 'l');
425 if (j == 3) assert(d == 'l');
426 if (j == 4) assert(d == 'o');
427 j++;
428 }
429 assert(j == 5);
430
431 j = 0;
432 foreach (size_t i, char d; cast(wstring)"hello")
433 {
434 printf("i = %d, d = x%x\n", i, d);
435 if (j == 0) assert(d == 'h');
436 if (j == 1) assert(d == 'e');
437 if (j == 2) assert(d == 'l');
438 if (j == 3) assert(d == 'l');
439 if (j == 4) assert(d == 'o');
440 assert(i == j);
441 j++;
442 }
443 assert(j == 5);
444 }
445
446 /**************************************************/
447
448 void test15()
449 {
450 int j;
451
452 j = 0;
453 foreach (dchar d; cast(wstring)"hello")
454 {
455 printf("d = x%x\n", d);
456 if (j == 0) assert(d == 'h');
457 if (j == 1) assert(d == 'e');
458 if (j == 2) assert(d == 'l');
459 if (j == 3) assert(d == 'l');
460 if (j == 4) assert(d == 'o');
461 j++;
462 }
463 assert(j == 5);
464
465 j = 0;
466 foreach (size_t i, dchar d; cast(wstring)"hello")
467 {
468 printf("i = %d, d = x%x\n", i, d);
469 if (j == 0) assert(d == 'h');
470 if (j == 1) assert(d == 'e');
471 if (j == 2) assert(d == 'l');
472 if (j == 3) assert(d == 'l');
473 if (j == 4) assert(d == 'o');
474 assert(i == j);
475 j++;
476 }
477 assert(j == 5);
478 }
479
480 /**************************************************/
481
482 void test16()
483 {
484 int j;
485
486 j = 0;
487 foreach (char d; cast(dstring)"hello")
488 {
489 printf("d = x%x\n", d);
490 if (j == 0) assert(d == 'h');
491 if (j == 1) assert(d == 'e');
492 if (j == 2) assert(d == 'l');
493 if (j == 3) assert(d == 'l');
494 if (j == 4) assert(d == 'o');
495 j++;
496 }
497 assert(j == 5);
498
499 j = 0;
500 foreach (size_t i, char d; cast(dstring)"hello")
501 {
502 printf("i = %d, d = x%x\n", i, d);
503 if (j == 0) assert(d == 'h');
504 if (j == 1) assert(d == 'e');
505 if (j == 2) assert(d == 'l');
506 if (j == 3) assert(d == 'l');
507 if (j == 4) assert(d == 'o');
508 assert(i == j);
509 j++;
510 }
511 assert(j == 5);
512 }
513
514 /**************************************************/
515
516 void test17()
517 {
518 int j;
519
520 j = 0;
521 foreach (wchar d; cast(dstring)"hello")
522 {
523 printf("d = x%x\n", d);
524 if (j == 0) assert(d == 'h');
525 if (j == 1) assert(d == 'e');
526 if (j == 2) assert(d == 'l');
527 if (j == 3) assert(d == 'l');
528 if (j == 4) assert(d == 'o');
529 j++;
530 }
531 assert(j == 5);
532
533 j = 0;
534 foreach (size_t i, wchar d; cast(dstring)"hello")
535 {
536 printf("i = %d, d = x%x\n", i, d);
537 if (j == 0) assert(d == 'h');
538 if (j == 1) assert(d == 'e');
539 if (j == 2) assert(d == 'l');
540 if (j == 3) assert(d == 'l');
541 if (j == 4) assert(d == 'o');
542 assert(i == j);
543 j++;
544 }
545 assert(j == 5);
546 }
547
548 /**************************************************/
549
550 void test18()
551 {
552 string a = "\xE2\x89\xA0"; // \u2260 encoded as 3 UTF-8 bytes
553
554 foreach (dchar c; a)
555 {
556 printf("a[] = %x\n", c); // prints 'a[] = 2260'
557 assert(c == 0x2260);
558 }
559
560 dstring b = "\u2260";
561
562 int i;
563 foreach (char c; b)
564 {
565 printf("%x, ", c); // prints e2, 89, a0
566 if (i == 0) assert(c == 0xE2);
567 else if (i == 1) assert(c == 0x89);
568 else if (i == 2) assert(c == 0xA0);
569 else
570 assert(0);
571 i++;
572 }
573 printf("\n");
574 }
575
576 /**************************************************/
577
578 void test19()
579 {
580 string string = x"F0 9D 83 93";
581
582 int count=0;
583 dchar tmp;
584 foreach(dchar value ; string){
585 tmp=value;
586 count++;
587 }
588 assert(count==1);
589 assert(tmp==0x01D0D3);
590 }
591
592 /**************************************************/
593
594 struct S20
595 {
596 int opApply(int delegate(ref int i) dg)
597 {
598 return 0;
599 }
600 }
601
602 S20 foo20;
603
604 void test20()
605 {
606 label:
607 foreach(int i; foo20)
608 {
609 continue label;
610 }
611 }
612
613
614 /**************************************************/
615
616 void foo21(string[] args)
617 {
618 printf("args.length = %d\n", args.length);
619 assert(args.length == 3);
620 foreach (i, arg; args)
621 {
622 assert(typeid(typeof(i)) == typeid(size_t));
623 assert(typeid(typeof(arg)) == typeid(string));
624 writefln("args[%d] = '%s'", i, arg);
625 }
626 foreach (arg; args)
627 {
628 assert(typeid(typeof(arg)) == typeid(string));
629 writefln("args[] = '%s'", arg);
630 }
631 }
632
633 void test21()
634 {
635 string[] args;
636
637 args.length = 3;
638 args[0] = "a";
639 args[1] = "bc";
640 args[2] = "d";
641 foo21(args);
642 }
643
644 /**************************************************/
645
646 void test22()
647 {
648 int[string] map;
649
650 map["hello"] = 3;
651 map["world"] = 4;
652
653 foreach (key, value; map)
654 {
655 assert(typeid(typeof(key)) == typeid(string));
656 assert(typeid(typeof(value)) == typeid(int));
657 writefln("map[%s] = %s", key, value);
658 }
659 foreach (key, int value; map)
660 {
661 assert(typeid(typeof(key)) == typeid(string));
662 assert(typeid(typeof(value)) == typeid(int));
663 writefln("map[%s] = %s", key, value);
664 }
665 foreach (string key, value; map)
666 {
667 assert(typeid(typeof(key)) == typeid(string));
668 assert(typeid(typeof(value)) == typeid(int));
669 writefln("map[%s] = %s", key, value);
670 }
671 foreach (value; map)
672 {
673 assert(typeid(typeof(value)) == typeid(int));
674 writefln("map[] = %s", value);
675 }
676 }
677
678 /**************************************************/
679
680 class Foo23
681 {
682 int array[2];
683
684 int opApply(int delegate(ref int) dg)
685 {
686 int result;
687 for (int i = 0; i < array.length; i++)
688 {
689 result = dg(array[i]);
690 if (result)
691 break;
692 }
693 return result;
694 }
695
696 int opApply(int delegate(ref size_t, ref int) dg)
697 {
698 int result;
699 for (size_t i = 0; i < array.length; i++)
700 {
701 result = dg(i, array[i]);
702 if (result)
703 break;
704 }
705 return result;
706 }
707 }
708
709 void test23()
710 {
711 Foo23 a = new Foo23();
712 int i;
713
714 a.array[0] = 73;
715 a.array[1] = 82;
716
717 foreach (u; a)
718 {
719 assert(typeid(typeof(u)) == typeid(int));
720 i++;
721 u++;
722 //writefln("u = %d", u);
723 assert((i == 1) ? u == 74 : u == 83);
724 }
725 assert(i == 2);
726 assert(a.array[0] == 73);
727 assert(a.array[1] == 82);
728
729 foreach (j, u; a)
730 {
731 assert(typeid(typeof(j)) == typeid(size_t));
732 assert(typeid(typeof(u)) == typeid(int));
733 i++;
734 u++;
735 writefln("u = %d", u);
736 assert((i == 3) ? u == 74 : u == 83);
737 assert(j == i - 3);
738 }
739 assert(i == 4);
740 assert(a.array[0] == 73);
741 assert(a.array[1] == 82);
742 }
743
744 /**************************************************/
745
746 struct Collection24
747 {
748 int opApply(int delegate(ref int) dg){
749 return 0;
750 }
751 }
752
753 bool foo24()
754 {
755 Collection24 a,b;
756
757 foreach(int x; a){
758 foreach(int y; b){
759 return false;
760 }
761 }
762
763 return true;
764 }
765
766 void test24()
767 {
768 assert(foo24() == true);
769 }
770
771 /**************************************************/
772
773 void test25()
774 {
775 alias void function(string[string]) FN;
776 FN fn = function (string[string] aarray)
777 {
778 foreach (string s; aarray)
779 {
780 writeln(s);
781 assert(s == "b");
782 }
783 };
784 string[string] aarray;
785 aarray["a"] = "b";
786 fn(aarray);
787 }
788
789 /**************************************************/
790
791 struct Foo26
792 {
793 uint array[2];
794
795 int forward(int delegate(ref uint) dg)
796 {
797 int result;
798 for (int i = 0; i < array.length; i++)
799 {
800 result = dg(array[i]);
801 if (result)
802 break;
803 }
804 return result;
805 }
806
807 int forward(int delegate(ref uint) dg, int x) { return 1; }
808
809 int reverse(int delegate(ref uint) dg, int x) { return 1; }
810
811 int reverse(int delegate(ref uint) dg)
812 {
813 int result;
814 foreach_reverse (v; array)
815 {
816 auto u = v;
817 result = dg(u);
818 if (result)
819 break;
820 }
821 return result;
822 }
823 }
824
825
826 void test26()
827 {
828 Foo26 a;
829 int i;
830
831 a.array[0] = 73;
832 a.array[1] = 82;
833
834 foreach (u; &a.forward)
835 {
836 writeln(u);
837 i++;
838 u++;
839 }
840 assert(i == 2);
841 assert(a.array[0] == 73);
842 assert(a.array[1] == 82);
843
844 foreach (uint u; &a.reverse)
845 {
846 writeln(u);
847 }
848 }
849
850 /**************************************************/
851
852 struct S27
853 {
854 int[] a;
855
856 bool empty() { return a.length == 0; }
857
858 void popFront() { a = a[1 .. $]; }
859 int front() { return a[0]; }
860
861 void popBack() { a = a[0 .. $ - 1]; }
862 ref int back() { return a[$ - 1]; }
863 }
864
865 void test27()
866 {
867 S27 s;
868 s.a = [5,6,7];
869 string r;
870
871 foreach (e; s)
872 {
873 printf("%d\n", e);
874 r ~= cast(char)(e + '0');
875 }
876 assert(r == "567");
877
878 r = null;
879 foreach_reverse (ref e; s)
880 {
881 e++;
882 printf("%d\n", e);
883 r ~= cast(char)(e + '0');
884 }
885 assert(r == "876");
886
887 r = null;
888 foreach (e; s)
889 {
890 printf("%d\n", e);
891 r ~= cast(char)(e + '0');
892 }
893 assert(r == "678");
894 }
895
896 /**************************************************/
897
898 int main()
899 {
900 test1();
901 test2();
902 test3();
903 test4();
904 test5();
905 test6();
906 test7();
907 test8();
908 test9();
909 test10();
910 test11();
911 test12();
912 test13();
913 test14();
914 test15();
915 test16();
916 test17();
917 test18();
918 test19();
919 test20();
920 test21();
921 test22();
922 test23();
923 test24();
924 test25();
925 test26();
926 test27();
927
928 printf("Success\n");
929 return 0;
930 }