FSF GCC merge 02/23/03
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.other / dyncast2.C
blob6a2e7382a4c575cb5e80c0d122598b2a1d202668
1 // Author: Alfred Miniarik <a8601248@unet.univie.ac.at>
2 // test of dynamic_cast
3 // runtime detecting of valid 
4 // downcasts within nonpublic 
5 // baseclasses.
7 extern "C" void abort();
8 extern "C" int printf (const char *, ...);
10 static int errors = 0;
12 void error(int i)
14   printf("Error %i\n",i);
15   errors++;
18 // 1. downcast
19 // 1.1 single inheritance case
21 struct A {virtual ~A(){};int i;};
22 struct B : A {int i;};
23 struct C : B {int i;};
24 struct CC : C {};
25 class D : C {int i;};
27 struct E : D {int i;};
28 class F : E {int i;};
30 void 
31 test01 ()
33   D d;
34   if((C*)&d != dynamic_cast<C*> ((A*)&d)) error(1);
35   if((C*)&d != dynamic_cast<C*> ((B*)&d)) error(2);
36   if((B*)&d != dynamic_cast<B*> ((A*)&d)) error(3);
38   E e;
39   if((C*)&e != dynamic_cast<C*> ((A*)&e)) error(4);
41   F f;
42   if((C*)&f != dynamic_cast<C*> ((B*)&f)) error(5);
43   if((B*)&f != dynamic_cast<B*> ((A*)&f)) error(6);
44   if((E*)&f != dynamic_cast<E*> ((D*)&f)) error(7);
45   if(dynamic_cast<E*> ((C*)&f)) error(8); //counter example
46 }               
48 // 1.2 multiple inheritance case
50 struct G : CC, F{};
51                 
52 void 
53 test02 ()
55   G g;
56   if((B*)(F*)&g != dynamic_cast<B*> ((A*)(F*)&g)) error(9);
57   if(dynamic_cast<D*> ((A*)(F*)&g)) error(10);
58   if(dynamic_cast<G*> ((B*)(F*)&g)) error(11);
61 // 2. crosscast (always fail)
63 struct I : C{};
64 struct J : F{};
65 struct K : I, J{};
66 class L : K{};
67                 
68 void 
69 test03 ()
71   L l;
72   if(dynamic_cast<J*> ((I*)&l)) error(12);
73   if(dynamic_cast<J*> ((E*)&l)) error(13);
74   if(dynamic_cast<I*> ((J*)&l)) error(14);
77 int 
78 main ()
80   test01();
81   test02();
82   test03();
83   return errors ? 1 : 0;