non_null_ptr_ut.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/memory/non_null_ptr.h>
  3. namespace NYT {
  4. namespace {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. enum class EFuncResult
  7. {
  8. Base,
  9. ConstBase,
  10. Derived,
  11. ConstDerived,
  12. };
  13. enum class EBinaryFuncResult
  14. {
  15. OK,
  16. NotOK,
  17. };
  18. struct TBase
  19. { };
  20. struct TDerived
  21. : public TBase
  22. { };
  23. EFuncResult Foo(TNonNullPtr<TBase> /*base*/)
  24. {
  25. return EFuncResult::Base;
  26. }
  27. EFuncResult Foo(TNonNullPtr<const TBase> /*base*/)
  28. {
  29. return EFuncResult::ConstBase;
  30. }
  31. EFuncResult Foo(TNonNullPtr<TDerived> /*derived*/)
  32. {
  33. return EFuncResult::Derived;
  34. }
  35. EFuncResult Foo(TNonNullPtr<const TDerived> /*derived*/)
  36. {
  37. return EFuncResult::ConstDerived;
  38. }
  39. [[maybe_unused]] EBinaryFuncResult Foo(int* /*derived*/)
  40. {
  41. return EBinaryFuncResult::NotOK;
  42. }
  43. EBinaryFuncResult Foo(TNonNullPtr<int> /*derived*/)
  44. {
  45. return EBinaryFuncResult::OK;
  46. }
  47. EBinaryFuncResult Bar(TNonNullPtr<const int> /*arg*/)
  48. {
  49. return EBinaryFuncResult::OK;
  50. }
  51. EBinaryFuncResult Baz(TNonNullPtr<int> /*arg*/)
  52. {
  53. return EBinaryFuncResult::OK;
  54. }
  55. TEST(TNonNullPtrTest, Simple)
  56. {
  57. TDerived derived{};
  58. const auto& constDerived = derived;
  59. EXPECT_EQ(EFuncResult::Derived, Foo(GetPtr(derived)));
  60. EXPECT_EQ(EFuncResult::ConstDerived, Foo(GetPtr(constDerived)));
  61. TBase base{};
  62. const auto& constBase = base;
  63. EXPECT_EQ(EFuncResult::Base, Foo(GetPtr(base)));
  64. EXPECT_EQ(EFuncResult::ConstBase, Foo(GetPtr(constBase)));
  65. int i{};
  66. EXPECT_EQ(EBinaryFuncResult::OK, Foo(GetPtr(i)));
  67. }
  68. TEST(TNonNullPtrTest, CastToConst)
  69. {
  70. int i{};
  71. EXPECT_EQ(EBinaryFuncResult::OK, Bar(GetPtr(i)));
  72. }
  73. TEST(TNonNullPtrTest, ConstructionFromRawPointer)
  74. {
  75. int i{};
  76. EXPECT_EQ(EBinaryFuncResult::OK, Baz(&i));
  77. }
  78. ////////////////////////////////////////////////////////////////////////////////
  79. } // namespace
  80. } // namespace NYT