erased_storage_ut.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <library/cpp/testing/gtest/gtest.h>
  2. #include <library/cpp/yt/memory/erased_storage.h>
  3. #include <library/cpp/int128/int128.h>
  4. #include <library/cpp/yt/misc/guid.h>
  5. #include <util/generic/string.h>
  6. #include <util/system/types.h>
  7. #include <vector>
  8. namespace NYT {
  9. namespace {
  10. ////////////////////////////////////////////////////////////////////////////////
  11. struct TWithFieldInitalizer
  12. {
  13. // NB: This class is not trivially default constructible.
  14. int Field{};
  15. };
  16. static_assert(!std::is_trivially_default_constructible_v<TWithFieldInitalizer>);
  17. struct TCopyWithSideEffects
  18. {
  19. TCopyWithSideEffects(const TCopyWithSideEffects&)
  20. { }
  21. };
  22. static_assert(!std::is_trivially_copy_constructible_v<TCopyWithSideEffects>);
  23. struct TWithSubStruct
  24. {
  25. TWithFieldInitalizer Field;
  26. };
  27. class TWithPrivateMembers
  28. {
  29. public:
  30. TWithPrivateMembers() = default;
  31. private:
  32. [[maybe_unused]] std::array<std::byte, 8> Data_;
  33. };
  34. ////////////////////////////////////////////////////////////////////////////////
  35. TEST(TErasedStorageTest, Types)
  36. {
  37. static_assert(CTriviallyErasable<int>);
  38. static_assert(CTriviallyErasable<i32>);
  39. static_assert(CTriviallyErasable<i64>);
  40. static_assert(CTriviallyErasable<i128>);
  41. static_assert(CTriviallyErasable<std::array<i128, 2>>);
  42. static_assert(CTriviallyErasable<TGuid>);
  43. static_assert(CTriviallyErasable<void*>);
  44. static_assert(CTriviallyErasable<double>);
  45. static_assert(CTriviallyErasable<const char*>);
  46. static_assert(CTriviallyErasable<TWithFieldInitalizer>);
  47. static_assert(CTriviallyErasable<TWithSubStruct>);
  48. static_assert(CTriviallyErasable<TWithPrivateMembers>);
  49. static_assert(CTriviallyErasable<char[8]>);
  50. static_assert(!CTriviallyErasable<TString>);
  51. static_assert(!CTriviallyErasable<std::vector<int>>);
  52. static_assert(!CTriviallyErasable<std::array<i128, 3>>);
  53. static_assert(!CTriviallyErasable<int&>);
  54. static_assert(!CTriviallyErasable<const int&>);
  55. static_assert(!CTriviallyErasable<int&&>);
  56. static_assert(!CTriviallyErasable<TCopyWithSideEffects>);
  57. }
  58. TEST(TErasedStorageTest, JustWorks)
  59. {
  60. int var = 42;
  61. TErasedStorage stor(var);
  62. EXPECT_EQ(stor.AsConcrete<int>(), 42);
  63. var = 66;
  64. EXPECT_EQ(stor.AsConcrete<int>(), 42);
  65. }
  66. TEST(TErasedStorageTest, CopyAssign)
  67. {
  68. int var = 42;
  69. TErasedStorage stor(var);
  70. EXPECT_EQ(stor.AsConcrete<int>(), 42);
  71. {
  72. int anotherVar = 77;
  73. stor = TErasedStorage(anotherVar);
  74. }
  75. EXPECT_EQ(stor.AsConcrete<int>(), 77);
  76. double thirdVar = 9.92145214;
  77. stor = TErasedStorage(thirdVar);
  78. EXPECT_DOUBLE_EQ(stor.AsConcrete<double>(), 9.92145214);
  79. }
  80. TEST(TErasedStorageTest, Pointer)
  81. {
  82. TString message("Hello world");
  83. TErasedStorage stor(&message);
  84. EXPECT_EQ(*stor.AsConcrete<TString*>(), TString("Hello world"));
  85. message = "Goodbye world";
  86. EXPECT_EQ(*stor.AsConcrete<TString*>(), "Goodbye world");
  87. }
  88. TEST(TErasedStorageTest, MutateStorage)
  89. {
  90. int var = 0;
  91. TErasedStorage stor(var);
  92. EXPECT_EQ(stor.AsConcrete<int>(), 0);
  93. auto& ref = stor.AsConcrete<int>();
  94. ref = 88;
  95. EXPECT_EQ(stor.AsConcrete<int>(), 88);
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////
  98. } // namespace
  99. } // namespace NYT