// MIT License // // Copyright (c) 2017 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 #include #include #include "serialization_test_utils.h" #include using StdMap = bitsery::ext::StdMap; using testing::Eq; template Container createData() { return {}; } template<> std::unordered_map createData>() { return { std::make_pair("some key", MyStruct1{ 874, 456 }), std::make_pair("other key", MyStruct1{ -34, 8645 }), std::make_pair("secret key", MyStruct1{ -4878, 3468975 }) }; } template<> std::unordered_multimap createData>() { return { std::pair(545, 45.485f), std::pair(6748, -7891.5f), std::pair(845, -457.0f) }; } template<> std::map createData>() { return { std::make_pair(MyEnumClass::E3, MyStruct1{ 874, 456 }), std::make_pair(MyEnumClass::E6, MyStruct1{ -34, 8645 }), std::make_pair(MyEnumClass::E2, MyStruct1{ -4878, 3468975 }) }; } template<> std::multimap createData>() { return { // these are optimized with range and entropy std::pair(-45, -984196845ll), std::pair(54, 1ll), std::pair(98, 3ll) }; } template class SerializeExtensionStdMap : public testing::Test { public: using TContainer = T; const TContainer src = createData(); TContainer res{}; }; using SerializeExtensionStdMapTypes = ::testing::Types, std::unordered_multimap, std::map, std::multimap>; TYPED_TEST_SUITE(SerializeExtensionStdMap, SerializeExtensionStdMapTypes, ); namespace bitsery { template void serialize(S& s, std::unordered_map& o) { s.ext(o, StdMap{ 10 }, [](S& s, std::string& key, MyStruct1& value) { s.text1b(key, 100); s.object(value); }); } template void serialize(S& s, std::unordered_multimap& o) { s.ext(o, StdMap{ 10 }, [](S& s, int32_t& key, float& value) { s.value4b(key); s.value4b(value); }); } template void serialize(S& s, std::map& o) { s.ext(o, StdMap{ 10 }, [](S& s, MyEnumClass& key, MyStruct1& value) { s.value4b(key); s.object(value); }); } template void serialize(S& s, std::multimap& o) { s.ext(o, StdMap{ 10 }, [](S& s, int32_t& key, int64_t& value) { s.enableBitPacking([&key, &value](typename S::BPEnabledType& sbp) { int64_t values[3]{ 1ll, 2ll, 3ll }; sbp.ext(key, bitsery::ext::ValueRange{ -100, 100 }); sbp.ext8b(value, bitsery::ext::Entropy{ values }); }); }); } } TYPED_TEST(SerializeExtensionStdMap, SerializeAndDeserializeEquals) { SerializationContext ctx1; ctx1.createSerializer().object(this->src); ctx1.createDeserializer().object(this->res); EXPECT_THAT(this->res, Eq(this->src)); }