// { dg-do run }
|
// { dg-do run }
|
// Author: Alfred Miniarik
|
// Author: Alfred Miniarik
|
// test of dynamic_cast
|
// test of dynamic_cast
|
// runtime detecting of nonpublic
|
// runtime detecting of nonpublic
|
// inheritance within a cast
|
// inheritance within a cast
|
// and therefor failing with result 0.
|
// and therefor failing with result 0.
|
|
|
extern "C" void abort();
|
extern "C" void abort();
|
extern "C" int printf (const char *, ...);
|
extern "C" int printf (const char *, ...);
|
|
|
static int errors = 0;
|
static int errors = 0;
|
void error(int i)
|
void error(int i)
|
{
|
{
|
printf("Error %i\n",i);
|
printf("Error %i\n",i);
|
errors++;
|
errors++;
|
}
|
}
|
|
|
// 1. downcast
|
// 1. downcast
|
|
|
// 1.1. single inheritance case
|
// 1.1. single inheritance case
|
|
|
struct A {virtual ~A(){}};
|
struct A {virtual ~A(){}};
|
struct AA : A {};
|
struct AA : A {};
|
struct B : A {};
|
struct B : A {};
|
struct BB : B {};
|
struct BB : B {};
|
class C : B {};
|
class C : B {};
|
struct D : C {};
|
struct D : C {};
|
|
|
struct CC : B {};
|
struct CC : B {};
|
class DD : CC {};
|
class DD : CC {};
|
|
|
class CCC : protected B {};
|
class CCC : protected B {};
|
class DDD : protected CCC {};
|
class DDD : protected CCC {};
|
|
|
void
|
void
|
test01 ()
|
test01 ()
|
{
|
{
|
D d;
|
D d;
|
if(dynamic_cast ((A*)&d)) error(1);
|
if(dynamic_cast ((A*)&d)) error(1);
|
if(dynamic_cast ((B*)&d)) error(2);
|
if(dynamic_cast ((B*)&d)) error(2);
|
if(&d != dynamic_cast ((C*)&d)) error(3); //counter example
|
if(&d != dynamic_cast ((C*)&d)) error(3); //counter example
|
if(dynamic_cast ((B*)&d)) error(4);
|
if(dynamic_cast ((B*)&d)) error(4);
|
|
|
DD dd;
|
DD dd;
|
if(dynamic_cast ((A*)&dd)) error(5);
|
if(dynamic_cast ((A*)&dd)) error(5);
|
if(dynamic_cast ((B*)&dd)) error(6);
|
if(dynamic_cast ((B*)&dd)) error(6);
|
|
|
DDD ddd;
|
DDD ddd;
|
if(dynamic_cast ((A*)&ddd)) error(7);
|
if(dynamic_cast ((A*)&ddd)) error(7);
|
if(dynamic_cast ((B*)&ddd)) error(8);
|
if(dynamic_cast ((B*)&ddd)) error(8);
|
if(dynamic_cast ((B*)&ddd)) error(9);
|
if(dynamic_cast ((B*)&ddd)) error(9);
|
}
|
}
|
|
|
// 1.2. multiple inheritance case
|
// 1.2. multiple inheritance case
|
// 1.2.1. all bases are public
|
// 1.2.1. all bases are public
|
|
|
struct E : D, CC {};
|
struct E : D, CC {};
|
struct EE : CC, D {}; //Will search in reverse order.
|
struct EE : CC, D {}; //Will search in reverse order.
|
|
|
void
|
void
|
test02 ()
|
test02 ()
|
{
|
{
|
E e;
|
E e;
|
if(dynamic_cast ((A*)(D*)&e)) error(10);
|
if(dynamic_cast ((A*)(D*)&e)) error(10);
|
if(dynamic_cast ((B*)(D*)&e)) error(11);
|
if(dynamic_cast ((B*)(D*)&e)) error(11);
|
if(&e != dynamic_cast ((C*)(D*)&e)) error(12); //counter example
|
if(&e != dynamic_cast ((C*)(D*)&e)) error(12); //counter example
|
if(&e != dynamic_cast ((B*)(CC*)&e)) error(13); //counter example
|
if(&e != dynamic_cast ((B*)(CC*)&e)) error(13); //counter example
|
if((CC*)&e != dynamic_cast ((B*)(CC*)&e)) error(14); //counter example
|
if((CC*)&e != dynamic_cast ((B*)(CC*)&e)) error(14); //counter example
|
|
|
EE ee;
|
EE ee;
|
if(dynamic_cast ((A*)(D*)&ee)) error(15);
|
if(dynamic_cast ((A*)(D*)&ee)) error(15);
|
if(dynamic_cast ((B*)(D*)&ee)) error(16);
|
if(dynamic_cast ((B*)(D*)&ee)) error(16);
|
if(&ee != dynamic_cast ((C*)(D*)&ee)) error(17); //counter example
|
if(&ee != dynamic_cast ((C*)(D*)&ee)) error(17); //counter example
|
if(&ee != dynamic_cast ((B*)(CC*)&ee)) error(18); //counter example
|
if(&ee != dynamic_cast ((B*)(CC*)&ee)) error(18); //counter example
|
if((CC*)&ee != dynamic_cast ((B*)(CC*)&ee)) error(19); //counter example
|
if((CC*)&ee != dynamic_cast ((B*)(CC*)&ee)) error(19); //counter example
|
}
|
}
|
|
|
// 1.2.2 one or more branches are nonpublic
|
// 1.2.2 one or more branches are nonpublic
|
|
|
struct X : private BB, E {};
|
struct X : private BB, E {};
|
struct Y : AA, private B {};
|
struct Y : AA, private B {};
|
|
|
class XX : BB, E {};
|
class XX : BB, E {};
|
|
|
void
|
void
|
test03 ()
|
test03 ()
|
{
|
{
|
X x;
|
X x;
|
if(&x != dynamic_cast((B*)(CC*)(E*)&x)) error(20); //counter example
|
if(&x != dynamic_cast((B*)(CC*)(E*)&x)) error(20); //counter example
|
XX xx;
|
XX xx;
|
if(dynamic_cast((B*)(CC*)(E*)&xx)) error(21);
|
if(dynamic_cast((B*)(CC*)(E*)&xx)) error(21);
|
Y y;
|
Y y;
|
if(dynamic_cast((B*)&y)) error (22);
|
if(dynamic_cast((B*)&y)) error (22);
|
if(dynamic_cast((A*)(B*)&y)) error (23);
|
if(dynamic_cast((A*)(B*)&y)) error (23);
|
}
|
}
|
|
|
// 2. crosscast
|
// 2. crosscast
|
|
|
struct J {virtual ~J(){}};
|
struct J {virtual ~J(){}};
|
struct K : CC, private J {};
|
struct K : CC, private J {};
|
class KK : J, CC{};
|
class KK : J, CC{};
|
|
|
void
|
void
|
test04 ()
|
test04 ()
|
{
|
{
|
E e;
|
E e;
|
if(dynamic_cast ((B*)(D*)&e)) error(24);
|
if(dynamic_cast ((B*)(D*)&e)) error(24);
|
if((CC*)&e != dynamic_cast ((C*)(D*)&e)) error(25); //counter example
|
if((CC*)&e != dynamic_cast ((C*)(D*)&e)) error(25); //counter example
|
K k;
|
K k;
|
if(dynamic_cast ((B*)&k)) error(26);
|
if(dynamic_cast ((B*)&k)) error(26);
|
KK kk;
|
KK kk;
|
if(dynamic_cast ((CC*)&kk)) error(27);
|
if(dynamic_cast ((CC*)&kk)) error(27);
|
}
|
}
|
|
|
int
|
int
|
main ()
|
main ()
|
{
|
{
|
test01();
|
test01();
|
test02();
|
test02();
|
test03();
|
test03();
|
test04();
|
test04();
|
return errors ? 1 : 0;
|
return errors ? 1 : 0;
|
}
|
}
|
|
|