瀏覽代碼

Adding test drivers for maybe_null and not_null

Samuel Jaffe 9 年之前
父節點
當前提交
f813feb353
共有 2 個文件被更改,包括 147 次插入0 次删除
  1. 90 0
      maybe_null.t.h
  2. 57 0
      not_null.t.h

+ 90 - 0
maybe_null.t.h

@@ -0,0 +1,90 @@
+//
+//  maybe_null.t.h
+//  pointers
+//
+//  Created by Sam Jaffe on 12/2/16.
+//
+
+#pragma once
+
+#include <cxxtest/TestSuite.h>
+#include <memory>
+
+#undef DEBUG
+#define DEBUG
+#include "maybe_null.hpp"
+
+class maybe_null_TestSuite : public CxxTest::TestSuite {
+public:
+  using ptr_base = std::shared_ptr<int>;
+  using ptr_t = maybe_null<ptr_base>;
+public:
+  void test_can_hold_nullptr() const {
+    ptr_t mn{nullptr};
+    TS_ASSERT_EQUALS(bool(mn), false);
+  }
+  
+  void test_is_valid_wo_nullptr() const {
+    ptr_t mn = make_with(5);
+    TS_ASSERT_EQUALS(bool(mn), true);
+  }
+  
+  void test_nullptr_throws_or_false() const {
+    ptr_t mn{nullptr};
+    TS_ASSERT_THROWS(*mn, unchecked_pointer_exception);
+  }
+  
+  void test_object_contains_ptr() const {
+    ptr_base i{new int};
+    ptr_t n{i};
+    TS_ASSERT_EQUALS(bool(n), true);
+    TS_ASSERT_EQUALS(n.get(), i.get());
+    TS_ASSERT_EQUALS(*n, *i);
+  }
+  
+  void test_object_sets_value() const {
+    static int const value1{5};
+    static int const value2{4};
+    
+    ptr_base i{new int{value1}};
+    ptr_t n{i};
+    TS_ASSERT_EQUALS(bool(n), true);
+    TS_ASSERT_EQUALS(*n, value1);
+    *n = value2;
+    TS_ASSERT_EQUALS(bool(n), true);
+    TS_ASSERT_EQUALS(*i, value2);
+  }
+  
+  void test_do_not_own() const {
+    bool has_delete{false};
+    struct test_t {
+      ~test_t() { _r = true; }
+      bool & _r;
+    };
+    {
+      std::shared_ptr<test_t> test_struct{new test_t{has_delete}};
+    
+      TS_ASSERT_EQUALS(test_struct->_r, has_delete);
+      {
+      maybe_null<std::shared_ptr<test_t>> nn(test_struct);
+      }
+      TS_ASSERT_EQUALS(has_delete, false);
+    }
+    TS_ASSERT_EQUALS(has_delete, true);
+  }
+  
+  void test_type_conversion() {
+    struct base {};
+    struct derived : public base {};
+    derived d;
+    maybe_null<derived*> m{&d};
+    maybe_null<base*> b = maybe_null<base*>(m);
+    TS_ASSERT_EQUALS(b.get(), m.get());
+  }
+  
+private:
+  template <typename T>
+  static maybe_null<std::shared_ptr<T>> make_with(T p) {
+    return maybe_null<std::shared_ptr<T>>(std::make_shared<T>(p));
+  }
+};

+ 57 - 0
not_null.t.h

@@ -0,0 +1,57 @@
+//
+//  not_null.t.h
+//  pointers
+//
+//  Created by Sam Jaffe on 12/2/16.
+//
+
+#pragma once
+
+#include <cxxtest/TestSuite.h>
+
+#include "not_null.hpp"
+
+class not_null_TestSuite : public CxxTest::TestSuite {
+public:
+  using ptr_base = std::shared_ptr<int>;
+  using ptr_t = not_null<ptr_base>;
+public:
+  void test_nullptr_throws() const {
+    TS_ASSERT_THROWS(ptr_t(ptr_base(nullptr)), null_pointer_exception);
+  }
+  
+  void test_object_contains_ptr() const {
+    ptr_base i{new int};
+    ptr_t n{i};
+    TS_ASSERT_EQUALS(n.get(), i.get());
+    TS_ASSERT_EQUALS(*n, *i);
+  }
+  
+  void test_object_sets_value() const {
+    static int const value1{5};
+    static int const value2{4};
+    ptr_base i{new int(value1)};
+    ptr_t n{i};
+    TS_ASSERT_EQUALS(*n, value1);
+    *n = value2;
+    TS_ASSERT_EQUALS(*i, value2);
+  }
+  
+  void test_do_not_own() const {
+    bool has_delete{false};
+    struct test_t {
+      ~test_t() { _r = true; }
+      bool & _r;
+    };
+    {
+      std::shared_ptr<test_t> test_struct{new test_t{has_delete}};
+      
+      TS_ASSERT_EQUALS(test_struct->_r, has_delete);
+      {
+        not_null<std::shared_ptr<test_t>> nn(test_struct);
+      }
+      TS_ASSERT_EQUALS(has_delete, false);
+    }
+    TS_ASSERT_EQUALS(has_delete, true);
+  }
+};