From c197a3ab4d1498198bc190216bce41ee3887ad3a Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sat, 9 Nov 2013 04:58:13 +0000 Subject: [PATCH] Test the polymorphic behavior of this utility. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194320 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/polymorphic_ptr_test.cpp | 34 +++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/unittests/ADT/polymorphic_ptr_test.cpp b/unittests/ADT/polymorphic_ptr_test.cpp index ce001d1e309..d9a2c3fdd11 100644 --- a/unittests/ADT/polymorphic_ptr_test.cpp +++ b/unittests/ADT/polymorphic_ptr_test.cpp @@ -22,7 +22,8 @@ struct S { }; // A function that forces the return of a copy. -polymorphic_ptr dummy_copy(const polymorphic_ptr &arg) { return arg; } +template +T dummy_copy(const T &arg) { return arg; } TEST(polymorphic_ptr_test, Basic) { polymorphic_ptr null; @@ -83,4 +84,35 @@ TEST(polymorphic_ptr_test, Basic) { EXPECT_EQ(42, p3->x); } +struct Base { + virtual ~Base() {} + virtual Base *clone() = 0; + virtual StringRef name() { return "Base"; } +}; + +struct DerivedA : Base { + virtual DerivedA *clone() { return new DerivedA(); } + virtual StringRef name() { return "DerivedA"; } +}; +struct DerivedB : Base { + virtual DerivedB *clone() { return new DerivedB(); } + virtual StringRef name() { return "DerivedB"; } +}; + +TEST(polymorphic_ptr_test, Polymorphism) { + polymorphic_ptr a(new DerivedA()); + polymorphic_ptr b(new DerivedB()); + + EXPECT_EQ("DerivedA", a->name()); + EXPECT_EQ("DerivedB", b->name()); + + polymorphic_ptr copy = dummy_copy(a); + EXPECT_NE(a, copy); + EXPECT_EQ("DerivedA", copy->name()); + + copy = dummy_copy(b); + EXPECT_NE(b, copy); + EXPECT_EQ("DerivedB", copy->name()); +} + }