const_ptr_test.cxx 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //
  2. // const_ptr.t.h
  3. // pointers
  4. //
  5. // Created by Sam Jaffe on 1/5/17.
  6. //
  7. #include "pointers/const_propogating_ptr.hpp"
  8. #include "pointers/const_ptr.hpp"
  9. #include <gmock/gmock.h>
  10. #include "test_stubs.h"
  11. TEST(ConstPtrTest, NonConstCPCallConstMethods) {
  12. std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
  13. const_ptr<std::shared_ptr<const_tracking_stub>> cp{p};
  14. EXPECT_TRUE(cp->is_const());
  15. }
  16. TEST(ConstPtrTest, ConstCPCallConstMethods) {
  17. std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
  18. const_ptr<std::shared_ptr<const_tracking_stub>> const cp{p};
  19. EXPECT_TRUE(cp->is_const());
  20. }
  21. TEST(ConstPtrTest, CanConstructFromConstPropogatingPtr) {
  22. std::shared_ptr<const_tracking_stub> p{new const_tracking_stub};
  23. const_propogating_ptr<std::shared_ptr<const_tracking_stub>> cpp{p};
  24. const_ptr<std::shared_ptr<const_tracking_stub>> cp{p};
  25. EXPECT_THAT(cp.get(), cpp.get());
  26. EXPECT_FALSE(cpp->is_const());
  27. EXPECT_TRUE(cp->is_const());
  28. }
  29. TEST(ConstPtrTest, DoesNotDeleteRawPtr) {
  30. bool has_delete{false};
  31. destructor_sentinal * test_struct = new destructor_sentinal{has_delete};
  32. const_ptr<destructor_sentinal *>{test_struct};
  33. EXPECT_FALSE(has_delete);
  34. delete test_struct;
  35. EXPECT_TRUE(has_delete);
  36. }