Debian Codegraph @

gcc/testsuite/gdc.test/runnable/testtypeid.d

raw
1
2 import core.vararg;
3 import core.stdc.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("%zd 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(void);
80 assert(!(ti is null));
81 ti = typeid(void[]);
82 assert(!(ti is null));
83 ti = typeid(bool[]);
84 assert(!(ti is null));
85 }
86
87 /******************************************************/
88
89 void test6()
90 {
91 TypeInfo ti = typeid(void*);
92 assert(!(ti is null));
93 assert(ti.tsize==(void*).sizeof);
94 assert(ti.toString()=="void*");
95 }
96
97 /******************************************************/
98
99 void test7()
100 {
101 TypeInfo ti = typeid(bool*);
102 assert(!(ti is null));
103 assert(ti.tsize==(bool*).sizeof);
104 assert(ti.toString()=="bool*");
105 }
106
107 /******************************************************/
108
109 void test8()
110 {
111 TypeInfo ti = typeid(byte*);
112 assert(!(ti is null));
113 assert(ti.tsize==(byte*).sizeof);
114 assert(ti.toString()=="byte*");
115 }
116
117 /******************************************************/
118
119 void test9()
120 {
121 TypeInfo ti = typeid(byte[]);
122 assert(!(ti is null));
123 assert(ti.tsize==(byte[]).sizeof);
124 assert(ti.toString()=="byte[]");
125 }
126
127 /******************************************************/
128
129 void test10()
130 {
131 TypeInfo ti = typeid(short*);
132 assert(!(ti is null));
133 assert(ti.tsize==(short*).sizeof);
134 assert(ti.toString()=="short*");
135 }
136
137 /******************************************************/
138
139 void test11()
140 {
141 TypeInfo ti = typeid(ushort*);
142 assert(!(ti is null));
143 assert(ti.tsize==(ushort*).sizeof);
144 assert(ti.toString()=="ushort*");
145 }
146
147 /******************************************************/
148
149 void test12()
150 {
151 TypeInfo ti = typeid(int*);
152 assert(!(ti is null));
153 assert(ti.tsize==(int*).sizeof);
154 assert(ti.toString()=="int*");
155 }
156
157 /******************************************************/
158
159 void test13()
160 {
161 TypeInfo ti = typeid(uint*);
162 assert(!(ti is null));
163 assert(ti.tsize==(uint*).sizeof);
164 assert(ti.toString()=="uint*");
165 }
166
167 /******************************************************/
168
169 void test14()
170 {
171 TypeInfo ti = typeid(ulong*);
172 assert(!(ti is null));
173 assert(ti.tsize==(ulong*).sizeof);
174 assert(ti.toString()=="ulong*");
175 }
176
177 /******************************************************/
178
179 void test15()
180 {
181 TypeInfo ti = typeid(long*);
182 assert(!(ti is null));
183 assert(ti.tsize==(long*).sizeof);
184 assert(ti.toString()=="long*");
185 }
186
187 /******************************************************/
188
189 void test16()
190 {
191 TypeInfo ti = typeid(float*);
192 assert(!(ti is null));
193 assert(ti.tsize==(float*).sizeof);
194 assert(ti.toString()=="float*");
195 }
196
197 /******************************************************/
198
199 void test19()
200 {
201 TypeInfo ti = typeid(double*);
202 assert(!(ti is null));
203 assert(ti.tsize==(double*).sizeof);
204 assert(ti.toString()=="double*");
205 }
206
207 /******************************************************/
208
209 void test22()
210 {
211 TypeInfo ti = typeid(real*);
212 assert(!(ti is null));
213 assert(ti.tsize==(real*).sizeof);
214 assert(ti.toString()=="real*");
215 }
216
217 /******************************************************/
218
219 void test25()
220 {
221 TypeInfo ti = typeid(char*);
222 assert(!(ti is null));
223 assert(ti.tsize==(char*).sizeof);
224 assert(ti.toString()=="char*");
225 }
226
227 /******************************************************/
228
229 void test26()
230 {
231 TypeInfo ti = typeid(wchar*);
232 assert(!(ti is null));
233 assert(ti.tsize==(wchar*).sizeof);
234 assert(ti.toString()=="wchar*");
235 }
236
237 /******************************************************/
238
239 void test27()
240 {
241 TypeInfo ti = typeid(dchar*);
242 assert(!(ti is null));
243 assert(ti.tsize==(dchar*).sizeof);
244 assert(ti.toString()=="dchar*");
245 }
246
247 /******************************************************/
248
249 enum MyEnum { A, B }
250
251 void test28()
252 {
253 TypeInfo ti = typeid(MyEnum);
254 assert(!(ti is null));
255 assert(ti.tsize==(MyEnum).sizeof);
256 assert(ti.toString()=="testtypeid.MyEnum");
257 }
258
259 /******************************************************/
260
261 void test29()
262 {
263 alias void function() func;
264 TypeInfo ti = typeid(func);
265 assert(ti !is null);
266 assert(ti.tsize == func.sizeof);
267 }
268
269 /******************************************************/
270
271 void test30()
272 {
273 alias int delegate() del;
274 TypeInfo ti = typeid(del);
275 assert(ti !is null);
276 assert(ti.tsize == del.sizeof);
277 }
278
279 /******************************************************/
280
281 void test31()
282 {
283 TypeInfo ti = typeid(void);
284 assert(!(ti is null));
285 assert(ti.tsize == void.sizeof);
286 assert(ti.toString()=="void");
287 }
288
289 /******************************************************/
290
291 class Foo32 { int x = 3; }
292 class Bar32 { long y = 4; }
293
294 void printargs(int x, ...)
295 {
296 printf("%zd arguments\n", _arguments.length);
297 for (int i = 0; i < _arguments.length; i++)
298 {
299 //writeln(_arguments[i].toString());
300
301 if (_arguments[i] == typeid(int))
302 {
303 int j = va_arg!int(_argptr);
304 printf("\t%d\n", j);
305 }
306 else if (_arguments[i] == typeid(long))
307 {
308 long j = va_arg!long(_argptr);
309 printf("\t%lld\n", j);
310 }
311 else if (_arguments[i] == typeid(double))
312 {
313 double d = va_arg!double(_argptr);
314 printf("\t%g\n", d);
315 }
316 else if (_arguments[i] == typeid(Foo32))
317 {
318 Foo32 f = va_arg!Foo32(_argptr);
319 assert(f.x == 3);
320 printf("\t%p\n", f);
321 }
322 else if (_arguments[i] == typeid(Bar32))
323 {
324 Bar32 b = va_arg!Bar32(_argptr);
325 assert(b.y == 4);
326 printf("\t%p\n", b);
327 }
328 else
329 assert(0);
330 }
331 }
332
333 void test32()
334 {
335 Foo32 f = new Foo32();
336 Bar32 b = new Bar32();
337
338 printf("%p\n", f);
339 printargs(1, 2, 3L, 4.5, f, b);
340 }
341
342 /******************************************************/
343
344 void test33()
345 {
346 }
347
348 /******************************************************/
349
350 void test34()
351 {
352 class C { }
353 C c;
354 auto a = typeid(C).info;
355 }
356
357 /******************************************************/
358
359 void test35()
360 {
361 auto ti = typeid(shared(int));
362
363 auto sti = cast(TypeInfo_Shared)ti;
364 assert(sti);
365
366 // allow both next and base as field names in TypeInfo_Const
367 static if (is(typeof(&sti.base) == TypeInfo*))
368 assert(sti.base == typeid(int));
369 else
370 assert(sti.next == typeid(int));
371 }
372
373 /******************************************************/
374
375 void test36()
376 {
377 int i;
378 assert(typeid(i++) == typeid(int));
379 assert(i == 1);
380 assert(typeid(i + 1) == typeid(int));
381 }
382
383 /******************************************************/
384
385 class A37 {}
386 class B37 : A37 {}
387
388 void test37()
389 {
390 auto a = new B37;
391 //writeln(typeid(A));
392 assert(typeid(a) == typeid(B37));
393 }
394
395 /******************************************************/
396
397 void test38()
398 {
399 static if (is(cent))
400 {
401 TypeInfo ti = typeid(cent*);
402 assert(!(ti is null));
403 assert(ti.tsize==(cent*).sizeof);
404 assert(ti.toString()=="cent*");
405 }
406 }
407
408 /******************************************************/
409
410 void test39()
411 {
412 static if (is(ucent))
413 {
414 TypeInfo ti = typeid(ucent*);
415 assert(!(ti is null));
416 assert(ti.tsize==(ucent*).sizeof);
417 assert(ti.toString()=="ucent*");
418 }
419 }
420
421 /******************************************************/
422
423 void test40()
424 {
425 static if (is(cent))
426 {
427 cent i;
428 assert(typeid(i++) == typeid(cent));
429 assert(i == 1);
430 assert(typeid(i + 1) == typeid(cent));
431 }
432 }
433
434 /******************************************************/
435 // https://issues.dlang.org/show_bug.cgi?id=9442
436
437 class C
438 {
439 this()
440 {
441 c = this;
442 auto x = typeid(c); // NG
443 auto y = typeid(this.c); // ok
444 }
445
446 C c;
447 }
448
449 void test9442()
450 {
451 auto c = new C();
452 }
453
454 /******************************************************/
455 // https://issues.dlang.org/show_bug.cgi?id=10451
456
457 struct Foo10451;
458
459 struct Bar10451
460 {
461 Foo10451*[] foos;
462 }
463
464 void test10451()
465 {
466 Foo10451*[] foos = [];
467 foos ~= null;
468 foos = new Foo10451*[2];
469 }
470
471 /******************************************************/
472 // https://issues.dlang.org/show_bug.cgi?id=11010
473
474 struct S11010 { S11010* p; }
475
476 class C11010 { C11010 p; }
477 class D11010 : C11010 {}
478
479 void test11010()
480 {
481 TypeInfo ti;
482
483 S11010 s;
484 ti = typeid(s.p);
485 assert(cast(TypeInfo_Pointer)ti !is null);
486 assert(ti.toString() == "testtypeid.S11010*");
487
488 C11010 c = new C11010();
489 c.p = new D11010();
490 ti = typeid(c.p);
491 assert(cast(TypeInfo_Class)ti !is null);
492 assert(ti.toString() == "testtypeid.D11010");
493 }
494
495 /******************************************************/
496 // https://issues.dlang.org/show_bug.cgi?id=13045
497
498 void test13045a()
499 {
500 static struct S
501 {
502 int[] a;
503 }
504
505 auto s1 = S([1,2]);
506 auto s2 = S([1,2]);
507 assert(s1 !is s2);
508 assert(s1 == s2);
509 assert(typeid(S).getHash(&s1) == typeid(S).getHash(&s2)); // should succeed
510 }
511
512 void test13045b()
513 {
514 bool thrown(T)(lazy T cond)
515 {
516 import core.exception;
517 try
518 cond();
519 catch (Error e)
520 return true;
521 return false;
522 }
523
524 struct S
525 {
526 size_t toHash() const nothrow @safe
527 {
528 // all getHash call should reach here
529 throw new Error("");
530 }
531 }
532 struct T
533 {
534 S s;
535 }
536 S s;
537 assert(thrown(typeid(S).getHash(&s))); // OK
538 S[1] ssa;
539 assert(thrown(typeid(S[1]).getHash(&ssa))); // OK
540 S[] sda = [S(), S()];
541 assert(thrown(typeid(S[]).getHash(&sda))); // OK
542
543 T t;
544 assert(thrown(typeid(T).getHash(&t))); // OK <- NG
545 T[1] tsa;
546 assert(thrown(typeid(T[1]).getHash(&tsa))); // OK <- NG
547 T[] tda = [T(), T()];
548 assert(thrown(typeid(T[]).getHash(&tda))); // OK <- NG
549 }
550
551 /******************************************************/
552 // https://issues.dlang.org/show_bug.cgi?id=15680
553
554 void test15680()
555 {
556 auto tid = typeid(null);
557 auto iz = tid.initializer();
558 assert(iz.length == typeof(null).sizeof);
559 assert(iz.ptr is null || *(cast(void**)iz.ptr) is null);
560 }
561
562 /******************************************************/
563
564 int main()
565 {
566 test2();
567 test3();
568 test4();
569 test6();
570 test7();
571 test8();
572 test9();
573 test10();
574 test11();
575 test12();
576 test13();
577 test14();
578 test15();
579 test16();
580 test19();
581 test22();
582 test25();
583 test26();
584 test27();
585 test28();
586 test29();
587 test30();
588 test31();
589 test32();
590 test33();
591 test34();
592 test35();
593 test36();
594 test37();
595 test38();
596 test39();
597 test40();
598 test9442();
599 test10451();
600 test11010();
601 test13045a();
602 test13045b();
603 test15680();
604
605 return 0;
606 }