Merge dmd upstream e2fe2687b
[official-gcc.git] / gcc / testsuite / gdc.test / runnable / test34.d
blob003024db97772ce46a0fa83c9c79690f7e51ddb4
2 module test34;
4 import std.stdio;
5 import std.string;
6 import std.format;
7 import core.exception;
10 /************************************************/
12 class Foo {}
13 class Bar {}
15 void test1()
17 TypeInfo ti_foo = typeid(Foo);
18 TypeInfo ti_bar = typeid(Bar);
20 auto hfoo = ti_foo.toHash();
21 auto hbar = ti_bar.toHash();
22 writefln("typeid(Foo).toHash: ", hfoo);
23 writefln("typeid(Bar).toHash: ", hbar);
24 assert(hfoo != hbar);
26 auto e = (ti_foo == ti_bar);
27 writefln("opEquals: ", e ? "equal" : "not equal");
28 assert(!e);
30 auto c = (ti_foo.opCmp(ti_bar) == 0);
31 writefln("opCmp: ", c ? "equal" : "not equal");
32 assert(!c);
36 /************************************************/
38 void test2()
40 assert( [2,3]!=[2,4] );
41 assert( [3,2]!=[4,2] );
42 assert( !([2,3]==[2,4]) );
43 assert( ([2,3]==[2,3]) );
46 /************************************************/
48 struct Struct
50 int langID;
51 long _force_nrvo;
54 Struct[1] table;
56 Struct getfirst()
58 foreach(v; table) {
59 writeln(v.langID);
60 assert(v.langID == 1);
61 return v;
63 assert(0);
66 Struct getsecond()
68 foreach(ref v; table) {
69 writeln(v.langID);
70 assert(v.langID == 1);
71 return v;
73 assert(0);
76 void test3()
78 table[0].langID = 1;
80 auto v = getfirst();
81 writeln(v.langID);
82 assert(v.langID == 1);
84 v = getsecond();
85 writeln(v.langID);
86 assert(v.langID == 1);
89 /************************************************/
91 class ExOuter
93 class ExInner
95 this()
97 typeof(this.outer) X;
98 static assert(is(typeof(X) == ExOuter));
103 void test4()
107 /************************************************/
109 int status5;
111 struct MyStruct5
115 void rec5(int i, MyStruct5 s)
117 if( i > 0 )
119 status5++;
120 rec5(i-1, s);
124 void test5()
126 assert(status5==0);
127 MyStruct5 st;
128 rec5(1030, st);
129 assert(status5==1030);
132 /************************************************/
134 class C6
136 const int a;
138 this()
140 a = 3;
143 this(int x)
145 this();
149 void test6()
153 /************************************************/
155 template parseUinteger(string s)
157 static if (s.length == 0)
158 { const char[] value = "";
159 const char[] rest = "";
161 else static if (s[0] >= '0' && s[0] <= '9')
162 { const char[] value = s[0] ~ parseUinteger!(s[1..$]).value;
163 const char[] rest = parseUinteger!(s[1..$]).rest;
165 else
166 { const char[] value = "";
167 const char[] rest = s;
171 template parseInteger(string s)
173 static if (s.length == 0)
174 { const char[] value = "";
175 const char[] rest = "";
177 else static if (s[0] >= '0' && s[0] <= '9')
178 { const char[] value = s[0] ~ parseUinteger!(s[1..$]).value;
179 const char[] rest = parseUinteger!(s[1..$]).rest;
181 else static if (s.length >= 2 &&
182 s[0] == '-' && s[1] >= '0' && s[1] <= '9')
183 { const char[] value = s[0..2] ~ parseUinteger!(s[2..$]).value;
184 const char[] rest = parseUinteger!(s[2..$]).rest;
186 else
187 { const char[] value = "";
188 const char[] rest = s;
192 void test7()
194 writeln(parseUinteger!("1234abc").value);
195 writeln(parseUinteger!("1234abc").rest);
196 writeln(parseInteger!("-1234abc").value);
197 writeln(parseInteger!("-1234abc").rest);
199 assert(parseUinteger!("1234abc").value == "1234");
200 assert(parseUinteger!("1234abc").rest == "abc");
201 assert(parseInteger!("-1234abc").value == "-1234");
202 assert(parseInteger!("-1234abc").rest == "abc");
205 /************************************************/
207 struct Foo8 { }
209 enum Enum { RED }
211 //typedef int myint;
213 alias int myalias;
215 void test8()
218 assert((1+2).stringof == "1 + 2");
219 assert(Foo8.stringof == "Foo8");
220 assert(test.Foo8.stringof == "test.Foo8");
221 assert(int.stringof == "int");
222 assert((int*[5][]).stringof == "int*[5][]");
223 assert(Enum.RED.stringof == "Enum.RED");
224 assert(test.myint.stringof == "test.myint");
225 assert(myalias.stringof == "myalias");
226 assert((5).stringof == "5");
227 assert(typeof(5).stringof == "typeof(5)");
231 /************************************************/
234 class Base9 {
235 public void fnc(){
239 class Foo9 : Base9 {
240 alias Base9.fnc fnc;
241 public void fnc(){
243 static this(){
244 alias void function() T;
245 T ptr = & fnc;
250 void test9()
254 /************************************************/
256 bool isalnum(dchar c) { return c>='0' && c >= '9'; }
258 char[] toHtmlFilename(char[] fname)
260 foreach (ref c; fname)
262 if (!isalnum(c) && c != '.' && c != '-')
263 c = '_';
265 return fname;
268 void test10()
272 /************************************************/
274 class A34 { }
275 class B34 : A34 { }
277 void test11()
279 A34 test=new B34;
280 writefln("Test is ", test.toString);
281 assert(test.toString == "test34.B34");
282 A34 test_2=cast(A34)(new B34);
283 writefln("Test 2 is ", test_2.toString);
284 assert(test_2.toString == "test34.B34");
287 /************************************************/
289 template Foo12(T: T[U], U)
291 alias int Foo12;
294 void test12()
296 Foo12!(int[long]) x;
297 assert(is(typeof(x) == int));
300 /************************************************/
302 class C13
304 int a = 4;
305 this()
307 printf("C13.this()\n");
308 assert(a == 4);
309 a = 5;
313 void test13()
315 C13 c = cast(C13)Object.factory("test34.C13");
316 assert(c.a == 5);
317 Object o = Object.factory("test35.C13");
318 assert(o is null);
321 /************************************************/
323 class Base15 {
324 int func(int a) { return 1; }
328 class Foo15 : Base15 {
329 alias Base15.func func;
333 class Bar15 : Foo15 {
334 alias Foo15.func func;
335 int func(string a) { return 2; }
338 void test15()
340 Bar15 b = new Bar15();
341 assert(b.func("hello") == 2);
342 assert(b.func(5) == 1);
345 /************************************************/
347 struct Basic16(T, U) {}
349 struct Iterator16(T : Basic16!(T, U), U)
351 static void Foo()
353 writeln(typeid(T), typeid(U));
354 assert(is(T == int));
355 assert(is(U == float));
359 void test16()
361 Iterator16!(Basic16!(int, float)).Foo();
364 /************************************************/
366 struct S17(T)
368 struct iterator {}
371 int insert17(T) (S17!(T) lst, S17!(T).iterator i)
373 return 3;
376 void test17()
378 S17!(int) a;
379 S17!(int).iterator i;
380 auto x = insert17(a, i);
381 assert(x == 3);
384 /************************************************/
386 void test18()
388 real t = 0.;
389 for(int i=0; i<10; i++)
391 t += 1.;
392 real r = (2*t);
393 printf("%Lg %Lg %Lg\n", t, r, 2*t);
394 assert(2*t == (i+1)*2);
398 /************************************************/
400 void test19()
402 char c = '3';
403 void[] ca = cast(void[])[c];
404 char[] x = cast(char[])ca;
405 assert(x[0] == '3');
408 /************************************************/
410 enum type20
416 class myclass20
418 template XX(uint a, uint c)
420 static uint XX(){ return (a*256+c);}
422 void testcase()
424 switch (cast(uint)type20.a)
426 case XX!(cast(uint)type20.a,cast(uint)type20.b)():
427 break;
428 default: assert(0);
433 void test20()
437 /************************************************/
439 struct S21
441 alias int Foo;
442 int x;
445 void test21()
447 S21 s;
448 typeof(s).Foo j;
449 assert(is(typeof(j) == int));
452 /************************************************/
454 void test22()
456 auto i = 3, j = 4;
457 assert(is(typeof(i) == int));
458 assert(is(typeof(j) == int));
461 /************************************************/
463 static m23 = 5, n23 = 6;
465 void test23()
467 auto i = 3, j = 4;
468 assert(is(typeof(i) == int));
469 assert(is(typeof(j) == int));
470 assert(is(typeof(m23) == int));
471 assert(is(typeof(n23) == int));
474 /************************************************/
476 const int a24 = 0;
477 const int foo24 = 4;
478 const int[1] bar24 = [foo24 * 2];
479 const int zap24 = (1 << bar24[a24]);
481 void test24()
483 assert(zap24 == 256);
486 /************************************************/
488 struct List25(T) { }
489 struct CircularQueue25(T) { }
491 void front25(T)(ref List25!(T) list) { }
492 void front25(T)(ref CircularQueue25!(T) queue) { }
494 void test25()
496 List25!(int) x;
497 front25(x);
500 /************************************************/
502 struct Foo26
504 const string x;
507 static Foo26 foo26 = {"something"};
509 void test26()
511 assert(foo26.x == "something");
514 /************************************************/
516 template Mang(alias F)
518 class G { }
519 alias void function (G ) H;
520 const string mangledname = H.mangleof;
523 template moo(alias A)
525 pragma(msg," ");
526 const string a = Mang!(A).mangledname;
527 pragma(msg," ");
528 static assert(Mang!(A).mangledname == a); // FAILS !!!
529 pragma(msg," ");
532 void test27()
534 int q;
535 string b = moo!(q).a;
538 /************************************************/
540 struct Color
542 static void fromRgb(uint rgb)
546 static void fromRgb(ubyte alpha, uint rgb)
551 void test28()
553 Color.fromRgb(0);
554 Color.fromRgb(cast(uint)0);
557 /************************************************/
559 void test29()
561 const char[] t="abcd";
562 const ubyte[] t2=cast(ubyte[])t;
563 const char[] t3=['a','b','c','d'];
564 const ubyte[] t4=cast(ubyte[])t3;
565 assert(t4[1] == 'b');
568 /************************************************/
570 void test30()
572 const char[] test = "" ~ 'a' ~ 'b' ~ 'c';
573 char[] test2 = (cast(char[])null)~'a'~'b'~'c';
574 const char[] test3 = (cast(char[])null)~'a'~'b'~'c';
575 char[] test4 = (cast(char[])[])~'a'~'b'~'c';
576 const char[] test5 = (cast(char[])[])~'a'~'b'~'c';
577 const char[] test6 = null;
578 const char[] test7 = test6~'a'~'b'~'c';
581 /************************************************/
583 class C31
585 synchronized invariant() { int x; }
588 void test31()
592 /************************************************/
594 ulong foo32()
596 return cast(ulong) (cast(ulong) 1176576512 + cast(float) -2);
599 void test32()
601 assert(foo32()==1176576510);
604 /************************************************/
606 class RangeCoder
608 uint[258] cumCount; // 256 + end + total
609 uint lower;
610 uint upper;
611 ulong range;
613 this() {
614 for (int i=0; i<cumCount.length; i++)
615 cumCount[i] = i;
616 lower = 0;
617 upper = 0xffffffff;
618 range = 0x100000000;
621 void encode(uint symbol) {
622 uint total = cumCount[$ - 1];
623 // "Error: Access Violation" in following line
624 upper = lower + cast(uint)((cumCount[symbol+1] * range) / total) - 1;
625 lower = lower + cast(uint)((cumCount[symbol] * range) / total);
629 void test33()
631 RangeCoder rc = new RangeCoder();
632 rc.encode(77);
635 /************************************************/
637 struct Vector34
639 float x, y, z;
641 public static Vector34 opCall(float x = 0, float y = 0, float z = 0)
643 Vector34 v;
645 v.x = x;
646 v.y = y;
647 v.z = z;
649 return v;
652 public string toString()
654 return std.string.format("<%f, %f, %f>", x, y, z);
658 class Foo34
660 private Vector34 v;
662 public this()
664 v = Vector34(1, 0, 0);
667 public void foo()
669 bar();
672 private void bar()
674 auto s = foobar();
675 writef("Returned: %s\n", s);
676 assert(std.string.format("%s", s) == "<1.000000, 0.000000, 0.000000>");
679 public Vector34 foobar()
681 writef("Returning %s\n", v);
683 return v;
684 Vector34 b = Vector34();
685 return b;
689 void test34()
691 Foo34 f = new Foo34();
692 f.foo();
696 /************************************************/
698 void foo35()
700 uint a;
701 uint b;
702 uint c;
703 extern (Windows) int function(int i, int j, int k) xxx;
705 a = 1;
706 b = 2;
707 c = 3;
709 xxx = cast(typeof(xxx))(a + b);
710 version(GNU)
712 version(X86) asm
714 "int $3;" : : : ;
716 else version(X86_64) asm
718 "int $3;" : : : ;
720 else
722 import gcc.builtins;
723 __builtin_trap();
726 else
727 asm { int 3; }
728 xxx( 4, 5, 6 );
731 void test35()
735 /************************************************/
737 void test36()
739 int* p = void, c = void;
742 /************************************************/
744 void test37()
746 synchronized
748 synchronized
750 writefln("Hello world!");
755 /************************************************/
757 struct Rect {
758 int left, top, right, bottom;
761 void test38()
763 print38(sizeTest(false));
764 print38(sizeTest(true));
765 print38(defaultRect);
768 static Rect sizeTest(bool empty) {
769 if (empty) {
770 Rect result;
771 return result;
772 //return Rect.init;
773 } else {
774 return defaultRect;
775 /+Rect result = defaultRect;
776 return result;+/
780 void print38(Rect r) {
781 writefln("(%d, %d)-(%d, %d)", r.left, r.top, r.right, r.bottom);
782 assert(r.left == 0);
783 assert(r.right == 0);
784 assert(r.top == 0);
785 assert(r.bottom == 0);
788 Rect defaultRect() {
789 return Rect.init;
792 /************************************************/
794 void test39()
796 double[][] foo = [[1.0],[2.0]];
798 writeln(foo[0]); // --> [1] , ok
799 writeln(foo[1]); // --> [2] , ok
801 writeln(foo); // --> [[1],4.63919e-306] ack!
802 writefln("%s", foo); // --> ditto
803 auto f = std.string.format("%s", foo);
804 assert(f == "[[1], [2]]");
806 double[1][2] bar;
807 bar[0][0] = 1.0;
808 bar[1][0] = 2.0;
810 writeln(bar); // Error: Access violation
811 auto r = std.string.format("%s", bar);
812 assert(r == "[[1], [2]]");
815 /************************************************/
817 void test40()
819 int[char] x;
820 x['b'] = 123;
821 writeln(x);
822 auto r = std.string.format("%s", x);
823 assert(r == "['b':123]");
824 writeln(x['b']);
827 /************************************************/
829 void test41()
833 /************************************************/
835 enum Enum42 {
836 A = 1
839 void test42() {
840 Enum42[] enums = new Enum42[1];
841 assert(enums[0] == Enum42.A);
845 /************************************************/
847 struct A43 {}
849 struct B43(L) {
850 A43 l;
853 void test43()
855 A43 a;
856 auto b = B43!(A43)(a);
859 /************************************************/
861 void test44()
863 int[ const char[] ] a = ["abc":3, "def":4];
866 /************************************************/
868 void test45()
870 //char[3][] a = ["abc", "def"];
871 //writefln(a);
872 //char[][2] b = ["abc", "def"];
873 //writefln(b);
876 /************************************************/
878 struct bignum
880 bool smaller()
882 if (true) return false;
883 else return false;
884 assert(0);
887 void equal()
889 if (!smaller)
890 return;
894 void test46()
898 /************************************************/
900 static size_t myfind(string haystack, char needle) {
901 foreach (i, c ; haystack) {
902 if (c == needle) return i;
904 return size_t.max;
907 static size_t skip_digits(string s) {
908 foreach (i, c ; s) {
909 if (c < '0' || c > '9') return i;
911 return s.length;
914 static uint matoi(string s) {
915 uint result = 0;
916 foreach (c ; s) {
917 if (c < '0' || c > '9') break;
918 result = result * 10 + (c - '0');
920 return result;
923 enum { leading, skip, width, modifier, format, fmt_length, extra };
925 static string GetFormat(string s) {
926 uint pos = 0;
927 string result;
928 // find the percent sign
929 while (pos < s.length && s[pos] != '%') {
930 ++pos;
932 const leading_chars = pos;
933 result ~= cast(char) pos;
934 if (pos < s.length) ++pos; // go right after the '%'
935 // skip?
936 if (pos < s.length && s[pos] == '*') {
937 result ~= 1;
938 ++pos;
939 } else {
940 result ~= 0;
942 // width?
943 result ~= cast(char) matoi(s);
944 pos += skip_digits(s[pos .. $]);
945 // modifier?
946 if (pos < s.length && myfind("hjlLqtz", s[pos]) != size_t.max) {
947 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@
948 static if (true) {
949 result ~= s[pos++];
950 } else {
951 result ~= s[pos];
952 ++pos;
954 // @@@@@@@@@@@@@@@@@@@@@@@@@@@@
955 } else {
956 result ~= '\0';
958 return result;
961 void test47()
963 static string test = GetFormat(" %*Lf");
964 assert(test[modifier] == 'L', "`" ~ test[modifier] ~ "'");
967 /************************************************/
969 class B48() {}
970 class C48 {}
972 int foo48()(B48!()) { return 1; }
973 int foo48()(C48 c) { return 2; }
975 void test48()
977 auto i = foo48(new B48!());
978 assert(i == 1);
980 i = foo48(new C48);
981 assert(i == 2);
984 /************************************************/
986 void test49()
988 struct A { int v; }
990 A a = A(10);
992 version (all)
994 writefln("Before test 1: ", a.v);
995 if (a == a.init) { writeln(a.v,"(a==a.init)"); assert(0); }
996 else { writeln(a.v,"(a!=a.init)"); assert(a.v == 10); }
998 else
1000 writefln("Before test 1: ", a.v);
1001 if (a == a.init) { writeln(a.v,"(a==a.init)"); assert(a.v == 10); }
1002 else { writeln(a.v,"(a!=a.init)"); assert(0); }
1005 a.v = 100;
1006 writefln("Before test 2: ", a.v);
1007 if (a == a.init) { writeln(a.v,"(a==a.init)"); assert(0); }
1008 else { writeln(a.v,"(a!=a.init)"); assert(a.v == 100); }
1010 a = A(1000);
1011 writefln("Before test 3: ", a.v);
1012 if (a == a.init) { writeln(a.v,"(a==a.init)"); assert(0); }
1013 else { writeln(a.v,"(a!=a.init)"); assert(a.v == 1000); }
1015 version (all)
1016 assert(a.init.v == 0);
1017 else
1018 assert(a.init.v == 10);
1021 /************************************************/
1023 struct S51
1025 int i = 3;
1026 void div() { assert(i == 3); }
1029 void test51()
1031 S51().div();
1034 /************************************************/
1036 void test52()
1038 struct Foo {
1039 alias int Y;
1041 with (Foo) {
1042 Y y;
1046 /************************************************/
1048 struct TestStruct
1050 int dummy0 = 0;
1051 int dummy1 = 1;
1052 int dummy2 = 2;
1055 void func53(TestStruct[2] testarg)
1057 writeln(testarg[0].dummy0);
1058 writeln(testarg[0].dummy1);
1059 writeln(testarg[0].dummy2);
1061 writeln(testarg[1].dummy0);
1062 writeln(testarg[1].dummy1);
1063 writeln(testarg[1].dummy2);
1065 assert(testarg[0].dummy0 == 0);
1066 assert(testarg[0].dummy1 == 1);
1067 assert(testarg[0].dummy2 == 2);
1069 assert(testarg[1].dummy0 == 0);
1070 assert(testarg[1].dummy1 == 1);
1071 assert(testarg[1].dummy2 == 2);
1074 TestStruct m53[2];
1076 void test53()
1078 writeln(&m53);
1079 func53(m53);
1082 /************************************************/
1084 void test54()
1086 double a = 0;
1087 double b = 1;
1088 // Internal error: ..\ztc\cg87.c 3233
1089 // a += (1? b: 1+1i)*1i;
1090 writeln(a);
1091 // assert(a == 0);
1092 // Internal error: ..\ztc\cod2.c 1680
1093 // a += (b?1:b-1i)*1i;
1094 writeln(a);
1095 // assert(a == 0);
1098 /************************************************/
1100 class B55 {}
1101 class D55 : B55 {}
1103 template foo55(S, T : S) { } // doesn't work
1105 alias foo55!(B55, D55) bar55;
1107 void test55()
1111 /************************************************/
1113 template t56() { alias Object t56; }
1114 pragma(msg, t56!().stringof);
1116 void test56()
1120 /************************************************/
1122 void test57()
1124 alias long[char[]] AA;
1126 static if (is(AA T : T[U], U : const char[]))
1128 writeln(typeid(T));
1129 writeln(typeid(U));
1131 assert(is(T == long));
1132 assert(is(U == const(char)[]));
1135 static if (is(AA A : A[B], B : int))
1137 assert(0);
1140 static if (is(int[10] W : W[V], int V))
1142 writeln(typeid(W));
1143 assert(is(W == int));
1144 writeln(V);
1145 assert(V == 10);
1148 static if (is(int[10] X : X[Y], int Y : 5))
1150 assert(0);
1154 /************************************************/
1156 static this()
1158 printf("one\n");
1161 static this()
1163 printf("two\n");
1166 static ~this()
1168 printf("~two\n");
1171 static ~this()
1173 printf("~one\n");
1177 void test59()
1181 /************************************************/
1183 class C60
1185 extern (C++) int bar60(int i, int j, int k)
1187 printf("this = %p\n", this);
1188 printf("i = %d\n", i);
1189 printf("j = %d\n", j);
1190 printf("k = %d\n", k);
1191 assert(i == 4);
1192 assert(j == 5);
1193 assert(k == 6);
1194 return 1;
1199 extern (C++)
1200 int foo60(int i, int j, int k)
1202 printf("i = %d\n", i);
1203 printf("j = %d\n", j);
1204 printf("k = %d\n", k);
1205 assert(i == 1);
1206 assert(j == 2);
1207 assert(k == 3);
1208 return 1;
1211 void test60()
1213 foo60(1, 2, 3);
1215 C60 c = new C60();
1216 c.bar60(4, 5, 6);
1219 /***************************************************/
1221 template Foo61(alias a) {}
1223 struct Bar61 {}
1224 const Bar61 bar61 = {};
1226 alias Foo61!(bar61) baz61;
1228 void test61()
1232 /************************************************/
1234 T foo62(T)(lazy T value)
1236 return value;
1239 void test62()
1241 foo62(new float[1]);
1244 /************************************************/
1246 void main()
1248 test1();
1249 test2();
1250 test3();
1251 test4();
1252 test5();
1253 test6();
1254 test7();
1255 test8();
1256 test9();
1257 test10();
1258 test11();
1259 test12();
1260 test13();
1261 test15();
1262 test16();
1263 test17();
1264 test18();
1265 test19();
1266 test20();
1267 test21();
1268 test22();
1269 test23();
1270 test24();
1271 test25();
1272 test26();
1273 test27();
1274 test28();
1275 test29();
1276 test30();
1277 test31();
1278 test32();
1279 test33();
1280 test34();
1281 test35();
1282 test36();
1283 test37();
1284 test38();
1285 test39();
1286 test40();
1287 test41();
1288 test42();
1289 test43();
1290 test44();
1291 test45();
1292 test46();
1293 test47();
1294 test48();
1295 test49();
1296 test51();
1297 test52();
1298 test53();
1299 test54();
1300 test55();
1301 test56();
1302 test57();
1303 test59();
1304 test60();
1305 test61();
1306 test62();
1308 writefln("Success");