summaryrefslogtreecommitdiff
path: root/gcc/testsuite/g++.old-deja/g++.other/dyncast2.C
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /gcc/testsuite/g++.old-deja/g++.other/dyncast2.C
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
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.
Diffstat (limited to 'gcc/testsuite/g++.old-deja/g++.other/dyncast2.C')
-rw-r--r--gcc/testsuite/g++.old-deja/g++.other/dyncast2.C85
1 files changed, 85 insertions, 0 deletions
diff --git a/gcc/testsuite/g++.old-deja/g++.other/dyncast2.C b/gcc/testsuite/g++.old-deja/g++.other/dyncast2.C
new file mode 100644
index 000000000..bea82cb46
--- /dev/null
+++ b/gcc/testsuite/g++.old-deja/g++.other/dyncast2.C
@@ -0,0 +1,85 @@
+// { dg-do run }
+// Author: Alfred Miniarik <a8601248@unet.univie.ac.at>
+// test of dynamic_cast
+// runtime detecting of valid
+// downcasts within nonpublic
+// baseclasses.
+
+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(){} int i;};
+struct B : A {int i;};
+struct C : B {int i;};
+struct CC : C {};
+class D : C {int i;};
+
+struct E : D {int i;};
+class F : E {int i;};
+
+void
+test01 ()
+{
+ D d;
+ if((C*)&d != dynamic_cast<C*> ((A*)&d)) error(1);
+ if((C*)&d != dynamic_cast<C*> ((B*)&d)) error(2);
+ if((B*)&d != dynamic_cast<B*> ((A*)&d)) error(3);
+
+ E e;
+ if((C*)&e != dynamic_cast<C*> ((A*)&e)) error(4);
+
+ F f;
+ if((C*)&f != dynamic_cast<C*> ((B*)&f)) error(5);
+ if((B*)&f != dynamic_cast<B*> ((A*)&f)) error(6);
+ if((E*)&f != dynamic_cast<E*> ((D*)&f)) error(7);
+ if(dynamic_cast<E*> ((C*)&f)) error(8); //counter example
+}
+
+// 1.2 multiple inheritance case
+
+struct G : CC, F{};
+
+void
+test02 ()
+{
+ G g;
+ if((B*)(F*)&g != dynamic_cast<B*> ((A*)(F*)&g)) error(9);
+ if(dynamic_cast<D*> ((A*)(F*)&g)) error(10);
+ if(dynamic_cast<G*> ((B*)(F*)&g)) error(11);
+}
+
+// 2. crosscast (always fail)
+
+struct I : C{};
+struct J : F{};
+struct K : I, J{};
+class L : K{};
+
+void
+test03 ()
+{
+ L l;
+ if(dynamic_cast<J*> ((I*)&l)) error(12);
+ if(dynamic_cast<J*> ((E*)&l)) error(13);
+ if(dynamic_cast<I*> ((J*)&l)) error(14);
+}
+
+int
+main ()
+{
+ test01();
+ test02();
+ test03();
+ return errors ? 1 : 0;
+}