From 554fd8c5195424bdbcabf5de30fdc183aba391bd Mon Sep 17 00:00:00 2001 From: upstream source tree Date: Sun, 15 Mar 2015 20:14:05 -0400 Subject: obtained gcc-4.6.4.tar.bz2 from upstream website; verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository. --- gcc/testsuite/g++.old-deja/g++.other/dyncast1.C | 123 ++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 gcc/testsuite/g++.old-deja/g++.other/dyncast1.C (limited to 'gcc/testsuite/g++.old-deja/g++.other/dyncast1.C') diff --git a/gcc/testsuite/g++.old-deja/g++.other/dyncast1.C b/gcc/testsuite/g++.old-deja/g++.other/dyncast1.C new file mode 100644 index 000000000..6feec30e6 --- /dev/null +++ b/gcc/testsuite/g++.old-deja/g++.other/dyncast1.C @@ -0,0 +1,123 @@ +// { dg-do run } +// Author: Alfred Miniarik +// test of dynamic_cast +// runtime detecting of nonpublic +// inheritance within a cast +// and therefor failing with result 0. + +extern "C" void abort(); +extern "C" int printf (const char *, ...); + +static int errors = 0; +void error(int i) +{ + printf("Error %i\n",i); + errors++; +} + +// 1. downcast + +// 1.1. single inheritance case + +struct A {virtual ~A(){}}; +struct AA : A {}; +struct B : A {}; +struct BB : B {}; +class C : B {}; +struct D : C {}; + +struct CC : B {}; +class DD : CC {}; + +class CCC : protected B {}; +class DDD : protected CCC {}; + +void +test01 () +{ + D d; + if(dynamic_cast ((A*)&d)) error(1); + if(dynamic_cast ((B*)&d)) error(2); + if(&d != dynamic_cast ((C*)&d)) error(3); //counter example + if(dynamic_cast ((B*)&d)) error(4); + + DD dd; + if(dynamic_cast ((A*)&dd)) error(5); + if(dynamic_cast ((B*)&dd)) error(6); + + DDD ddd; + if(dynamic_cast ((A*)&ddd)) error(7); + if(dynamic_cast ((B*)&ddd)) error(8); + if(dynamic_cast ((B*)&ddd)) error(9); +} + +// 1.2. multiple inheritance case +// 1.2.1. all bases are public + +struct E : D, CC {}; +struct EE : CC, D {}; //Will search in reverse order. + +void +test02 () +{ + E e; + if(dynamic_cast ((A*)(D*)&e)) error(10); + if(dynamic_cast ((B*)(D*)&e)) error(11); + if(&e != dynamic_cast ((C*)(D*)&e)) error(12); //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 + + EE ee; + if(dynamic_cast ((A*)(D*)&ee)) error(15); + if(dynamic_cast ((B*)(D*)&ee)) error(16); + if(&ee != dynamic_cast ((C*)(D*)&ee)) error(17); //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 +} + +// 1.2.2 one or more branches are nonpublic + +struct X : private BB, E {}; +struct Y : AA, private B {}; + +class XX : BB, E {}; + +void +test03 () +{ + X x; + if(&x != dynamic_cast((B*)(CC*)(E*)&x)) error(20); //counter example + XX xx; + if(dynamic_cast((B*)(CC*)(E*)&xx)) error(21); + Y y; + if(dynamic_cast((B*)&y)) error (22); + if(dynamic_cast((A*)(B*)&y)) error (23); +} + +// 2. crosscast + +struct J {virtual ~J(){}}; +struct K : CC, private J {}; +class KK : J, CC{}; + +void +test04 () +{ + E e; + if(dynamic_cast ((B*)(D*)&e)) error(24); + if((CC*)&e != dynamic_cast ((C*)(D*)&e)) error(25); //counter example + K k; + if(dynamic_cast ((B*)&k)) error(26); + KK kk; + if(dynamic_cast ((CC*)&kk)) error(27); +} + +int +main () +{ + test01(); + test02(); + test03(); + test04(); + return errors ? 1 : 0; +} -- cgit v1.2.3