| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- //
- // 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);
- }
- };
|