d: Merge upstream dmd, druntime 4ca4140e58, phobos 454dff14d.
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / test8.d
blobf10076223d59b6e2b39ddbaeb03fb48116d087b5
1 /*
2 TEST_OUTPUT:
3 ---
4 runnable/test8.d(261): Deprecation: identity comparison of static arrays implicitly coerces them to slices, which are compared by reference
5 ---
6 */
8 module testxxx8;
10 import core.vararg;
12 extern(C)
14 int atoi(const char*);
15 int printf(const char*, ...);
16 size_t strlen(const char*);
17 version(Windows)
19 int _snprintf(char*, size_t, const char*, ...);
20 alias _snprintf snprintf;
22 else
23 int snprintf(char*, size_t, const char*, ...);
26 /***********************************/
28 struct Foo1
30 static int x = 3;
31 int y = 4;
34 void test1()
36 Foo1 f;
38 assert(Foo1.x == 3);
39 assert(f.x == 3);
40 assert(f.y == 4);
43 /***********************************/
45 class Foo2
47 static int x = 5;
48 int y = 6;
51 void test2()
53 Foo2 f = new Foo2();
55 assert(Foo2.x == 5);
56 assert(f.x == 5);
57 assert(f.y == 6);
61 /***********************************/
63 struct Foo3
65 static int bar() { return 3; }
66 int y = 4;
69 void test3()
71 Foo3 f;
73 assert(Foo3.bar() == 3);
74 assert(f.bar() == 3);
77 /***********************************/
79 class Foo4
81 static int bar() { return 3; }
82 int y = 4;
85 void test4()
87 Foo4 f = new Foo4();
89 assert(Foo4.bar() == 3);
90 assert(f.bar() == 3);
94 /***********************************/
96 struct Foo5
98 int bar() { return y + 3; }
99 int y = 4;
102 void test5()
104 Foo5 f;
106 assert(f.bar() == 7);
109 /***********************************/
111 class Foo6
113 int bar() { return y + 3; }
114 final int abc() { return y + 8; }
115 int y = 4;
118 class FooX6 : Foo6
120 override int bar() { return y + 5; }
123 void test6()
125 Foo6 f = new FooX6();
127 assert(f.bar() == 9);
128 assert(f.abc() == 12);
132 /***********************************/
134 void bar7(char[3] cad)
136 assert(cad.length == 3);
137 printf("cad[0] = %d\n", cad[0]);
138 assert(cad[0] == 0xFF);
139 assert(cad[1] == 1);
140 assert(cad[2] == 0xFF);
143 void test7()
145 char[3] foo;
147 foo[1] = 1;
148 bar7(foo);
152 /***********************************/
154 class gap8
156 this(char[3] cad)
158 assert(cad[0] == 0xFF);
159 assert(cad[1] == 1);
160 assert(cad[2] == 0xFF);
164 void test8()
166 char[3] foo;
167 gap8 g;
169 foo[1] = 1;
170 g = new gap8(foo);
174 /***********************************/
176 class Foo11
178 public:
179 int a = 47;
181 protected:
182 int b;
184 private:
185 int c;
187 int bar()
189 return a + b + c;
193 class Bar11 : Foo11
195 int abc()
197 return a + b;
201 void test11()
203 Foo11 f = new Foo11();
205 int i = f.a;
206 assert(i == 47);
209 /***********************************/
211 class A12
213 protected void foo() { }
216 class B12: A12
218 override void foo() { super.foo(); }
221 void test12()
225 /***********************************/
227 alias void *HWND;
229 const HWND hWnd = cast(HWND)(null);
231 void test13()
235 /***********************************/
237 string bar14()
239 return "f";
242 char foo14()
244 return bar14()[0];
247 void test14()
249 char f = foo14();
250 assert(f == 'f');
254 /***********************************/
256 void test15()
258 char[30] a;
259 char[30] b;
261 assert(a !is b);
264 /***********************************/
266 void test16()
268 static int function() fp = &func16;
269 int i = fp();
270 assert(i == 648);
273 int func16()
275 return 648;
279 /***********************************/
281 string returnSameString(string inputstr)
283 return inputstr;
286 string passString()
288 return returnSameString("First string" ~ "Concatenated with second");
291 string butThisWorks()
293 string s = "Third string";
294 s = s ~ "Concatenated with fourth";
295 return returnSameString(s);
298 void test17()
300 string s;
302 s = passString();
303 printf("passString() = %.*s\n", cast(int)s.length, s.ptr);
304 assert(s == "First stringConcatenated with second");
306 s = butThisWorks();
307 printf("butThisWorks() = %.*s\n", cast(int)s.length, s.ptr);
308 assert(s == "Third stringConcatenated with fourth");
311 /***********************************/
315 class A20
317 private:
318 static int a;
320 public:
321 int foo(B20 j) { return j.b; }
324 class B20
326 private:
327 static int b;
329 public:
330 int bar(A20 j) { return j.a; }
333 void test20()
337 /***********************************/
339 alias int* IP;
341 void test21()
343 int i = 5;
344 IP ip = cast(IP) &i;
345 assert(*ip == 5);
348 /***********************************/
350 struct RECT
352 int left = 1;
353 int top = 2;
354 int right = 3;
355 int bottom = 4;
358 struct Rect
360 RECT theRect;
364 void Test(Rect pos)
366 //printf("left = %d\n", pos.theRect.left);
367 assert(pos.theRect.left == 1);
368 assert(pos.theRect.top == 2);
369 assert(pos.theRect.right == 3);
370 assert(pos.theRect.bottom == 4);
373 class Window
375 Rect position;
377 void createWindow()
379 Test(position);
383 void test22()
385 Window w = new Window();
386 w.createWindow();
389 /***********************************/
391 struct Size
393 int width;
394 int height;
397 Size computeSize()
399 Size foo;
401 foo.width = 12;
402 foo.height = 34;
404 printf("Inside: %d,%d\n",foo.width,foo.height);
406 return foo;
410 void test24()
412 Size bar;
413 bar = computeSize();
415 printf("Outside: %d,%d\n",bar.width,bar.height);
416 assert(bar.width == 12);
417 assert(bar.height == 34);
420 /***********************************/
422 void test25()
423 { int i = 5;
425 while (i)
427 break;
431 /***********************************/
433 int test26()
437 out (result)
441 { int i = 5;
443 while (i)
445 break;
447 return i;
450 /***********************************/
452 class A27
454 int a;
456 this()
458 a = 1;
462 class B27 : A27
466 class C27 : B27
468 this()
470 super();
473 this(int i)
478 void test27()
480 A27 a = new A27();
481 assert(a.a == 1);
483 B27 b = new B27();
484 assert(b.a == 1);
486 C27 c = new C27();
487 assert(c.a == 1);
489 C27 c2 = new C27(2);
490 assert(c2.a == 1);
494 /***********************************/
496 const char[1] sep = '/';
498 string testx28(string s, string t)
500 return cast(string)(s ~ sep ~ t);
503 void test28()
505 string r;
507 r = testx28("ab", "cd");
508 assert(r == "ab/cd");
511 /***********************************/
513 void test29()
518 /***********************************/
520 bool func30(int x, int y)
522 bool b;
523 b|=(x==y);
524 return b;
527 void test30()
529 bool b;
531 b = func30(1,1);
532 assert(b == true);
533 b = func30(1,2);
534 assert(b == false);
537 /***********************************/
539 int a31;
541 void test31()
543 testxxx8.a31 = 3;
544 assert(a31 == 3);
547 /***********************************/
549 void test32()
551 string[] foo;
552 int i;
554 foo = new string[45];
555 for (i = 0; i < 45; i++)
556 foo[i] = "hello";
557 for (i = 0; i < 45; i++)
558 assert(foo[i] == "hello");
562 /***********************************/
564 void test33()
566 string[] foo;
567 int i = 45;
569 foo = new string[i];
570 for (i = 0; i < 45; i++)
571 foo[i] = "hello";
572 for (i = 0; i < 45; i++)
573 assert(foo[i] == "hello");
577 /***********************************/
579 void test34()
581 int[3][4] a;
582 int[5][6] b = 16;
583 int i, j;
585 for (i = 0; i < 4; i++)
586 for (j = 0; j < 3; j++)
587 assert(a[i][j] == 0);
589 for (i = 0; i < 6; i++)
590 for (j = 0; j < 5; j++)
591 assert(b[i][j] == 16);
594 /***********************************/
595 // https://issues.dlang.org/show_bug.cgi?id=19178
597 float[3][4] arr2f = 10;
598 Int3_4[1] arr3i = 20;
599 short[3][4][1][1] arr4s = 30;
601 enum Int3 : int[3] {
602 a = [0, 1, 2],
605 enum Int3_4 : Int3[4] {
606 b = Int3[4].init,
609 struct S35
611 int[3][3] arr = [2, 1];
614 void test35()
616 for (int i = 0; i < 4; i++)
618 for (int j = 0; j < 3; j++)
620 // printf("[%d %d]: %f %d %d\n", i, j, arr2f[i][j], arr3i[0][i][j], arr4s[0][0][i][j]);
621 assert(arr2f[i][j] == 10);
622 assert(arr3i[0][i][j] == 20);
623 assert(arr4s[0][0][i][j] == 30);
627 S35 t = S35.init;
628 assert(t.arr[0] == [2, 2, 2]);
629 assert(t.arr[1] == [1, 1, 1]);
630 assert(t.arr[2] == [0, 0, 0]);
632 /***********************************/
634 string itoa(int i)
636 char[32] buffer;
637 snprintf(buffer.ptr, 32, "%d", i);
638 return buffer[0 .. strlen(buffer.ptr)].idup;
641 string testa36(int i, int j, string a, string b, string c)
643 string s = "string 0;" ~ itoa(i) ~
644 "string 1;" ~ itoa(j) ~
645 "string 2;" ~ itoa(i) ~
646 "string 3;";
648 // string s = a ~ b ~ c;
649 return s;
652 void test36()
654 string s = testa36(26, 47, "a", "b", "c");
656 printf("s = '%.*s'\n", cast(int)s.length, s.ptr);
657 assert(s == "string 0;26string 1;47string 2;26string 3;");
660 /***********************************/
662 void test37()
664 string[ulong] x;
665 ulong v1 = 297321415603;
666 ulong v2 = 331681153971;
667 x[v1] = "aa";
668 printf( "%llx %llx\n", v1, v2 );
669 assert(!(v2 in x));
673 /***********************************/
675 void test38()
677 int n = atoi("1");
678 static char[8192 + 1] flags;
679 long i, k;
680 int count = 0;
684 while (n--)
686 count = 0;
688 for (i = 2; i <= 8192; i++)
689 flags[cast(size_t)i] = 1;
691 for (i = 2; i <= 8192; i++)
693 if (flags[cast(size_t)i])
695 for (k = i+i; k <= 8192; k += i)
696 flags[cast(size_t)k] = 0;
698 count++;
703 printf("Count: %d\n", count);
704 assert(count == 1028);
706 catch(Throwable)
708 printf("Exception: %lld\n", k);
709 assert(0);
714 /***********************************/
716 interface I39
720 class C39 : I39
722 int x = 432;
725 void test39()
727 C39 c = new C39;
729 printf("%p %d\n", c, c.x);
730 assert(c.x == 432);
731 printf("%p\n", cast(I39) c);
732 c = cast(C39) cast(I39) c;
733 printf("%p\n", c);
734 assert(c !is null);
738 /***********************************/
740 void test40()
742 Object x;
744 x = null;
745 x = 0 ? x : null;
746 x = 0 ? null : x;
749 /***********************************/
751 int foo42(const(char) *x, ...)
753 va_list ap;
755 va_start!(typeof(x))(ap, x);
756 assert(ap != va_list.init);
758 int i;
759 i = va_arg!(typeof(i))(ap);
760 assert(i == 3);
762 long l;
763 l = va_arg!(typeof(l))(ap);
764 assert(l == 23);
766 uint k;
767 k = va_arg!(typeof(k))(ap);
768 assert(k == 4);
770 va_end(ap);
772 return cast(int)(i + l + k);
775 void test42()
777 int j;
779 j = foo42("hello", 3, 23L, 4);
780 printf("j = %d\n", j);
781 assert(j == 30);
784 /***********************************/
786 int x44;
788 class A44 {
789 this() scope { printf("A44 ctor\n"); x44 += 1; }
790 ~this() scope { printf("A44 dtor\n"); x44 += 0x100; }
792 class B44 : A44 { }
794 void foo44() { scope B44 b = new B44; }
796 void test44()
798 printf("foo44...\n");
799 foo44();
800 printf("...foo44\n");
801 assert(x44 == 0x101);
804 /***********************************/
807 import std.stdarg;
808 import std.utf;
810 int unFormat( bool delegate( out dchar ) getc,
811 bool delegate( dchar ) ungetc,
812 TypeInfo[] arguments,
813 void* argptr )
815 size_t arg = 0;
816 dchar[] fmt;
818 if( arguments[arg] is typeid( string ) )
819 fmt = toUTF32( va_arg!(string)( argptr ) );
820 else if( arguments[arg] is typeid( wchar[] ) )
821 fmt = toUTF32( va_arg!(wchar[])( argptr ) );
822 else if( arguments[arg] is typeid( dchar[] ) )
823 fmt = va_arg!(dchar[])( argptr );
824 else
825 return 0;
829 void test45()
833 /***********************************/
835 int sreadf( ... )
837 va_arg!(string)( _argptr );
838 return 0;
842 void test46()
844 printf( "hello world\n" );
847 /***********************************/
849 void test48()
851 try{
852 }finally{
853 debug(p48) { }
857 /***********************************/
859 void test49()
861 int k = 1;
862 if(k == 0)
863 debug{printf("test");}
866 /***********************************/
868 void test50()
869 { int x;
871 if (x)
872 version (none)
873 foo;
876 /***********************************/
878 int main()
880 test1();
881 test2();
882 test3();
883 test4();
884 test5();
885 test6();
886 test7();
887 test8();
888 test11();
889 test12();
890 test13();
891 test14();
892 test15();
893 test16();
894 test17();
895 test20();
896 test21();
897 test22();
898 test24();
899 test25();
900 test26();
901 test27();
902 test28();
903 test29();
904 test30();
905 test31();
906 test32();
907 test33();
908 test34();
909 test35();
910 test36();
911 test37();
912 test38();
913 test39();
914 test40();
915 test42();
916 test44();
917 test45();
918 test46();
919 test48();
920 test49();
921 test50();
923 printf("Success\n");
924 return 0;