#include #include #include #include #include #include #include namespace NYT { namespace { //////////////////////////////////////////////////////////////////////////////// struct TWithFieldInitalizer { // NB: This class is not trivially default constructible. int Field{}; }; static_assert(!std::is_trivially_default_constructible_v); struct TCopyWithSideEffects { TCopyWithSideEffects(const TCopyWithSideEffects&) { } }; static_assert(!std::is_trivially_copy_constructible_v); struct TWithSubStruct { TWithFieldInitalizer Field; }; class TWithPrivateMembers { public: TWithPrivateMembers() = default; private: [[maybe_unused]] std::array Data_; }; //////////////////////////////////////////////////////////////////////////////// TEST(TErasedStorageTest, Types) { static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable>); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(CTriviallyErasable); static_assert(!CTriviallyErasable); static_assert(!CTriviallyErasable>); static_assert(!CTriviallyErasable>); static_assert(!CTriviallyErasable); static_assert(!CTriviallyErasable); static_assert(!CTriviallyErasable); static_assert(!CTriviallyErasable); } TEST(TErasedStorageTest, JustWorks) { int var = 42; TErasedStorage stor(var); EXPECT_EQ(stor.AsConcrete(), 42); var = 66; EXPECT_EQ(stor.AsConcrete(), 42); } TEST(TErasedStorageTest, CopyAssign) { int var = 42; TErasedStorage stor(var); EXPECT_EQ(stor.AsConcrete(), 42); { int anotherVar = 77; stor = TErasedStorage(anotherVar); } EXPECT_EQ(stor.AsConcrete(), 77); double thirdVar = 9.92145214; stor = TErasedStorage(thirdVar); EXPECT_DOUBLE_EQ(stor.AsConcrete(), 9.92145214); } TEST(TErasedStorageTest, Pointer) { TString message("Hello world"); TErasedStorage stor(&message); EXPECT_EQ(*stor.AsConcrete(), TString("Hello world")); message = "Goodbye world"; EXPECT_EQ(*stor.AsConcrete(), "Goodbye world"); } TEST(TErasedStorageTest, MutateStorage) { int var = 0; TErasedStorage stor(var); EXPECT_EQ(stor.AsConcrete(), 0); auto& ref = stor.AsConcrete(); ref = 88; EXPECT_EQ(stor.AsConcrete(), 88); } //////////////////////////////////////////////////////////////////////////////// } // namespace } // namespace NYT