]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/testtypeid.d
Add D front-end, libphobos library, and D2 testsuite.
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / testtypeid.d
1
2 import core.vararg;
3 import std.stdio;
4
5 /******************************************************/
6
7 void test2()
8 {
9 assert(typeid(int) == typeid(int));
10 assert(typeid(int) != typeid(uint));
11 }
12
13 /******************************************************/
14
15 class FOO3 { }
16
17 FOO3 foox3;
18
19 void foo3(int x, ...)
20 {
21 printf("%d arguments\n", _arguments.length);
22 for (int i = 0; i < _arguments.length; i++)
23 {
24 writeln(_arguments[i].toString());
25
26 if (_arguments[i] is typeid(int))
27 {
28 int j = va_arg!int(_argptr);
29 printf("\t%d\n", j);
30 assert(j == 2);
31 }
32 else if (_arguments[i] == typeid(long))
33 {
34 long j = va_arg!long(_argptr);
35 printf("\t%lld\n", j);
36 assert(j == 3);
37 }
38 else if (_arguments[i] is typeid(double))
39 {
40 double d = va_arg!double(_argptr);
41 printf("\t%g\n", d);
42 assert(d == 4.5);
43 }
44 else if (_arguments[i] is typeid(FOO3))
45 {
46 FOO3 f = va_arg!FOO3(_argptr);
47 printf("\t%p\n", f);
48 assert(f is foox3);
49 }
50 else
51 assert(0);
52 }
53 }
54
55 void test3()
56 {
57 FOO3 f = new FOO3();
58
59 printf("\t%p\n", f);
60 foox3 = f;
61
62 foo3(1,2,3L,4.5,f);
63 foo3(1,2,3L,4.5,f);
64 }
65
66 /******************************************************/
67
68 void test4()
69 {
70 TypeInfo ti;
71
72 ti = typeid(float[]);
73 assert(!(ti is null));
74 ti = typeid(double[]);
75 assert(!(ti is null));
76 ti = typeid(real[]);
77 assert(!(ti is null));
78
79 ti = typeid(ifloat[]);
80 assert(!(ti is null));
81 ti = typeid(idouble[]);
82 assert(!(ti is null));
83 ti = typeid(ireal[]);
84 assert(!(ti is null));
85
86 ti = typeid(cfloat[]);
87 assert(!(ti is null));
88 ti = typeid(cdouble[]);
89 assert(!(ti is null));
90 ti = typeid(creal[]);
91 assert(!(ti is null));
92
93 ti = typeid(void);
94 assert(!(ti is null));
95 ti = typeid(void[]);
96 assert(!(ti is null));
97 ti = typeid(bool[]);
98 assert(!(ti is null));
99 }
100
101 /******************************************************/
102
103 void test6()
104 {
105 TypeInfo ti = typeid(void*);
106 assert(!(ti is null));
107 assert(ti.tsize==(void*).sizeof);
108 assert(ti.toString()=="void*");
109 }
110
111 /******************************************************/
112
113 void test7()
114 {
115 TypeInfo ti = typeid(bool*);
116 assert(!(ti is null));
117 assert(ti.tsize==(bool*).sizeof);
118 assert(ti.toString()=="bool*");
119 }
120
121 /******************************************************/
122
123 void test8()
124 {
125 TypeInfo ti = typeid(byte*);
126 assert(!(ti is null));
127 assert(ti.tsize==(byte*).sizeof);
128 assert(ti.toString()=="byte*");
129 }
130
131 /******************************************************/
132
133 void test9()
134 {
135 TypeInfo ti = typeid(byte[]);
136 assert(!(ti is null));
137 assert(ti.tsize==(byte[]).sizeof);
138 assert(ti.toString()=="byte[]");
139 }
140
141 /******************************************************/
142
143 void test10()
144 {
145 TypeInfo ti = typeid(short*);
146 assert(!(ti is null));
147 assert(ti.tsize==(short*).sizeof);
148 assert(ti.toString()=="short*");
149 }
150
151 /******************************************************/
152
153 void test11()
154 {
155 TypeInfo ti = typeid(ushort*);
156 assert(!(ti is null));
157 assert(ti.tsize==(ushort*).sizeof);
158 assert(ti.toString()=="ushort*");
159 }
160
161 /******************************************************/
162
163 void test12()
164 {
165 TypeInfo ti = typeid(int*);
166 assert(!(ti is null));
167 assert(ti.tsize==(int*).sizeof);
168 assert(ti.toString()=="int*");
169 }
170
171 /******************************************************/
172
173 void test13()
174 {
175 TypeInfo ti = typeid(uint*);
176 assert(!(ti is null));
177 assert(ti.tsize==(uint*).sizeof);
178 assert(ti.toString()=="uint*");
179 }
180
181 /******************************************************/
182
183 void test14()
184 {
185 TypeInfo ti = typeid(ulong*);
186 assert(!(ti is null));
187 assert(ti.tsize==(ulong*).sizeof);
188 assert(ti.toString()=="ulong*");
189 }
190
191 /******************************************************/
192
193 void test15()
194 {
195 TypeInfo ti = typeid(long*);
196 assert(!(ti is null));
197 assert(ti.tsize==(long*).sizeof);
198 assert(ti.toString()=="long*");
199 }
200
201 /******************************************************/
202
203 void test16()
204 {
205 TypeInfo ti = typeid(float*);
206 assert(!(ti is null));
207 assert(ti.tsize==(float*).sizeof);
208 assert(ti.toString()=="float*");
209 }
210
211 /******************************************************/
212
213 void test17()
214 {
215 TypeInfo ti = typeid(ifloat*);
216 assert(!(ti is null));
217 assert(ti.tsize==(ifloat*).sizeof);
218 assert(ti.toString()=="ifloat*");
219 }
220
221 /******************************************************/
222
223 void test18()
224 {
225 TypeInfo ti = typeid(cfloat*);
226 assert(!(ti is null));
227 assert(ti.tsize==(cfloat*).sizeof);
228 assert(ti.toString()=="cfloat*");
229 }
230
231 /******************************************************/
232
233 void test19()
234 {
235 TypeInfo ti = typeid(double*);
236 assert(!(ti is null));
237 assert(ti.tsize==(double*).sizeof);
238 assert(ti.toString()=="double*");
239 }
240
241 /******************************************************/
242
243 void test20()
244 {
245 TypeInfo ti = typeid(idouble*);
246 assert(!(ti is null));
247 assert(ti.tsize==(idouble*).sizeof);
248 assert(ti.toString()=="idouble*");
249 }
250
251 /******************************************************/
252
253 void test21()
254 {
255 TypeInfo ti = typeid(cdouble*);
256 assert(!(ti is null));
257 assert(ti.tsize==(cdouble*).sizeof);
258 assert(ti.toString()=="cdouble*");
259 }
260
261 /******************************************************/
262
263 void test22()
264 {
265 TypeInfo ti = typeid(real*);
266 assert(!(ti is null));
267 assert(ti.tsize==(real*).sizeof);
268 assert(ti.toString()=="real*");
269 }
270
271 /******************************************************/
272
273 void test23()
274 {
275 TypeInfo ti = typeid(ireal*);
276 assert(!(ti is null));
277 assert(ti.tsize==(ireal*).sizeof);
278 assert(ti.toString()=="ireal*");
279 }
280
281 /******************************************************/
282
283 void test24()
284 {
285 TypeInfo ti = typeid(creal*);
286 assert(!(ti is null));
287 assert(ti.tsize==(creal*).sizeof);
288 assert(ti.toString()=="creal*");
289 }
290
291 /******************************************************/
292
293 void test25()
294 {
295 TypeInfo ti = typeid(char*);
296 assert(!(ti is null));
297 assert(ti.tsize==(char*).sizeof);
298 assert(ti.toString()=="char*");
299 }
300
301 /******************************************************/
302
303 void test26()
304 {
305 TypeInfo ti = typeid(wchar*);
306 assert(!(ti is null));
307 assert(ti.tsize==(wchar*).sizeof);
308 assert(ti.toString()=="wchar*");
309 }
310
311 /******************************************************/
312
313 void test27()
314 {
315 TypeInfo ti = typeid(dchar*);
316 assert(!(ti is null));
317 assert(ti.tsize==(dchar*).sizeof);
318 assert(ti.toString()=="dchar*");
319 }
320
321 /******************************************************/
322
323 enum MyEnum { A, B }
324
325 void test28()
326 {
327 TypeInfo ti = typeid(MyEnum);
328 assert(!(ti is null));
329 assert(ti.tsize==(MyEnum).sizeof);
330 assert(ti.toString()=="testtypeid.MyEnum");
331 }
332
333 /******************************************************/
334
335 void test29()
336 {
337 alias void function() func;
338 TypeInfo ti = typeid(func);
339 assert(ti !is null);
340 assert(ti.tsize == func.sizeof);
341 }
342
343 /******************************************************/
344
345 void test30()
346 {
347 alias int delegate() del;
348 TypeInfo ti = typeid(del);
349 assert(ti !is null);
350 assert(ti.tsize == del.sizeof);
351 }
352
353 /******************************************************/
354
355 void test31()
356 {
357 TypeInfo ti = typeid(void);
358 assert(!(ti is null));
359 assert(ti.tsize == void.sizeof);
360 assert(ti.toString()=="void");
361 }
362
363 /******************************************************/
364
365 class Foo32 { int x = 3; }
366 class Bar32 { long y = 4; }
367
368 void printargs(int x, ...)
369 {
370 printf("%d arguments\n", _arguments.length);
371 for (int i = 0; i < _arguments.length; i++)
372 {
373 writeln(_arguments[i].toString());
374
375 if (_arguments[i] == typeid(int))
376 {
377 int j = va_arg!int(_argptr);
378 printf("\t%d\n", j);
379 }
380 else if (_arguments[i] == typeid(long))
381 {
382 long j = va_arg!long(_argptr);
383 printf("\t%lld\n", j);
384 }
385 else if (_arguments[i] == typeid(double))
386 {
387 double d = va_arg!double(_argptr);
388 printf("\t%g\n", d);
389 }
390 else if (_arguments[i] == typeid(Foo32))
391 {
392 Foo32 f = va_arg!Foo32(_argptr);
393 assert(f.x == 3);
394 printf("\t%p\n", f);
395 }
396 else if (_arguments[i] == typeid(Bar32))
397 {
398 Bar32 b = va_arg!Bar32(_argptr);
399 assert(b.y == 4);
400 printf("\t%p\n", b);
401 }
402 else
403 assert(0);
404 }
405 }
406
407 void test32()
408 {
409 Foo32 f = new Foo32();
410 Bar32 b = new Bar32();
411
412 printf("%p\n", f);
413 printargs(1, 2, 3L, 4.5, f, b);
414 }
415
416 /******************************************************/
417
418 void test33()
419 {
420 }
421
422 /******************************************************/
423
424 void test34()
425 {
426 class C { }
427 C c;
428 auto a = typeid(C).info;
429 }
430
431 /******************************************************/
432
433 void test35()
434 {
435 auto ti = typeid(shared(int));
436
437 auto sti = cast(TypeInfo_Shared)ti;
438 assert(sti);
439
440 // allow both next and base as field names in TypeInfo_Const
441 static if (is(typeof(&sti.base) == TypeInfo*))
442 assert(sti.base == typeid(int));
443 else
444 assert(sti.next == typeid(int));
445 }
446
447 /******************************************************/
448
449 void test36()
450 {
451 int i;
452 assert(typeid(i++) == typeid(int));
453 assert(i == 1);
454 assert(typeid(i + 1) == typeid(int));
455 }
456
457 /******************************************************/
458
459 class A37 {}
460 class B37 : A37 {}
461
462 void test37()
463 {
464 auto a = new B37;
465 //writeln(typeid(A));
466 assert(typeid(a) == typeid(B37));
467 }
468
469 /******************************************************/
470
471 void test38()
472 {
473 static if (is(cent))
474 {
475 TypeInfo ti = typeid(cent*);
476 assert(!(ti is null));
477 assert(ti.tsize==(cent*).sizeof);
478 assert(ti.toString()=="cent*");
479 }
480 }
481
482 /******************************************************/
483
484 void test39()
485 {
486 static if (is(ucent))
487 {
488 TypeInfo ti = typeid(ucent*);
489 assert(!(ti is null));
490 assert(ti.tsize==(ucent*).sizeof);
491 assert(ti.toString()=="ucent*");
492 }
493 }
494
495 /******************************************************/
496
497 void test40()
498 {
499 static if (is(cent))
500 {
501 cent i;
502 assert(typeid(i++) == typeid(cent));
503 assert(i == 1);
504 assert(typeid(i + 1) == typeid(cent));
505 }
506 }
507
508 /******************************************************/
509 // 9442
510
511 class C
512 {
513 this()
514 {
515 c = this;
516 auto x = typeid(c); // NG
517 auto y = typeid(this.c); // ok
518 }
519
520 C c;
521 }
522
523 void test9442()
524 {
525 auto c = new C();
526 }
527
528 /******************************************************/
529 // 10451
530
531 struct Foo10451;
532
533 struct Bar10451
534 {
535 Foo10451*[] foos;
536 }
537
538 void test10451()
539 {
540 Foo10451*[] foos = [];
541 foos ~= null;
542 foos = new Foo10451*[2];
543 }
544
545 /******************************************************/
546 // 11010
547
548 struct S11010 { S11010* p; }
549
550 class C11010 { C11010 p; }
551 class D11010 : C11010 {}
552
553 void test11010()
554 {
555 TypeInfo ti;
556
557 S11010 s;
558 ti = typeid(s.p);
559 assert(cast(TypeInfo_Pointer)ti !is null);
560 assert(ti.toString() == "testtypeid.S11010*");
561
562 C11010 c = new C11010();
563 c.p = new D11010();
564 ti = typeid(c.p);
565 assert(cast(TypeInfo_Class)ti !is null);
566 assert(ti.toString() == "testtypeid.D11010");
567 }
568
569 /******************************************************/
570 // 13045
571
572 void test13045a()
573 {
574 static struct S
575 {
576 int[] a;
577 }
578
579 auto s1 = S([1,2]);
580 auto s2 = S([1,2]);
581 assert(s1 !is s2);
582 assert(s1 == s2);
583 assert(typeid(S).getHash(&s1) == typeid(S).getHash(&s2)); // should succeed
584 }
585
586 void test13045b()
587 {
588 bool thrown(T)(lazy T cond)
589 {
590 import core.exception;
591 try
592 cond();
593 catch (Error e)
594 return true;
595 return false;
596 }
597
598 struct S
599 {
600 size_t toHash() const nothrow @safe
601 {
602 // all getHash call should reach here
603 throw new Error("");
604 }
605 }
606 struct T
607 {
608 S s;
609 }
610 S s;
611 assert(thrown(typeid(S).getHash(&s))); // OK
612 S[1] ssa;
613 assert(thrown(typeid(S[1]).getHash(&ssa))); // OK
614 S[] sda = [S(), S()];
615 assert(thrown(typeid(S[]).getHash(&sda))); // OK
616
617 T t;
618 assert(thrown(typeid(T).getHash(&t))); // OK <- NG
619 T[1] tsa;
620 assert(thrown(typeid(T[1]).getHash(&tsa))); // OK <- NG
621 T[] tda = [T(), T()];
622 assert(thrown(typeid(T[]).getHash(&tda))); // OK <- NG
623 }
624
625 /******************************************************/
626 // 15680
627
628 void test15680()
629 {
630 auto tid = typeid(null);
631 auto iz = tid.initializer();
632 assert(iz.length == typeof(null).sizeof);
633 assert(iz.ptr is null || *(cast(void**)iz.ptr) is null);
634 }
635
636 /******************************************************/
637
638 int main()
639 {
640 test2();
641 test3();
642 test4();
643 test6();
644 test7();
645 test8();
646 test9();
647 test10();
648 test11();
649 test12();
650 test13();
651 test14();
652 test15();
653 test16();
654 test17();
655 test18();
656 test19();
657 test20();
658 test21();
659 test22();
660 test23();
661 test24();
662 test25();
663 test26();
664 test27();
665 test28();
666 test29();
667 test30();
668 test31();
669 test32();
670 test33();
671 test34();
672 test35();
673 test36();
674 test37();
675 test38();
676 test39();
677 test40();
678 test9442();
679 test10451();
680 test11010();
681 test13045a();
682 test13045b();
683 test15680();
684
685 return 0;
686 }