not_null.t.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // not_null.t.h
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 12/2/16.
  6. //
  7. #pragma once
  8. #include <cxxtest/TestSuite.h>
  9. #include "not_null.hpp"
  10. class not_null_TestSuite : public CxxTest::TestSuite {
  11. public:
  12. using ptr_base = std::shared_ptr<int>;
  13. using ptr_t = not_null<ptr_base>;
  14. public:
  15. void test_nullptr_throws() const {
  16. TS_ASSERT_THROWS(ptr_t(ptr_base(nullptr)), null_pointer_exception);
  17. }
  18. void test_object_contains_ptr() const {
  19. ptr_base i{new int};
  20. ptr_t n{i};
  21. TS_ASSERT_EQUALS(n.get(), i.get());
  22. TS_ASSERT_EQUALS(*n, *i);
  23. }
  24. void test_object_sets_value() const {
  25. static int const value1{5};
  26. static int const value2{4};
  27. ptr_base i{new int(value1)};
  28. ptr_t n{i};
  29. TS_ASSERT_EQUALS(*n, value1);
  30. *n = value2;
  31. TS_ASSERT_EQUALS(*i, value2);
  32. }
  33. void test_do_not_own() const {
  34. bool has_delete{false};
  35. struct test_t {
  36. ~test_t() { _r = true; }
  37. bool & _r;
  38. };
  39. test_t * test_struct = new test_t{has_delete};
  40. not_null<test_t *>{test_struct};
  41. TS_ASSERT_EQUALS(has_delete, false);
  42. delete test_struct;
  43. TS_ASSERT_EQUALS(has_delete, true);
  44. }
  45. };