not_null.t.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. {
  40. std::shared_ptr<test_t> test_struct{new test_t{has_delete}};
  41. TS_ASSERT_EQUALS(test_struct->_r, has_delete);
  42. {
  43. not_null<std::shared_ptr<test_t>> nn(test_struct);
  44. }
  45. TS_ASSERT_EQUALS(has_delete, false);
  46. }
  47. TS_ASSERT_EQUALS(has_delete, true);
  48. }
  49. };