diff options
author | ishfb <ishfb@yandex-team.ru> | 2022-02-10 16:48:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:08 +0300 |
commit | 069c69f58fd78de3cf2e6eb2304047281e21fb65 (patch) | |
tree | b222e5ac2e2e98872661c51ccceee5da0d291e13 /util | |
parent | 0170772a2dbf133f32e17ca137ff64790d43831f (diff) | |
download | ydb-069c69f58fd78de3cf2e6eb2304047281e21fb65.tar.gz |
Restoring authorship annotation for <ishfb@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util')
-rw-r--r-- | util/generic/algorithm.h | 102 | ||||
-rw-r--r-- | util/generic/algorithm_ut.cpp | 186 | ||||
-rw-r--r-- | util/generic/fwd.h | 4 | ||||
-rw-r--r-- | util/generic/iterator_range_ut.cpp | 16 | ||||
-rw-r--r-- | util/generic/ptr.h | 24 | ||||
-rw-r--r-- | util/generic/ptr_ut.cpp | 276 | ||||
-rw-r--r-- | util/generic/xrange.h | 2 | ||||
-rw-r--r-- | util/generic/xrange_ut.cpp | 82 | ||||
-rw-r--r-- | util/ysaveload.h | 24 | ||||
-rw-r--r-- | util/ysaveload_ut.cpp | 44 |
10 files changed, 380 insertions, 380 deletions
diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h index f905557de7..badfb88993 100644 --- a/util/generic/algorithm.h +++ b/util/generic/algorithm.h @@ -4,35 +4,35 @@ #include "utility.h" #include <util/system/defaults.h> -#include <util/generic/fwd.h> +#include <util/generic/fwd.h> #include <numeric> #include <algorithm> #include <iterator> #include <utility> -namespace NPrivate { - template <class I, class F, class P> - I ExtremeElementBy(I begin, I end, F func, P pred) { - if (begin == end) { - return end; - } - - auto bestValue = func(*begin); - auto bestPos = begin; - - for (auto i = ++begin; i != end; ++i) { - auto curValue = func(*i); - if (pred(curValue, bestValue)) { - bestValue = curValue; - bestPos = i; - } - } - - return bestPos; - } -} - +namespace NPrivate { + template <class I, class F, class P> + I ExtremeElementBy(I begin, I end, F func, P pred) { + if (begin == end) { + return end; + } + + auto bestValue = func(*begin); + auto bestPos = begin; + + for (auto i = ++begin; i != end; ++i) { + auto curValue = func(*i); + if (pred(curValue, bestValue)) { + bestValue = curValue; + bestPos = i; + } + } + + return bestPos; + } +} + template <class T> static inline void Sort(T f, T l) { std::sort(f, l); @@ -145,14 +145,14 @@ static inline I FindIf(I f, I l, P p) { return std::find_if(f, l, p); } -template <class C, class P> -static inline auto FindIf(C&& c, P p) { - using std::begin; - using std::end; - - return FindIf(begin(c), end(c), p); -} - +template <class C, class P> +static inline auto FindIf(C&& c, P p) { + using std::begin; + using std::end; + + return FindIf(begin(c), end(c), p); +} + template <class I, class P> static inline bool AllOf(I f, I l, P pred) { return std::all_of(f, l, pred); @@ -517,38 +517,38 @@ static inline T MaxElement(T begin, T end, C comp) { return std::max_element(begin, end, comp); } -template <class I, class F> -I MaxElementBy(I begin, I end, F&& func) { - using TValue = decltype(func(*begin)); - return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TGreater<TValue>()); -} - -template <class C, class F> +template <class I, class F> +I MaxElementBy(I begin, I end, F&& func) { + using TValue = decltype(func(*begin)); + return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TGreater<TValue>()); +} + +template <class C, class F> auto MaxElementBy(C& c, F&& func) { return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); } template <class C, class F> auto MaxElementBy(const C& c, F&& func) { - return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); -} - -template <class I, class F> -I MinElementBy(I begin, I end, F&& func) { - using TValue = decltype(func(*begin)); - return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TLess<TValue>()); -} - -template <class C, class F> + return MaxElementBy(std::begin(c), std::end(c), std::forward<F>(func)); +} + +template <class I, class F> +I MinElementBy(I begin, I end, F&& func) { + using TValue = decltype(func(*begin)); + return ::NPrivate::ExtremeElementBy(begin, end, std::forward<F>(func), TLess<TValue>()); +} + +template <class C, class F> auto MinElementBy(C& c, F&& func) { return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); } template <class C, class F> auto MinElementBy(const C& c, F&& func) { - return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); -} - + return MinElementBy(std::begin(c), std::end(c), std::forward<F>(func)); +} + template <class TOp, class... TArgs> void ApplyToMany(TOp op, TArgs&&... args) { int dummy[] = {((void)op(std::forward<TArgs>(args)), 0)...}; diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index bdb9e594f0..8d732fcc0c 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -611,91 +611,91 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT_EQUAL(p2.first, TString("data")); UNIT_ASSERT_EQUAL(p2.second, TString("test")); } - + Y_UNIT_TEST(TestMaxElementBy) { - const int array[] = {1, 2, 5, 3, 4, 5}; - UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(array, [](int x) { - return x * x; + const int array[] = {1, 2, 5, 3, 4, 5}; + UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(array, [](int x) { + return x * x; }), 5); - + const TVector<int> vec(array, array + Y_ARRAY_SIZE(array)); - UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(vec, [](int x) { - return -1.0 * x; + UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(vec, [](int x) { + return -1.0 * x; }), 1); - + int arrayMutable[] = {1, 2, 5, 3, 4, 5}; auto maxPtr = MaxElementBy(arrayMutable, [](int x) { return x; }); *maxPtr += 100; UNIT_ASSERT_VALUES_EQUAL(*maxPtr, 105); - auto identity = [](char x) { - return x; - }; - auto singleElementSequence = {'z'}; - UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(singleElementSequence, identity), 'z'); - + auto identity = [](char x) { + return x; + }; + auto singleElementSequence = {'z'}; + UNIT_ASSERT_VALUES_EQUAL(*MaxElementBy(singleElementSequence, identity), 'z'); + const TString strings[] = {"one", "two", "three", "four"}; UNIT_ASSERT_STRINGS_EQUAL(*MaxElementBy(strings, [](TString s) { return s.size(); }), "three"); - } - + } + Y_UNIT_TEST(TestMinElementBy) { - const int array[] = {2, 3, 4, 1, 5}; - UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(array, [](int x) -> char { - return 'a' + x; + const int array[] = {2, 3, 4, 1, 5}; + UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(array, [](int x) -> char { + return 'a' + x; }), 1); - + const TVector<int> vec(std::begin(array), std::end(array)); - UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(vec, [](int x) { - return -x; + UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(vec, [](int x) { + return -x; }), 5); - + int arrayMutable[] = {1, 2, 5, 3, 4, 5}; auto minPtr = MinElementBy(arrayMutable, [](int x) { return x; }); *minPtr += 100; UNIT_ASSERT_VALUES_EQUAL(*minPtr, 101); - auto identity = [](char x) { - return x; - }; - auto singleElementSequence = {'z'}; - UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(singleElementSequence, identity), 'z'); - + auto identity = [](char x) { + return x; + }; + auto singleElementSequence = {'z'}; + UNIT_ASSERT_VALUES_EQUAL(*MinElementBy(singleElementSequence, identity), 'z'); + const TVector<TStringBuf> strings = {"one", "two", "three", "four"}; - auto stringLength = [](TStringBuf s) { - return s.size(); - }; - UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings, stringLength), "one"); - UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings.rbegin(), strings.rend(), stringLength), "two"); - } - + auto stringLength = [](TStringBuf s) { + return s.size(); + }; + UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings, stringLength), "one"); + UNIT_ASSERT_STRINGS_EQUAL(*MinElementBy(strings.rbegin(), strings.rend(), stringLength), "two"); + } + Y_UNIT_TEST(MaxElementByReturnsEndForEmptyRange) { const TVector<int> empty; - UNIT_ASSERT_EQUAL(MaxElementBy(empty, [](int) { return 0; }), empty.end()); - } - + UNIT_ASSERT_EQUAL(MaxElementBy(empty, [](int) { return 0; }), empty.end()); + } + Y_UNIT_TEST(MaxElementByDoesntCallFunctorForEmptyRange) { const TVector<int> empty; - auto functor = [](int) { - UNIT_ASSERT(false); - return 0; - }; - MaxElementBy(empty, functor); - } - + auto functor = [](int) { + UNIT_ASSERT(false); + return 0; + }; + MaxElementBy(empty, functor); + } + Y_UNIT_TEST(MinElementByReturnsEndForEmptyRange) { const TVector<int> empty; - UNIT_ASSERT_EQUAL(MinElementBy(empty, [](int) { return 0; }), empty.end()); - } - + UNIT_ASSERT_EQUAL(MinElementBy(empty, [](int) { return 0; }), empty.end()); + } + Y_UNIT_TEST(MinElementByDoesntCallFunctorForEmptyRange) { const TVector<int> empty; - auto functor = [](int) { - UNIT_ASSERT(false); - return 0; - }; - MinElementBy(empty, functor); - } - + auto functor = [](int) { + UNIT_ASSERT(false); + return 0; + }; + MinElementBy(empty, functor); + } + Y_UNIT_TEST(TestApplyToMany) { int res = 0; ApplyToMany([&res](auto v) { res += v; }, 1, 2, 3, 4, 5); @@ -757,45 +757,45 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } Y_UNIT_TEST(FindIfForContainer) { - using std::begin; - using std::end; - - int array[] = {1, 2, 3, 4, 5}; - UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x == 1; }), begin(array)); - UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x > 5; }), end(array)); - + using std::begin; + using std::end; + + int array[] = {1, 2, 3, 4, 5}; + UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x == 1; }), begin(array)); + UNIT_ASSERT_EQUAL(FindIf(array, [](int x) { return x > 5; }), end(array)); + TVector<int> vector = {1, 2, 3, 4, 5}; - UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x == 1; }), begin(vector)); - UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x > 5; }), end(vector)); - - // Compilability test. Check if the returned iterator is non const - auto iter = FindIf(vector, [](int x) { return x == 1; }); - *iter = 5; - - // Compilability test. Check if the returned iterator is const. Should not compile + UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x == 1; }), begin(vector)); + UNIT_ASSERT_EQUAL(FindIf(vector, [](int x) { return x > 5; }), end(vector)); + + // Compilability test. Check if the returned iterator is non const + auto iter = FindIf(vector, [](int x) { return x == 1; }); + *iter = 5; + + // Compilability test. Check if the returned iterator is const. Should not compile const TVector<int> constVector = {1, 2, 3, 4, 5}; - auto constIter = FindIf(constVector, [](int x) { return x == 1; }); - Y_UNUSED(constIter); - // *constIter = 5; - } - - struct TRange { - }; - - const TRange* begin(const TRange& r) { - return &r; - } - - const TRange* end(const TRange& r) { - return &r + 1; - } - + auto constIter = FindIf(constVector, [](int x) { return x == 1; }); + Y_UNUSED(constIter); + // *constIter = 5; + } + + struct TRange { + }; + + const TRange* begin(const TRange& r) { + return &r; + } + + const TRange* end(const TRange& r) { + return &r + 1; + } + Y_UNIT_TEST(FindIfForUserType) { - // Compileability test. Should work for user types with begin/end overloads - TRange range; - auto i = FindIf(range, [](auto) { return false; }); - Y_UNUSED(i); - } + // Compileability test. Should work for user types with begin/end overloads + TRange range; + auto i = FindIf(range, [](auto) { return false; }); + Y_UNUSED(i); + } Y_UNIT_TEST(TestLowerBoundBy) { using TIntPairs = TVector<std::pair<i32, i32>>; @@ -847,4 +847,4 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { std::vector<int> v = {1, 2, 777}; UNIT_ASSERT_VALUES_EQUAL(TString("begin;1;2;777"), Accumulate(v, TString("begin"), [](auto&& a, auto& b) { return a + ";" + ToString(b); })); } -}; +}; diff --git a/util/generic/fwd.h b/util/generic/fwd.h index 79b6ad8515..5cc2da40e5 100644 --- a/util/generic/fwd.h +++ b/util/generic/fwd.h @@ -26,8 +26,8 @@ template <class T = void> struct TLess; template <class T = void> -struct TGreater; - +struct TGreater; + template <class T = void> struct TEqualTo; diff --git a/util/generic/iterator_range_ut.cpp b/util/generic/iterator_range_ut.cpp index eb65d65808..a7e3670ae1 100644 --- a/util/generic/iterator_range_ut.cpp +++ b/util/generic/iterator_range_ut.cpp @@ -1,15 +1,15 @@ #include "iterator_range.h" - + #include <library/cpp/testing/unittest/registar.h> -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/generic/vector.h> - + Y_UNIT_TEST_SUITE(IteratorRange) { Y_UNIT_TEST(DefaultConstructor) { TIteratorRange<int*> range; UNIT_ASSERT(range.empty()); } - + Y_UNIT_TEST(DefaultConstructorSentinel) { TIteratorRange<int*, void*> range; UNIT_ASSERT(range.empty()); @@ -21,7 +21,7 @@ Y_UNIT_TEST_SUITE(IteratorRange) { Y_UNUSED(i); } } - + Y_UNIT_TEST(RangeBasedForLoopSentinel) { // compileability test for (int i : TIteratorRange<int*, void*>()) { @@ -35,8 +35,8 @@ Y_UNIT_TEST_SUITE(IteratorRange) { UNIT_ASSERT_VALUES_EQUAL(range.size(), Y_ARRAY_SIZE(values)); UNIT_ASSERT(Equal(range.begin(), range.end(), values)); UNIT_ASSERT(!range.empty()); - } - + } + Y_UNIT_TEST(WorksSentinel) { struct TRangeSentinel { }; @@ -95,4 +95,4 @@ Y_UNIT_TEST_SUITE(IteratorRange) { // we should be able to use TIteratorRange as a container parameter for standard algorithms UNIT_ASSERT(AllOf(range, [](int x) { return x > 0; })); } -} +} diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 2f2d9ec08f..19db0e3ec5 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -262,13 +262,13 @@ public: : T_(that.Release()) { } - + template <class U, class = TGuardConversion<T, U>> inline THolder(THolder<U, D>&& that) noexcept : T_(that.Release()) { } - + THolder(const THolder&) = delete; THolder& operator=(const THolder&) = delete; @@ -320,13 +320,13 @@ public: this->Reset(that.Release()); return *this; } - + template <class U> THolder& operator=(THolder<U, D>&& that) noexcept { this->Reset(that.Release()); return *this; } - + #ifdef __cpp_impl_three_way_comparison template <class Other> inline bool operator==(const Other& p) const noexcept { @@ -347,8 +347,8 @@ private: template <typename T, typename... Args> [[nodiscard]] THolder<T> MakeHolder(Args&&... args) { return THolder<T>(new T(std::forward<Args>(args)...)); -} - +} + /* * usage: * class T: public TRefCounted<T> @@ -950,18 +950,18 @@ using TSimpleSharedPtr = TSharedPtr<T, TSimpleCounter, D>; template <typename T, typename C, typename... Args> [[nodiscard]] TSharedPtr<T, C> MakeShared(Args&&... args) { return new T{std::forward<Args>(args)...}; -} - +} + template <typename T, typename... Args> [[nodiscard]] inline TAtomicSharedPtr<T> MakeAtomicShared(Args&&... args) { return MakeShared<T, TAtomicCounter>(std::forward<Args>(args)...); -} - +} + template <typename T, typename... Args> [[nodiscard]] inline TSimpleSharedPtr<T> MakeSimpleShared(Args&&... args) { return MakeShared<T, TSimpleCounter>(std::forward<Args>(args)...); -} - +} + class TCopyClone { public: template <class T> diff --git a/util/generic/ptr_ut.cpp b/util/generic/ptr_ut.cpp index c32a691093..c2dcff23f6 100644 --- a/util/generic/ptr_ut.cpp +++ b/util/generic/ptr_ut.cpp @@ -18,7 +18,7 @@ class TPointerTest: public TTestBase { UNIT_TEST(TestHolderPtrMoveConstructorInheritance); UNIT_TEST(TestHolderPtrMoveAssignment); UNIT_TEST(TestHolderPtrMoveAssignmentInheritance); - UNIT_TEST(TestMakeHolder); + UNIT_TEST(TestMakeHolder); UNIT_TEST(TestTrulePtr); UNIT_TEST(TestAutoToHolder); UNIT_TEST(TestCopyPtr); @@ -30,7 +30,7 @@ class TPointerTest: public TTestBase { UNIT_TEST(TestCopyOnWritePtr1); UNIT_TEST(TestCopyOnWritePtr2); UNIT_TEST(TestOperatorBool); - UNIT_TEST(TestMakeShared); + UNIT_TEST(TestMakeShared); UNIT_TEST(TestComparison); UNIT_TEST(TestSimpleIntrusivePtrCtorTsan); UNIT_TEST(TestRefCountedPtrsInHashSet) @@ -70,7 +70,7 @@ private: void TestHolderPtrMoveConstructorInheritance(); void TestHolderPtrMoveAssignment(); void TestHolderPtrMoveAssignmentInheritance(); - void TestMakeHolder(); + void TestMakeHolder(); void TestTrulePtr(); void TestAutoToHolder(); void TestCopyPtr(); @@ -82,7 +82,7 @@ private: void TestCopyOnWritePtr1(); void TestCopyOnWritePtr2(); void TestOperatorBool(); - void TestMakeShared(); + void TestMakeShared(); void TestComparison(); template <class T, class TRefCountedPtr> void TestRefCountedPtrsInHashSetImpl(); @@ -148,97 +148,97 @@ void TPointerTest::TestHolderPtr() { UNIT_ASSERT_VALUES_EQUAL(cnt, 0); } -THolder<int> CreateInt(int value) { - THolder<int> res(new int); - *res = value; - return res; -} - +THolder<int> CreateInt(int value) { + THolder<int> res(new int); + *res = value; + return res; +} + void TPointerTest::TestHolderPtrMoveConstructor() { - THolder<int> h = CreateInt(42); - UNIT_ASSERT_VALUES_EQUAL(*h, 42); -} - + THolder<int> h = CreateInt(42); + UNIT_ASSERT_VALUES_EQUAL(*h, 42); +} + void TPointerTest::TestHolderPtrMoveAssignment() { - THolder<int> h(new int); - h = CreateInt(42); - UNIT_ASSERT_VALUES_EQUAL(*h, 42); -} - -struct TBase { + THolder<int> h(new int); + h = CreateInt(42); + UNIT_ASSERT_VALUES_EQUAL(*h, 42); +} + +struct TBase { virtual ~TBase() = default; -}; - +}; + struct TDerived: public TBase { -}; - +}; + void TPointerTest::TestHolderPtrMoveConstructorInheritance() { - // compileability test - THolder<TBase> basePtr(THolder<TDerived>(new TDerived)); -} - + // compileability test + THolder<TBase> basePtr(THolder<TDerived>(new TDerived)); +} + void TPointerTest::TestHolderPtrMoveAssignmentInheritance() { - // compileability test - THolder<TBase> basePtr; - basePtr = THolder<TDerived>(new TDerived); -} - + // compileability test + THolder<TBase> basePtr; + basePtr = THolder<TDerived>(new TDerived); +} + void TPointerTest::TestMakeHolder() { - { - auto ptr = MakeHolder<int>(5); - UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); - } - { - struct TRec { - int X, Y; + { + auto ptr = MakeHolder<int>(5); + UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); + } + { + struct TRec { + int X, Y; TRec() : X(1) , Y(2) { - } - }; - THolder<TRec> ptr = MakeHolder<TRec>(); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - struct TRec { - int X, Y; + } + }; + THolder<TRec> ptr = MakeHolder<TRec>(); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + struct TRec { + int X, Y; TRec(int x, int y) : X(x) , Y(y) { } - }; - auto ptr = MakeHolder<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - class TRec { - private: + }; + auto ptr = MakeHolder<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + class TRec { + private: int X_, Y_; - public: + public: TRec(int x, int y) : X_(x) , Y_(y) { - } - + } + int GetX() const { return X_; } int GetY() const { return Y_; } - }; - auto ptr = MakeHolder<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); - } -} - + }; + auto ptr = MakeHolder<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); + } +} + void TPointerTest::TestTrulePtr() { { TAutoPtr<A> a1(MakeA()); @@ -620,94 +620,94 @@ void TPointerTest::TestOperatorBool() { // bool c = (a < b); // does not compile } } - + void TPointerTest::TestMakeShared() { - { - TSimpleSharedPtr<int> ptr = MakeSimpleShared<int>(5); - UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); - } - { - struct TRec { - int X, Y; + { + TSimpleSharedPtr<int> ptr = MakeSimpleShared<int>(5); + UNIT_ASSERT_VALUES_EQUAL(*ptr, 5); + } + { + struct TRec { + int X, Y; TRec() : X(1) , Y(2) { - } - }; - auto ptr = MakeAtomicShared<TRec>(); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - struct TRec { - int X, Y; - }; - TAtomicSharedPtr<TRec> ptr = MakeAtomicShared<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); - } - { - class TRec { - private: + } + }; + auto ptr = MakeAtomicShared<TRec>(); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + struct TRec { + int X, Y; + }; + TAtomicSharedPtr<TRec> ptr = MakeAtomicShared<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->X, 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->Y, 2); + } + { + class TRec { + private: int X_, Y_; - public: + public: TRec(int x, int y) : X_(x) , Y_(y) { - } - + } + int GetX() const { return X_; } int GetY() const { return Y_; } - }; - TSimpleSharedPtr<TRec> ptr = MakeSimpleShared<TRec>(1, 2); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); - UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); - } - { - enum EObjectState { - OS_NOT_CREATED, - OS_CREATED, - OS_DESTROYED, - }; - - struct TObject { - EObjectState& State; - - TObject(EObjectState& state) - : State(state) - { - State = OS_CREATED; - } - - ~TObject() { - State = OS_DESTROYED; - } - }; - - auto throwsException = []() { - throw yexception(); - return 5; - }; - - auto testFunction = [](TSimpleSharedPtr<TObject>, int) { - }; - - EObjectState state = OS_NOT_CREATED; - try { - testFunction(MakeSimpleShared<TObject>(state), throwsException()); - } catch (yexception&) { - } - - UNIT_ASSERT(state == OS_NOT_CREATED || state == OS_DESTROYED); - } -} + }; + TSimpleSharedPtr<TRec> ptr = MakeSimpleShared<TRec>(1, 2); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetX(), 1); + UNIT_ASSERT_VALUES_EQUAL(ptr->GetY(), 2); + } + { + enum EObjectState { + OS_NOT_CREATED, + OS_CREATED, + OS_DESTROYED, + }; + + struct TObject { + EObjectState& State; + + TObject(EObjectState& state) + : State(state) + { + State = OS_CREATED; + } + + ~TObject() { + State = OS_DESTROYED; + } + }; + + auto throwsException = []() { + throw yexception(); + return 5; + }; + + auto testFunction = [](TSimpleSharedPtr<TObject>, int) { + }; + + EObjectState state = OS_NOT_CREATED; + try { + testFunction(MakeSimpleShared<TObject>(state), throwsException()); + } catch (yexception&) { + } + + UNIT_ASSERT(state == OS_NOT_CREATED || state == OS_DESTROYED); + } +} template <class TPtr> void TestPtrComparison(const TPtr& ptr) { diff --git a/util/generic/xrange.h b/util/generic/xrange.h index f7b8e0cebe..5fc8c82912 100644 --- a/util/generic/xrange.h +++ b/util/generic/xrange.h @@ -1,7 +1,7 @@ #pragma once #include "typetraits.h" -#include "utility.h" +#include "utility.h" #include <util/system/yassert.h> #include <iterator> diff --git a/util/generic/xrange_ut.cpp b/util/generic/xrange_ut.cpp index 86f316918e..8106da03e7 100644 --- a/util/generic/xrange_ut.cpp +++ b/util/generic/xrange_ut.cpp @@ -1,5 +1,5 @@ -#include "xrange.h" - +#include "xrange.h" + #include "algorithm.h" #include "maybe.h" #include "vector.h" @@ -112,50 +112,50 @@ Y_UNIT_TEST_SUITE(XRange) { UNIT_ASSERT(arr == data); } } - - template <class XRangeContainer> - void TestEmptyRanges(const XRangeContainer& c) { - for (const auto& emptyRange : c) { - UNIT_ASSERT_VALUES_EQUAL(emptyRange.size(), 0); - - for (auto i : emptyRange) { + + template <class XRangeContainer> + void TestEmptyRanges(const XRangeContainer& c) { + for (const auto& emptyRange : c) { + UNIT_ASSERT_VALUES_EQUAL(emptyRange.size(), 0); + + for (auto i : emptyRange) { Y_UNUSED(i); - UNIT_ASSERT(false); - } - - using TValueType = decltype(*emptyRange.begin()); + UNIT_ASSERT(false); + } + + using TValueType = decltype(*emptyRange.begin()); const TVector<TValueType> asVector = emptyRange; - UNIT_ASSERT(asVector.empty()); - } - } - + UNIT_ASSERT(asVector.empty()); + } + } + Y_UNIT_TEST(EmptySimpleRange) { - using TSimpleRange = decltype(xrange(1)); - - const TSimpleRange emptySimpleRanges[] = { - xrange(-1), - xrange(-10), - xrange(0, -5), - xrange(10, 10), - xrange(10, 9), - }; - - TestEmptyRanges(emptySimpleRanges); - } - + using TSimpleRange = decltype(xrange(1)); + + const TSimpleRange emptySimpleRanges[] = { + xrange(-1), + xrange(-10), + xrange(0, -5), + xrange(10, 10), + xrange(10, 9), + }; + + TestEmptyRanges(emptySimpleRanges); + } + Y_UNIT_TEST(EmptySteppedRange) { - using TSteppedRange = decltype(xrange(1, 10, 1)); - - const TSteppedRange emptySteppedRanges[] = { - xrange(5, 5, 1), - xrange(5, 0, 5), - xrange(0, -1, 5), + using TSteppedRange = decltype(xrange(1, 10, 1)); + + const TSteppedRange emptySteppedRanges[] = { + xrange(5, 5, 1), + xrange(5, 0, 5), + xrange(0, -1, 5), xrange(0, 1, -1), - xrange(0, -10, 10), - }; - - TestEmptyRanges(emptySteppedRanges); - } + xrange(0, -10, 10), + }; + + TestEmptyRanges(emptySteppedRanges); + } template <class TRange> static void TestIteratorDifferenceImpl(TRange range, int a, int b, TMaybe<int> step) { diff --git a/util/ysaveload.h b/util/ysaveload.h index 4941ffbc3e..02efb4049b 100644 --- a/util/ysaveload.h +++ b/util/ysaveload.h @@ -650,27 +650,27 @@ namespace NPrivate { } } -template <typename... Args> +template <typename... Args> struct TSerializer<std::variant<Args...>> { using TVar = std::variant<Args...>; - - static_assert(sizeof...(Args) < 256, "We use ui8 to store tag"); - - static void Save(IOutputStream* os, const TVar& v) { + + static_assert(sizeof...(Args) < 256, "We use ui8 to store tag"); + + static void Save(IOutputStream* os, const TVar& v) { ::Save<ui8>(os, v.index()); std::visit([os](const auto& data) { - ::Save(os, data); + ::Save(os, data); }, v); - } - - static void Load(IInputStream* is, TVar& v) { + } + + static void Load(IInputStream* is, TVar& v) { ui8 index; ::Load(is, index); if (Y_UNLIKELY(index >= sizeof...(Args))) { ::NPrivate::ThrowUnexpectedVariantTagException(index); } LoadImpl(is, v, index, std::index_sequence_for<Args...>{}); - } + } private: template <size_t... Is> @@ -679,8 +679,8 @@ private: constexpr TLoader loaders[] = {::NPrivate::LoadVariantAlternative<TVar, Args, Is>...}; loaders[index](is, v); } -}; - +}; + #endif template <class T> diff --git a/util/ysaveload_ut.cpp b/util/ysaveload_ut.cpp index 97ca40b916..723c68f391 100644 --- a/util/ysaveload_ut.cpp +++ b/util/ysaveload_ut.cpp @@ -15,7 +15,7 @@ #include <util/generic/buffer.h> #include <util/generic/hash_set.h> #include <util/generic/maybe.h> -#include <util/generic/variant.h> +#include <util/generic/variant.h> static inline char* AllocateFromPool(TMemoryPool& pool, size_t len) { return (char*)pool.Allocate(len); @@ -28,7 +28,7 @@ class TSaveLoadTest: public TTestBase { UNIT_TEST(TestNewNewStyle) UNIT_TEST(TestList) UNIT_TEST(TestTuple) - UNIT_TEST(TestVariant) + UNIT_TEST(TestVariant) UNIT_TEST(TestInheritNonVirtualClass) UNIT_TEST(TestInheritVirtualClass) UNIT_TEST_SUITE_END(); @@ -406,30 +406,30 @@ private: UNIT_ASSERT_VALUES_EQUAL(std::get<2>(toLoad), std::get<2>(toSave)); } - template <class TVariant, class T> - void TestVariantImpl(TVariant& v, const T& expected) { - v = expected; - - TBufferStream s; - ::Save(&s, v); - ::Load(&s, v); + template <class TVariant, class T> + void TestVariantImpl(TVariant& v, const T& expected) { + v = expected; + + TBufferStream s; + ::Save(&s, v); + ::Load(&s, v); UNIT_ASSERT_VALUES_EQUAL(std::get<T>(v), expected); - } - - void TestVariant() { + } + + void TestVariant() { std::variant<int, bool, TString, TVector<char>> v(1); - TestVariantImpl(v, 42); - TestVariantImpl(v, true); - TestVariantImpl(v, TString("foo")); + TestVariantImpl(v, 42); + TestVariantImpl(v, true); + TestVariantImpl(v, TString("foo")); TestVariantImpl(v, TVector<char>{'b', 'a', 'r'}); - - v = TString("baz"); - TBufferStream s; - ::Save(&s, v); - + + v = TString("baz"); + TBufferStream s; + ::Save(&s, v); + std::variant<char, bool> v2 = false; - UNIT_ASSERT_EXCEPTION(::Load(&s, v2), TLoadEOF); - } + UNIT_ASSERT_EXCEPTION(::Load(&s, v2), TLoadEOF); + } // tests serialization of class with three public string members template <class TDerived, class TInterface = TDerived> |