diff options
author | akhropov <akhropov@yandex-team.ru> | 2022-02-10 16:46:32 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:32 +0300 |
commit | 00afc96e9c0298054b7386fa7fb9e3cc3d67b974 (patch) | |
tree | cb7a9f4a92c0d4cc5a86eeed49ad71e810953c1f /library/cpp/binsaver | |
parent | 83a8efcf3af051e3dd59c00d1d5dafc96412ec1e (diff) | |
download | ydb-00afc96e9c0298054b7386fa7fb9e3cc3d67b974.tar.gz |
Restoring authorship annotation for <akhropov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/binsaver')
-rw-r--r-- | library/cpp/binsaver/bin_saver.h | 104 | ||||
-rw-r--r-- | library/cpp/binsaver/ut/binsaver_ut.cpp | 58 | ||||
-rw-r--r-- | library/cpp/binsaver/ut/ya.make | 2 | ||||
-rw-r--r-- | library/cpp/binsaver/ut_util/README.md | 2 | ||||
-rw-r--r-- | library/cpp/binsaver/ut_util/ut_util.cpp | 2 | ||||
-rw-r--r-- | library/cpp/binsaver/ut_util/ut_util.h | 134 | ||||
-rw-r--r-- | library/cpp/binsaver/ut_util/ya.make | 24 | ||||
-rw-r--r-- | library/cpp/binsaver/util_stream_io.h | 20 |
8 files changed, 173 insertions, 173 deletions
diff --git a/library/cpp/binsaver/bin_saver.h b/library/cpp/binsaver/bin_saver.h index 412424889f..88de676e8f 100644 --- a/library/cpp/binsaver/bin_saver.h +++ b/library/cpp/binsaver/bin_saver.h @@ -10,8 +10,8 @@ #include <util/generic/list.h> #include <util/generic/maybe.h> #include <util/generic/bitmap.h> -#include <util/generic/variant.h> -#include <util/generic/ylimits.h> +#include <util/generic/variant.h> +#include <util/generic/ylimits.h> #include <util/memory/blob.h> #include <util/digest/murmur.h> @@ -404,13 +404,13 @@ public: template <class T1, size_t N> int Add(const chunk_id, std::array<T1, N>* pData) { if (HasNonTrivialSerializer<T1>(0u)) { - for (size_t i = 0; i < N; ++i) - Add(1, &(*pData)[i]); - } else { + for (size_t i = 0; i < N; ++i) + Add(1, &(*pData)[i]); + } else { DataChunk((void*)pData->data(), pData->size() * sizeof(T1)); - } - return 0; - } + } + return 0; + } template <size_t N> int Add(const chunk_id, std::bitset<N>* pData) { @@ -451,46 +451,46 @@ public: return 0; } - template <class TVariantClass> - struct TLoadFromTypeFromListHelper { - template <class T0, class... TTail> - static void Do(IBinSaver& binSaver, ui32 typeIndex, TVariantClass* pData) { - if constexpr (sizeof...(TTail) == 0) { - Y_ASSERT(typeIndex == 0); - T0 chunk; - binSaver.Add(2, &chunk); - *pData = std::move(chunk); - } else { - if (typeIndex == 0) { - Do<T0>(binSaver, 0, pData); - } else { - Do<TTail...>(binSaver, typeIndex - 1, pData); - } - } - } - }; - - template <class... TVariantTypes> + template <class TVariantClass> + struct TLoadFromTypeFromListHelper { + template <class T0, class... TTail> + static void Do(IBinSaver& binSaver, ui32 typeIndex, TVariantClass* pData) { + if constexpr (sizeof...(TTail) == 0) { + Y_ASSERT(typeIndex == 0); + T0 chunk; + binSaver.Add(2, &chunk); + *pData = std::move(chunk); + } else { + if (typeIndex == 0) { + Do<T0>(binSaver, 0, pData); + } else { + Do<TTail...>(binSaver, typeIndex - 1, pData); + } + } + } + }; + + template <class... TVariantTypes> int Add(const chunk_id, std::variant<TVariantTypes...>* pData) { static_assert(std::variant_size_v<std::variant<TVariantTypes...>> < Max<ui32>()); - - ui32 index; - if (IsReading()) { - Add(1, &index); + + ui32 index; + if (IsReading()) { + Add(1, &index); TLoadFromTypeFromListHelper<std::variant<TVariantTypes...>>::template Do<TVariantTypes...>( - *this, - index, - pData - ); - } else { - index = pData->index(); // type cast is safe because of static_assert check above - Add(1, &index); + *this, + index, + pData + ); + } else { + index = pData->index(); // type cast is safe because of static_assert check above + Add(1, &index); std::visit([&](auto& dst) -> void { Add(2, &dst); }, *pData); - } - return 0; - } - - + } + return 0; + } + + void AddPolymorphicBase(chunk_id, IObjectBase* pObject) { (*pObject) & (*this); } @@ -504,9 +504,9 @@ public: else StoreObject(pData->GetBarePtr()); } - template <class T, class TPolicy> - int Add(const chunk_id, TMaybe<T, TPolicy>* pData) { - TMaybe<T, TPolicy>& data = *pData; + template <class T, class TPolicy> + int Add(const chunk_id, TMaybe<T, TPolicy>* pData) { + TMaybe<T, TPolicy>& data = *pData; if (IsReading()) { bool defined = false; Add(1, &defined); @@ -585,23 +585,23 @@ struct TRegisterSaveLoadType { #define REGISTER_SAVELOAD_CLASS(N, name) \ BASIC_REGISTER_CLASS(name) \ static TRegisterSaveLoadType<name> init##name##N(N); - + // using TObj/TRef on forward declared templ class will not work // but multiple registration with same id is allowed #define REGISTER_SAVELOAD_TEMPL1_CLASS(N, className, T) \ static TRegisterSaveLoadType<className<T>> init##className##T##N(N); - + #define REGISTER_SAVELOAD_TEMPL2_CLASS(N, className, T1, T2) \ typedef className<T1, T2> temp##className##T1##_##T2##temp; \ static TRegisterSaveLoadType<className<T1, T2>> init##className##T1##_##T2##N(N); - + #define REGISTER_SAVELOAD_TEMPL3_CLASS(N, className, T1, T2, T3) \ typedef className<T1, T2, T3> temp##className##T1##_##T2##_##T3##temp; \ static TRegisterSaveLoadType<className<T1, T2, T3>> init##className##T1##_##T2##_##T3##N(N); - + #define REGISTER_SAVELOAD_NM_CLASS(N, nmspace, className) \ BASIC_REGISTER_CLASS(nmspace::className) \ - static TRegisterSaveLoadType<nmspace::className> init_##nmspace##_##name##N(N); + static TRegisterSaveLoadType<nmspace::className> init_##nmspace##_##name##N(N); #define REGISTER_SAVELOAD_NM2_CLASS(N, nmspace1, nmspace2, className) \ BASIC_REGISTER_CLASS(nmspace1::nmspace2::className) \ diff --git a/library/cpp/binsaver/ut/binsaver_ut.cpp b/library/cpp/binsaver/ut/binsaver_ut.cpp index 37eba5406f..a42d161947 100644 --- a/library/cpp/binsaver/ut/binsaver_ut.cpp +++ b/library/cpp/binsaver/ut/binsaver_ut.cpp @@ -80,51 +80,51 @@ UNIT_ASSERT(IBinSaver::HasNonTrivialSerializer<TVector<TCustomSerializer>>(0u)); Y_UNIT_TEST(TestStroka) { - TestBinSaverSerialization(TString("QWERTY")); + TestBinSaverSerialization(TString("QWERTY")); } Y_UNIT_TEST(TestMoveOnlyType) { - TestBinSaverSerializationToBuffer(TMoveOnlyType()); + TestBinSaverSerializationToBuffer(TMoveOnlyType()); } Y_UNIT_TEST(TestVectorStrok) { - TestBinSaverSerialization(TVector<TString>{"A", "B", "C"}); + TestBinSaverSerialization(TVector<TString>{"A", "B", "C"}); } Y_UNIT_TEST(TestCArray) { - TestBinSaverSerialization(TTypeWithArray()); + TestBinSaverSerialization(TTypeWithArray()); } Y_UNIT_TEST(TestSets) { - TestBinSaverSerialization(THashSet<TString>{"A", "B", "C"}); - TestBinSaverSerialization(TSet<TString>{"A", "B", "C"}); + TestBinSaverSerialization(THashSet<TString>{"A", "B", "C"}); + TestBinSaverSerialization(TSet<TString>{"A", "B", "C"}); } Y_UNIT_TEST(TestMaps) { - TestBinSaverSerialization(THashMap<TString, ui32>{{"A", 1}, {"B", 2}, {"C", 3}}); - TestBinSaverSerialization(TMap<TString, ui32>{{"A", 1}, {"B", 2}, {"C", 3}}); + TestBinSaverSerialization(THashMap<TString, ui32>{{"A", 1}, {"B", 2}, {"C", 3}}); + TestBinSaverSerialization(TMap<TString, ui32>{{"A", 1}, {"B", 2}, {"C", 3}}); } Y_UNIT_TEST(TestBlob) { - TestBinSaverSerialization(TBlob::FromStringSingleThreaded("qwerty")); + TestBinSaverSerialization(TBlob::FromStringSingleThreaded("qwerty")); } -Y_UNIT_TEST(TestVariant) { - { +Y_UNIT_TEST(TestVariant) { + { using T = std::variant<TString, int>; - - TestBinSaverSerialization(T(TString(""))); - TestBinSaverSerialization(T(0)); - } - { + + TestBinSaverSerialization(T(TString(""))); + TestBinSaverSerialization(T(0)); + } + { using T = std::variant<TString, int, float>; - - TestBinSaverSerialization(T(TString("ask"))); - TestBinSaverSerialization(T(12)); - TestBinSaverSerialization(T(0.64f)); - } -} - + + TestBinSaverSerialization(T(TString("ask"))); + TestBinSaverSerialization(T(12)); + TestBinSaverSerialization(T(0.64f)); + } +} + Y_UNIT_TEST(TestPod) { struct TPod { ui32 A = 5; @@ -133,12 +133,12 @@ Y_UNIT_TEST(TestPod) { return A == other.A && B == other.B; } }; - TestBinSaverSerialization(TPod()); + TestBinSaverSerialization(TPod()); TPod custom; custom.A = 25; custom.B = 37; - TestBinSaverSerialization(custom); - TestBinSaverSerialization(TVector<TPod>{custom}); + TestBinSaverSerialization(custom); + TestBinSaverSerialization(TVector<TPod>{custom}); } Y_UNIT_TEST(TestSubPod) { @@ -158,12 +158,12 @@ Y_UNIT_TEST(TestSubPod) { return B == other.B; } }; - TestBinSaverSerialization(TPod()); + TestBinSaverSerialization(TPod()); TPod::TSub sub; sub.X = 1; TPod custom; custom.B = {sub}; - TestBinSaverSerialization(TVector<TPod>{custom}); + TestBinSaverSerialization(TVector<TPod>{custom}); } Y_UNIT_TEST(TestMemberAndOpIsMain) { @@ -192,7 +192,7 @@ Y_UNIT_TEST(TestMemberAndOpIsMain) { obj.S = "TString"; obj.A = 42; - TestBinSaverSerialization(obj); + TestBinSaverSerialization(obj); } } ; diff --git a/library/cpp/binsaver/ut/ya.make b/library/cpp/binsaver/ut/ya.make index 43dc20bff7..a60a2684f9 100644 --- a/library/cpp/binsaver/ut/ya.make +++ b/library/cpp/binsaver/ut/ya.make @@ -7,5 +7,5 @@ SRCS( ) PEERDIR(library/cpp/binsaver/ut_util) - + END() diff --git a/library/cpp/binsaver/ut_util/README.md b/library/cpp/binsaver/ut_util/README.md index 41641cd1e1..9f7e1b0b94 100644 --- a/library/cpp/binsaver/ut_util/README.md +++ b/library/cpp/binsaver/ut_util/README.md @@ -1 +1 @@ -Library for testing BinSaver serialization.
\ No newline at end of file +Library for testing BinSaver serialization.
\ No newline at end of file diff --git a/library/cpp/binsaver/ut_util/ut_util.cpp b/library/cpp/binsaver/ut_util/ut_util.cpp index 4cd8daa931..8e466a5acd 100644 --- a/library/cpp/binsaver/ut_util/ut_util.cpp +++ b/library/cpp/binsaver/ut_util/ut_util.cpp @@ -1 +1 @@ -#include "ut_util.h" +#include "ut_util.h" diff --git a/library/cpp/binsaver/ut_util/ut_util.h b/library/cpp/binsaver/ut_util/ut_util.h index 52e7bcf8e1..be8f89cc7c 100644 --- a/library/cpp/binsaver/ut_util/ut_util.h +++ b/library/cpp/binsaver/ut_util/ut_util.h @@ -1,71 +1,71 @@ -#pragma once - +#pragma once + #include <library/cpp/binsaver/bin_saver.h> #include <library/cpp/binsaver/mem_io.h> #include <library/cpp/binsaver/util_stream_io.h> - + #include <library/cpp/testing/unittest/registar.h> - -#include <util/generic/vector.h> -#include <util/stream/buffer.h> - -#include <functional> - - -/* comparerChecksInside == true means comparer uses UNIT_ASSERT... inside itself - * comparerChecksInside == false means comparer returns if its arguments are equal - */ - -template <class T, class TComparer = std::equal_to<T>, bool comparerChecksInside = false> -void UnitTestCheckWithComparer(const T& lhs, const T& rhs, const TComparer& comparer) { - if constexpr (comparerChecksInside) { - comparer(lhs, rhs); - } else { - UNIT_ASSERT(comparer(lhs, rhs)); - } -} - - -/* comparerChecksInside == true means comparer uses UNIT_ASSERT... inside itself - * comparerChecksInside == false means comparer returns true if its arguments are equal - */ - -template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> -void TestBinSaverSerializationToBuffer(const T& original, const TComparer& comparer = TComparer()) { - TBufferOutput out; - { - TYaStreamOutput yaOut(out); - - IBinSaver f(yaOut, false, false); - f.Add(0, const_cast<T*>(&original)); - } - TBufferInput in(out.Buffer()); - T restored; - { - TYaStreamInput yaIn(in); - IBinSaver f(yaIn, true, false); - f.Add(0, &restored); - } - UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored, comparer); -} - -template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> -void TestBinSaverSerializationToVector(const T& original, const TComparer& comparer = TComparer()) { - TVector<char> out; - SerializeToMem(&out, *const_cast<T*>(&original)); - T restored; - SerializeFromMem(&out, restored); - UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored, comparer); - - TVector<TVector<char>> out2D; - SerializeToMem(&out2D, *const_cast<T*>(&original)); - T restored2D; - SerializeFromMem(&out2D, restored2D); - UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored2D, comparer); -} - -template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> -void TestBinSaverSerialization(const T& original, const TComparer& comparer = TComparer()) { - TestBinSaverSerializationToBuffer<T, TComparer, comparerChecksInside>(original, comparer); - TestBinSaverSerializationToVector<T, TComparer, comparerChecksInside>(original, comparer); -} + +#include <util/generic/vector.h> +#include <util/stream/buffer.h> + +#include <functional> + + +/* comparerChecksInside == true means comparer uses UNIT_ASSERT... inside itself + * comparerChecksInside == false means comparer returns if its arguments are equal + */ + +template <class T, class TComparer = std::equal_to<T>, bool comparerChecksInside = false> +void UnitTestCheckWithComparer(const T& lhs, const T& rhs, const TComparer& comparer) { + if constexpr (comparerChecksInside) { + comparer(lhs, rhs); + } else { + UNIT_ASSERT(comparer(lhs, rhs)); + } +} + + +/* comparerChecksInside == true means comparer uses UNIT_ASSERT... inside itself + * comparerChecksInside == false means comparer returns true if its arguments are equal + */ + +template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> +void TestBinSaverSerializationToBuffer(const T& original, const TComparer& comparer = TComparer()) { + TBufferOutput out; + { + TYaStreamOutput yaOut(out); + + IBinSaver f(yaOut, false, false); + f.Add(0, const_cast<T*>(&original)); + } + TBufferInput in(out.Buffer()); + T restored; + { + TYaStreamInput yaIn(in); + IBinSaver f(yaIn, true, false); + f.Add(0, &restored); + } + UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored, comparer); +} + +template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> +void TestBinSaverSerializationToVector(const T& original, const TComparer& comparer = TComparer()) { + TVector<char> out; + SerializeToMem(&out, *const_cast<T*>(&original)); + T restored; + SerializeFromMem(&out, restored); + UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored, comparer); + + TVector<TVector<char>> out2D; + SerializeToMem(&out2D, *const_cast<T*>(&original)); + T restored2D; + SerializeFromMem(&out2D, restored2D); + UnitTestCheckWithComparer<T, TComparer, comparerChecksInside>(original, restored2D, comparer); +} + +template <typename T, typename TComparer = std::equal_to<T>, bool comparerChecksInside = false> +void TestBinSaverSerialization(const T& original, const TComparer& comparer = TComparer()) { + TestBinSaverSerializationToBuffer<T, TComparer, comparerChecksInside>(original, comparer); + TestBinSaverSerializationToVector<T, TComparer, comparerChecksInside>(original, comparer); +} diff --git a/library/cpp/binsaver/ut_util/ya.make b/library/cpp/binsaver/ut_util/ya.make index 7e60f13ef3..25c867f631 100644 --- a/library/cpp/binsaver/ut_util/ya.make +++ b/library/cpp/binsaver/ut_util/ya.make @@ -1,14 +1,14 @@ -LIBRARY() - -OWNER(gulin) - -SRCS( - ut_util.cpp -) - -PEERDIR( +LIBRARY() + +OWNER(gulin) + +SRCS( + ut_util.cpp +) + +PEERDIR( library/cpp/binsaver library/cpp/testing/unittest -) - -END() +) + +END() diff --git a/library/cpp/binsaver/util_stream_io.h b/library/cpp/binsaver/util_stream_io.h index d65d630b93..31d8df5351 100644 --- a/library/cpp/binsaver/util_stream_io.h +++ b/library/cpp/binsaver/util_stream_io.h @@ -4,11 +4,11 @@ #include <util/stream/input.h> #include <util/stream/output.h> -#include <util/stream/file.h> +#include <util/stream/file.h> class TYaStreamInput: public IBinaryStream { IInputStream& Stream; - + int WriteImpl(const void*, int) override { Y_ASSERT(0); return 0; @@ -41,13 +41,13 @@ inline void SerializeFromStream(IInputStream& stream, T& c) { template <class T> inline void SerializeFromFile(const TString& fileName, T& c) { - TIFStream in(fileName); - SerializeFromStream(in, c); -} - + TIFStream in(fileName); + SerializeFromStream(in, c); +} + class TYaStreamOutput: public IBinaryStream { IOutputStream& Stream; - + int WriteImpl(const void* what, int size) override { Stream.Write(what, (size_t)size); return size; @@ -78,9 +78,9 @@ inline void SerializeToArcadiaStream(IOutputStream& stream, T& c) { bs.Add(1, &c); } } - + template <class T> inline void SerializeToFile(const TString& fileName, T& c) { - TOFStream out(fileName); + TOFStream out(fileName); SerializeToArcadiaStream(out, c); -} +} |