| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- //
- // 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;
- };
- test_t * test_struct = new test_t{has_delete};
- not_null<test_t *>{test_struct};
- TS_ASSERT_EQUALS(has_delete, false);
- delete test_struct;
- TS_ASSERT_EQUALS(has_delete, true);
- }
- };
|