//MIT License // //Copyright (c) 2019 Mindaugas Vinkelis // //Permission is hereby granted, free of charge, to any person obtaining a copy //of this software and associated documentation files (the "Software"), to deal //in the Software without restriction, including without limitation the rights //to use, copy, modify, merge, publish, distribute, sublicense, and/or sell //copies of the Software, and to permit persons to whom the Software is //furnished to do so, subject to the following conditions: // //The above copyright notice and this permission notice shall be included in all //copies or substantial portions of the Software. // //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, //FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE //AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, //OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE //SOFTWARE. #include #include "serialization_test_utils.h" #if __cplusplus > 201402L using testing::Eq; #include template using OverloadValue = bitsery::ext::OverloadValue; TEST(SerializeExtensionStdVariant, UseSerializeFunction) { std::variant t1{MyStruct1{978, 15}}; std::variant r1{MyStruct2{}}; SerializationContext ctx; ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{}); ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{}); EXPECT_THAT(t1, Eq(r1)); } TEST(SerializeExtensionStdVariant, WhenTwoIndicesWithSameTypeThenDeserializeCorrectIndex) { std::variant t1{std::in_place_index_t<2>{}, MyStruct1{978, 15}}; std::variant r1{MyStruct2{}}; SerializationContext ctx; ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{}); ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{}); EXPECT_THAT(t1, Eq(r1)); } TEST(SerializeExtensionStdVariant, ValueTypesCanBeSerializedWithLambda) { std::variant t1{5.6f}; std::variant r1{MyStruct1{}}; SerializationContext ctx; auto fncFloat = [](auto& s, float& v) { s.value4b(v); }; auto fncChar = [](auto& s, char& v) { s.value1b(v); }; ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{fncFloat, fncChar}); ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{fncFloat, fncChar}); EXPECT_THAT(t1, Eq(r1)); } TEST(SerializeExtensionStdVariant, ValueTypesCanBeSerializedWithLambdaAndOrCallableObject) { std::variant t1{'Z'}; std::variant r1{MyStruct1{}}; SerializationContext ctx; auto fncFloat = [](auto& s, float& v) { s.value4b(v); }; ctx.createSerializer().ext(t1, bitsery::ext::StdVariant{fncFloat, OverloadValue{}}); ctx.createDeserializer().ext(r1, bitsery::ext::StdVariant{fncFloat, OverloadValue{}}); EXPECT_THAT(t1, Eq(r1)); } TEST(SerializeExtensionStdVariant, CanOverloadDefaultSerializationFunction) { std::variant t1{MyStruct1{5, 9}}; std::variant r1{MyStruct1{}}; SerializationContext ctx; auto exec = [](auto& s, std::variant& o) { using S = decltype(s); s.ext(o, bitsery::ext::StdVariant{ [](S& s, MyStruct1& v) { s.value4b(v.i1); //do not serialize other element, it should be 0 (default) }, OverloadValue{} }); }; ctx.createSerializer().object(t1, exec); ctx.createDeserializer().object(r1, exec); EXPECT_THAT(std::get<1>(r1).i2, Eq(0)); } struct NonDefaultConstructable { explicit NonDefaultConstructable(float x) : _x{x} {} float _x; bool operator==(const NonDefaultConstructable& rhs) const { return _x == rhs._x; } private: friend class bitsery::Access; NonDefaultConstructable() : _x{0.0f} {}; }; TEST(SerializeExtensionStdVariant, CanUseNonDefaultConstructableTypes) { std::variant t1{NonDefaultConstructable{123.456f}}; std::variant r1{MyStruct1{}}; SerializationContext ctx; auto exec = [](auto& s, std::variant& o) { using S = decltype(s); s.ext(o, bitsery::ext::StdVariant{ [](S& s, NonDefaultConstructable& v) { s.value4b(v._x); }, OverloadValue{} }); }; ctx.createSerializer().object(t1, exec); ctx.createDeserializer().object(r1, exec); EXPECT_THAT(t1, Eq(r1)); } TEST(SerializeExtensionStdVariant, CorrectlyHandleMonoState) { std::variant t1{}; std::variant r1{}; SerializationContext ctx; auto exec = [](auto& s, auto& o) { using S = decltype(s); s.ext(o, bitsery::ext::StdVariant{ [](S& s, NonDefaultConstructable& v) { s.value4b(v._x); }, }); }; ctx.createSerializer().object(t1, exec); ctx.createDeserializer().object(r1, exec); EXPECT_THAT(t1, Eq(r1)); std::variant t2{}; std::variant r2{}; SerializationContext ctx1; ctx1.createSerializer().ext(t2, bitsery::ext::StdVariant{}); ctx1.createDeserializer().ext(r2, bitsery::ext::StdVariant{}); EXPECT_THAT(t2, Eq(r2)); } #else #if defined(_MSC_VER) #pragma message("Tests for StdVariant requires C++17") #else #warning "Tests for StdVariant requires C++17" #endif #endif