1 // REQUIRED_ARGS:
2
3 import std.math: poly;
4 import core.stdc.stdarg;
5
6 extern(C)
7 {
8 int printf(const char*, ...);
version(Windows)9 version(Windows)
10 {
11 int _snprintf(char*, size_t, const char*, ...);
12 alias _snprintf snprintf;
13 }
14 else
15 int snprintf(char*, size_t, const char*, ...);
16 }
17
18 /*************************************/
19
20 // http://www.digitalmars.com/d/archives/digitalmars/D/bugs/4766.html
21 // Only with -O
22
randx()23 real randx()
24 {
25 return 1.2;
26 }
27
test1()28 void test1()
29 {
30 float x10=randx();
31 float x11=randx();
32 float x20=randx();
33 float x21=randx();
34 float y10=randx();
35 float y11=randx();
36 float y20=randx();
37 float y21=randx();
38
39 float tmp=(
40 x20*x21 + y10*y10 + y10*y11 + y11*y11 +
41 y11*y20 + y20*y20 + y10*y21 + y11*y21 +
42 y21*y21);
43 assert(tmp > 0);
44 }
45
46 /*************************************/
47
test2()48 void test2()
49 {
50 double x10=randx();
51 double x11=randx();
52 double x20=randx();
53 double x21=randx();
54 double y10=randx();
55 double y11=randx();
56 double y20=randx();
57 double y21=randx();
58
59 double tmp=(
60 x20*x21 + y10*y10 + y10*y11 + y11*y11 +
61 y11*y20 + y20*y20 + y10*y21 + y11*y21 +
62 y21*y21);
63 assert(tmp > 0);
64 }
65
66 /*************************************/
67
test3()68 void test3()
69 {
70 real x10=randx();
71 real x11=randx();
72 real x20=randx();
73 real x21=randx();
74 real y10=randx();
75 real y11=randx();
76 real y20=randx();
77 real y21=randx();
78
79 real tmp=(
80 x20*x21 + y10*y10 + y10*y11 + y11*y11 +
81 y11*y20 + y20*y20 + y10*y21 + y11*y21 +
82 y21*y21);
83 assert(tmp > 0);
84 }
85
86 /*************************************/
87
test4()88 void test4()
89 {
90 printf("main() : (-128 >= 0)=%s, (-128 <= 0)=%s\n",
91 cast(char*)(-128 >= 0 ? "true" : "false"),
92 cast(char*)(-128 <= 0 ? "true" : "false"));
93
94 printf("main() : (128 >= 0)=%s, (128 <= 0)=%s\n",
95 cast(char*)(128 >= 0 ? "true" : "false"),
96 cast(char*)(128 <= 0 ? "true" : "false"));
97
98 assert((-128 >= 0 ? "true" : "false") == "false"),
99 assert((-128 <= 0 ? "true" : "false") == "true");
100 assert((+128 >= 0 ? "true" : "false") == "true"),
101 assert((+128 <= 0 ? "true" : "false") == "false");
102 }
103
104
105 /*************************************/
106
foo5()107 int foo5() { assert(0); } // No return.
108
abc5()109 int abc5()
110 {
111 printf("foo = %d\n", foo5());
112 return 0;
113 }
114
test5()115 void test5()
116 {
117 }
118
119 /*************************************/
120
test6()121 void test6()
122 {
123 ireal a = 6.5i % 3i;
124 printf("%Lfi %Lfi\n", a, a - .5i);
125 assert(a == .5i);
126
127 a = 6.5i % 3;
128 printf("%Lfi %Lfi\n", a, a - .5i);
129 assert(a == .5i);
130
131 real b = 6.5 % 3i;
132 printf("%Lf %Lf\n", b, b - .5);
133 assert(b == .5);
134
135 b = 6.5 % 3;
136 printf("%Lf %Lf\n", b, b - .5);
137 assert(b == .5);
138 }
139
140 /*************************************/
141
test7()142 void test7()
143 {
144 cfloat f = 1+0i;
145 f %= 2fi;
146 printf("%f + %fi\n", f.re, f.im);
147 assert(f == 1 + 0i);
148
149 cdouble d = 1+0i;
150 d %= 2i;
151 printf("%f + %fi\n", d.re, d.im);
152 assert(d == 1 + 0i);
153
154 creal r = 1+0i;
155 r %= 2i;
156 printf("%Lf + %Lfi\n", r.re, r.im);
157 assert(r == 1 + 0i);
158 }
159
160 /*************************************/
161
test8()162 void test8()
163 {
164 cfloat f = 1+0i;
165 f %= 2i;
166 printf("%f + %fi\n", f.re, f.im);
167 assert(f == 1);
168
169 cdouble d = 1+0i;
170 d = d % 2i;
171 printf("%f + %fi\n", d.re, d.im);
172 assert(d == 1);
173
174 creal r = 1+0i;
175 r = r % 2i;
176 printf("%Lf + %Lfi\n", r.re, r.im);
177 assert(r == 1);
178 }
179
180 /*************************************/
181
182 class A9
183 {
this(int[]params...)184 this(int[] params ...)
185 {
186 for (int i = 0; i < params.length; i++)
187 {
188 assert(params[i] == i + 1);
189 }
190 }
191 }
192
193 class B9
194 {
this()195 this()
196 {
197 init();
198 }
199
init()200 private void init()
201 {
202 A9 test1 = new A9(1, 2, 3);
203 A9 test2 = new A9(1, 2, 3, 4);
204 int[3] arg; A9 test3 = new A9((arg[0]=1, arg[1]=2, arg[2]=3, arg));
205 }
206 }
207
test9()208 void test9()
209 {
210 B9 test2 = new B9();
211 }
212
213 /*************************************/
214
test10()215 void test10()
216 {
217 auto i = 5u;
218 auto s = typeid(typeof(i)).toString;
219 printf("%.*s\n", s.length, s.ptr);
220 assert(typeid(typeof(i)) == typeid(uint));
221 }
222
223 /*************************************/
224
test11()225 void test11()
226 {
227 printf("%d\n", 3);
228 printf("xhello world!\n");
229 }
230
231 /*************************************/
232
233 void assertEqual(real* a, real* b, string file = __FILE__, size_t line = __LINE__)
234 {
235 auto x = cast(ubyte*)a;
236 auto y = cast(ubyte*)b;
237
238 // Only compare the 10 value bytes, the padding bytes are of undefined
239 // value.
240 version (X86) enum count = 10;
241 else version (X86_64) enum count = 10;
242 else enum count = real.sizeof;
243 for (size_t i = 0; i < count; i++)
244 {
245 if (x[i] != y[i])
246 {
247 printf("%02d: %02x %02x\n", i, x[i], y[i]);
248 import core.exception;
249 throw new AssertError(file, line);
250 }
251 }
252 }
253
254 void assertEqual(creal* a, creal* b, string file = __FILE__, size_t line = __LINE__)
255 {
256 assertEqual(cast(real*)a, cast(real*)b, file, line);
257 assertEqual(cast(real*)a + 1, cast(real*)b + 1, file, line);
258 }
259
test12()260 void test12()
261 {
262 creal a = creal.nan;
263 creal b = real.nan + ireal.nan;
264 assertEqual(&a, &b);
265
266 real c= real.nan;
267 real d=a.re;
268 assertEqual(&c, &d);
269
270 d=a.im;
271 assertEqual(&c, &d);
272 }
273
274 /*************************************/
275
test13()276 void test13()
277 {
278 creal a = creal.infinity;
279 creal b = real.infinity + ireal.infinity;
280 assertEqual(&a, &b);
281
282 real c = real.infinity;
283 real d=a.re;
284 assertEqual(&c, &d);
285
286 d=a.im;
287 assertEqual(&c, &d);
288 }
289
290 /*************************************/
291
test14()292 void test14()
293 {
294 creal a = creal.nan;
295 creal b = creal.nan;
296 b = real.nan + ireal.nan;
297 assertEqual(&a, &b);
298
299 real c = real.nan;
300 real d=a.re;
301 assertEqual(&c, &d);
302
303 d=a.im;
304 assertEqual(&c, &d);
305 }
306
307 /*************************************/
308
309 ireal x15;
310
foo15()311 void foo15()
312 {
313 x15 = -x15;
314 }
315
bar15()316 void bar15()
317 {
318 return foo15();
319 }
320
test15()321 void test15()
322 {
323 x15=2i;
324 bar15();
325 assert(x15==-2i);
326 }
327
328 /*************************************/
329
330 real x16;
331
foo16()332 void foo16()
333 {
334 x16 = -x16;
335 }
336
bar16()337 void bar16()
338 {
339 return foo16();
340 }
341
test16()342 void test16()
343 {
344 x16=2;
345 bar16();
346 assert(x16==-2);
347 }
348
349 /*************************************/
350
351 class Bar17
352 {
this(...)353 this(...) {}
354 }
355
356 class Foo17
357 {
opAdd(Bar17 b)358 void opAdd (Bar17 b) {}
359 }
360
test17()361 void test17()
362 {
363 auto f = new Foo17;
364 f + new Bar17;
365 }
366
367
368 /*************************************/
369
u18(int n)370 template u18(int n)
371 {
372 static if (n==1) {
373 int a = 1;
374 } else
375 int b = 4;
376 }
377
test18()378 void test18()
379 {
380 mixin u18!(2);
381 assert(b == 4);
382 }
383
384 /*************************************/
385
386 class DP
387 {
388 private:
I(char[]p)389 void I(char[] p)
390 {
391 I(p[1..p.length]);
392 }
393 }
394
test19()395 void test19()
396 {
397 }
398
399 /*************************************/
400
401 struct Struct20
402 {
403 int i;
404 }
405
test20()406 void test20()
407 {
408 auto s = new Struct20;
409 s.i = 7;
410 }
411
412 /*************************************/
413
C21(float f)414 class C21(float f)
415 {
416 float ff = f;
417 }
418
test21()419 void test21()
420 {
421 auto a = new C21!(1.2);
422 C21!(1.2) b = new C21!(1.2);
423 }
424
425 /*************************************/
426
test22()427 void test22()
428 {
429 static creal[] params = [1+0i, 3+0i, 5+0i];
430
431 printf("params[0] = %Lf + %Lfi\n", params[0].re, params[0].im);
432 printf("params[1] = %Lf + %Lfi\n", params[1].re, params[1].im);
433 printf("params[2] = %Lf + %Lfi\n", params[2].re, params[2].im);
434
435 creal[] sums = new creal[3];
436 sums[] = 0+0i;
437
438 foreach(creal d; params)
439 {
440 creal prod = d;
441
442 printf("prod = %Lf + %Lfi\n", prod.re, prod.im);
443 for(int i; i<2; i++)
444 {
445 sums[i] += prod;
446 prod *= d;
447 }
448 sums[2] += prod;
449 }
450
451 printf("sums[0] = %Lf + %Lfi", sums[0].re, sums[0].im);
452 assert(sums[0].re==9);
453 assert(sums[0].im==0);
454 assert(sums[1].re==35);
455 assert(sums[1].im==0);
456 assert(sums[2].re==153);
457 assert(sums[2].im==0);
458 }
459
460 /*************************************/
461
462 const int c23 = b23 * b23;
463 const int a23 = 1;
464 const int b23 = a23 * 3;
465
T23(int n)466 template T23(int n)
467 {
468 int[n] x23;
469 }
470
471 mixin T23!(c23);
472
test23()473 void test23()
474 {
475 assert(x23.length==9);
476 }
477
478 /*************************************/
479
func_24_1(ifloat f,double d)480 ifloat func_24_1(ifloat f, double d)
481 {
482 // f /= cast(cdouble)d;
483 return f;
484 }
485
func_24_2(ifloat f,double d)486 ifloat func_24_2(ifloat f, double d)
487 {
488 f = cast(ifloat)(f / cast(cdouble)d);
489 return f;
490 }
491
func_24_3(float f,double d)492 float func_24_3(float f, double d)
493 {
494 // f /= cast(cdouble)d;
495 return f;
496 }
497
func_24_4(float f,double d)498 float func_24_4(float f, double d)
499 {
500 f = cast(float)(f / cast(cdouble)d);
501 return f;
502 }
503
test24()504 void test24()
505 {
506 ifloat f = func_24_1(10i, 8);
507 printf("%fi\n", f);
508 // assert(f == 1.25i);
509
510 f = func_24_2(10i, 8);
511 printf("%fi\n", f);
512 assert(f == 1.25i);
513
514 float g = func_24_3(10, 8);
515 printf("%f\n", g);
516 // assert(g == 1.25);
517
518 g = func_24_4(10, 8);
519 printf("%f\n", g);
520 assert(g == 1.25);
521 }
522
523 /*************************************/
524
cat(int n)525 template cat(int n)
526 {
527 const int dog = n;
528 }
529
530 const char [] bird = "canary";
531
532 const int sheep = cat!(bird.length).dog;
533
test25()534 void test25()
535 {
536 assert(sheep == 6);
537 }
538
539 /*************************************/
540
toString26(cdouble z)541 string toString26(cdouble z)
542 {
543 char[ulong.sizeof*8] buf;
544
545 auto len = snprintf(buf.ptr, buf.sizeof, "%f+%fi", z.re, z.im);
546 return buf[0 .. len].idup;
547 }
548
test26()549 void test26()
550 {
551 static cdouble[] A = [1+0i, 0+1i, 1+1i];
552 string s;
553
554 foreach( cdouble z; A )
555 {
556 s = toString26(z);
557 printf("%.*s ", s.length, s.ptr);
558 }
559 printf("\n");
560
561 for(int ii=0; ii<A.length; ii++ )
562 A[ii] += -1i*A[ii];
563
564 assert(A[0] == 1 - 1i);
565 assert(A[1] == 1 + 1i);
566 assert(A[2] == 2);
567
568 foreach( cdouble z; A )
569 {
570 s = toString26(z);
571 printf("%.*s ", s.length, s.ptr);
572 }
573 printf("\n");
574 }
575
576 /*************************************/
577
test27()578 void test27()
579 { int x;
580
581 string s = (int*function(int ...)[]).mangleof;
582 printf("%.*s\n", s.length, s.ptr);
583 assert((int*function(int ...)[]).mangleof == "APFiXPi");
584 assert(typeof(x).mangleof == "i");
585 assert(x.mangleof == "_D6test226test27FZ1xi");
586 }
587
588 /*************************************/
589
test28()590 void test28()
591 {
592 alias cdouble X;
593 X four = cast(X) (4.0i + 0.4);
594 }
595
596 /*************************************/
597
test29()598 void test29()
599 {
600 ulong a = 10_000_000_000_000_000,
601 b = 1_000_000_000_000_000;
602
603 printf("test29\n%lx\n%lx\n%lx\n", a, b, a / b);
604 assert((a / b) == 10);
605 }
606
607 static assert((10_000_000_000_000_000 / 1_000_000_000_000_000) == 10);
608
609 /*************************************/
610
chook(int n)611 template chook(int n)
612 {
613 const int chook = 3;
614 }
615
dog(alias f)616 template dog(alias f) {
617 const int dog = chook!(f.mangleof.length);
618 }
619
620 class pig {}
621
622 const int goose = dog!(pig);
623
test30()624 void test30()
625 {
626 printf("%d\n", goose);
627 assert(goose == 3);
628 }
629
630 /*************************************/
631
dog31(string sheep)632 template dog31(string sheep)
633 {
634 immutable string dog31 = "daschund";
635 }
636
test31()637 void test31()
638 {
639 string duck = dog31!("bird"[1..3]);
640
641 assert(duck == "daschund");
642 }
643
644 /*************************************/
645
646 struct particle
647 {
648 int active; /* Active (Yes/No) */
649 float life; /* Particle Life */
650 float fade; /* Fade Speed */
651
652 float r; /* Red Value */
653 float g; /* Green Value */
654 float b; /* Blue Value */
655
656 float x; /* X Position */
657 float y; /* Y Position */
658
659 float xi; /* X Direction */
660 float yi; /* Y Direction */
661
662 float xg; /* X Gravity */
663 float yg; /* Y Gravity */
664 }
665
666 particle particles[10000];
667
test32()668 void test32()
669 {
670 }
671
672 /*************************************/
673
674 class Foo33
675 {
foo()676 template foo()
677 {
678 int foo() { return 6; }
679 }
680 }
681
682
test33()683 void test33()
684 {
685 Foo33 f = new Foo33;
686
687 assert(f.foo!()() == 6);
688 with (f)
689 assert(foo!()() == 6);
690 }
691
692 /*************************************/
693
dog34(string duck)694 template dog34(string duck)
695 {
696 const int dog34 = 2;
697 }
698
test34()699 void test34()
700 {
701 int aardvark = dog34!("cat" ~ "pig");
702
703 assert(aardvark == 2);
704 }
705
706 /*************************************/
707
708 class A35
709 {
710 private bool quit;
halt()711 void halt() {quit = true;}
isHalted()712 bool isHalted() {return quit;}
713 }
714
test35()715 void test35()
716 {
717 auto a = new A35;
718
719 a.halt; // error here
720 a.halt();
721
722 a.isHalted; // error here
723 bool done = a.isHalted;
724 if (a.isHalted)
725 {
726 }
727 }
728
729
730 /*************************************/
731
test36()732 void test36()
733 {
734 bool q = (0.9 + 3.5L == 0.9L + 3.5L);
735 assert(q);
736 static assert(0.9 + 3.5L == 0.9L + 3.5L);
737 assert(0.9 + 3.5L == 0.9L + 3.5L);
738 }
739
740 /*************************************/
741
Foo37(T)742 abstract class Foo37(T)
743 {
744 void bar () { }
745 }
746
747 class Bar37 : Foo37!(int)
748 {
749 }
750
test37()751 void test37()
752 {
753 auto f = new Bar37;
754 }
755
756 /*************************************/
757
test38()758 void test38()
759 {
760 auto s=`hello`;
761 assert(s.length==5);
762 assert(s[0]=='h');
763 assert(s[1]=='e');
764 assert(s[2]=='l');
765 assert(s[3]=='l');
766 assert(s[4]=='o');
767 }
768
769 /*************************************/
770
test39()771 void test39()
772 {
773 int value=1;
774 string key = "eins";
775 int[char[]] array;
776
777 array[key]=value;
778 int* ptr = key in array;
779
780 assert(value == *ptr);
781 }
782
783 /*************************************/
784
test40()785 void test40()
786 {
787 auto s=r"hello";
788 assert(s.length==5);
789 assert(s[0]=='h');
790 assert(s[1]=='e');
791 assert(s[2]=='l');
792 assert(s[3]=='l');
793 assert(s[4]=='o');
794 }
795
796 /*************************************/
797
test41()798 void test41()
799 {
800 version (Windows)
801 {
802 version(D_InlineAsm){
803 double a = 1.2;
804 double b = 0.2;
805 double c = 1.4;
806
807 asm{
808 movq XMM0, a;
809 movq XMM1, b;
810 addsd XMM1, XMM0;
811 movq c, XMM1;
812 }
813
814 a += b;
815
816 b = a-c;
817 b = (b>0) ? b : (-1 * b);
818
819 assert(b < b.epsilon*4);
820 }
821 }
822 }
823
824 /*************************************/
825
826 const char[] tapir = "some horned animal";
827
828 const byte[] antelope = cast(byte []) tapir;
829
test42()830 void test42()
831 {
832 }
833
834 /*************************************/
835
test43()836 void test43()
837 {
838 string armadillo = "abc" ~ 'a';
839 assert(armadillo == "abca");
840 string armadillo2 = 'b' ~ "abc";
841 assert(armadillo2 == "babc");
842 }
843
844 /*************************************/
845
846 const uint baboon44 = 3;
847
848 const int monkey44 = 4;
849
850 const ape44 = monkey44 * baboon44;
851
test44()852 void test44()
853 {
854 assert(ape44 == 12);
855 }
856
857 /*************************************/
858
859 class A45
860 {
this()861 this()
862 {
863 b = new B();
864 b.x = 5; // illegal
865 }
866
867 class B
868 {
869 protected int x;
870 }
871
872 B b;
873 }
874
test45()875 void test45()
876 {
877 }
878
879
880 /*************************************/
881
C46(T)882 class C46(T)
883 {
884 private T i; // or protected or package
885 }
886
test46()887 void test46()
888 {
889 C46!(int) c = new C46!(int); // class t4.C46!(int).C46 member i is not accessible
890 c.i = 10;
891 }
892
893 /*************************************/
894
bug5809()895 void bug5809()
896 {
897 ushort[2] x = void;
898 x[0] = 0;
899 x[1] = 0x1234;
900 ushort *px = &x[0];
901
902 uint b = px[0];
903
904 assert(px[0] == 0);
905 }
906
907 /*************************************/
908
bug7546()909 void bug7546()
910 {
911 double p = -0.0;
912 assert(p == 0);
913 }
914
915
916 /*************************************/
917
poly_asm(real x,real[]A)918 real poly_asm(real x, real[] A)
919 in
920 {
921 assert(A.length > 0);
922 }
923 body
924 {
version(D_InlineAsm_X86)925 version (D_InlineAsm_X86)
926 {
927 version (linux)
928 {
929 asm // assembler by W. Bright
930 {
931 // EDX = (A.length - 1) * real.sizeof
932 mov ECX,A[EBP] ; // ECX = A.length
933 dec ECX ;
934 lea EDX,[ECX][ECX*8] ;
935 add EDX,ECX ;
936 add EDX,ECX ;
937 add EDX,ECX ;
938 add EDX,A+4[EBP] ;
939 fld real ptr [EDX] ; // ST0 = coeff[ECX]
940 jecxz return_ST ;
941 fld x[EBP] ; // ST0 = x
942 fxch ST(1) ; // ST1 = x, ST0 = r
943 align 4 ;
944 L2: fmul ST,ST(1) ; // r *= x
945 fld real ptr -12[EDX] ;
946 sub EDX,12 ; // deg--
947 faddp ST(1),ST ;
948 dec ECX ;
949 jne L2 ;
950 fxch ST(1) ; // ST1 = r, ST0 = x
951 fstp ST(0) ; // dump x
952 align 4 ;
953 return_ST: ;
954 ;
955 }
956 }
957 else version (OSX)
958 {
959 asm // assembler by W. Bright
960 {
961 // EDX = (A.length - 1) * real.sizeof
962 mov ECX,A[EBP] ; // ECX = A.length
963 dec ECX ;
964 lea EDX,[ECX*8] ;
965 add EDX,EDX ;
966 add EDX,A+4[EBP] ;
967 fld real ptr [EDX] ; // ST0 = coeff[ECX]
968 jecxz return_ST ;
969 fld x[EBP] ; // ST0 = x
970 fxch ST(1) ; // ST1 = x, ST0 = r
971 align 4 ;
972 L2: fmul ST,ST(1) ; // r *= x
973 fld real ptr -16[EDX] ;
974 sub EDX,16 ; // deg--
975 faddp ST(1),ST ;
976 dec ECX ;
977 jne L2 ;
978 fxch ST(1) ; // ST1 = r, ST0 = x
979 fstp ST(0) ; // dump x
980 align 4 ;
981 return_ST: ;
982 ;
983 }
984 }
985 else version (FreeBSD)
986 {
987 asm // assembler by W. Bright
988 {
989 // EDX = (A.length - 1) * real.sizeof
990 mov ECX,A[EBP] ; // ECX = A.length
991 dec ECX ;
992 lea EDX,[ECX][ECX*8] ;
993 add EDX,ECX ;
994 add EDX,ECX ;
995 add EDX,ECX ;
996 add EDX,A+4[EBP] ;
997 fld real ptr [EDX] ; // ST0 = coeff[ECX]
998 jecxz return_ST ;
999 fld x[EBP] ; // ST0 = x
1000 fxch ST(1) ; // ST1 = x, ST0 = r
1001 align 4 ;
1002 L2: fmul ST,ST(1) ; // r *= x
1003 fld real ptr -12[EDX] ;
1004 sub EDX,12 ; // deg--
1005 faddp ST(1),ST ;
1006 dec ECX ;
1007 jne L2 ;
1008 fxch ST(1) ; // ST1 = r, ST0 = x
1009 fstp ST(0) ; // dump x
1010 align 4 ;
1011 return_ST: ;
1012 ;
1013 }
1014 }
1015 else version (Solaris)
1016 {
1017 asm // assembler by W. Bright
1018 {
1019 // EDX = (A.length - 1) * real.sizeof
1020 mov ECX,A[EBP] ; // ECX = A.length
1021 dec ECX ;
1022 lea EDX,[ECX][ECX*8] ;
1023 add EDX,ECX ;
1024 add EDX,ECX ;
1025 add EDX,ECX ;
1026 add EDX,A+4[EBP] ;
1027 fld real ptr [EDX] ; // ST0 = coeff[ECX]
1028 jecxz return_ST ;
1029 fld x[EBP] ; // ST0 = x
1030 fxch ST(1) ; // ST1 = x, ST0 = r
1031 align 4 ;
1032 L2: fmul ST,ST(1) ; // r *= x
1033 fld real ptr -12[EDX] ;
1034 sub EDX,12 ; // deg--
1035 faddp ST(1),ST ;
1036 dec ECX ;
1037 jne L2 ;
1038 fxch ST(1) ; // ST1 = r, ST0 = x
1039 fstp ST(0) ; // dump x
1040 align 4 ;
1041 return_ST: ;
1042 ;
1043 }
1044 }
1045 else
1046 {
1047 asm // assembler by W. Bright
1048 {
1049 // EDX = (A.length - 1) * real.sizeof
1050 mov ECX,A[EBP] ; // ECX = A.length
1051 dec ECX ;
1052 lea EDX,[ECX][ECX*8] ;
1053 add EDX,ECX ;
1054 add EDX,A+4[EBP] ;
1055 fld real ptr [EDX] ; // ST0 = coeff[ECX]
1056 jecxz return_ST ;
1057 fld x[EBP] ; // ST0 = x
1058 fxch ST(1) ; // ST1 = x, ST0 = r
1059 align 4 ;
1060 L2: fmul ST,ST(1) ; // r *= x
1061 fld real ptr -10[EDX] ;
1062 sub EDX,10 ; // deg--
1063 faddp ST(1),ST ;
1064 dec ECX ;
1065 jne L2 ;
1066 fxch ST(1) ; // ST1 = r, ST0 = x
1067 fstp ST(0) ; // dump x
1068 align 4 ;
1069 return_ST: ;
1070 ;
1071 }
1072 }
1073 }
1074 else
1075 {
1076 printf("Sorry, you don't seem to have InlineAsm_X86\n");
1077 return 0;
1078 }
1079 }
1080
poly_c(real x,real[]A)1081 real poly_c(real x, real[] A)
1082 in
1083 {
1084 assert(A.length > 0);
1085 }
1086 body
1087 {
1088 ptrdiff_t i = A.length - 1;
1089 real r = A[i];
1090 while (--i >= 0)
1091 {
1092 r *= x;
1093 r += A[i];
1094 }
1095 return r;
1096 }
1097
test47()1098 void test47()
1099 {
1100 real x = 3.1;
1101 static real pp[] = [56.1, 32.7, 6];
1102 real r;
1103
1104 printf("The result should be %Lf\n",(56.1L + (32.7L + 6L * x) * x));
1105 printf("The C version outputs %Lf\n", poly_c(x, pp));
1106 printf("The asm version outputs %Lf\n", poly_asm(x, pp));
1107 printf("The std.math version outputs %Lf\n", poly(x, pp));
1108
1109 r = (56.1L + (32.7L + 6L * x) * x);
1110 assert(r == poly_c(x, pp));
1111 version (D_InlineAsm_X86)
1112 assert(r == poly_asm(x, pp));
1113 assert(r == poly(x, pp));
1114 }
1115
1116 /*************************************/
1117
1118 const c48 = 1uL-1;
1119
test48()1120 void test48()
1121 {
1122 assert(c48 == 0);
1123 }
1124
1125 /*************************************/
1126
cat49()1127 template cat49()
1128 {
1129 static assert(1); // OK
1130 static if (1)
1131 {
1132 static assert(1); // doesn't work
1133 static if (1)
1134 {
1135 static assert(1); // OK
1136 const int cat49 = 3;
1137 }
1138 }
1139 }
1140
test49()1141 void test49()
1142 {
1143 const int a = cat49!();
1144 assert(a == 3);
1145 }
1146
1147 /*************************************/
1148
test50()1149 void test50()
1150 {
1151 if (auto x = 1)
1152 {
1153 assert(typeid(typeof(x)) == typeid(int));
1154 assert(x == 1);
1155 }
1156 else
1157 assert(0);
1158
1159 if (int x = 1)
1160 {
1161 assert(typeid(typeof(x)) == typeid(int));
1162 assert(x == 1);
1163 }
1164 else
1165 assert(0);
1166
1167 if (1)
1168 {
1169 }
1170 else
1171 assert(0);
1172 }
1173
1174 /*************************************/
1175
test51()1176 void test51()
1177 {
1178 bool b;
1179 assert(b == false);
1180 b &= 1;
1181 assert(b == false);
1182 b |= 1;
1183 assert(b == true);
1184 b ^= 1;
1185 assert(b == false);
1186 b = b | true;
1187 assert(b == true);
1188 b = b & false;
1189 assert(b == false);
1190 b = b ^ true;
1191 assert(b == true);
1192 b = !b;
1193 assert(b == false);
1194 }
1195
1196 /*************************************/
1197
1198 alias int function (int) x52;
1199
T52(string str)1200 template T52(string str){
1201 const int T52 = 1;
1202 }
1203
1204 static assert(T52!(x52.mangleof));
1205
test52()1206 void test52()
1207 {
1208 }
1209
1210 /*************************************/
1211 import std.stdio;
1212 import core.stdc.stdarg;
1213
myfunc(int a1,...)1214 void myfunc(int a1, ...) {
1215 va_list argument_list;
1216 TypeInfo argument_type;
1217 string sa; int ia; double da;
1218 writefln("%d variable arguments", _arguments.length);
1219 writefln("argument types %s", _arguments);
1220 va_start(argument_list, a1);
1221 for (int i = 0; i < _arguments.length; ) {
1222 if ((argument_type=_arguments[i++]) == typeid(string)) {
1223 va_arg(argument_list, sa);
1224 writefln("%d) string arg = '%s', length %d", i+1, sa.length<=20? sa : "?", sa.length);
1225 } else if (argument_type == typeid(int)) {
1226 va_arg(argument_list, ia);
1227 writefln("%d) int arg = %d", i+1, ia);
1228 } else if (argument_type == typeid(double)) {
1229 va_arg(argument_list, da);
1230 writefln("%d) double arg = %f", i+1, da);
1231 } else {
1232 throw new Exception("invalid argument type");
1233 }
1234 }
1235 va_end(argument_list);
1236 }
1237
test6758()1238 void test6758() {
1239 myfunc(1, 2, 3, 4, 5, 6, 7, 8, "9", "10"); // Fails.
1240 myfunc(1, 2.0, 3, 4, 5, 6, 7, 8, "9", "10"); // Works OK.
1241 myfunc(1, 2, 3, 4, 5, 6, 7, "8", "9", "10"); // Works OK.
1242 myfunc(1, "2", 3, 4, 5, 6, 7, 8, "9", "10"); // Works OK.
1243 }
1244
1245
1246 /*************************************/
1247
main()1248 int main()
1249 {
1250 test1();
1251 test2();
1252 test3();
1253 test4();
1254 test5();
1255 test6();
1256 test7();
1257 test8();
1258 test9();
1259 test10();
1260 test11();
1261 test12();
1262 test13();
1263 test14();
1264 test15();
1265 test16();
1266 test17();
1267 test18();
1268 test19();
1269 test20();
1270 test21();
1271 test22();
1272 test23();
1273 test24();
1274 test25();
1275 test26();
1276 test27();
1277 test28();
1278 test29();
1279 test30();
1280 test31();
1281 test32();
1282 test33();
1283 test34();
1284 test35();
1285 test36();
1286 test37();
1287 test38();
1288 test39();
1289 test40();
1290 test41();
1291 test42();
1292 test43();
1293 test44();
1294 test45();
1295 test46();
1296 bug5809();
1297 bug7546();
1298 test47();
1299 test48();
1300 test49();
1301 test50();
1302 test51();
1303 test52();
1304 test6758();
1305
1306 printf("Success\n");
1307 return 0;
1308 }
1309