diff options
author | robot-contrib <robot-contrib@yandex-team.com> | 2023-01-07 12:09:30 +0300 |
---|---|---|
committer | robot-contrib <robot-contrib@yandex-team.com> | 2023-01-07 12:09:30 +0300 |
commit | 1fbf7c7fd33316a130488349efb542486e2dd29c (patch) | |
tree | cf7ddeecf3f70977fb844916366e50869d2fc1cc /library | |
parent | 3cc14e8259c8f4e7023ea7cf5ecc45fa3f94815d (diff) | |
download | ydb-1fbf7c7fd33316a130488349efb542486e2dd29c.tar.gz |
Update contrib/python/exceptiongroup to 1.1.0
Diffstat (limited to 'library')
8 files changed, 314 insertions, 260 deletions
diff --git a/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h b/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h index 5442fc1e05..1e902dbe45 100644 --- a/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h +++ b/library/cpp/yt/memory/atomic_intrusive_ptr-inl.h @@ -36,7 +36,7 @@ TAtomicIntrusivePtr<T>::~TAtomicIntrusivePtr() template <class T> TAtomicIntrusivePtr<T>& TAtomicIntrusivePtr<T>::operator=(TIntrusivePtr<T> other) { - ReleaseObject(Ptr_.exchange(AcquireObject(other.Release(), true))); + Store(std::move(other)); return *this; } @@ -103,6 +103,12 @@ TIntrusivePtr<T> TAtomicIntrusivePtr<T>::Exchange(TIntrusivePtr<T> other) } template <class T> +void TAtomicIntrusivePtr<T>::Store(TIntrusivePtr<T> other) +{ + ReleaseObject(Ptr_.exchange(AcquireObject(other.Release(), true))); +} + +template <class T> void TAtomicIntrusivePtr<T>::Reset() { ReleaseObject(Ptr_.exchange(nullptr)); diff --git a/library/cpp/yt/memory/atomic_intrusive_ptr.h b/library/cpp/yt/memory/atomic_intrusive_ptr.h index d3f5bb16db..f8c9a1a7fe 100644 --- a/library/cpp/yt/memory/atomic_intrusive_ptr.h +++ b/library/cpp/yt/memory/atomic_intrusive_ptr.h @@ -31,6 +31,7 @@ public: TIntrusivePtr<T> Acquire() const; TIntrusivePtr<T> Exchange(TIntrusivePtr<T> other); + void Store(TIntrusivePtr<T> other); void Reset(); bool CompareAndSwap(void*& comparePtr, T* target); diff --git a/library/cpp/yt/small_containers/compact_flat_map-inl.h b/library/cpp/yt/small_containers/compact_flat_map-inl.h index 45a4dd1de3..74557f2467 100644 --- a/library/cpp/yt/small_containers/compact_flat_map-inl.h +++ b/library/cpp/yt/small_containers/compact_flat_map-inl.h @@ -8,112 +8,143 @@ namespace NYT { /////////////////////////////////////////////////////////////////////////////// -template <class K, class V, unsigned N> +template <class K, class V, size_t N> template <class TInputIterator> TCompactFlatMap<K, V, N>::TCompactFlatMap(TInputIterator begin, TInputIterator end) { insert(begin, end); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> bool TCompactFlatMap<K, V, N>::operator==(const TCompactFlatMap& rhs) const { return Storage_ == rhs.Storage_; } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> bool TCompactFlatMap<K, V, N>::operator!=(const TCompactFlatMap& rhs) const { return !(*this == rhs); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::begin() { return Storage_.begin(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::begin() const { return Storage_.begin(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> +typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::cbegin() const +{ + return Storage_.begin(); +} + +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::end() { return Storage_.end(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::end() const { return Storage_.end(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> +typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::cend() const +{ + return Storage_.end(); +} + +template <class K, class V, size_t N> void TCompactFlatMap<K, V, N>::reserve(size_type n) { Storage_.reserve(n); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::size_type TCompactFlatMap<K, V, N>::size() const { return Storage_.size(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> int TCompactFlatMap<K, V, N>::ssize() const { return static_cast<int>(Storage_.size()); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> bool TCompactFlatMap<K, V, N>::empty() const { return Storage_.empty(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> void TCompactFlatMap<K, V, N>::clear() { Storage_.clear(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> +void TCompactFlatMap<K, V, N>::shrink_to_small() +{ + Storage_.shrink_to_small(); +} + +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::iterator TCompactFlatMap<K, V, N>::find(const K& k) { - auto [rangeBegin, rangeEnd] = EqualRange(k); + auto [rangeBegin, rangeEnd] = equal_range(k); return rangeBegin == rangeEnd ? end() : rangeBegin; } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> typename TCompactFlatMap<K, V, N>::const_iterator TCompactFlatMap<K, V, N>::find(const K& k) const { - auto [rangeBegin, rangeEnd] = EqualRange(k); + auto [rangeBegin, rangeEnd] = equal_range(k); return rangeBegin == rangeEnd ? end() : rangeBegin; } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> bool TCompactFlatMap<K, V, N>::contains(const K& k) const { return find(k) != end(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> auto TCompactFlatMap<K, V, N>::insert(const value_type& value) -> std::pair<iterator, bool> { - auto [rangeBegin, rangeEnd] = EqualRange(value.first); + return do_insert(value); +} + +template <class K, class V, size_t N> +auto TCompactFlatMap<K, V, N>::insert(value_type&& value) -> std::pair<iterator, bool> +{ + return do_insert(std::move(value)); +} + +template <class K, class V, size_t N> +template <class TArg> +auto TCompactFlatMap<K, V, N>::do_insert(TArg&& value) -> std::pair<iterator, bool> +{ + auto [rangeBegin, rangeEnd] = equal_range(value.first); if (rangeBegin != rangeEnd) { return {rangeBegin, false}; } else { - auto it = Storage_.insert(rangeBegin, value); + auto it = Storage_.insert(rangeBegin, std::forward<TArg>(value)); return {it, true}; } } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> template <class TInputIterator> void TCompactFlatMap<K, V, N>::insert(TInputIterator begin, TInputIterator end) { @@ -122,28 +153,28 @@ void TCompactFlatMap<K, V, N>::insert(TInputIterator begin, TInputIterator end) } } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> template <class... TArgs> auto TCompactFlatMap<K, V, N>::emplace(TArgs&&... args) -> std::pair<iterator, bool> { return insert(value_type(std::forward<TArgs>(args)...)); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> V& TCompactFlatMap<K, V, N>::operator[](const K& k) { auto [it, inserted] = insert({k, V()}); return it->second; } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> void TCompactFlatMap<K, V, N>::erase(const K& k) { - auto [rangeBegin, rangeEnd] = EqualRange(k); + auto [rangeBegin, rangeEnd] = equal_range(k); erase(rangeBegin, rangeEnd); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> void TCompactFlatMap<K, V, N>::erase(iterator pos) { Storage_.erase(pos); @@ -152,7 +183,7 @@ void TCompactFlatMap<K, V, N>::erase(iterator pos) Storage_.shrink_to_small(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> void TCompactFlatMap<K, V, N>::erase(iterator b, iterator e) { Storage_.erase(b, e); @@ -161,18 +192,18 @@ void TCompactFlatMap<K, V, N>::erase(iterator b, iterator e) Storage_.shrink_to_small(); } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> std::pair<typename TCompactFlatMap<K, V, N>::iterator, typename TCompactFlatMap<K, V, N>::iterator> -TCompactFlatMap<K, V, N>::EqualRange(const K& k) +TCompactFlatMap<K, V, N>::equal_range(const K& k) { auto result = std::equal_range(Storage_.begin(), Storage_.end(), k, TKeyComparer()); YT_ASSERT(std::distance(result.first, result.second) <= 1); return result; } -template <class K, class V, unsigned N> +template <class K, class V, size_t N> std::pair<typename TCompactFlatMap<K, V, N>::const_iterator, typename TCompactFlatMap<K, V, N>::const_iterator> -TCompactFlatMap<K, V, N>::EqualRange(const K& k) const +TCompactFlatMap<K, V, N>::equal_range(const K& k) const { auto result = std::equal_range(Storage_.begin(), Storage_.end(), k, TKeyComparer()); YT_ASSERT(std::distance(result.first, result.second) <= 1); diff --git a/library/cpp/yt/small_containers/compact_flat_map.h b/library/cpp/yt/small_containers/compact_flat_map.h index 13bdc0e9da..6263a3fada 100644 --- a/library/cpp/yt/small_containers/compact_flat_map.h +++ b/library/cpp/yt/small_containers/compact_flat_map.h @@ -21,7 +21,7 @@ namespace NYT { * Because of the latter, one should be very careful with iterators: virtually * any call to insert or erase may potentially invalidate all iterators. */ -template <class K, class V, unsigned N> +template <class K, class V, size_t N> class TCompactFlatMap { public: @@ -62,24 +62,29 @@ public: iterator begin(); const_iterator begin() const; + const_iterator cbegin() const; iterator end(); const_iterator end() const; + const_iterator cend() const; void reserve(size_type n); size_type size() const; int ssize() const; - bool empty() const; + [[nodiscard]] bool empty() const; void clear(); + void shrink_to_small(); + iterator find(const K& k); const_iterator find(const K& k) const; bool contains(const K& k) const; std::pair<iterator, bool> insert(const value_type& value); + std::pair<iterator, bool> insert(value_type&& value); template <class TInputIterator> void insert(TInputIterator begin, TInputIterator end); @@ -94,10 +99,13 @@ public: void erase(iterator b, iterator e); private: - std::pair<iterator, iterator> EqualRange(const K& k); - std::pair<const_iterator, const_iterator> EqualRange(const K& k) const; - TStorage Storage_; + + std::pair<iterator, iterator> equal_range(const K& k); + std::pair<const_iterator, const_iterator> equal_range(const K& k) const; + + template <class TArg> + std::pair<iterator, bool> do_insert(TArg&& value); }; //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/small_containers/compact_set-inl.h b/library/cpp/yt/small_containers/compact_set-inl.h index c027f44600..76bad7ad24 100644 --- a/library/cpp/yt/small_containers/compact_set-inl.h +++ b/library/cpp/yt/small_containers/compact_set-inl.h @@ -4,11 +4,13 @@ #include "compact_set.h" #endif +#include <library/cpp/yt/assert//assert.h> + namespace NYT { //////////////////////////////////////////////////////////////////////////////// -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> class TCompactSet<T, N, C>::const_iterator { private: @@ -35,7 +37,7 @@ private: template <class TOther> void ConstructFrom(TOther&& rhs) { - Y_VERIFY_DEBUG(Small == rhs.Small); + YT_ASSERT(Small == rhs.Small); if (Small) { new (&VIter)TVectorConstIterator(std::forward<TOther>(rhs).VIter); @@ -200,68 +202,67 @@ public: //////////////////////////////////////////////////////////////////////////////// -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> bool TCompactSet<T, N, C>::empty() const { - return Vector.empty() && Set.empty(); + return Vector_.empty() && Set_.empty(); } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> typename TCompactSet<T, N, C>::size_type TCompactSet<T, N, C>::size() const { - return IsSmall() ? Vector.size() : Set.size(); + return is_small() ? Vector_.size() : Set_.size(); } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> const T& TCompactSet<T, N, C>::front() const { - return IsSmall() ? Vector.front() : *Set.begin(); + return is_small() ? Vector_.front() : *Set_.begin(); } - -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> typename TCompactSet<T, N, C>::size_type TCompactSet<T, N, C>::count(const T& v) const { - if (IsSmall()) { - return std::binary_search(Vector.begin(), Vector.end(), v, C()) ? 1 : 0; + if (is_small()) { + return std::binary_search(Vector_.begin(), Vector_.end(), v, C()) ? 1 : 0; } else { - return Set.count(v); + return Set_.count(v); } } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> bool TCompactSet<T, N, C>::contains(const T& v) const { return count(v) == 1; } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> std::pair<typename TCompactSet<T, N, C>::const_iterator, bool> TCompactSet<T, N, C>::insert(const T& v) { - if (!IsSmall()) { - auto [it, inserted] = Set.insert(v); + if (!is_small()) { + auto [it, inserted] = Set_.insert(v); return {const_iterator(std::move(it)), inserted}; } - auto it = std::lower_bound(Vector.begin(), Vector.end(), v, C()); - if (it != Vector.end() && !C()(v, *it)) { + auto it = std::lower_bound(Vector_.begin(), Vector_.end(), v, C()); + if (it != Vector_.end() && !C()(v, *it)) { return {const_iterator(std::move(it)), false}; // Don't reinsert if it already exists. } - if (Vector.size() < N) { - auto newIt = Vector.insert(it, v); + if (Vector_.size() < N) { + auto newIt = Vector_.insert(it, v); return {const_iterator(std::move(newIt)), true}; } - Set.insert(Vector.begin(), Vector.end()); - Vector.clear(); + Set_.insert(Vector_.begin(), Vector_.end()); + Vector_.clear(); - auto [newIt, inserted] = Set.insert(v); - Y_VERIFY_DEBUG(inserted); + auto [newIt, inserted] = Set_.insert(v); + YT_ASSERT(inserted); return {const_iterator(std::move(newIt)), true}; } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> template <typename TIter> void TCompactSet<T, N, C>::insert(TIter i, TIter e) { @@ -270,57 +271,57 @@ void TCompactSet<T, N, C>::insert(TIter i, TIter e) } } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> bool TCompactSet<T, N, C>::erase(const T& v) { - if (!IsSmall()) { - return Set.erase(v); + if (!is_small()) { + return Set_.erase(v); } - auto [rangeBegin, rangeEnd] = std::equal_range(Vector.begin(), Vector.end(), v, C()); + auto [rangeBegin, rangeEnd] = std::equal_range(Vector_.begin(), Vector_.end(), v, C()); if (rangeBegin != rangeEnd) { - Vector.erase(rangeBegin, rangeEnd); + Vector_.erase(rangeBegin, rangeEnd); return true; } else { return false; } } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> void TCompactSet<T, N, C>::clear() { - Vector.clear(); - Set.clear(); + Vector_.clear(); + Set_.clear(); } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::begin() const { - return IsSmall() ? const_iterator(Vector.begin()) : const_iterator(Set.begin()); + return is_small() ? const_iterator(Vector_.begin()) : const_iterator(Set_.begin()); } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::cbegin() const { return begin(); } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::end() const { - return IsSmall() ? const_iterator(Vector.end()) : const_iterator(Set.end()); + return is_small() ? const_iterator(Vector_.end()) : const_iterator(Set_.end()); } -template <typename T, unsigned N, typename C> +template <typename T, size_t N, typename C> typename TCompactSet<T, N, C>::const_iterator TCompactSet<T, N, C>::cend() const { return end(); } -template <typename T, unsigned N, typename C> -bool TCompactSet<T, N, C>::IsSmall() const +template <typename T, size_t N, typename C> +bool TCompactSet<T, N, C>::is_small() const { - return Set.empty(); + return Set_.empty(); } //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/small_containers/compact_set.h b/library/cpp/yt/small_containers/compact_set.h index 910eef88eb..b325e42743 100644 --- a/library/cpp/yt/small_containers/compact_set.h +++ b/library/cpp/yt/small_containers/compact_set.h @@ -1,3 +1,4 @@ +// This is based on the following: //===- llvm/ADT/SmallSet.h - 'Normally small' sets --------------*- C++ -*-===// // // The LLVM Compiler Infrastructure @@ -7,7 +8,7 @@ // //===----------------------------------------------------------------------===// // -// This file defines the TCompactSet class. +// This file defines the CompactSet class. // //===----------------------------------------------------------------------===// @@ -15,8 +16,6 @@ #include "compact_vector.h" -#include <util/system/yassert.h> - #include <cstddef> #include <iterator> #include <set> @@ -24,45 +23,35 @@ namespace NYT { -/// TCompactSet - This maintains a set of unique values, optimizing for the case -/// when the set is small (less than N). In this case, the set can be -/// maintained with no mallocs. If the set gets large, we expand to using an -/// std::set to maintain reasonable lookup times. -/// -/// Note that any modification of the set may invalidate *all* iterators. -template <typename T, unsigned N, typename C = std::less<T>> +/////////////////////////////////////////////////////////////////////////////// + +//! Maintains a set of unique values, optimizing for the case +//! when the set is small (less than N). +/*! + * In this case, the set can be maintained with no mallocs. + * If the set gets large, we expand to using an + * std::set to maintain reasonable lookup times. + * + * Note that any modification of the set may invalidate *all* iterators. + */ +template <typename T, size_t N, typename C = std::less<T>> class TCompactSet { -private: - /// Use a CompactVector to hold the elements here (even though it will never - /// reach its 'large' stage) to avoid calling the default ctors of elements - /// we will never use. - TCompactVector<T, N> Vector; - std::set<T, C> Set; - - using TSetConstIterator = typename std::set<T, C>::const_iterator; - using TVectorConstIterator = typename TCompactVector<T, N>::const_iterator; - public: class const_iterator; using size_type = std::size_t; using key_type = T; - TCompactSet() {} - [[nodiscard]] bool empty() const; size_type size() const; const T& front() const; - /// count - Return true if the element is in the set. size_type count(const T& v) const; - /// contains - Return true if the element is in the set. bool contains(const T& v) const; - /// insert - Insert an element into the set if it isn't already there. std::pair<const_iterator, bool> insert(const T& v); template <typename TIter> @@ -79,9 +68,20 @@ public: const_iterator cend() const; private: - bool IsSmall() const; + // Use a TCompactVector to hold the elements here (even though it will never + // reach its 'large' stage) to avoid calling the default ctors of elements + // we will never use. + TCompactVector<T, N> Vector_; + std::set<T, C> Set_; + + using TSetConstIterator = typename std::set<T, C>::const_iterator; + using TVectorConstIterator = typename TCompactVector<T, N>::const_iterator; + + bool is_small() const; }; +/////////////////////////////////////////////////////////////////////////////// + } // namespace NYT #define COMPACT_SET_INL_H_ diff --git a/library/cpp/yt/small_containers/unittests/compact_flat_map_ut.cpp b/library/cpp/yt/small_containers/unittests/compact_flat_map_ut.cpp index 01aa32ff72..3f738e78b4 100644 --- a/library/cpp/yt/small_containers/unittests/compact_flat_map_ut.cpp +++ b/library/cpp/yt/small_containers/unittests/compact_flat_map_ut.cpp @@ -18,14 +18,14 @@ TMap CreateMap() return {data.begin(), data.end()}; } -TEST(CompactFlatMapTest, DefaultEmpty) +TEST(TCompactFlatMapTest, DefaultEmpty) { TMap m; EXPECT_TRUE(m.empty()); EXPECT_EQ(m.begin(), m.end()); } -TEST(CompactFlatMapTest, Reserve) +TEST(TCompactFlatMapTest, Reserve) { // No real way to test reserve - just use it and wiggle about. auto m1 = CreateMap(); @@ -35,7 +35,7 @@ TEST(CompactFlatMapTest, Reserve) EXPECT_EQ(m1.size(), m2.size()); } -TEST(CompactFlatMapTest, Size) +TEST(TCompactFlatMapTest, Size) { auto m = CreateMap(); @@ -53,7 +53,7 @@ TEST(CompactFlatMapTest, Size) EXPECT_EQ(m.ssize(), 4); } -TEST(CompactFlatMapTest, ClearAndEmpty) +TEST(TCompactFlatMapTest, ClearAndEmpty) { auto m = CreateMap(); @@ -71,7 +71,7 @@ TEST(CompactFlatMapTest, ClearAndEmpty) EXPECT_NE(m.begin(), m.end()); } -TEST(CompactFlatMapTest, FindMutable) +TEST(TCompactFlatMapTest, FindMutable) { auto m = CreateMap(); { @@ -91,7 +91,7 @@ TEST(CompactFlatMapTest, FindMutable) } } -TEST(CompactFlatMapTest, FindConst) +TEST(TCompactFlatMapTest, FindConst) { const auto& m = CreateMap(); { @@ -105,7 +105,7 @@ TEST(CompactFlatMapTest, FindConst) } } -TEST(CompactFlatMapTest, Insert) +TEST(TCompactFlatMapTest, Insert) { auto m = CreateMap(); @@ -129,7 +129,7 @@ TEST(CompactFlatMapTest, Insert) EXPECT_EQ(m.find("and")->second, "trunkless"); } -TEST(CompactFlatMapTest, Emplace) +TEST(TCompactFlatMapTest, Emplace) { auto m = CreateMap(); @@ -147,7 +147,7 @@ TEST(CompactFlatMapTest, Emplace) EXPECT_EQ(it->second, "place"); } -TEST(CompactFlatMapTest, Subscript) +TEST(TCompactFlatMapTest, Subscript) { auto m = CreateMap(); @@ -158,7 +158,7 @@ TEST(CompactFlatMapTest, Subscript) EXPECT_EQ(m.ssize(), 5); } -TEST(CompactFlatMapTest, Erase) +TEST(TCompactFlatMapTest, Erase) { auto m = CreateMap(); @@ -172,7 +172,7 @@ TEST(CompactFlatMapTest, Erase) EXPECT_TRUE(m.empty()); } -TEST(CompactFlatMapTest, GrowShrink) +TEST(TCompactFlatMapTest, GrowShrink) { TMap m; m.insert({"Two", "vast"}); @@ -192,7 +192,7 @@ TEST(CompactFlatMapTest, GrowShrink) // Must not crash or trigger asan. } -TEST(CompactFlatMapTest, GrowShrinkGrow) +TEST(TCompactFlatMapTest, GrowShrinkGrow) { TMap m; m.insert({"Two", "vast"}); diff --git a/library/cpp/yt/small_containers/unittests/compact_set_ut.cpp b/library/cpp/yt/small_containers/unittests/compact_set_ut.cpp index ebab5846e1..17b96a2195 100644 --- a/library/cpp/yt/small_containers/unittests/compact_set_ut.cpp +++ b/library/cpp/yt/small_containers/unittests/compact_set_ut.cpp @@ -21,181 +21,188 @@ namespace { //////////////////////////////////////////////////////////////////////////////// -TEST(CompactSetTest, Insert) { +TEST(TCompactSetTest, Insert) +{ + TCompactSet<int, 4> s1; - TCompactSet<int, 4> s1; + for (int i = 0; i < 4; i++) + s1.insert(i); - for (int i = 0; i < 4; i++) - s1.insert(i); + for (int i = 0; i < 4; i++) + s1.insert(i); - for (int i = 0; i < 4; i++) - s1.insert(i); + EXPECT_EQ(4u, s1.size()); - EXPECT_EQ(4u, s1.size()); + for (int i = 0; i < 4; i++) + EXPECT_EQ(1u, s1.count(i)); - for (int i = 0; i < 4; i++) - EXPECT_EQ(1u, s1.count(i)); - - EXPECT_EQ(0u, s1.count(4)); + EXPECT_EQ(0u, s1.count(4)); } -TEST(CompactSetTest, Grow) { - TCompactSet<int, 4> s1; +TEST(TCompactSetTest, Grow) +{ + TCompactSet<int, 4> s1; - for (int i = 0; i < 8; i++) - s1.insert(i); + for (int i = 0; i < 8; i++) + s1.insert(i); - EXPECT_EQ(8u, s1.size()); + EXPECT_EQ(8u, s1.size()); - for (int i = 0; i < 8; i++) - EXPECT_EQ(1u, s1.count(i)); + for (int i = 0; i < 8; i++) + EXPECT_EQ(1u, s1.count(i)); - EXPECT_EQ(0u, s1.count(8)); + EXPECT_EQ(0u, s1.count(8)); } -TEST(CompactSetTest, Erase) { - TCompactSet<int, 4> s1; +TEST(TCompactSetTest, Erase) +{ + TCompactSet<int, 4> s1; - for (int i = 0; i < 8; i++) - s1.insert(i); + for (int i = 0; i < 8; i++) + s1.insert(i); - EXPECT_EQ(8u, s1.size()); + EXPECT_EQ(8u, s1.size()); - // Remove elements one by one and check if all other elements are still there. - for (int i = 0; i < 8; i++) { - EXPECT_EQ(1u, s1.count(i)); - EXPECT_TRUE(s1.erase(i)); - EXPECT_EQ(0u, s1.count(i)); - EXPECT_EQ(8u - i - 1, s1.size()); - for (int j = i + 1; j < 8; j++) - EXPECT_EQ(1u, s1.count(j)); - } + // Remove elements one by one and check if all other elements are still there. + for (int i = 0; i < 8; i++) { + EXPECT_EQ(1u, s1.count(i)); + EXPECT_TRUE(s1.erase(i)); + EXPECT_EQ(0u, s1.count(i)); + EXPECT_EQ(8u - i - 1, s1.size()); + for (int j = i + 1; j < 8; j++) + EXPECT_EQ(1u, s1.count(j)); + } - EXPECT_EQ(0u, s1.count(8)); + EXPECT_EQ(0u, s1.count(8)); } -TEST(CompactSetTest, IteratorInt) { - TCompactSet<int, 4> s1; - - // Test the 'small' case. - for (int i = 0; i < 3; i++) - s1.insert(i); - - std::vector<int> V(s1.begin(), s1.end()); - // Make sure the elements are in the expected order. - std::sort(V.begin(), V.end()); - for (int i = 0; i < 3; i++) - EXPECT_EQ(i, V[i]); - - // Test the 'big' case by adding a few more elements to switch to std::set - // internally. - for (int i = 3; i < 6; i++) - s1.insert(i); - - V.assign(s1.begin(), s1.end()); - // Make sure the elements are in the expected order. - std::sort(V.begin(), V.end()); - for (int i = 0; i < 6; i++) - EXPECT_EQ(i, V[i]); +TEST(TCompactSetTest, IteratorInt) +{ + TCompactSet<int, 4> s1; + + // Test the 'small' case. + for (int i = 0; i < 3; i++) + s1.insert(i); + + std::vector<int> V(s1.begin(), s1.end()); + // Make sure the elements are in the expected order. + std::sort(V.begin(), V.end()); + for (int i = 0; i < 3; i++) + EXPECT_EQ(i, V[i]); + + // Test the 'big' case by adding a few more elements to switch to std::set + // internally. + for (int i = 3; i < 6; i++) + s1.insert(i); + + V.assign(s1.begin(), s1.end()); + // Make sure the elements are in the expected order. + std::sort(V.begin(), V.end()); + for (int i = 0; i < 6; i++) + EXPECT_EQ(i, V[i]); } -TEST(CompactSetTest, IteratorString) { - // Test CompactSetIterator for TCompactSet with a type with non-trivial - // ctors/dtors. - TCompactSet<std::string, 2> s1; - - s1.insert("str 1"); - s1.insert("str 2"); - s1.insert("str 1"); - - std::vector<std::string> V(s1.begin(), s1.end()); - std::sort(V.begin(), V.end()); - EXPECT_EQ(2u, s1.size()); - EXPECT_EQ("str 1", V[0]); - EXPECT_EQ("str 2", V[1]); - - s1.insert("str 4"); - s1.insert("str 0"); - s1.insert("str 4"); - - V.assign(s1.begin(), s1.end()); - // Make sure the elements are in the expected order. - std::sort(V.begin(), V.end()); - EXPECT_EQ(4u, s1.size()); - EXPECT_EQ("str 0", V[0]); - EXPECT_EQ("str 1", V[1]); - EXPECT_EQ("str 2", V[2]); - EXPECT_EQ("str 4", V[3]); +TEST(TCompactSetTest, IteratorString) +{ + // Test CompactSetIterator for TCompactSet with a type with non-trivial + // ctors/dtors. + TCompactSet<std::string, 2> s1; + + s1.insert("str 1"); + s1.insert("str 2"); + s1.insert("str 1"); + + std::vector<std::string> V(s1.begin(), s1.end()); + std::sort(V.begin(), V.end()); + EXPECT_EQ(2u, s1.size()); + EXPECT_EQ("str 1", V[0]); + EXPECT_EQ("str 2", V[1]); + + s1.insert("str 4"); + s1.insert("str 0"); + s1.insert("str 4"); + + V.assign(s1.begin(), s1.end()); + // Make sure the elements are in the expected order. + std::sort(V.begin(), V.end()); + EXPECT_EQ(4u, s1.size()); + EXPECT_EQ("str 0", V[0]); + EXPECT_EQ("str 1", V[1]); + EXPECT_EQ("str 2", V[2]); + EXPECT_EQ("str 4", V[3]); } -TEST(CompactSetTest, IteratorIncMoveCopy) { - // Test CompactSetIterator for TCompactSet with a type with non-trivial - // ctors/dtors. - TCompactSet<std::string, 2> s1; - - s1.insert("str 1"); - s1.insert("str 2"); - - auto Iter = s1.begin(); - EXPECT_EQ("str 1", *Iter); - ++Iter; - EXPECT_EQ("str 2", *Iter); - - s1.insert("str 4"); - s1.insert("str 0"); - auto Iter2 = s1.begin(); - Iter = std::move(Iter2); - EXPECT_EQ("str 0", *Iter); +TEST(TCompactSetTest, IteratorIncMoveCopy) +{ + // Test CompactSetIterator for TCompactSet with a type with non-trivial + // ctors/dtors. + TCompactSet<std::string, 2> s1; + + s1.insert("str 1"); + s1.insert("str 2"); + + auto Iter = s1.begin(); + EXPECT_EQ("str 1", *Iter); + ++Iter; + EXPECT_EQ("str 2", *Iter); + + s1.insert("str 4"); + s1.insert("str 0"); + auto Iter2 = s1.begin(); + Iter = std::move(Iter2); + EXPECT_EQ("str 0", *Iter); } // These test weren't taken from llvm. -TEST(CompactSetTest, Empty) { - TCompactSet<int, 10> v; - EXPECT_TRUE(v.empty()); - - auto data = {1, 2, 3, 4, 5}; - - v.insert(data.begin(), data.end()); // not crossing size threshold - v.erase(4); - v.erase(2); - v.erase(3); - v.erase(5); - v.erase(1); - EXPECT_TRUE(v.empty()); - - auto data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - v.insert(data2.begin(), data2.end()); // crossing size threshold - v.erase(7); - v.erase(3); - v.erase(1); - v.erase(10); - v.erase(9); - v.erase(0); - v.erase(2); - v.erase(6); - v.erase(4); - v.erase(5); - v.erase(8); - EXPECT_TRUE(v.empty()); +TEST(TCompactSetTest, Empty) +{ + TCompactSet<int, 10> v; + EXPECT_TRUE(v.empty()); + + auto data = {1, 2, 3, 4, 5}; + + v.insert(data.begin(), data.end()); // not crossing size threshold + v.erase(4); + v.erase(2); + v.erase(3); + v.erase(5); + v.erase(1); + EXPECT_TRUE(v.empty()); + + auto data2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + v.insert(data2.begin(), data2.end()); // crossing size threshold + v.erase(7); + v.erase(3); + v.erase(1); + v.erase(10); + v.erase(9); + v.erase(0); + v.erase(2); + v.erase(6); + v.erase(4); + v.erase(5); + v.erase(8); + EXPECT_TRUE(v.empty()); } -TEST(CompactSetTest, ForEach) { - TCompactSet<int, 10> v; +TEST(TCompactSetTest, ForEach) +{ + TCompactSet<int, 10> v; - auto data = {10, 9, 3, 4, 1, 5, 6, 8}; + auto data = {10, 9, 3, 4, 1, 5, 6, 8}; - v.insert(data.begin(), data.end()); + v.insert(data.begin(), data.end()); - std::vector<int> buf(v.begin(), v.end()); - std::vector<int> expected{1, 3, 4, 5, 6, 8, 9, 10}; - EXPECT_EQ(expected, buf); + std::vector<int> buf(v.begin(), v.end()); + std::vector<int> expected{1, 3, 4, 5, 6, 8, 9, 10}; + EXPECT_EQ(expected, buf); - auto data2 = {7, 1, 2, 0}; - v.insert(data2.begin(), data2.end()); - buf.assign(v.begin(), v.end()); - expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - EXPECT_EQ(expected, buf); + auto data2 = {7, 1, 2, 0}; + v.insert(data2.begin(), data2.end()); + buf.assign(v.begin(), v.end()); + expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + EXPECT_EQ(expected, buf); } //////////////////////////////////////////////////////////////////////////////// |