]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/complex.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / complex.d
1 // PERMUTE_ARGS:
2
3 import std.stdio;
4 import std.math;
5 import core.stdc.stdio;
6
7 /***************************************/
8
9 void test1()
10 {
11 creal c = 3.0 + 4.0i;
12 c = sqrt(c);
13 printf("re = %Lg, im = %Lg\n", c.re, c.im);
14 assert(c.re == 2.0);
15 assert(c.im == 1.0);
16
17 float f = sqrt(25.0f);
18 assert(f == 5.0);
19 double d = sqrt(4.0);
20 assert(d == 2.0);
21 real r = sqrt(9.0L);
22 assert(r == 3.0);
23 }
24
25 /***************************************/
26
27 ireal f2() { return 1i; }
28
29 void test2()
30 {
31 creal v = 0+0i;
32
33 v += f2();
34 assert(v == 0 + 1i);
35
36 v = v + f2();
37 assert(v == 0 + 2i);
38 }
39
40 /***************************************/
41
42 cdouble[1] a3;
43 cdouble[1] b3;
44
45 cdouble[] concat3() {
46 return a3~b3;
47 }
48
49 void test3()
50 {
51 a3[]=0.5+1.0i;
52 b3[]=0.5+3.0i;
53
54 cdouble[] arr=concat3();
55
56 assert(arr.length==2);
57 assert(arr[0]==0.5+1.0i);
58 assert(arr[1]==0.5+3.0i);
59 }
60
61 /***************************************/
62
63 creal[1] a4;
64 creal[1] b4;
65
66 creal[] concat4() {
67 return a4~b4;
68 }
69
70 void test4()
71 {
72 a4[]=0.5+1.0i;
73 b4[]=0.5+3.0i;
74
75 creal[] arr=concat4();
76
77 assert(arr.length==2);
78 assert(arr[0]==0.5+1.0i);
79 assert(arr[1]==0.5+3.0i);
80 }
81
82 /***************************************/
83
84 void test5()
85 {
86 ifloat i=1.0fi;
87 // i += 2.2;
88 // assert(i == 1i);
89 }
90
91 /***************************************/
92
93 void test6()
94 {
95 float i=1.0f;
96 // i /= 2.2fi;
97 // assert(i == 0);
98 }
99
100 /***************************************/
101
102 void test7()
103 {
104 creal x=1.0i+2.0;
105 creal[] arr;
106
107 arr = arr ~ x;
108 assert(arr.length==1);
109 assert(arr[0]==1.0i+2.0);
110
111 x=0.0i+5.0;
112 assert(arr[0]==1.0i+2.0);
113 }
114
115 /****************************************/
116
117 creal[1] a8;
118 creal[1] b8;
119
120 creal[] concat8() {
121 return a8 ~ b8;
122 }
123
124 void test8()
125 {
126 a8[]=0.5L+1.0Li;
127 b8[]=0.5L+3.0Li;
128
129 creal[] arr=concat8();
130
131 assert(arr.length==2);
132 assert(arr[0]==0.5L+1.0Li);
133 assert(arr[1]==0.5L+3.0Li);
134 }
135
136 /***************************************/
137
138 creal[1] a9;
139 creal[1] b9;
140
141 creal[] concat9() {
142 return a9~b9;
143 }
144
145 void test9()
146 {
147 a9[]=0.5L+1.0Li;
148 b9[]=0.5L+3.0Li;
149
150 creal[] arr=concat9();
151
152 assert(arr.length==2);
153 assert(arr[0]==0.5L+1.0Li);
154 assert(arr[1]==0.5L+3.0Li);
155 }
156
157
158 /***************************************/
159
160 void test10()
161 {
162 ifloat a = 1.0i;
163 assert(a.im == 1.0);
164
165 const ifloat b = 2.0i;
166 static assert(b.im == 2.0); // FAIL
167
168 }
169
170 /***************************************/
171
172 void test11()
173 {
174 real r = real.nan;
175 assert( r!=0 );
176 if (r==0) assert(0);
177
178 ireal ir = ireal.nan;
179 assert( ir!=0 );
180 assert( ir!=0i );
181 if (ir==0) assert(0);
182 if (ir==0i) assert(0);
183
184 creal cr = creal.nan;
185 assert( cr!=0 );
186 assert( cr!=0i );
187 if (cr==0) assert(0);
188 if (cr==0i) assert(0);
189
190 double d = double.nan;
191 assert( d!=0 );
192 if (d==0) assert(0);
193
194 idouble id = idouble.nan;
195 assert( id!=0 );
196 assert( id!=0i );
197 if (id==0) assert(0);
198 if (id==0i) assert(0);
199
200 cdouble cd = cdouble.nan;
201 assert( cd!=0 );
202 assert( cd!=0i );
203 if (cd==0) assert(0);
204 if (cd==0i) assert(0);
205
206 float f = float.nan;
207 assert( f!=0 );
208 if (f==0) assert(0);
209
210 ifloat ifx = ifloat.nan;
211 assert( ifx!=0 );
212 assert( ifx!=0i );
213 if (ifx==0) assert(0);
214 if (ifx==0i) assert(0);
215
216 cfloat cf = cfloat.nan;
217 assert( cf!=0 );
218 assert( cf!=0i );
219 if (cf==0) assert(0);
220 if (cf==0i) assert(0);
221 }
222
223 /***************************************/
224
225 void test12()
226 {
227 real x = 3;
228 creal a = (2 + 4i) % 3;
229 writeln(a);
230 assert(a == 2 + 1i);
231
232 creal b = (2 + 4i) % x;
233 writeln(b);
234 assert(b == a);
235 }
236
237 /***************************************/
238
239 void test13()
240 {
241 ireal a = 5i;
242 ireal b = a % 2;
243 writeln(b);
244 assert(b == 1i);
245 }
246
247 /***************************************/
248
249 cdouble inv( cdouble expr )
250 {
251 return (1.0 + 0.0i) / expr;
252 }
253
254 /***************************************/
255
256 void test14()
257 {
258 cfloat c;
259 cfloat d;
260 assert(c != d);
261
262 cdouble e;
263 cdouble f;
264 assert(e != f);
265
266 creal g;
267 creal h;
268 assert(g != h);
269 }
270
271 /***************************************/
272
273 void test7581()
274 {
275 cfloat a() { return cfloat.nan; }
276 assert(a() != 0);
277 }
278
279 /***************************************/
280
281 float f() { return 1.0f; }
282 ifloat i() { return 1.0fi; }
283
284 void test7594()
285 {
286 assert(f() + i() == 1.0f + 1.0fi);
287 }
288
289 /***************************************/
290
291 cdouble conv(cfloat a)
292 {
293 return a;
294 }
295
296 void test7593()
297 {
298 assert(conv(1.0f+1.0fi) == 1.0+1.0i);
299 }
300
301 /***************************************/
302
303 cfloat get() { return cfloat.nan; }
304
305 void test7591()
306 {
307 assert(!(get() == 0));
308 }
309
310 /***************************************/
311
312 void foo8966(cfloat x)
313 {
314 assert(x.re == 3.0f);
315 }
316
317 __gshared cfloat[] a8966;
318
319 void test8966()
320 {
321 a8966 = new cfloat[2];
322 a8966[0] = 3.0f + 1.0fi;
323 foo8966(a8966[0]);
324 }
325
326 /***************************************/
327
328 void formatTest2(cfloat s, double re, double im)
329 {
330 assert(s.re == re);
331 assert(s.im == im);
332 }
333
334 cfloat getcf()
335 {
336 return 2 + 1i;
337 }
338
339 void test10677()
340 {
341 formatTest2( getcf(), 2, 1 );
342 }
343
344 /***************************************/
345
346 void test7806()
347 {
348 for (idouble i = -2i; i <= 2i; i += .125i)
349 for (double r = -2; r <= 2; r += .0625)
350 {
351 cdouble c = r + i;
352 printf("%g %gi\n", c.re, c.im);
353 }
354 }
355
356 /***************************************/
357
358 void test7976() {
359 creal[] a = new creal[2];
360 auto b = a[0] = a[1];
361 }
362
363 /***************************************/
364
365 cfloat foo15f(ifloat re, float im)
366 {
367 return re + im;
368 }
369
370 cfloat bar15f(float re, ifloat im)
371 {
372 return re + im;
373 }
374
375 cdouble foo15(idouble re, double im)
376 {
377 return re + im;
378 }
379
380 cdouble bar15(double re, idouble im)
381 {
382 return re + im;
383 }
384
385 creal foo15r(ireal re, real im)
386 {
387 return re + im;
388 }
389
390 creal bar15r(real re, ireal im)
391 {
392 return re + im;
393 }
394
395 void test15()
396 {
397 assert(foo15f(1.0fi, 2.0f) == 2.0f + 1.0fi);
398 assert(bar15f(1.0f, 2.0fi) == 1.0f + 2.0fi);
399
400 assert(foo15(1.0i, 2.0) == 2.0 + 1.0i);
401 assert(bar15(1.0, 2.0i) == 1.0 + 2.0i);
402
403 assert(foo15r(1.0Li, 2.0L) == 2.0L + 1.0Li);
404 assert(bar15r(1.0L, 2.0Li) == 1.0L + 2.0Li);
405 }
406
407 /***************************************/
408 // https://issues.dlang.org/show_bug.cgi?id=17087
409
410 cfloat toComplex(int x) { return cast(cfloat)x; }
411
412 void test17087()
413 {
414 assert (toComplex(1) == 1.0);
415 }
416
417 /***************************************/
418 // https://issues.dlang.org/show_bug.cgi?id=17677
419
420 void test17677()
421 {
422 cfloat v2 = 0.0f + 0.0fi;
423 ulong v1 = 1;
424 auto z = v2 + v1;
425 assert(z == 1.0f);
426 }
427
428 /***************************************/
429
430 int main(char[][] args)
431 {
432
433 test1();
434 test2();
435 test3();
436 test4();
437 test5();
438 test6();
439 test7();
440 test8();
441 test9();
442 test10();
443 test11();
444 test12();
445 test13();
446 test14();
447 test7581();
448 test7594();
449 test7593();
450 test7591();
451 test8966();
452 test10677();
453 test7806();
454 test7976();
455 test15();
456 test17087();
457 test17677();
458
459 printf("Success!\n");
460 return 0;
461 }
462