summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/ADT/polymorphic_ptr_test.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2013-11-09 04:06:02 +0000
committerChandler Carruth <chandlerc@gmail.com>2013-11-09 04:06:02 +0000
commit64b0556071d1b8bc7286c94f071594276d91558a (patch)
tree298daed20b58148e255504240d22bc4f08bba716 /llvm/unittests/ADT/polymorphic_ptr_test.cpp
parent5f847c007b6aa194a760b674e84cf33acf4ec20f (diff)
downloadbcm5719-llvm-64b0556071d1b8bc7286c94f071594276d91558a.tar.gz
bcm5719-llvm-64b0556071d1b8bc7286c94f071594276d91558a.zip
Add a polymorphic_ptr<T> 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. llvm-svn: 194315
Diffstat (limited to 'llvm/unittests/ADT/polymorphic_ptr_test.cpp')
-rw-r--r--llvm/unittests/ADT/polymorphic_ptr_test.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/llvm/unittests/ADT/polymorphic_ptr_test.cpp b/llvm/unittests/ADT/polymorphic_ptr_test.cpp
new file mode 100644
index 00000000000..e1e9c42fce0
--- /dev/null
+++ b/llvm/unittests/ADT/polymorphic_ptr_test.cpp
@@ -0,0 +1,71 @@
+//===- llvm/unittest/ADT/polymorphic_ptr.h - polymorphic_ptr<T> 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<S> null;
+ EXPECT_FALSE((bool)null);
+ EXPECT_TRUE(!null);
+ EXPECT_EQ((S*)0, null.get());
+
+ S *s = new S(42);
+ polymorphic_ptr<S> 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<S> 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);
+}
+
+}
OpenPOWER on IntegriCloud