diff options
author | Howard Hinnant <hhinnant@apple.com> | 2012-01-31 23:52:20 +0000 |
---|---|---|
committer | Howard Hinnant <hhinnant@apple.com> | 2012-01-31 23:52:20 +0000 |
commit | 830713c63ecaf0a5d7690db92f83f2741754de15 (patch) | |
tree | a6fd467f9990d78a216364863341519dcd929b49 /test/catch_class_03.cpp | |
parent | dfb07f80c1a0063130c5738ff7d060d22f79b8bf (diff) |
More test cases concentrating on catching class types.
git-svn-id: https://llvm.org/svn/llvm-project/libcxxabi/trunk@149453 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/catch_class_03.cpp')
-rw-r--r-- | test/catch_class_03.cpp | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/test/catch_class_03.cpp b/test/catch_class_03.cpp new file mode 100644 index 0000000..ad0c9ad --- /dev/null +++ b/test/catch_class_03.cpp @@ -0,0 +1,190 @@ +//===---------------------- catch_class_03.cpp ----------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +/* + This test checks that adjustedPtr is correct as there exist offsets in this + object for the various subobjects, all of which have a unique id_ to + check against. +*/ + +#include <exception> +#include <stdlib.h> +#include <assert.h> + +struct B +{ + static int count; + int id_; + explicit B(int id) : id_(id) {count++;} + B(const B& a) : id_(a.id_) {count++;} + ~B() {count--;} +}; + +int B::count = 0; + +struct C1 + : B +{ + static int count; + int id_; + explicit C1(int id) : B(id-2), id_(id) {count++;} + C1(const C1& a) : B(a.id_-2), id_(a.id_) {count++;} + ~C1() {count--;} +}; + +int C1::count = 0; + +struct C2 + : B +{ + static int count; + int id_; + explicit C2(int id) : B(id-2), id_(id) {count++;} + C2(const C2& a) : B(a.id_-2), id_(a.id_) {count++;} + ~C2() {count--;} +}; + +int C2::count = 0; + +struct A + : C1, C2 +{ + static int count; + int id_; + explicit A(int id) : C1(id-1), C2(id-2), id_(id) {count++;} + A(const A& a) : C1(a.id_-1), C2(a.id_-2), id_(a.id_) {count++;} + ~A() {count--;} +}; + +int A::count = 0; + +void f1() +{ + assert(A::count == 0); + assert(C1::count == 0); + assert(C2::count == 0); + assert(B::count == 0); + A a(5); + assert(A::count == 1); + assert(C1::count == 1); + assert(C2::count == 1); + assert(B::count == 2); + + assert(a.id_ == 5); + assert(static_cast<C1&>(a).id_ == 4); + assert(static_cast<C2&>(a).id_ == 3); + assert(static_cast<B&>(static_cast<C1&>(a)).id_ == 2); + assert(static_cast<B&>(static_cast<C2&>(a)).id_ == 1); + throw a; + assert(false); +} + +void f2() +{ + try + { + assert(A::count == 0); + assert(C1::count == 0); + assert(C2::count == 0); + assert(B::count == 0); + f1(); + assert(false); + } + catch (const A& a) // can catch A + { + assert(a.id_ == 5); + assert(static_cast<const C1&>(a).id_ == 4); + assert(static_cast<const C2&>(a).id_ == 3); + assert(static_cast<const B&>(static_cast<const C1&>(a)).id_ == 2); + assert(static_cast<const B&>(static_cast<const C2&>(a)).id_ == 1); + throw; + } + catch (const C1&) + { + assert(false); + } + catch (const C2&) + { + assert(false); + } + catch (const B&) + { + assert(false); + } +} + +void f3() +{ + try + { + assert(A::count == 0); + assert(C1::count == 0); + assert(C2::count == 0); + assert(B::count == 0); + f2(); + assert(false); + } + catch (const B& a) // can not catch B (ambiguous base) + { + assert(false); + } + catch (const C1& c1) // can catch C1 + { + assert(c1.id_ == 4); + assert(static_cast<const B&>(c1).id_ == 2); + throw; + } + catch (const C2&) + { + assert(false); + } +} + +void f4() +{ + try + { + assert(A::count == 0); + assert(C1::count == 0); + assert(C2::count == 0); + assert(B::count == 0); + f3(); + assert(false); + } + catch (const B& a) // can not catch B (ambiguous base) + { + assert(false); + } + catch (const C2& c2) // can catch C2 + { + assert(c2.id_ == 3); + assert(static_cast<const B&>(c2).id_ == 1); + throw; + } + catch (const C1&) + { + assert(false); + } +} + +int main() +{ + try + { + f4(); + assert(false); + } + catch (...) + { + } + assert(A::count == 0); + assert(C1::count == 0); + assert(C2::count == 0); + assert(B::count == 0); +} |