Debian Codegraph @

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

raw
1 /*
2 REQUIRED_ARGS: -verrors=simple
3 TEST_OUTPUT:
4 ---
5 runnable/test8.d(262): Deprecation: identity comparison of static arrays implicitly coerces them to slices, which are compared by reference
6 ---
7 */
8
9 module testxxx8;
10
11 import core.vararg;
12
13 extern(C)
14 {
15 int atoi(const char*);
16 int printf(const char*, ...);
17 size_t strlen(const char*);
18 version(Windows)
19 {
20 int _snprintf(char*, size_t, const char*, ...);
21 alias _snprintf snprintf;
22 }
23 else
24 int snprintf(char*, size_t, const char*, ...);
25 }
26
27 /***********************************/
28
29 struct Foo1
30 {
31 static int x = 3;
32 int y = 4;
33 }
34
35 void test1()
36 {
37 Foo1 f;
38
39 assert(Foo1.x == 3);
40 assert(f.x == 3);
41 assert(f.y == 4);
42 }
43
44 /***********************************/
45
46 class Foo2
47 {
48 static int x = 5;
49 int y = 6;
50 }
51
52 void test2()
53 {
54 Foo2 f = new Foo2();
55
56 assert(Foo2.x == 5);
57 assert(f.x == 5);
58 assert(f.y == 6);
59 }
60
61
62 /***********************************/
63
64 struct Foo3
65 {
66 static int bar() { return 3; }
67 int y = 4;
68 }
69
70 void test3()
71 {
72 Foo3 f;
73
74 assert(Foo3.bar() == 3);
75 assert(f.bar() == 3);
76 }
77
78 /***********************************/
79
80 class Foo4
81 {
82 static int bar() { return 3; }
83 int y = 4;
84 }
85
86 void test4()
87 {
88 Foo4 f = new Foo4();
89
90 assert(Foo4.bar() == 3);
91 assert(f.bar() == 3);
92 }
93
94
95 /***********************************/
96
97 struct Foo5
98 {
99 int bar() { return y + 3; }
100 int y = 4;
101 }
102
103 void test5()
104 {
105 Foo5 f;
106
107 assert(f.bar() == 7);
108 }
109
110 /***********************************/
111
112 class Foo6
113 {
114 int bar() { return y + 3; }
115 final int abc() { return y + 8; }
116 int y = 4;
117 }
118
119 class FooX6 : Foo6
120 {
121 override int bar() { return y + 5; }
122 }
123
124 void test6()
125 {
126 Foo6 f = new FooX6();
127
128 assert(f.bar() == 9);
129 assert(f.abc() == 12);
130 }
131
132
133 /***********************************/
134
135 void bar7(char[3] cad)
136 {
137 assert(cad.length == 3);
138 printf("cad[0] = %d\n", cad[0]);
139 assert(cad[0] == 0xFF);
140 assert(cad[1] == 1);
141 assert(cad[2] == 0xFF);
142 }
143
144 void test7()
145 {
146 char[3] foo;
147
148 foo[1] = 1;
149 bar7(foo);
150 }
151
152
153 /***********************************/
154
155 class gap8
156 {
157 this(char[3] cad)
158 {
159 assert(cad[0] == 0xFF);
160 assert(cad[1] == 1);
161 assert(cad[2] == 0xFF);
162 }
163 }
164
165 void test8()
166 {
167 char[3] foo;
168 gap8 g;
169
170 foo[1] = 1;
171 g = new gap8(foo);
172 }
173
174
175 /***********************************/
176
177 class Foo11
178 {
179 public:
180 int a = 47;
181
182 protected:
183 int b;
184
185 private:
186 int c;
187
188 int bar()
189 {
190 return a + b + c;
191 }
192 }
193
194 class Bar11 : Foo11
195 {
196 int abc()
197 {
198 return a + b;
199 }
200 }
201
202 void test11()
203 {
204 Foo11 f = new Foo11();
205
206 int i = f.a;
207 assert(i == 47);
208 }
209
210 /***********************************/
211
212 class A12
213 {
214 protected void foo() { }
215 }
216
217 class B12: A12
218 {
219 override void foo() { super.foo(); }
220 }
221
222 void test12()
223 {
224 }
225
226 /***********************************/
227
228 alias void *HWND;
229
230 const HWND hWnd = cast(HWND)(null);
231
232 void test13()
233 {
234 }
235
236 /***********************************/
237
238 string bar14()
239 {
240 return "f";
241 }
242
243 char foo14()
244 {
245 return bar14()[0];
246 }
247
248 void test14()
249 {
250 char f = foo14();
251 assert(f == 'f');
252 }
253
254
255 /***********************************/
256
257 void test15()
258 {
259 char[30] a;
260 char[30] b;
261
262 assert(a !is b);
263 }
264
265 /***********************************/
266
267 void test16()
268 {
269 static int function() fp = &func16;
270 int i = fp();
271 assert(i == 648);
272 }
273
274 int func16()
275 {
276 return 648;
277 }
278
279
280 /***********************************/
281
282 string returnSameString(string inputstr)
283 {
284 return inputstr;
285 }
286
287 string passString()
288 {
289 return returnSameString("First string" ~ "Concatenated with second");
290 }
291
292 string butThisWorks()
293 {
294 string s = "Third string";
295 s = s ~ "Concatenated with fourth";
296 return returnSameString(s);
297 }
298
299 void test17()
300 {
301 string s;
302
303 s = passString();
304 printf("passString() = %.*s\n", cast(int)s.length, s.ptr);
305 assert(s == "First stringConcatenated with second");
306
307 s = butThisWorks();
308 printf("butThisWorks() = %.*s\n", cast(int)s.length, s.ptr);
309 assert(s == "Third stringConcatenated with fourth");
310 }
311
312 /***********************************/
313
314
315
316 class A20
317 {
318 private:
319 static int a;
320
321 public:
322 int foo(B20 j) { return j.b; }
323 }
324
325 class B20
326 {
327 private:
328 static int b;
329
330 public:
331 int bar(A20 j) { return j.a; }
332 }
333
334 void test20()
335 {
336 }
337
338 /***********************************/
339
340 alias int* IP;
341
342 void test21()
343 {
344 int i = 5;
345 IP ip = cast(IP) &i;
346 assert(*ip == 5);
347 }
348
349 /***********************************/
350
351 struct RECT
352 {
353 int left = 1;
354 int top = 2;
355 int right = 3;
356 int bottom = 4;
357 }
358
359 struct Rect
360 {
361 RECT theRect;
362 }
363
364
365 void Test(Rect pos)
366 {
367 //printf("left = %d\n", pos.theRect.left);
368 assert(pos.theRect.left == 1);
369 assert(pos.theRect.top == 2);
370 assert(pos.theRect.right == 3);
371 assert(pos.theRect.bottom == 4);
372 }
373
374 class Window
375 {
376 Rect position;
377
378 void createWindow()
379 {
380 Test(position);
381 }
382 }
383
384 void test22()
385 {
386 Window w = new Window();
387 w.createWindow();
388 }
389
390 /***********************************/
391
392 struct Size
393 {
394 int width;
395 int height;
396 }
397
398 Size computeSize()
399 {
400 Size foo;
401
402 foo.width = 12;
403 foo.height = 34;
404
405 printf("Inside: %d,%d\n",foo.width,foo.height);
406
407 return foo;
408 }
409
410
411 void test24()
412 {
413 Size bar;
414 bar = computeSize();
415
416 printf("Outside: %d,%d\n",bar.width,bar.height);
417 assert(bar.width == 12);
418 assert(bar.height == 34);
419 }
420
421 /***********************************/
422
423 void test25()
424 { int i = 5;
425
426 while (i)
427 {
428 break;
429 }
430 }
431
432 /***********************************/
433
434 int test26()
435 in
436 {
437 }
438 out (result)
439 {
440 }
441 do
442 { int i = 5;
443
444 while (i)
445 {
446 break;
447 }
448 return i;
449 }
450
451 /***********************************/
452
453 class A27
454 {
455 int a;
456
457 this()
458 {
459 a = 1;
460 }
461 }
462
463 class B27 : A27
464 {
465 }
466
467 class C27 : B27
468 {
469 this()
470 {
471 super();
472 }
473
474 this(int i)
475 {
476 }
477 }
478
479 void test27()
480 {
481 A27 a = new A27();
482 assert(a.a == 1);
483
484 B27 b = new B27();
485 assert(b.a == 1);
486
487 C27 c = new C27();
488 assert(c.a == 1);
489
490 C27 c2 = new C27(2);
491 assert(c2.a == 1);
492 }
493
494
495 /***********************************/
496
497 const char[1] sep = '/';
498
499 string testx28(string s, string t)
500 {
501 return cast(string)(s ~ sep ~ t);
502 }
503
504 void test28()
505 {
506 string r;
507
508 r = testx28("ab", "cd");
509 assert(r == "ab/cd");
510 }
511
512 /***********************************/
513
514 void test29()
515 {
516 }
517
518
519 /***********************************/
520
521 bool func30(int x, int y)
522 {
523 bool b;
524 b|=(x==y);
525 return b;
526 }
527
528 void test30()
529 {
530 bool b;
531
532 b = func30(1,1);
533 assert(b == true);
534 b = func30(1,2);
535 assert(b == false);
536 }
537
538 /***********************************/
539
540 int a31;
541
542 void test31()
543 {
544 testxxx8.a31 = 3;
545 assert(a31 == 3);
546 }
547
548 /***********************************/
549
550 void test32()
551 {
552 string[] foo;
553 int i;
554
555 foo = new string[45];
556 for (i = 0; i < 45; i++)
557 foo[i] = "hello";
558 for (i = 0; i < 45; i++)
559 assert(foo[i] == "hello");
560 }
561
562
563 /***********************************/
564
565 void test33()
566 {
567 string[] foo;
568 int i = 45;
569
570 foo = new string[i];
571 for (i = 0; i < 45; i++)
572 foo[i] = "hello";
573 for (i = 0; i < 45; i++)
574 assert(foo[i] == "hello");
575 }
576
577
578 /***********************************/
579
580 void test34()
581 {
582 int[3][4] a;
583 int[5][6] b = 16;
584 int i, j;
585
586 for (i = 0; i < 4; i++)
587 for (j = 0; j < 3; j++)
588 assert(a[i][j] == 0);
589
590 for (i = 0; i < 6; i++)
591 for (j = 0; j < 5; j++)
592 assert(b[i][j] == 16);
593 }
594
595 /***********************************/
596 // https://issues.dlang.org/show_bug.cgi?id=19178
597
598 float[3][4] arr2f = 10;
599 Int3_4[1] arr3i = 20;
600 short[3][4][1][1] arr4s = 30;
601
602 enum Int3 : int[3] {
603 a = [0, 1, 2],
604 }
605
606 enum Int3_4 : Int3[4] {
607 b = Int3[4].init,
608 }
609
610 struct S35
611 {
612 int[3][3] arr = [2, 1];
613 }
614
615 void test35()
616 {
617 for (int i = 0; i < 4; i++)
618 {
619 for (int j = 0; j < 3; j++)
620 {
621 // printf("[%d %d]: %f %d %d\n", i, j, arr2f[i][j], arr3i[0][i][j], arr4s[0][0][i][j]);
622 assert(arr2f[i][j] == 10);
623 assert(arr3i[0][i][j] == 20);
624 assert(arr4s[0][0][i][j] == 30);
625 }
626 }
627
628 S35 t = S35.init;
629 assert(t.arr[0] == [2, 2, 2]);
630 assert(t.arr[1] == [1, 1, 1]);
631 assert(t.arr[2] == [0, 0, 0]);
632 }
633 /***********************************/
634
635 string itoa(int i)
636 {
637 char[32] buffer;
638 snprintf(buffer.ptr, 32, "%d", i);
639 return buffer[0 .. strlen(buffer.ptr)].idup;
640 }
641
642 string testa36(int i, int j, string a, string b, string c)
643 {
644 string s = "string 0;" ~ itoa(i) ~
645 "string 1;" ~ itoa(j) ~
646 "string 2;" ~ itoa(i) ~
647 "string 3;";
648
649 // string s = a ~ b ~ c;
650 return s;
651 }
652
653 void test36()
654 {
655 string s = testa36(26, 47, "a", "b", "c");
656
657 printf("s = '%.*s'\n", cast(int)s.length, s.ptr);
658 assert(s == "string 0;26string 1;47string 2;26string 3;");
659 }
660
661 /***********************************/
662
663 void test37()
664 {
665 string[ulong] x;
666 ulong v1 = 297321415603;
667 ulong v2 = 331681153971;
668 x[v1] = "aa";
669 printf( "%llx %llx\n", v1, v2 );
670 assert(!(v2 in x));
671 }
672
673
674 /***********************************/
675
676 void test38()
677 {
678 int n = atoi("1");
679 static char[8192 + 1] flags;
680 long i, k;
681 int count = 0;
682
683 try
684 {
685 while (n--)
686 {
687 count = 0;
688
689 for (i = 2; i <= 8192; i++)
690 flags[cast(size_t)i] = 1;
691
692 for (i = 2; i <= 8192; i++)
693 {
694 if (flags[cast(size_t)i])
695 {
696 for (k = i+i; k <= 8192; k += i)
697 flags[cast(size_t)k] = 0;
698
699 count++;
700 }
701 }
702 }
703
704 printf("Count: %d\n", count);
705 assert(count == 1028);
706 }
707 catch(Throwable)
708 {
709 printf("Exception: %lld\n", k);
710 assert(0);
711 }
712 }
713
714
715 /***********************************/
716
717 interface I39
718 {
719 }
720
721 class C39 : I39
722 {
723 int x = 432;
724 }
725
726 void test39()
727 {
728 C39 c = new C39;
729
730 printf("%p %d\n", c, c.x);
731 assert(c.x == 432);
732 printf("%p\n", cast(I39) c);
733 c = cast(C39) cast(I39) c;
734 printf("%p\n", c);
735 assert(c !is null);
736 }
737
738
739 /***********************************/
740
741 void test40()
742 {
743 Object x;
744
745 x = null;
746 x = 0 ? x : null;
747 x = 0 ? null : x;
748 }
749
750 /***********************************/
751
752 int foo42(const(char) *x, ...)
753 {
754 va_list ap;
755
756 va_start!(typeof(x))(ap, x);
757 assert(ap != va_list.init);
758
759 int i;
760 i = va_arg!(typeof(i))(ap);
761 assert(i == 3);
762
763 long l;
764 l = va_arg!(typeof(l))(ap);
765 assert(l == 23);
766
767 uint k;
768 k = va_arg!(typeof(k))(ap);
769 assert(k == 4);
770
771 va_end(ap);
772
773 return cast(int)(i + l + k);
774 }
775
776 void test42()
777 {
778 int j;
779
780 j = foo42("hello", 3, 23L, 4);
781 printf("j = %d\n", j);
782 assert(j == 30);
783 }
784
785 /***********************************/
786
787 int x44;
788
789 class A44 {
790 this() scope { printf("A44 ctor\n"); x44 += 1; }
791 ~this() scope { printf("A44 dtor\n"); x44 += 0x100; }
792 }
793 class B44 : A44 { }
794
795 void foo44() { scope B44 b = new B44; }
796
797 void test44()
798 {
799 printf("foo44...\n");
800 foo44();
801 printf("...foo44\n");
802 assert(x44 == 0x101);
803 }
804
805 /***********************************/
806
807 /*
808 import std.stdarg;
809 import std.utf;
810
811 int unFormat( bool delegate( out dchar ) getc,
812 bool delegate( dchar ) ungetc,
813 TypeInfo[] arguments,
814 void* argptr )
815 {
816 size_t arg = 0;
817 dchar[] fmt;
818
819 if( arguments[arg] is typeid( string ) )
820 fmt = toUTF32( va_arg!(string)( argptr ) );
821 else if( arguments[arg] is typeid( wchar[] ) )
822 fmt = toUTF32( va_arg!(wchar[])( argptr ) );
823 else if( arguments[arg] is typeid( dchar[] ) )
824 fmt = va_arg!(dchar[])( argptr );
825 else
826 return 0;
827 }
828 */
829
830 void test45()
831 {
832 }
833
834 /***********************************/
835
836 int sreadf( ... )
837 {
838 va_arg!(string)( _argptr );
839 return 0;
840 }
841
842
843 void test46()
844 {
845 printf( "hello world\n" );
846 }
847
848 /***********************************/
849
850 void test48()
851 {
852 try{
853 }finally{
854 debug(p48) { }
855 }
856 }
857
858 /***********************************/
859
860 void test49()
861 {
862 int k = 1;
863 if(k == 0)
864 debug{printf("test");}
865 }
866
867 /***********************************/
868
869 void test50()
870 { int x;
871
872 if (x)
873 version (none)
874 foo;
875 }
876
877 /***********************************/
878
879 int main()
880 {
881 test1();
882 test2();
883 test3();
884 test4();
885 test5();
886 test6();
887 test7();
888 test8();
889 test11();
890 test12();
891 test13();
892 test14();
893 test15();
894 test16();
895 test17();
896 test20();
897 test21();
898 test22();
899 test24();
900 test25();
901 test26();
902 test27();
903 test28();
904 test29();
905 test30();
906 test31();
907 test32();
908 test33();
909 test34();
910 test35();
911 test36();
912 test37();
913 test38();
914 test39();
915 test40();
916 test42();
917 test44();
918 test45();
919 test46();
920 test48();
921 test49();
922 test50();
923
924 printf("Success\n");
925 return 0;
926 }