1 /*
2 REQUIRED_ARGS: -d
3 TEST_OUTPUT:
4 ---
5 ---
6 */
7
8 import core.stdc.math : isnan, signbit;
9 import core.stdc.stdio;
10
AliasSeq(T...)11 template AliasSeq(T...) { alias T AliasSeq; }
12
13 /************************************/
14
15 static assert(-(6i) == -6i);
16 static assert(-(1 + 6i) == -1 - 6i);
17
18 static assert(!3.7i == 0);
19 static assert(!0.0i == 1);
20 static assert(!(2+3.7i) == 0);
21 static assert(!(0+3.7i) == 0);
22 static assert(!(2+0.0i) == 0);
23 static assert(!(0+0.0i) == 1);
24
25 static assert(-6i + 2i == -4i);
26 static assert(6i - 1i == 5i);
27
28 static assert((3.6 + 7.2i) / (1 + 0i) == 3.6 + 7.2i);
29 static assert((3.6 + 7.2i) / (0.0 + 1i) == 7.2 - 3.6i);
30
31 static assert((7.2i < 6.2i) == 0);
32
33 static assert((7.2i == 6.2i) == 0);
34 static assert((7.2i != 6.2i) == 1);
35
36 static assert((7.2i == 7.2i) == 1);
37 static assert((7.2i != 7.2i) == 0);
38
39 static assert((5.1 is 5.1i) == 0);
40 static assert((5.1 !is 5.1i) == 1);
41
42 /***************************************/
43
f2()44 ireal f2() { return 1i; }
45
test2()46 void test2()
47 {
48 creal v = 0+0i;
49
50 v += f2();
51 assert(v == 0 + 1i);
52
53 v = v + f2();
54 assert(v == 0 + 2i);
55 }
56
57 /***************************************/
58
59 cdouble[1] a3;
60 cdouble[1] b3;
61
concat3()62 cdouble[] concat3() {
63 return a3~b3;
64 }
65
test3()66 void test3()
67 {
68 a3[]=0.5+1.0i;
69 b3[]=0.5+3.0i;
70
71 cdouble[] arr=concat3();
72
73 assert(arr.length==2);
74 assert(arr[0]==0.5+1.0i);
75 assert(arr[1]==0.5+3.0i);
76 }
77
78 /***************************************/
79
80 creal[1] a4;
81 creal[1] b4;
82
concat4()83 creal[] concat4() {
84 return a4~b4;
85 }
86
test4()87 void test4()
88 {
89 a4[]=0.5+1.0i;
90 b4[]=0.5+3.0i;
91
92 creal[] arr=concat4();
93
94 assert(arr.length==2);
95 assert(arr[0]==0.5+1.0i);
96 assert(arr[1]==0.5+3.0i);
97 }
98
99 /***************************************/
100
test5()101 void test5()
102 {
103 ifloat i=1.0fi;
104 // i += 2.2;
105 // assert(i == 1i);
106 }
107
108 /***************************************/
109
test6()110 void test6()
111 {
112 float i=1.0f;
113 // i /= 2.2fi;
114 // assert(i == 0);
115 }
116
117 /***************************************/
118
test7()119 void test7()
120 {
121 creal x=1.0i+2.0;
122 creal[] arr;
123
124 arr = arr ~ x;
125 assert(arr.length==1);
126 assert(arr[0]==1.0i+2.0);
127
128 x=0.0i+5.0;
129 assert(arr[0]==1.0i+2.0);
130 }
131
132 /****************************************/
133
134 creal[1] a8;
135 creal[1] b8;
136
concat8()137 creal[] concat8() {
138 return a8 ~ b8;
139 }
140
test8()141 void test8()
142 {
143 a8[]=0.5L+1.0Li;
144 b8[]=0.5L+3.0Li;
145
146 creal[] arr=concat8();
147
148 assert(arr.length==2);
149 assert(arr[0]==0.5L+1.0Li);
150 assert(arr[1]==0.5L+3.0Li);
151 }
152
153 /***************************************/
154
155 creal[1] a9;
156 creal[1] b9;
157
concat9()158 creal[] concat9() {
159 return a9~b9;
160 }
161
test9()162 void test9()
163 {
164 a9[]=0.5L+1.0Li;
165 b9[]=0.5L+3.0Li;
166
167 creal[] arr=concat9();
168
169 assert(arr.length==2);
170 assert(arr[0]==0.5L+1.0Li);
171 assert(arr[1]==0.5L+3.0Li);
172 }
173
174
175 /***************************************/
176
test10()177 void test10()
178 {
179 ifloat a = 1.0i;
180 assert(a.im == 1.0);
181
182 const ifloat b = 2.0i;
183 static assert(b.im == 2.0); // FAIL
184
185 }
186
187 /***************************************/
188
test11()189 void test11()
190 {
191 real r = real.nan;
192 assert( r!=0 );
193 if (r==0) assert(0);
194
195 ireal ir = ireal.nan;
196 assert( ir!=0 );
197 assert( ir!=0i );
198 if (ir==0) assert(0);
199 if (ir==0i) assert(0);
200
201 creal cr = creal.nan;
202 assert( cr!=0 );
203 assert( cr!=0i );
204 if (cr==0) assert(0);
205 if (cr==0i) assert(0);
206
207 double d = double.nan;
208 assert( d!=0 );
209 if (d==0) assert(0);
210
211 idouble id = idouble.nan;
212 assert( id!=0 );
213 assert( id!=0i );
214 if (id==0) assert(0);
215 if (id==0i) assert(0);
216
217 cdouble cd = cdouble.nan;
218 assert( cd!=0 );
219 assert( cd!=0i );
220 if (cd==0) assert(0);
221 if (cd==0i) assert(0);
222
223 float f = float.nan;
224 assert( f!=0 );
225 if (f==0) assert(0);
226
227 ifloat ifx = ifloat.nan;
228 assert( ifx!=0 );
229 assert( ifx!=0i );
230 if (ifx==0) assert(0);
231 if (ifx==0i) assert(0);
232
233 cfloat cf = cfloat.nan;
234 assert( cf!=0 );
235 assert( cf!=0i );
236 if (cf==0) assert(0);
237 if (cf==0i) assert(0);
238 }
239
240 /***************************************/
241
test12()242 void test12()
243 {
244 real x = 3;
245 creal a = (2 + 4i) % 3;
246 printf("%Lg %Lgi\n", a.re, a.im);
247 assert(a == 2 + 1i);
248
249 creal b = (2 + 4i) % x;
250 printf("%Lg %Lgi\n", b.re, b.im);
251 assert(b == a);
252 }
253
254 /***************************************/
255
test13()256 void test13()
257 {
258 ireal a = 5i;
259 ireal b = a % 2;
260 printf("%Lg %Lgi\n", b.re, b.im);
261 assert(b == 1i);
262 }
263
264 /***************************************/
265
inv(cdouble expr)266 cdouble inv( cdouble expr )
267 {
268 return (1.0 + 0.0i) / expr;
269 }
270
271 /***************************************/
272
test14()273 void test14()
274 {
275 cfloat c;
276 cfloat d;
277 assert(c != d);
278
279 cdouble e;
280 cdouble f;
281 assert(e != f);
282
283 creal g;
284 creal h;
285 assert(g != h);
286 }
287
288 /***************************************/
289
test7581()290 void test7581()
291 {
292 cfloat a() { return cfloat.nan; }
293 assert(a() != 0);
294 }
295
296 /***************************************/
297
f()298 float f() { return 1.0f; }
i()299 ifloat i() { return 1.0fi; }
300
test7594()301 void test7594()
302 {
303 assert(f() + i() == 1.0f + 1.0fi);
304 }
305
306 /***************************************/
307
conv(cfloat a)308 cdouble conv(cfloat a)
309 {
310 return a;
311 }
312
test7593()313 void test7593()
314 {
315 assert(conv(1.0f+1.0fi) == 1.0+1.0i);
316 }
317
318 /***************************************/
319
get()320 cfloat get() { return cfloat.nan; }
321
test7591()322 void test7591()
323 {
324 assert(!(get() == 0));
325 }
326
327 /***************************************/
328
foo8966(cfloat x)329 void foo8966(cfloat x)
330 {
331 assert(x.re == 3.0f);
332 }
333
334 __gshared cfloat[] a8966;
335
test8966()336 void test8966()
337 {
338 a8966 = new cfloat[2];
339 a8966[0] = 3.0f + 1.0fi;
340 foo8966(a8966[0]);
341 }
342
343 /***************************************/
344
formatTest2(cfloat s,double re,double im)345 void formatTest2(cfloat s, double re, double im)
346 {
347 assert(s.re == re);
348 assert(s.im == im);
349 }
350
getcf()351 cfloat getcf()
352 {
353 return 2 + 1i;
354 }
355
test10677()356 void test10677()
357 {
358 formatTest2( getcf(), 2, 1 );
359 }
360
361 /***************************************/
362
test7806()363 void test7806()
364 {
365 for (idouble i = -2i; i <= 2i; i += .125i)
366 for (double r = -2; r <= 2; r += .0625)
367 {
368 cdouble c = r + i;
369 printf("%g %gi\n", c.re, c.im);
370 }
371 }
372
373 /***************************************/
374
test7976()375 void test7976() {
376 creal[] a = new creal[2];
377 auto b = a[0] = a[1];
378 }
379
380 /***************************************/
381
foo15f(ifloat re,float im)382 cfloat foo15f(ifloat re, float im)
383 {
384 return re + im;
385 }
386
bar15f(float re,ifloat im)387 cfloat bar15f(float re, ifloat im)
388 {
389 return re + im;
390 }
391
foo15(idouble re,double im)392 cdouble foo15(idouble re, double im)
393 {
394 return re + im;
395 }
396
bar15(double re,idouble im)397 cdouble bar15(double re, idouble im)
398 {
399 return re + im;
400 }
401
foo15r(ireal re,real im)402 creal foo15r(ireal re, real im)
403 {
404 return re + im;
405 }
406
bar15r(real re,ireal im)407 creal bar15r(real re, ireal im)
408 {
409 return re + im;
410 }
411
test15()412 void test15()
413 {
414 assert(foo15f(1.0fi, 2.0f) == 2.0f + 1.0fi);
415 assert(bar15f(1.0f, 2.0fi) == 1.0f + 2.0fi);
416
417 assert(foo15(1.0i, 2.0) == 2.0 + 1.0i);
418 assert(bar15(1.0, 2.0i) == 1.0 + 2.0i);
419
420 assert(foo15r(1.0Li, 2.0L) == 2.0L + 1.0Li);
421 assert(bar15r(1.0L, 2.0Li) == 1.0L + 2.0Li);
422 }
423
424 /************************************/
425
test16()426 void test16()
427 {
428 real n = -0.0;
429 const real m = -0.0;
430
431 creal c = -0.0 + 3i;
432 creal d = n + 3i;
433 creal e = m + 3i;
434
435 assert(signbit(c.re) != 0);
436 assert(signbit(d.re) != 0);
437 assert(signbit(e.re) != 0);
438 }
439
440 /************************************/
441
test17()442 void test17()
443 {
444 void test(cdouble v)
445 {
446 auto x2 = cdouble(v);
447 assert(x2 == v);
448 }
449 test(1.2+3.4i);
450 }
451
452 /************************************/
453
factorial18(float n,cdouble c,string sss,string ttt)454 template factorial18(float n, cdouble c, string sss, string ttt)
455 {
456 static if (n == 1)
457 const float factorial18 = 1;
458 else
459 const float factorial18 = n * 2;
460 }
461
bar18(wstring abc,dstring def)462 template bar18(wstring abc, dstring def)
463 {
464 const int x = 3;
465 }
466
test18()467 void test18()
468 {
469 float f = factorial18!(4.25, 6.8+3i, "hello", null);
470 printf("%g\n", f);
471 assert(f == 8.5);
472 int i = bar18!("abc"w, "def"d).x;
473 printf("%d\n", i);
474 assert(i == 3);
475 }
476
477 /*****************************************/
478
test19()479 void test19()
480 {
481 float f;
482 double d;
483 real r;
484
485 if (f > ifloat.max)
486 goto Loverflow;
487 if (d > ifloat.max)
488 goto Loverflow;
489 if (r > ifloat.max)
490 goto Loverflow;
491
492 if (ifloat.max < f)
493 goto Loverflow;
494 if (ifloat.max < d)
495 goto Loverflow;
496 if (ifloat.max < r)
497 goto Loverflow;
498
499 return;
500
501 Loverflow:
502 return;
503 }
504
505 /*****************************************/
506
test20()507 void test20()
508 {
509 double d = 1;
510 cdouble cd = 1+0i;
511 assert(cd == 1.0 + 0i);
512 }
513
514 /*****************************************/
515
test21()516 void test21()
517 {
518 cdouble[] a;
519 cdouble[] b;
520 foreach(ref cdouble d; b)
521 {
522 d = -a[0];
523 for(;;){}
524 }
525 }
526
527 /*************************************/
528
test22()529 void test22()
530 {
531 static creal[] params = [1+0i, 3+0i, 5+0i];
532
533 printf("params[0] = %Lf + %Lfi\n", params[0].re, params[0].im);
534 printf("params[1] = %Lf + %Lfi\n", params[1].re, params[1].im);
535 printf("params[2] = %Lf + %Lfi\n", params[2].re, params[2].im);
536
537 creal[] sums = new creal[3];
538 sums[] = 0+0i;
539
540 foreach(creal d; params)
541 {
542 creal prod = d;
543
544 printf("prod = %Lf + %Lfi\n", prod.re, prod.im);
545 for(int i; i<2; i++)
546 {
547 sums[i] += prod;
548 prod *= d;
549 }
550 sums[2] += prod;
551 }
552
553 printf("sums[0] = %Lf + %Lfi", sums[0].re, sums[0].im);
554 assert(sums[0].re==9);
555 assert(sums[0].im==0);
556 assert(sums[1].re==35);
557 assert(sums[1].im==0);
558 assert(sums[2].re==153);
559 assert(sums[2].im==0);
560 }
561
562 /*******************************************/
563
564 cdouble y23;
565
f23(cdouble x)566 cdouble f23(cdouble x)
567 {
568 return (y23 = x);
569 }
570
test23()571 void test23()
572 {
573 f23(1.0+2.0i);
574 assert(y23 == 1.0+2.0i);
575 }
576
577 /*************************************/
578
func_24_1(ifloat f,double d)579 ifloat func_24_1(ifloat f, double d)
580 {
581 // f /= cast(cdouble)d;
582 return f;
583 }
584
func_24_2(ifloat f,double d)585 ifloat func_24_2(ifloat f, double d)
586 {
587 f = cast(ifloat)(f / cast(cdouble)d);
588 return f;
589 }
590
func_24_3(float f,double d)591 float func_24_3(float f, double d)
592 {
593 // f /= cast(cdouble)d;
594 return f;
595 }
596
func_24_4(float f,double d)597 float func_24_4(float f, double d)
598 {
599 f = cast(float)(f / cast(cdouble)d);
600 return f;
601 }
602
test24()603 void test24()
604 {
605 ifloat f = func_24_1(10i, 8);
606 printf("%fi\n", f);
607 // assert(f == 1.25i);
608
609 f = func_24_2(10i, 8);
610 printf("%fi\n", f);
611 assert(f == 1.25i);
612
613 float g = func_24_3(10, 8);
614 printf("%f\n", g);
615 // assert(g == 1.25);
616
617 g = func_24_4(10, 8);
618 printf("%f\n", g);
619 assert(g == 1.25);
620 }
621
622 /*******************************************/
623
test25()624 void test25()
625 {
626 ireal x = 4.0Li;
627 ireal y = 4.0Li;
628 ireal z = 4Li;
629 creal c = 4L + 0Li;
630 }
631
632 /*************************************/
633
toString26(cdouble z)634 string toString26(cdouble z)
635 {
636 char[ulong.sizeof*8] buf;
637
638 auto len = snprintf(buf.ptr, buf.sizeof, "%f+%fi", z.re, z.im);
639 return buf[0 .. len].idup;
640 }
641
test26()642 void test26()
643 {
644 static cdouble[] A = [1+0i, 0+1i, 1+1i];
645 string s;
646
647 foreach( cdouble z; A )
648 {
649 s = toString26(z);
650 printf("%.*s ", cast(int)s.length, s.ptr);
651 }
652 printf("\n");
653
654 for(int ii=0; ii<A.length; ii++ )
655 A[ii] += -1i*A[ii];
656
657 assert(A[0] == 1 - 1i);
658 assert(A[1] == 1 + 1i);
659 assert(A[2] == 2);
660
661 foreach( cdouble z; A )
662 {
663 s = toString26(z);
664 printf("%.*s ", cast(int)s.length, s.ptr);
665 }
666 printf("\n");
667 }
668
669 /*************************************/
670
test27()671 void test27()
672 {
673 creal z = 1. + 2.0i;
674
675 real r = z.re;
676 assert(r == 1.0);
677
678 real i = z.im;
679 assert(i == 2.0);
680
681 assert(r.im == 0.0);
682 assert(r.re == 1.0);
683
684 assert(i.re == 2.0);
685 assert(i.im == 0.0i);
686 }
687
688 /*************************************/
689
test28()690 void test28()
691 {
692 alias cdouble X;
693 X four = cast(X) (4.0i + 0.4);
694 }
695
696 /*************************************/
697
test29()698 void test29()
699 {
700 ireal a = 6.5i % 3i;
701 printf("%Lfi %Lfi\n", a, a - .5i);
702 assert(a == .5i);
703
704 a = 6.5i % 3;
705 printf("%Lfi %Lfi\n", a, a - .5i);
706 assert(a == .5i);
707
708 real b = 6.5 % 3i;
709 printf("%Lf %Lf\n", b, b - .5);
710 assert(b == .5);
711
712 b = 6.5 % 3;
713 printf("%Lf %Lf\n", b, b - .5);
714 assert(b == .5);
715 }
716
717 /*************************************/
718
test30()719 void test30()
720 {
721 cfloat f = 1+0i;
722 f %= 2fi;
723 printf("%f + %fi\n", f.re, f.im);
724 assert(f == 1 + 0i);
725
726 cdouble d = 1+0i;
727 d %= 2i;
728 printf("%f + %fi\n", d.re, d.im);
729 assert(d == 1 + 0i);
730
731 creal r = 1+0i;
732 r %= 2i;
733 printf("%Lf + %Lfi\n", r.re, r.im);
734 assert(r == 1 + 0i);
735 }
736
737 /*************************************/
738
test31()739 void test31()
740 {
741 cfloat f = 1+0i;
742 f %= 2i;
743 printf("%f + %fi\n", f.re, f.im);
744 assert(f == 1);
745
746 cdouble d = 1+0i;
747 d = d % 2i;
748 printf("%f + %fi\n", d.re, d.im);
749 assert(d == 1);
750
751 creal r = 1+0i;
752 r = r % 2i;
753 printf("%Lf + %Lfi\n", r.re, r.im);
754 assert(r == 1);
755 }
756
757 /*************************************/
758
759 void assertEqual(real* a, real* b, string file = __FILE__, size_t line = __LINE__)
760 {
761 auto x = cast(ubyte*)a;
762 auto y = cast(ubyte*)b;
763
764 // Only compare the 10 value bytes, the padding bytes are of undefined
765 // value.
766 version (X86) enum count = 10;
767 else version (X86_64) enum count = 10;
768 else enum count = real.sizeof;
769 for (size_t i = 0; i < count; i++)
770 {
771 if (x[i] != y[i])
772 {
773 printf("%02zd: %02x %02x\n", i, x[i], y[i]);
774 import core.exception;
775 throw new AssertError(file, line);
776 }
777 }
778 }
779
780 void assertEqual(creal* a, creal* b, string file = __FILE__, size_t line = __LINE__)
781 {
782 assertEqual(cast(real*)a, cast(real*)b, file, line);
783 assertEqual(cast(real*)a + 1, cast(real*)b + 1, file, line);
784 }
785
test32()786 void test32()
787 {
788 creal a = creal.nan;
789 creal b = real.nan + ireal.nan;
790 assertEqual(&a, &b);
791
792 real c= real.nan;
793 real d=a.re;
794 assertEqual(&c, &d);
795
796 d=a.im;
797 assertEqual(&c, &d);
798 }
799
800 /*************************************/
801
test33()802 void test33()
803 {
804 creal a = creal.infinity;
805 creal b = real.infinity + ireal.infinity;
806 assertEqual(&a, &b);
807
808 real c = real.infinity;
809 real d=a.re;
810 assertEqual(&c, &d);
811
812 d=a.im;
813 assertEqual(&c, &d);
814 }
815
816 /*************************************/
817
test34()818 void test34()
819 {
820 creal a = creal.nan;
821 creal b = creal.nan;
822 b = real.nan + ireal.nan;
823 assertEqual(&a, &b);
824
825 real c = real.nan;
826 real d=a.re;
827 assertEqual(&c, &d);
828
829 d=a.im;
830 assertEqual(&c, &d);
831 }
832
833 /*************************************/
834
835 ireal x35;
836
foo35()837 void foo35()
838 {
839 x35 = -x35;
840 }
841
bar35()842 void bar35()
843 {
844 return foo35();
845 }
846
test35()847 void test35()
848 {
849 x35=2i;
850 bar35();
851 assert(x35==-2i);
852 }
853
854 /*************************************/
855
test36()856 void test36()
857 {
858 ireal imag = 2.5i;
859 printf ("test of imag*imag = %Lf\n",imag*imag);
860 assert(imag * imag == -6.25);
861 }
862
863 /*************************************/
864
test37()865 void test37()
866 {
867 creal z1 = 1. - 2.0i;
868 ireal imag_part = z1.im/1i;
869 }
870
871 /***********************************/
872
test38()873 void test38()
874 {
875 ireal imag = 2.5i;
876 //printf ("test of imag*imag = %Lf\n",imag*imag);
877 real f = imag * imag;
878 assert(f == -6.25);
879 }
880
881 /***********************************/
882
test39()883 void test39()
884 {
885 creal z = 1 + 2.5i;
886 real e = z.im;
887
888 printf ("e = %Lf\n", e);
889 assert(e == 2.5);
890 }
891
892 /***********************************/
893
test40()894 void test40()
895 {
896 ifloat b = cast(ifloat)1i;
897 assert(b == 1.0i);
898
899 ifloat c = 2fi;
900 assert(c == 2.0i);
901
902 c = 0fi;
903 assert(c == 0i);
904 }
905
906 /***************************************************/
907
test41()908 void test41()
909 {
910 creal a=1.3L+9.7Li;
911 assert(a.re == 1.3L);
912 assert(a.im == 9.7L);
913 }
914
915 /***************************************************/
916
test42()917 void test42()
918 {
919 creal c = 2.7L + 0i;
920 assert(c.re==2.7L);
921 assert(c.im==0.0L);
922 }
923
924 /***********************************/
925
test43()926 void test43()
927 {
928 creal C,Cj;
929 real y1,x1;
930
931 C = x1 + y1*1i + Cj;
932 C = 1i*y1 + x1 + Cj;
933 C = Cj + 1i*y1 + x1;
934 C = y1*1i + Cj + x1;
935 C = 1i*y1 + Cj;
936 C = Cj + 1i*y1;
937 }
938
939 /***************************************************/
940
test44()941 void test44()
942 {
943 ifloat f = 1.0fi;
944 // f *= 2.0fi; // illegal but compiles
945 printf("%g\n", f);
946 // assert(f == 0i);
947 }
948
949 /******************************************************/
950
test45()951 void test45()
952 {
953 TypeInfo ti;
954
955 ti = typeid(ifloat[]);
956 assert(!(ti is null));
957 ti = typeid(idouble[]);
958 assert(!(ti is null));
959 ti = typeid(ireal[]);
960 assert(!(ti is null));
961
962 ti = typeid(cfloat[]);
963 assert(!(ti is null));
964 ti = typeid(cdouble[]);
965 assert(!(ti is null));
966 ti = typeid(creal[]);
967 assert(!(ti is null));
968 }
969
970 /******************************************************/
971
test46()972 void test46()
973 {
974 TypeInfo ti = typeid(ifloat*);
975 assert(!(ti is null));
976 assert(ti.tsize==(ifloat*).sizeof);
977 assert(ti.toString()=="ifloat*");
978 }
979
980 /******************************************************/
981
test47()982 void test47()
983 {
984 TypeInfo ti = typeid(cfloat*);
985 assert(!(ti is null));
986 assert(ti.tsize==(cfloat*).sizeof);
987 assert(ti.toString()=="cfloat*");
988 }
989
990 /******************************************************/
991
test48()992 void test48()
993 {
994 TypeInfo ti = typeid(idouble*);
995 assert(!(ti is null));
996 assert(ti.tsize==(idouble*).sizeof);
997 assert(ti.toString()=="idouble*");
998 }
999
1000 /******************************************************/
1001
test49()1002 void test49()
1003 {
1004 TypeInfo ti = typeid(cdouble*);
1005 assert(!(ti is null));
1006 assert(ti.tsize==(cdouble*).sizeof);
1007 assert(ti.toString()=="cdouble*");
1008 }
1009
1010 /***********************************/
1011
foo51(creal a)1012 void foo51(creal a)
1013 {
1014 assert(a == -8i);
1015 }
1016
test51()1017 void test51()
1018 {
1019 assert((2-2i)*(2-2i) == -8i);
1020
1021 cdouble a = (2-2i)*(2-2i);
1022 assert(a == -8i);
1023
1024 foo51((2-2i)*(2-2i));
1025 }
1026
1027 /******************************************************/
1028
test52()1029 void test52()
1030 {
1031 TypeInfo ti = typeid(ireal*);
1032 assert(!(ti is null));
1033 assert(ti.tsize==(ireal*).sizeof);
1034 assert(ti.toString()=="ireal*");
1035 }
1036
1037 /******************************************************/
1038
test53()1039 void test53()
1040 {
1041 TypeInfo ti = typeid(creal*);
1042 assert(!(ti is null));
1043 assert(ti.tsize==(creal*).sizeof);
1044 assert(ti.toString()=="creal*");
1045 }
1046
1047 /*******************************************/
1048
init(T)1049 auto init(T)(T val) { return 1; }
1050
test54()1051 void test54()
1052 {
1053 // See built-in 'init' property
1054 assert(10i .init is idouble.nan);
1055
1056 // x.init() has parens, so it runs UFCS call
1057 assert(10i .init() == 1);
1058
1059 // x.init!YYY matches templatized UFCS call.
1060 assert(10i .init!idouble() == 1);
1061 }
1062
1063 /*******************************************/
1064
1065 creal x55;
1066
foo55()1067 void foo55()
1068 {
1069 x55 = -x55;
1070 }
1071
bar55()1072 void bar55()
1073 {
1074 return foo55();
1075 }
1076
test55()1077 void test55()
1078 {
1079 x55 = 2.0L + 0.0Li;
1080 bar55();
1081 assert(x55 == -2.0L + 0.0Li);
1082 }
1083
1084 /***************************************************/
1085
Q(s...)1086 template Q(s...) { alias s q; }
1087
test56()1088 void test56()
1089 {
1090 enum complex80 = Q!( 1+1.0i ).q.stringof;
1091 }
1092
1093 /********************************************************/
1094
test57()1095 void test57()
1096 {
1097 assert(__traits(isArithmetic, ifloat) == true);
1098 assert(__traits(isArithmetic, idouble) == true);
1099 assert(__traits(isArithmetic, ireal) == true);
1100 assert(__traits(isArithmetic, cfloat) == true);
1101 assert(__traits(isArithmetic, cdouble) == true);
1102 assert(__traits(isArithmetic, creal) == true);
1103
1104 assert(__traits(isScalar, ifloat) == true);
1105 assert(__traits(isScalar, idouble) == true);
1106 assert(__traits(isScalar, ireal) == true);
1107 assert(__traits(isScalar, cfloat) == true);
1108 assert(__traits(isScalar, cdouble) == true);
1109 assert(__traits(isScalar, creal) == true);
1110
1111 assert(__traits(isFloating, ifloat) == true);
1112 assert(__traits(isFloating, idouble) == true);
1113 assert(__traits(isFloating, ireal) == true);
1114 assert(__traits(isFloating, cfloat) == true);
1115 assert(__traits(isFloating, cdouble) == true);
1116 assert(__traits(isFloating, creal) == true);
1117
1118 assert(__traits(isIntegral, ifloat) == false);
1119 assert(__traits(isIntegral, idouble) == false);
1120 assert(__traits(isIntegral, ireal) == false);
1121 assert(__traits(isIntegral, cfloat) == false);
1122 assert(__traits(isIntegral, cdouble) == false);
1123 assert(__traits(isIntegral, creal) == false);
1124
1125 assert(__traits(isUnsigned, ifloat) == false);
1126 assert(__traits(isUnsigned, idouble) == false);
1127 assert(__traits(isUnsigned, ireal) == false);
1128 assert(__traits(isUnsigned, cfloat) == false);
1129 assert(__traits(isUnsigned, cdouble) == false);
1130 assert(__traits(isUnsigned, creal) == false);
1131
1132 assert(__traits(isAssociativeArray, ifloat) == false);
1133 assert(__traits(isAssociativeArray, idouble) == false);
1134 assert(__traits(isAssociativeArray, ireal) == false);
1135 assert(__traits(isAssociativeArray, cfloat) == false);
1136 assert(__traits(isAssociativeArray, cdouble) == false);
1137 assert(__traits(isAssociativeArray, creal) == false);
1138
1139 assert(__traits(isStaticArray, ifloat) == false);
1140 assert(__traits(isStaticArray, idouble) == false);
1141 assert(__traits(isStaticArray, ireal) == false);
1142 assert(__traits(isStaticArray, cfloat) == false);
1143 assert(__traits(isStaticArray, cdouble) == false);
1144 assert(__traits(isStaticArray, creal) == false);
1145
1146 assert(__traits(isAbstractClass, ifloat) == false);
1147 assert(__traits(isAbstractClass, idouble) == false);
1148 assert(__traits(isAbstractClass, ireal) == false);
1149 assert(__traits(isAbstractClass, cfloat) == false);
1150 assert(__traits(isAbstractClass, cdouble) == false);
1151 assert(__traits(isAbstractClass, creal) == false);
1152 }
1153
1154 /*******************************************/
1155 // https://issues.dlang.org/show_bug.cgi?id=3382
1156
toreal(ireal x)1157 real toreal(ireal x){ return x.im; }
1158
test3382()1159 void test3382()
1160 {
1161 assert(1.4i.toreal() == 1.4);
1162 }
1163
1164 /***************************************************/
1165
1166 alias ireal BUG3919;
1167 alias typeof(BUG3919.init*BUG3919.init) ICE3919;
1168 alias typeof(BUG3919.init/BUG3919.init) ICE3920;
1169
1170 /***************************************************/
1171 // https://issues.dlang.org/show_bug.cgi?id=8454
1172
sqrt8454(double d)1173 double sqrt8454(double d) { return d/2; }
foo8454(cdouble m)1174 void foo8454(cdouble m) {}
1175
test8454()1176 void test8454()
1177 {
1178 foo8454(0 - sqrt8454(1.0) * 1i);
1179 }
1180
1181 /************************************/
1182 // https://issues.dlang.org/show_bug.cgi?id=9046
1183
test9046()1184 void test9046()
1185 {
1186 foreach (T; AliasSeq!(ifloat, idouble, ireal, cfloat, cdouble, creal))
1187 foreach (U; AliasSeq!(T, const T, immutable T, shared T, shared const T, inout T, shared inout T))
1188 {
1189 static assert(is(typeof(U.init) == U));
1190 }
1191 }
1192
1193 /********************************************/
1194 // https://issues.dlang.org/show_bug.cgi?id=9112
1195
test9112a()1196 void test9112a() // T() and T(v)
1197 {
1198 void test(T)(T v)
1199 {
1200 foreach (string qual; AliasSeq!("", "const ", "immutable "))
1201 {
1202 mixin("alias U = "~qual~T.stringof~";");
1203 //pragma(msg, U);
1204
1205 mixin("auto x1 = "~qual~T.stringof~"();"); // U() default construction syntax
1206 mixin("auto x2 = "~qual~T.stringof~"(v);"); // U(v)
1207 static assert(!__traits(compiles, mixin(qual~T.stringof~"(v, v)"))); // U(v, v)
1208 static assert(is(typeof(x1) == U));
1209 static assert(is(typeof(x2) == U));
1210 static if ( is(typeof(U.nan) : real)) assert( isnan(x1.re) && !isnan(x1.im), U.stringof);
1211 static if ( is(typeof(U.nan) : ireal)) assert(!isnan(x1.re) && isnan(x1.im), U.stringof);
1212 static if ( is(typeof(U.nan) : creal)) assert( isnan(x1.re) && isnan(x1.im), U.stringof);
1213 static if (!is(typeof(U.nan))) assert( x1 == U.init, U.stringof);
1214 assert(x2 == v, U.stringof);
1215 }
1216 }
1217 test!(ifloat )(1.4142i);
1218 test!(idouble)(1.4142i);
1219 test!(ireal )(1.4142i);
1220 test!(cfloat )(1.2+3.4i);
1221 test!(cdouble)(1.2+3.4i);
1222 test!(creal )(1.2+3.4i);
1223
1224 static assert(!__traits(compiles, double(3.14i)));
1225 }
1226
test9112b()1227 void test9112b() // new T(v)
1228 {
1229 void test(T)(T v)
1230 {
1231 foreach (string qual; AliasSeq!("", "const ", "immutable "))
1232 {
1233 mixin("alias U = "~qual~T.stringof~";");
1234 //pragma(msg, U);
1235
1236 mixin("auto p1 = new "~qual~T.stringof~"();"); // U() default construction syntax
1237 mixin("auto p2 = new "~qual~T.stringof~"(v);"); // U(v)
1238 static assert(!__traits(compiles, mixin("new "~qual~T.stringof~"(v, v)"))); // U(v, v)
1239 static assert(is(typeof(p1) == U*));
1240 static assert(is(typeof(p2) == U*));
1241 assert( p1 !is null);
1242 assert( p2 !is null);
1243 auto x1 = *p1;
1244 auto x2 = *p2;
1245 static if ( is(typeof(U.nan) : real)) assert( isnan(x1.re) && !isnan(x1.im), U.stringof);
1246 static if ( is(typeof(U.nan) : ireal)) assert(!isnan(x1.re) && isnan(x1.im), U.stringof);
1247 static if ( is(typeof(U.nan) : creal)) assert( isnan(x1.re) && isnan(x1.im), U.stringof);
1248 static if (!is(typeof(U.nan))) assert( x1 == U.init, U.stringof);
1249 assert(x2 == v, U.stringof);
1250 }
1251 }
1252
1253 test!(ifloat )(1.4142i);
1254 test!(idouble)(1.4142i);
1255 test!(ireal )(1.4142i);
1256 test!(cfloat )(1.2+3.4i);
1257 test!(cdouble)(1.2+3.4i);
1258 test!(creal )(1.2+3.4i);
1259
1260 static assert(!__traits(compiles, new double(3.14i)));
1261 }
1262
1263 /***************************************************/
1264 // https://issues.dlang.org/show_bug.cgi?id=10639
1265
1266 struct S1
1267 {
1268 cdouble val;
1269 }
1270
formatTest(S1 s,double re,double im)1271 void formatTest(S1 s, double re, double im)
1272 {
1273 assert(s.val.re == re);
1274 assert(s.val.im == im);
1275 }
1276
test10639()1277 void test10639()
1278 {
1279 S1 s = S1(3+2.25i);
1280 formatTest(s, 3, 2.25);
1281 }
1282
1283 /***************************************************/
1284 // https://issues.dlang.org/show_bug.cgi?id=10842
1285
Test10842(F,T)1286 template Test10842(F, T)
1287 {
1288 bool res;
1289 F from()
1290 {
1291 res = true;
1292 return F.init;
1293 }
1294 T to()
1295 {
1296 // The cast operand had incorrectly been eliminated
1297 return cast(T)from();
1298 }
1299 bool test()
1300 {
1301 res = false;
1302 to();
1303 return res;
1304 }
1305 }
1306
test10842()1307 void test10842()
1308 {
1309 foreach (From; AliasSeq!(bool, byte, ubyte, short, ushort, int, uint, long, ulong, float, double, real))
1310 {
1311 foreach (To; AliasSeq!(ifloat, idouble, ireal))
1312 {
1313 if (!Test10842!(From, To).test())
1314 assert(0);
1315 }
1316 }
1317
1318 foreach (From; AliasSeq!(ifloat, idouble, ireal))
1319 {
1320 foreach (To; AliasSeq!(/*bool*, */byte, ubyte, short, ushort, int, uint, long, ulong, float, double, real))
1321 {
1322 if (!Test10842!(From, To).test())
1323 assert(0);
1324 }
1325 }
1326 }
1327
1328 /***************************************************/
1329
test10927()1330 void test10927()
1331 {
1332 static assert( (1+2i) ^^ 3 == -11 - 2i );
1333 auto a = (1+2i) ^^ 3;
1334 }
1335
1336 /******************************************/
1337 // https://issues.dlang.org/show_bug.cgi?id=13252
1338
1339 alias TypeTuple13252(T...) = T;
1340
1341 static assert(is(typeof(TypeTuple13252!(cast(cfloat )(1 + 2i))[0]) == cfloat ));
1342 static assert(is(typeof(TypeTuple13252!(cast(cdouble)(1 + 2i))[0]) == cdouble));
1343
1344 /***************************************************/
1345 // https://issues.dlang.org/show_bug.cgi?id=14218
1346
test14218()1347 void test14218()
1348 {
1349 version (DigitalMars)
1350 {
1351 // Questionable but currently accepted by DMD (but not GDC).
1352 foreach (To; AliasSeq!(ifloat, idouble, ireal))
1353 {
1354 auto x = cast(To)null;
1355 assert(x == 0); // 0i
1356 }
1357
1358 // Internal error: backend/el.c in el_long()
1359 //foreach (To; AliasSeq!(cfloat, cdouble, creal))
1360 //{
1361 // static assert(!__traits(compiles, { auto x = cast(To)null; }));
1362 //}
1363 }
1364 }
1365
1366 /******************************************/
1367 // https://issues.dlang.org/show_bug.cgi?id=15653
1368
1369 alias TypeTuple15653(T...) = T;
1370
test15653()1371 void test15653()
1372 {
1373 void foo(U, T)(const T x) { static assert(is(T == U)); }
1374 void bar(U, T)(immutable T x) { static assert(is(T == U)); }
1375
1376 struct X { int a; long[2] b; }
1377 struct Y { int* a; long[] b; }
1378
1379 foreach (U; TypeTuple15653!(
1380 ifloat, idouble, ireal,
1381 cfloat, cdouble, creal))
1382 {
1383 foo!U(U.init); // OK
1384 bar!U(U.init); // Was error, now OK
1385
1386 U u;
1387 foo!U(u); // OK
1388 bar!U(u); // Was error, now OK
1389 }
1390 }
1391
1392 /***************************************/
1393 // https://issues.dlang.org/show_bug.cgi?id=17087
1394
toComplex(int x)1395 cfloat toComplex(int x) { return cast(cfloat)x; }
1396
test17087()1397 void test17087()
1398 {
1399 assert (toComplex(1) == 1.0);
1400 }
1401
1402 /***************************************/
1403 // https://issues.dlang.org/show_bug.cgi?id=17677
1404
test17677()1405 void test17677()
1406 {
1407 cfloat v2 = 0.0f + 0.0fi;
1408 ulong v1 = 1;
1409 auto z = v2 + v1;
1410 assert(z == 1.0f);
1411 }
1412
1413 /***************************************/
1414 // https://issues.dlang.org/show_bug.cgi?id=17677
1415
getreal_rcx(cfloat z)1416 float getreal_rcx(cfloat z)
1417 {
1418 return z.re;
1419 }
getimag_rcx(cfloat z)1420 float getimag_rcx(cfloat z)
1421 {
1422 return z.im;
1423 }
1424
getreal_rdx(cfloat z,int)1425 float getreal_rdx(cfloat z, int)
1426 {
1427 return z.re;
1428 }
getimag_rdx(cfloat z,int)1429 float getimag_rdx(cfloat z, int)
1430 {
1431 return z.im;
1432 }
1433
getreal_r8(cfloat z,int,int)1434 float getreal_r8(cfloat z, int, int)
1435 {
1436 return z.re;
1437 }
getimag_r8(cfloat z,int,int)1438 float getimag_r8(cfloat z, int, int)
1439 {
1440 return z.im;
1441 }
1442
getreal_r9(cfloat z,int,int,int)1443 float getreal_r9(cfloat z, int, int, int)
1444 {
1445 return z.re;
1446 }
getimag_r9(cfloat z,int,int,int)1447 float getimag_r9(cfloat z, int, int, int)
1448 {
1449 return z.im;
1450 }
1451
getreal_stack(cfloat z,int,int,int,int)1452 float getreal_stack(cfloat z, int, int, int, int)
1453 {
1454 return z.re;
1455 }
getimag_stack(cfloat z,int,int,int,int)1456 float getimag_stack(cfloat z, int, int, int, int)
1457 {
1458 return z.im;
1459 }
1460
test18772a()1461 void test18772a()
1462 {
1463 cfloat[1] A;
1464 float[1] B;
1465 int i = 0;
1466 A[0] = 2.0f + 4i;
1467 B[0] = 3.0f;
1468 assert(6.0 == getreal_rcx(A[i] * B[i]));
1469 assert(12.0 == getimag_rcx(A[i] * B[i]));
1470
1471 assert(6.0 == getreal_rdx(A[i] * B[i], 1));
1472 assert(12.0 == getimag_rdx(A[i] * B[i], 1));
1473
1474 assert(6.0 == getreal_r8(A[i] * B[i], 1, 2));
1475 assert(12.0 == getimag_r8(A[i] * B[i], 1, 2));
1476
1477 assert(6.0 == getreal_r9(A[i] * B[i], 1, 2, 3));
1478 assert(12.0 == getimag_r9(A[i] * B[i], 1, 2, 3));
1479
1480 assert(6.0 == getreal_stack(A[i] * B[i], 1, 2, 3, 4));
1481 assert(12.0 == getimag_stack(A[i] * B[i], 1, 2, 3, 4));
1482 }
1483
test18772b(T)1484 void test18772b(T)()
1485 {
1486 static auto getre0(T z)
1487 {
1488 return z.re;
1489 }
1490 static auto getim0(T z)
1491 {
1492 return z.im;
1493 }
1494
1495 T z = 3 + 4i;
1496 auto d = z.re;
1497
1498 assert(getre0(d * z) == d * 3);
1499 assert(getim0(d * z) == d * 4);
1500 }
1501
test18772()1502 void test18772()
1503 {
1504 test18772a();
1505
1506 test18772b!cfloat();
1507 test18772b!cdouble();
1508 test18772b!creal();
1509 }
1510
1511 /***************************************/
1512
main(char[][]args)1513 int main(char[][] args)
1514 {
1515
1516 test2();
1517 test3();
1518 test4();
1519 test5();
1520 test6();
1521 test7();
1522 test8();
1523 test9();
1524 test10();
1525 test11();
1526 test12();
1527 test13();
1528 test14();
1529 test7581();
1530 test7594();
1531 test7593();
1532 test7591();
1533 test8966();
1534 test10677();
1535 test7806();
1536 test7976();
1537 test15();
1538 test16();
1539 test17();
1540 test18();
1541 test19();
1542 test20();
1543 test21();
1544 test22();
1545 test23();
1546 test24();
1547 test25();
1548 test26();
1549 test27();
1550 test28();
1551 test29();
1552 test30();
1553 test31();
1554 test32();
1555 test33();
1556 test34();
1557 test35();
1558 test36();
1559 test37();
1560 test38();
1561 test39();
1562 test40();
1563 test41();
1564 test42();
1565 test43();
1566 test44();
1567 test45();
1568 test46();
1569 test47();
1570 test48();
1571 test49();
1572 test51();
1573 test52();
1574 test53();
1575 test54();
1576 test55();
1577 test56();
1578 test57();
1579 test8454();
1580 test9046();
1581 test9112a();
1582 test9112b();
1583 test10639();
1584 test10842();
1585 test14218();
1586 test15653();
1587 test17087();
1588 test17677();
1589 test18772();
1590
1591 printf("Success!\n");
1592 return 0;
1593 }
1594