not_null_test.cxx 966 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // not_null.t.h
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 12/2/16.
  6. //
  7. #include "pointer/not_null.hpp"
  8. #include <gmock/gmock.h>
  9. #include "test_stubs.h"
  10. TEST(NonNullPtrTest, ThrowsIfNullptrIsSnuckIn) {
  11. EXPECT_THROW(not_null<std::shared_ptr<int>>(std::shared_ptr<int>(nullptr)),
  12. null_pointer_exception);
  13. }
  14. TEST(NonNullPtrTest, ContainsInnerPtrForEquality) {
  15. std::shared_ptr<int> i{new int};
  16. not_null<std::shared_ptr<int>> n{i};
  17. EXPECT_THAT(n.get(), i.get());
  18. EXPECT_THAT(*n, *i);
  19. }
  20. TEST(NonNullPtrTest, CanModifyInnerValue) {
  21. std::shared_ptr<int> i{new int(5)};
  22. not_null<std::shared_ptr<int>> n{i};
  23. EXPECT_THAT(*n, 5);
  24. *n = 4;
  25. EXPECT_THAT(*i, 4);
  26. }
  27. TEST(NonNullPtrTest, DoesNotDeleteRawPtr) {
  28. bool has_delete{false};
  29. destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
  30. not_null<destructor_sentinal *>{test_struct};
  31. EXPECT_FALSE(has_delete);
  32. delete test_struct;
  33. EXPECT_TRUE(has_delete);
  34. }