1 // RUNNABLE_PHOBOS_TEST
5 import core.stdc.stdarg;
9 int printf(const char*, ...);
12 int _snprintf(char*, size_t, const char*, ...);
13 alias _snprintf snprintf;
16 int snprintf(char*, size_t, const char*, ...);
19 /*************************************/
21 // http://www.digitalmars.com/d/archives/digitalmars/D/bugs/4766.html
41 x20*x21 + y10*y10 + y10*y11 + y11*y11 +
42 y11*y20 + y20*y20 + y10*y21 + y11*y21 +
47 /*************************************/
61 x20*x21 + y10*y10 + y10*y11 + y11*y11 +
62 y11*y20 + y20*y20 + y10*y21 + y11*y21 +
67 /*************************************/
81 x20*x21 + y10*y10 + y10*y11 + y11*y11 +
82 y11*y20 + y20*y20 + y10*y21 + y11*y21 +
87 /*************************************/
91 printf("main() : (-128 >= 0)=%s, (-128 <= 0)=%s\n",
92 cast(char*)(-128 >= 0 ? "true" : "false"),
93 cast(char*)(-128 <= 0 ? "true" : "false"));
95 printf("main() : (128 >= 0)=%s, (128 <= 0)=%s\n",
96 cast(char*)(128 >= 0 ? "true" : "false"),
97 cast(char*)(128 <= 0 ? "true" : "false"));
99 assert((-128 >= 0 ? "true" : "false") == "false"),
100 assert((-128 <= 0 ? "true" : "false") == "true");
101 assert((+128 >= 0 ? "true" : "false") == "true"),
102 assert((+128 <= 0 ? "true" : "false") == "false");
106 /*************************************/
108 int foo5() { assert(0); } // No return.
112 printf("foo = %d\n", foo5());
120 /*************************************/
125 printf("%Lfi %Lfi\n", a, a - .5i);
129 printf("%Lfi %Lfi\n", a, a - .5i);
133 printf("%Lf %Lf\n", b, b - .5);
137 printf("%Lf %Lf\n", b, b - .5);
141 /*************************************/
147 printf("%f + %fi\n", f.re, f.im);
152 printf("%f + %fi\n", d.re, d.im);
157 printf("%Lf + %Lfi\n", r.re, r.im);
161 /*************************************/
167 printf("%f + %fi\n", f.re, f.im);
172 printf("%f + %fi\n", d.re, d.im);
177 printf("%Lf + %Lfi\n", r.re, r.im);
181 /*************************************/
185 this(int[] params ...)
187 for (int i = 0; i < params.length; i++)
189 assert(params[i] == i + 1);
203 A9 test1 = new A9(1, 2, 3);
204 A9 test2 = new A9(1, 2, 3, 4);
206 arg[0]=1, arg[1]=2, arg[2]=3;
207 A9 test3 = new A9(arg);
216 /*************************************/
221 auto s = typeid(typeof(i)).toString;
222 printf("%.*s\n", s.length, s.ptr);
223 assert(typeid(typeof(i)) == typeid(uint));
226 /*************************************/
231 printf("xhello world!\n");
234 /*************************************/
236 void assertEqual(real* a, real* b, string file = __FILE__, size_t line = __LINE__)
238 auto x = cast(ubyte*)a;
239 auto y = cast(ubyte*)b;
241 // Only compare the 10 value bytes, the padding bytes are of undefined
243 version (X86) enum count = 10;
244 else version (X86_64) enum count = 10;
245 else enum count = real.sizeof;
246 for (size_t i = 0; i < count; i++)
250 printf("%02d: %02x %02x\n", i, x[i], y[i]);
251 import core.exception;
252 throw new AssertError(file, line);
257 void assertEqual(creal* a, creal* b, string file = __FILE__, size_t line = __LINE__)
259 assertEqual(cast(real*)a, cast(real*)b, file, line);
260 assertEqual(cast(real*)a + 1, cast(real*)b + 1, file, line);
266 creal b = real.nan + ireal.nan;
277 /*************************************/
281 creal a = creal.infinity;
282 creal b = real.infinity + ireal.infinity;
285 real c = real.infinity;
293 /*************************************/
299 b = real.nan + ireal.nan;
310 /*************************************/
331 /*************************************/
352 /*************************************/
361 void opAdd (Bar17 b) {}
371 /*************************************/
387 /*************************************/
402 /*************************************/
411 auto s = new Struct20;
415 /*************************************/
424 auto a = new C21!(1.2);
425 C21!(1.2) b = new C21!(1.2);
428 /*************************************/
432 static creal[] params = [1+0i, 3+0i, 5+0i];
434 printf("params[0] = %Lf + %Lfi\n", params[0].re, params[0].im);
435 printf("params[1] = %Lf + %Lfi\n", params[1].re, params[1].im);
436 printf("params[2] = %Lf + %Lfi\n", params[2].re, params[2].im);
438 creal[] sums = new creal[3];
441 foreach(creal d; params)
445 printf("prod = %Lf + %Lfi\n", prod.re, prod.im);
454 printf("sums[0] = %Lf + %Lfi", sums[0].re, sums[0].im);
455 assert(sums[0].re==9);
456 assert(sums[0].im==0);
457 assert(sums[1].re==35);
458 assert(sums[1].im==0);
459 assert(sums[2].re==153);
460 assert(sums[2].im==0);
463 /*************************************/
465 const int c23 = b23 * b23;
467 const int b23 = a23 * 3;
478 assert(x23.length==9);
481 /*************************************/
483 ifloat func_24_1(ifloat f, double d)
485 // f /= cast(cdouble)d;
489 ifloat func_24_2(ifloat f, double d)
491 f = cast(ifloat)(f / cast(cdouble)d);
495 float func_24_3(float f, double d)
497 // f /= cast(cdouble)d;
501 float func_24_4(float f, double d)
503 f = cast(float)(f / cast(cdouble)d);
509 ifloat f = func_24_1(10i, 8);
511 // assert(f == 1.25i);
513 f = func_24_2(10i, 8);
517 float g = func_24_3(10, 8);
519 // assert(g == 1.25);
521 g = func_24_4(10, 8);
526 /*************************************/
533 const char [] bird = "canary";
535 const int sheep = cat!(bird.length).dog;
542 /*************************************/
544 string toString26(cdouble z)
546 char[ulong.sizeof*8] buf;
548 auto len = snprintf(buf.ptr, buf.sizeof, "%f+%fi", z.re, z.im);
549 return buf[0 .. len].idup;
554 static cdouble[] A = [1+0i, 0+1i, 1+1i];
557 foreach( cdouble z; A )
560 printf("%.*s ", s.length, s.ptr);
564 for(int ii=0; ii<A.length; ii++ )
567 assert(A[0] == 1 - 1i);
568 assert(A[1] == 1 + 1i);
571 foreach( cdouble z; A )
574 printf("%.*s ", s.length, s.ptr);
579 /*************************************/
584 string s = (int*function(int ...)[]).mangleof;
585 printf("%.*s\n", s.length, s.ptr);
586 assert((int*function(int ...)[]).mangleof == "APFiXPi");
587 assert(typeof(x).mangleof == "i");
588 assert(x.mangleof == "_D6test226test27FZ1xi");
591 /*************************************/
596 X four = cast(X) (4.0i + 0.4);
599 /*************************************/
603 ulong a = 10_000_000_000_000_000,
604 b = 1_000_000_000_000_000;
606 printf("test29\n%lx\n%lx\n%lx\n", a, b, a / b);
607 assert((a / b) == 10);
610 static assert((10_000_000_000_000_000 / 1_000_000_000_000_000) == 10);
612 /*************************************/
614 template chook(int n)
619 template dog(alias f) {
620 const int dog = chook!(f.mangleof.length);
625 const int goose = dog!(pig);
629 printf("%d\n", goose);
633 /*************************************/
635 template dog31(string sheep)
637 immutable string dog31 = "daschund";
642 string duck = dog31!("bird"[1..3]);
644 assert(duck == "daschund");
647 /*************************************/
651 int active; /* Active (Yes/No) */
652 float life; /* Particle Life */
653 float fade; /* Fade Speed */
655 float r; /* Red Value */
656 float g; /* Green Value */
657 float b; /* Blue Value */
659 float x; /* X Position */
660 float y; /* Y Position */
662 float xi; /* X Direction */
663 float yi; /* Y Direction */
665 float xg; /* X Gravity */
666 float yg; /* Y Gravity */
669 particle particles[10000];
675 /*************************************/
681 int foo() { return 6; }
690 assert(f.foo!()() == 6);
692 assert(foo!()() == 6);
695 /*************************************/
697 template dog34(string duck)
704 int aardvark = dog34!("cat" ~ "pig");
706 assert(aardvark == 2);
709 /*************************************/
714 void halt() {quit = true;}
715 bool isHalted() {return quit;}
722 a.halt; // error here
725 a.isHalted; // error here
726 bool done = a.isHalted;
733 /*************************************/
737 bool q = (0.9 + 3.5L == 0.9L + 3.5L);
739 static assert(0.9 + 3.5L == 0.9L + 3.5L);
740 assert(0.9 + 3.5L == 0.9L + 3.5L);
743 /*************************************/
745 abstract class Foo37(T)
750 class Bar37 : Foo37!(int)
759 /*************************************/
772 /*************************************/
781 int* ptr = key in array;
783 assert(value == *ptr);
786 /*************************************/
799 /*************************************/
805 version(D_InlineAsm){
820 b = (b>0) ? b : (-1 * b);
822 assert(b < b.epsilon*4);
827 /*************************************/
829 const char[] tapir = "some horned animal";
831 const byte[] antelope = cast(byte []) tapir;
837 /*************************************/
841 string armadillo = "abc" ~ 'a';
842 assert(armadillo == "abca");
843 string armadillo2 = 'b' ~ "abc";
844 assert(armadillo2 == "babc");
847 /*************************************/
849 const uint baboon44 = 3;
851 const int monkey44 = 4;
853 const ape44 = monkey44 * baboon44;
860 /*************************************/
883 /*************************************/
887 private T i; // or protected or package
892 C46!(int) c = new C46!(int); // class t4.C46!(int).C46 member i is not accessible
896 /*************************************/
910 /*************************************/
919 /*************************************/
921 real poly_asm(real x, real[] A)
924 assert(A.length > 0);
928 version (D_InlineAsm_X86)
932 asm // assembler by W. Bright
934 // EDX = (A.length - 1) * real.sizeof
935 mov ECX,A[EBP] ; // ECX = A.length
937 lea EDX,[ECX][ECX*8] ;
942 fld real ptr [EDX] ; // ST0 = coeff[ECX]
944 fld x[EBP] ; // ST0 = x
945 fxch ST(1) ; // ST1 = x, ST0 = r
947 L2: fmul ST,ST(1) ; // r *= x
948 fld real ptr -12[EDX] ;
949 sub EDX,12 ; // deg--
953 fxch ST(1) ; // ST1 = r, ST0 = x
954 fstp ST(0) ; // dump x
962 asm // assembler by W. Bright
964 // EDX = (A.length - 1) * real.sizeof
965 mov ECX,A[EBP] ; // ECX = A.length
970 fld real ptr [EDX] ; // ST0 = coeff[ECX]
972 fld x[EBP] ; // ST0 = x
973 fxch ST(1) ; // ST1 = x, ST0 = r
975 L2: fmul ST,ST(1) ; // r *= x
976 fld real ptr -16[EDX] ;
977 sub EDX,16 ; // deg--
981 fxch ST(1) ; // ST1 = r, ST0 = x
982 fstp ST(0) ; // dump x
988 else version (FreeBSD)
990 asm // assembler by W. Bright
992 // EDX = (A.length - 1) * real.sizeof
993 mov ECX,A[EBP] ; // ECX = A.length
995 lea EDX,[ECX][ECX*8] ;
1000 fld real ptr [EDX] ; // ST0 = coeff[ECX]
1002 fld x[EBP] ; // ST0 = x
1003 fxch ST(1) ; // ST1 = x, ST0 = r
1005 L2: fmul ST,ST(1) ; // r *= x
1006 fld real ptr -12[EDX] ;
1007 sub EDX,12 ; // deg--
1011 fxch ST(1) ; // ST1 = r, ST0 = x
1012 fstp ST(0) ; // dump x
1018 else version (Solaris)
1020 asm // assembler by W. Bright
1022 // EDX = (A.length - 1) * real.sizeof
1023 mov ECX,A[EBP] ; // ECX = A.length
1025 lea EDX,[ECX][ECX*8] ;
1030 fld real ptr [EDX] ; // ST0 = coeff[ECX]
1032 fld x[EBP] ; // ST0 = x
1033 fxch ST(1) ; // ST1 = x, ST0 = r
1035 L2: fmul ST,ST(1) ; // r *= x
1036 fld real ptr -12[EDX] ;
1037 sub EDX,12 ; // deg--
1041 fxch ST(1) ; // ST1 = r, ST0 = x
1042 fstp ST(0) ; // dump x
1050 asm // assembler by W. Bright
1052 // EDX = (A.length - 1) * real.sizeof
1053 mov ECX,A[EBP] ; // ECX = A.length
1055 lea EDX,[ECX][ECX*8] ;
1058 fld real ptr [EDX] ; // ST0 = coeff[ECX]
1060 fld x[EBP] ; // ST0 = x
1061 fxch ST(1) ; // ST1 = x, ST0 = r
1063 L2: fmul ST,ST(1) ; // r *= x
1064 fld real ptr -10[EDX] ;
1065 sub EDX,10 ; // deg--
1069 fxch ST(1) ; // ST1 = r, ST0 = x
1070 fstp ST(0) ; // dump x
1079 printf("Sorry, you don't seem to have InlineAsm_X86\n");
1084 real poly_c(real x, real[] A)
1087 assert(A.length > 0);
1091 ptrdiff_t i = A.length - 1;
1104 static real pp[] = [56.1, 32.7, 6];
1107 printf("The result should be %Lf\n",(56.1L + (32.7L + 6L * x) * x));
1108 printf("The C version outputs %Lf\n", poly_c(x, pp));
1109 printf("The asm version outputs %Lf\n", poly_asm(x, pp));
1110 printf("The std.math version outputs %Lf\n", poly(x, pp));
1112 r = (56.1L + (32.7L + 6L * x) * x);
1113 assert(r == poly_c(x, pp));
1114 version (D_InlineAsm_X86)
1115 assert(r == poly_asm(x, pp));
1116 assert(r == poly(x, pp));
1119 /*************************************/
1128 /*************************************/
1132 static assert(1); // OK
1135 static assert(1); // doesn't work
1138 static assert(1); // OK
1139 const int cat49 = 3;
1146 const int a = cat49!();
1150 /*************************************/
1156 assert(typeid(typeof(x)) == typeid(int));
1164 assert(typeid(typeof(x)) == typeid(int));
1177 /*************************************/
1199 /*************************************/
1201 alias int function (int) x52;
1203 template T52(string str){
1207 static assert(T52!(x52.mangleof));
1213 /*************************************/
1215 import core.stdc.stdarg;
1217 void myfunc(int a1, ...) {
1218 va_list argument_list;
1219 TypeInfo argument_type;
1220 string sa; int ia; double da;
1221 writefln("%d variable arguments", _arguments.length);
1222 writefln("argument types %s", _arguments);
1223 va_start(argument_list, a1);
1224 for (int i = 0; i < _arguments.length; ) {
1225 if ((argument_type=_arguments[i++]) == typeid(string)) {
1226 va_arg(argument_list, sa);
1227 writefln("%d) string arg = '%s', length %d", i+1, sa.length<=20? sa : "?", sa.length);
1228 } else if (argument_type == typeid(int)) {
1229 va_arg(argument_list, ia);
1230 writefln("%d) int arg = %d", i+1, ia);
1231 } else if (argument_type == typeid(double)) {
1232 va_arg(argument_list, da);
1233 writefln("%d) double arg = %f", i+1, da);
1235 throw new Exception("invalid argument type");
1238 va_end(argument_list);
1242 myfunc(1, 2, 3, 4, 5, 6, 7, 8, "9", "10"); // Fails.
1243 myfunc(1, 2.0, 3, 4, 5, 6, 7, 8, "9", "10"); // Works OK.
1244 myfunc(1, 2, 3, 4, 5, 6, 7, "8", "9", "10"); // Works OK.
1245 myfunc(1, "2", 3, 4, 5, 6, 7, 8, "9", "10"); // Works OK.
1249 /*************************************/
1309 printf("Success\n");