Browse Source

Adding tests for nullptr construction, and for custom copy-functions

Samuel Jaffe 8 years ago
parent
commit
5515a0bd3c
1 changed files with 35 additions and 0 deletions
  1. 35 0
      copy_ptr.t.h

+ 35 - 0
copy_ptr.t.h

@@ -9,11 +9,18 @@
 
 #include <cxxtest/TestSuite.h>
 
+#include <stdexcept>
+
 #include "copy_ptr.hpp"
 
 class copy_ptr_TestSuite : public CxxTest::TestSuite {
 private:
   struct copy_me {};
+  
+  static copy_me * throw_clone(copy_me const * p) {
+    if (p == nullptr) { throw std::runtime_error{"NULL"}; }
+    return new copy_me{*p};
+  }
 
   class base {
   public:
@@ -42,6 +49,26 @@ public:
     TS_ASSERT_DIFFERS( c1.get(), c2.get() );
   }
   
+  void test_copy_with_nullptr() {
+    copy_ptr<copy_me> c1 { nullptr };
+    copy_ptr<copy_me> c2 { c1 };
+    TS_ASSERT_EQUALS( c1.get(), nullptr );
+    TS_ASSERT_EQUALS( c1.get(), c2.get() );
+  }
+
+  void test_copy_custom_func() {
+    using ptr_t = copy_ptr<copy_me, &copy_ptr_TestSuite::throw_clone>;
+    ptr_t c1 { new copy_me };
+    ptr_t c2 { c1 };
+    TS_ASSERT_DIFFERS( c1.get(), c2.get() );
+  }
+
+  void test_copy_custom_func_with_nullptr() {
+    using ptr_t = copy_ptr<copy_me, &copy_ptr_TestSuite::throw_clone>;
+    ptr_t c1 { nullptr };
+    TS_ASSERT_THROWS( ptr_t{ c1 }, std::runtime_error );
+  }
+  
   void test_copy_object_is_deep_equals() {
     using vec_t = std::vector<int>;
     vec_t my_vec = { 1, 3, 5, 3, 6, 1, 2, -1, 0 };
@@ -59,6 +86,14 @@ public:
     TS_ASSERT_EQUALS( ptr_t( c1 )->id(), derived_1::ID);
   }
   
+  void test_clone_polymorphic_with_nullptr() {
+    using ptr_t = clone_ptr<base, &base::clone>;
+    ptr_t c1 { nullptr };
+    ptr_t c2 { c1 };
+    TS_ASSERT_EQUALS( c1.get(), nullptr );
+    TS_ASSERT_EQUALS( c1.get(), c2.get() );
+  }
+  
   void test_does_own() const {
     bool has_delete{false};
     struct test_t {