From aec427786be4f4dd0c8ecc0aa17899e6c836e45d Mon Sep 17 00:00:00 2001 From: Chandler Carruth Date: Sat, 9 Nov 2013 04:06:02 +0000 Subject: Add a polymorphic_ptr smart pointer data type. It's a somewhat silly unique ownership smart pointer which is *deep* copyable by assuming it can call a T::clone() method to allocate a copy of the owned data. This is mostly useful with containers or other collections of uniquely owned data in C++98 where they *might* copy. With C++11 we can likely remove this in favor of move-only types and containers wrapped around those types. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@194315 91177308-0d34-0410-b5e6-96231b3b80d8 --- unittests/ADT/CMakeLists.txt | 1 + unittests/ADT/polymorphic_ptr_test.cpp | 71 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 unittests/ADT/polymorphic_ptr_test.cpp (limited to 'unittests') diff --git a/unittests/ADT/CMakeLists.txt b/unittests/ADT/CMakeLists.txt index 2125657..8ad303a 100644 --- a/unittests/ADT/CMakeLists.txt +++ b/unittests/ADT/CMakeLists.txt @@ -35,6 +35,7 @@ set(ADTSources TripleTest.cpp TwineTest.cpp VariadicFunctionTest.cpp + polymorphic_ptr_test.cpp ) # They cannot be compiled on MSVC9 due to its bug. diff --git a/unittests/ADT/polymorphic_ptr_test.cpp b/unittests/ADT/polymorphic_ptr_test.cpp new file mode 100644 index 0000000..e1e9c42 --- /dev/null +++ b/unittests/ADT/polymorphic_ptr_test.cpp @@ -0,0 +1,71 @@ +//===- llvm/unittest/ADT/polymorphic_ptr.h - polymorphic_ptr tests -----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "gtest/gtest.h" +#include "llvm/ADT/polymorphic_ptr.h" +#include "llvm/Support/raw_ostream.h" + +using namespace llvm; + +namespace { + +TEST(polymorphic_ptr_test, Basic) { + struct S { + S(int x) : x(x) {} + int x; + }; + + polymorphic_ptr null; + EXPECT_FALSE((bool)null); + EXPECT_TRUE(!null); + EXPECT_EQ((S*)0, null.get()); + + S *s = new S(42); + polymorphic_ptr p(s); + EXPECT_TRUE((bool)p); + EXPECT_FALSE(!p); + EXPECT_TRUE(p != null); + EXPECT_FALSE(p == null); + EXPECT_TRUE(p == s); + EXPECT_TRUE(s == p); + EXPECT_FALSE(p != s); + EXPECT_FALSE(s != p); + EXPECT_EQ(s, &*p); + EXPECT_EQ(s, p.operator->()); + EXPECT_EQ(s, p.get()); + EXPECT_EQ(42, p->x); + + EXPECT_EQ(s, p.take()); + EXPECT_FALSE((bool)p); + EXPECT_TRUE(!p); + p = s; + EXPECT_TRUE((bool)p); + EXPECT_FALSE(!p); + EXPECT_EQ(s, &*p); + EXPECT_EQ(s, p.operator->()); + EXPECT_EQ(s, p.get()); + EXPECT_EQ(42, p->x); + + polymorphic_ptr p2((llvm_move(p))); + EXPECT_FALSE((bool)p); + EXPECT_TRUE(!p); + EXPECT_TRUE((bool)p2); + EXPECT_FALSE(!p2); + EXPECT_EQ(s, &*p2); + + using std::swap; + swap(p, p2); + EXPECT_TRUE((bool)p); + EXPECT_FALSE(!p); + EXPECT_EQ(s, &*p); + EXPECT_FALSE((bool)p2); + EXPECT_TRUE(!p2); +} + +} -- cgit v1.1