diff options
author | Mikhail Borisov <borisov.mikhail@gmail.com> | 2022-02-10 16:45:40 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:40 +0300 |
commit | 5d50718e66d9c037dc587a0211110b7d25a66185 (patch) | |
tree | e98df59de24d2ef7c77baed9f41e4875a2fef972 /util/generic | |
parent | a6a92afe03e02795227d2641b49819b687f088f8 (diff) | |
download | ydb-5d50718e66d9c037dc587a0211110b7d25a66185.tar.gz |
Restoring authorship annotation for Mikhail Borisov <borisov.mikhail@gmail.com>. Commit 2 of 2.
Diffstat (limited to 'util/generic')
25 files changed, 1027 insertions, 1027 deletions
diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h index 821ba709d0..badfb88993 100644 --- a/util/generic/algorithm.h +++ b/util/generic/algorithm.h @@ -73,16 +73,16 @@ static inline void StableSort(T f, T l, C c) { std::stable_sort(f, l, c); } -template <class TContainer> -static inline void StableSort(TContainer& container) { - StableSort(container.begin(), container.end()); -} - -template <class TContainer, typename TCompare> -static inline void StableSort(TContainer& container, TCompare compare) { - StableSort(container.begin(), container.end(), compare); -} - +template <class TContainer> +static inline void StableSort(TContainer& container) { + StableSort(container.begin(), container.end()); +} + +template <class TContainer, typename TCompare> +static inline void StableSort(TContainer& container, TCompare compare) { + StableSort(container.begin(), container.end(), compare); +} + template <class TIterator, typename TGetKey> static inline void StableSortBy(TIterator begin, TIterator end, const TGetKey& getKey) { StableSort(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); @@ -278,11 +278,11 @@ static inline I LowerBound(I f, I l, const T& v, C c) { return std::lower_bound(f, l, v, c); } -template <class I, class T, class TGetKey> -static inline I LowerBoundBy(I f, I l, const T& v, const TGetKey& getKey) { - return std::lower_bound(f, l, v, [&](auto&& left, auto&& right) { return getKey(left) < right; }); -} - +template <class I, class T, class TGetKey> +static inline I LowerBoundBy(I f, I l, const T& v, const TGetKey& getKey) { + return std::lower_bound(f, l, v, [&](auto&& left, auto&& right) { return getKey(left) < right; }); +} + template <class I, class T> static inline I UpperBound(I f, I l, const T& v) { return std::upper_bound(f, l, v); @@ -293,11 +293,11 @@ static inline I UpperBound(I f, I l, const T& v, C c) { return std::upper_bound(f, l, v, c); } -template <class I, class T, class TGetKey> -static inline I UpperBoundBy(I f, I l, const T& v, const TGetKey& getKey) { - return std::upper_bound(f, l, v, [&](auto&& left, auto&& right) { return left < getKey(right); }); -} - +template <class I, class T, class TGetKey> +static inline I UpperBoundBy(I f, I l, const T& v, const TGetKey& getKey) { + return std::upper_bound(f, l, v, [&](auto&& left, auto&& right) { return left < getKey(right); }); +} + template <class T> static inline T Unique(T f, T l) { return std::unique(f, l); diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index 58a3b56956..8d732fcc0c 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -446,61 +446,61 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } Y_UNIT_TEST(SortTestTwoIterators) { - TVector<int> collection = {10, 2, 7}; - Sort(collection.begin(), collection.end()); - TVector<int> expected = {2, 7, 10}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {10, 2, 7}; + Sort(collection.begin(), collection.end()); + TVector<int> expected = {2, 7, 10}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(SortTestTwoIteratorsAndComparator) { - TVector<int> collection = {10, 2, 7}; - Sort(collection.begin(), collection.end(), [](int l, int r) { return l > r; }); - TVector<int> expected = {10, 7, 2}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {10, 2, 7}; + Sort(collection.begin(), collection.end(), [](int l, int r) { return l > r; }); + TVector<int> expected = {10, 7, 2}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(SortTestContainer) { - TVector<int> collection = {10, 2, 7}; - Sort(collection); - TVector<int> expected = {2, 7, 10}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {10, 2, 7}; + Sort(collection); + TVector<int> expected = {2, 7, 10}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(SortTestContainerAndComparator) { - TVector<int> collection = {10, 2, 7}; - Sort(collection, [](int l, int r) { return l > r; }); - TVector<int> expected = {10, 7, 2}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {10, 2, 7}; + Sort(collection, [](int l, int r) { return l > r; }); + TVector<int> expected = {10, 7, 2}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(StableSortTestTwoIterators) { - TVector<int> collection = {10, 2, 7}; - StableSort(collection.begin(), collection.end()); - TVector<int> expected = {2, 7, 10}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {10, 2, 7}; + StableSort(collection.begin(), collection.end()); + TVector<int> expected = {2, 7, 10}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(StableSortTestTwoIteratorsAndComparator) { - TVector<int> collection = {404, 101, 106, 203, 102, 205, 401}; - StableSort(collection.begin(), collection.end(), [](int l, int r) { return (l / 100) < (r / 100); }); - TVector<int> expected = {101, 106, 102, 203, 205, 404, 401}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {404, 101, 106, 203, 102, 205, 401}; + StableSort(collection.begin(), collection.end(), [](int l, int r) { return (l / 100) < (r / 100); }); + TVector<int> expected = {101, 106, 102, 203, 205, 404, 401}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(StableSortTestContainer) { - TVector<int> collection = {10, 2, 7}; - StableSort(collection); - TVector<int> expected = {2, 7, 10}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {10, 2, 7}; + StableSort(collection); + TVector<int> expected = {2, 7, 10}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(StableSortTestContainerAndComparator) { - TVector<int> collection = {404, 101, 106, 203, 102, 205, 401}; - StableSort(collection, [](int l, int r) { return (l / 100) < (r / 100); }); - TVector<int> expected = {101, 106, 102, 203, 205, 404, 401}; - UNIT_ASSERT_VALUES_EQUAL(collection, expected); - } - + TVector<int> collection = {404, 101, 106, 203, 102, 205, 401}; + StableSort(collection, [](int l, int r) { return (l / 100) < (r / 100); }); + TVector<int> expected = {101, 106, 102, 203, 205, 404, 401}; + UNIT_ASSERT_VALUES_EQUAL(collection, expected); + } + Y_UNIT_TEST(SortByTest) { TVector<int> collection = {10, 2, 7}; SortBy(collection, [](int x) { return -x; }); @@ -796,45 +796,45 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { auto i = FindIf(range, [](auto) { return false; }); Y_UNUSED(i); } - + Y_UNIT_TEST(TestLowerBoundBy) { using TIntPairs = TVector<std::pair<i32, i32>>; - - auto data = TIntPairs{{1, 5}, {3, 2}, {3, 4}, {8, 0}, {5, 4}}; - auto getKey = [](const auto& x) { return x.second; }; - - StableSortBy(data, getKey); - - auto it = LowerBoundBy(data.begin(), data.end(), 4, getKey); - UNIT_ASSERT(it != data.end()); - UNIT_ASSERT_EQUAL(it->second, 4); - UNIT_ASSERT_EQUAL(it->first, 3); - - UNIT_ASSERT(it > data.begin()); - UNIT_ASSERT_EQUAL((it - 1)->second, 2); - - UNIT_ASSERT((it + 1) < data.end()); - UNIT_ASSERT_EQUAL((it + 1)->second, 4); - } - + + auto data = TIntPairs{{1, 5}, {3, 2}, {3, 4}, {8, 0}, {5, 4}}; + auto getKey = [](const auto& x) { return x.second; }; + + StableSortBy(data, getKey); + + auto it = LowerBoundBy(data.begin(), data.end(), 4, getKey); + UNIT_ASSERT(it != data.end()); + UNIT_ASSERT_EQUAL(it->second, 4); + UNIT_ASSERT_EQUAL(it->first, 3); + + UNIT_ASSERT(it > data.begin()); + UNIT_ASSERT_EQUAL((it - 1)->second, 2); + + UNIT_ASSERT((it + 1) < data.end()); + UNIT_ASSERT_EQUAL((it + 1)->second, 4); + } + Y_UNIT_TEST(TestUpperBoundBy) { using TIntPairs = TVector<std::pair<i32, i32>>; - - auto data = TIntPairs{{1, 5}, {3, 2}, {3, 4}, {8, 0}, {5, 4}}; - auto getKey = [](const auto& x) { return x.second; }; - - StableSortBy(data, getKey); - - auto it = UpperBoundBy(data.begin(), data.end(), 4, getKey); - UNIT_ASSERT(it != data.end()); - UNIT_ASSERT_EQUAL(it->second, 5); - UNIT_ASSERT_EQUAL(it->first, 1); - - UNIT_ASSERT(it > data.begin()); - UNIT_ASSERT_EQUAL((it - 1)->second, 4); - - UNIT_ASSERT((it + 1) == data.end()); - } + + auto data = TIntPairs{{1, 5}, {3, 2}, {3, 4}, {8, 0}, {5, 4}}; + auto getKey = [](const auto& x) { return x.second; }; + + StableSortBy(data, getKey); + + auto it = UpperBoundBy(data.begin(), data.end(), 4, getKey); + UNIT_ASSERT(it != data.end()); + UNIT_ASSERT_EQUAL(it->second, 5); + UNIT_ASSERT_EQUAL(it->first, 1); + + UNIT_ASSERT(it > data.begin()); + UNIT_ASSERT_EQUAL((it - 1)->second, 4); + + UNIT_ASSERT((it + 1) == data.end()); + } Y_UNIT_TEST(TestFindInContainer) { std::vector<int> v = {1, 2, 1000, 15, 100}; diff --git a/util/generic/benchmark/fastclp2/metrics/main.py b/util/generic/benchmark/fastclp2/metrics/main.py index 9e260653e2..5573c6a5d7 100644 --- a/util/generic/benchmark/fastclp2/metrics/main.py +++ b/util/generic/benchmark/fastclp2/metrics/main.py @@ -2,4 +2,4 @@ import yatest.common as yc def test_export_metrics(metrics): - metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/fastclp2/fastclp2', threads=8)) + metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/fastclp2/fastclp2', threads=8)) diff --git a/util/generic/benchmark/log2/metrics/main.py b/util/generic/benchmark/log2/metrics/main.py index 3ce98f91d0..26f6b57812 100644 --- a/util/generic/benchmark/log2/metrics/main.py +++ b/util/generic/benchmark/log2/metrics/main.py @@ -2,4 +2,4 @@ import yatest.common as yc def test_export_metrics(metrics): - metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/log2/log2', threads=8)) + metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/log2/log2', threads=8)) diff --git a/util/generic/benchmark/rotate_bits/metrics/main.py b/util/generic/benchmark/rotate_bits/metrics/main.py index 599825423a..b30555775f 100644 --- a/util/generic/benchmark/rotate_bits/metrics/main.py +++ b/util/generic/benchmark/rotate_bits/metrics/main.py @@ -2,4 +2,4 @@ import yatest.common as yc def test_export_metrics(metrics): - metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/rotate_bits/rotate_bits', threads=8)) + metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/rotate_bits/rotate_bits', threads=8)) diff --git a/util/generic/benchmark/string/benchmarks.h b/util/generic/benchmark/string/benchmarks.h index 8bd6ec9e7f..e347d7ff47 100644 --- a/util/generic/benchmark/string/benchmarks.h +++ b/util/generic/benchmark/string/benchmarks.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + // Define BENCHMARK_PREFIX and BENCHMARKED_CLASS before including this file. #include <util/generic/xrange.h> diff --git a/util/generic/benchmark/vector_count_ctor/metrics/main.py b/util/generic/benchmark/vector_count_ctor/metrics/main.py index d84e229110..835b44fe5f 100644 --- a/util/generic/benchmark/vector_count_ctor/metrics/main.py +++ b/util/generic/benchmark/vector_count_ctor/metrics/main.py @@ -2,4 +2,4 @@ import yatest.common as yc def test_export_metrics(metrics): - metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/vector_count_ctor/vector_count_ctor', threads=8)) + metrics.set_benchmark(yc.execute_benchmark('util/generic/benchmark/vector_count_ctor/vector_count_ctor', threads=8)) diff --git a/util/generic/flags.h b/util/generic/flags.h index 5898195d7c..a1f5921d42 100644 --- a/util/generic/flags.h +++ b/util/generic/flags.h @@ -59,10 +59,10 @@ public: return Value_; } - constexpr static TFlags FromBaseType(TInt value) { + constexpr static TFlags FromBaseType(TInt value) { return TFlags(TFlag(value)); - } - + } + constexpr friend TFlags operator|(TFlags l, TFlags r) { return TFlags(TFlag(l.Value_ | r.Value_)); } diff --git a/util/generic/flags_ut.cpp b/util/generic/flags_ut.cpp index ff11d07f31..5377c6a058 100644 --- a/util/generic/flags_ut.cpp +++ b/util/generic/flags_ut.cpp @@ -100,18 +100,18 @@ Y_UNIT_TEST_SUITE(TFlagsTest) { UNIT_ASSERT_VALUES_EQUAL(hash[value0], 0); UNIT_ASSERT_VALUES_EQUAL(hash[value1], 1); } - + Y_UNIT_TEST(TestBaseType) { - ui16 goodValue = 7; - auto goodFlags = ETest1::FromBaseType(goodValue); + ui16 goodValue = 7; + auto goodFlags = ETest1::FromBaseType(goodValue); UNIT_ASSERT(goodFlags& ETestFlag1::Test1); UNIT_ASSERT(goodFlags& ETestFlag1::Test2); UNIT_ASSERT(goodFlags& ETestFlag1::Test4); - UNIT_ASSERT_VALUES_EQUAL(goodValue, goodFlags.ToBaseType()); - - // Passed value is not checked, but preserved as is - ui16 badValue = 1024; - auto badFlags = ETest1::FromBaseType(badValue); - UNIT_ASSERT_VALUES_EQUAL(badValue, badFlags.ToBaseType()); - } + UNIT_ASSERT_VALUES_EQUAL(goodValue, goodFlags.ToBaseType()); + + // Passed value is not checked, but preserved as is + ui16 badValue = 1024; + auto badFlags = ETest1::FromBaseType(badValue); + UNIT_ASSERT_VALUES_EQUAL(badValue, badFlags.ToBaseType()); + } } diff --git a/util/generic/hash_ut.pyx b/util/generic/hash_ut.pyx index 172b5d1252..ecf6dac2e6 100644 --- a/util/generic/hash_ut.pyx +++ b/util/generic/hash_ut.pyx @@ -11,9 +11,9 @@ from cython.operator cimport dereference as deref def _check_convert(THashMap[TString, int] x): - return x - - + return x + + class TestHash(unittest.TestCase): def test_constructors_and_assignments(self): @@ -75,10 +75,10 @@ class TestHash(unittest.TestCase): self.assertTrue(it != tmp.end()) self.assertEqual(deref(it).second, 1) - def test_convert(self): - src = {'foo': 1, 'bar': 42} - self.assertEqual(_check_convert(src), src) - - bad_src = {'foo': 1, 'bar': 'baz'} - with self.assertRaises(TypeError): - _check_convert(bad_src) + def test_convert(self): + src = {'foo': 1, 'bar': 42} + self.assertEqual(_check_convert(src), src) + + bad_src = {'foo': 1, 'bar': 'baz'} + with self.assertRaises(TypeError): + _check_convert(bad_src) diff --git a/util/generic/iterator.h b/util/generic/iterator.h index b34be9c28c..19e9d20976 100644 --- a/util/generic/iterator.h +++ b/util/generic/iterator.h @@ -1,8 +1,8 @@ #pragma once #include <iterator> -#include <utility> - +#include <utility> + namespace NStlIterator { template <class T> class TProxy { @@ -68,31 +68,31 @@ class TInputRangeAdaptor { public: // TODO: private class TIterator { public: - static constexpr bool IsNoexceptNext = noexcept(std::declval<TSlave>().Next()); - + static constexpr bool IsNoexceptNext = noexcept(std::declval<TSlave>().Next()); + using difference_type = std::ptrdiff_t; using pointer = decltype(std::declval<TSlave>().Next()); using reference = decltype(*std::declval<TSlave>().Next()); using value_type = std::remove_cv_t<std::remove_reference_t<reference>>; using iterator_category = std::input_iterator_tag; - inline TIterator() noexcept + inline TIterator() noexcept : Slave_(nullptr) , Cur_() { } - inline TIterator(TSlave* slave) noexcept(IsNoexceptNext) + inline TIterator(TSlave* slave) noexcept(IsNoexceptNext) : Slave_(slave) , Cur_(Slave_->Next()) { } - inline bool operator==(const TIterator& it) const noexcept { + inline bool operator==(const TIterator& it) const noexcept { return Cur_ == it.Cur_; } - inline bool operator!=(const TIterator& it) const noexcept { + inline bool operator!=(const TIterator& it) const noexcept { return !(*this == it); } @@ -104,7 +104,7 @@ public: // TODO: private return *Cur_; } - inline TIterator& operator++() noexcept(IsNoexceptNext) { + inline TIterator& operator++() noexcept(IsNoexceptNext) { Cur_ = Slave_->Next(); return *this; diff --git a/util/generic/list_ut.pyx b/util/generic/list_ut.pyx index 9ae44c5ffd..129e5bc9b6 100644 --- a/util/generic/list_ut.pyx +++ b/util/generic/list_ut.pyx @@ -1,158 +1,158 @@ from util.generic.list cimport TList - -import unittest + +import unittest from cython.operator cimport preincrement - - + + class TestList(unittest.TestCase): - - def test_ctor1(self): + + def test_ctor1(self): cdef TList[int] tmp = TList[int]() - self.assertEqual(tmp.size(), 0) - - def test_ctor2(self): + self.assertEqual(tmp.size(), 0) + + def test_ctor2(self): cdef TList[int] tmp = TList[int](10, 42) - self.assertEqual(tmp.size(), 10) + self.assertEqual(tmp.size(), 10) self.assertEqual(tmp.front(), 42) - - def test_ctor3(self): + + def test_ctor3(self): cdef TList[int] tmp = TList[int](10, 42) cdef TList[int] tmp2 = TList[int](tmp) - self.assertEqual(tmp2.size(), 10) + self.assertEqual(tmp2.size(), 10) self.assertEqual(tmp2.front(), 42) - - def test_operator_assign(self): + + def test_operator_assign(self): cdef TList[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - + tmp2.push_back(1) + tmp2.push_back(2) + cdef TList[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - tmp3 = tmp2 - - def test_compare(self): + tmp3.push_back(1) + tmp3.push_back(3) + + tmp3 = tmp2 + + def test_compare(self): cdef TList[int] tmp1 - tmp1.push_back(1) - tmp1.push_back(2) - + tmp1.push_back(1) + tmp1.push_back(2) + cdef TList[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - + tmp2.push_back(1) + tmp2.push_back(2) + cdef TList[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - self.assertTrue(tmp1 == tmp2) - self.assertTrue(tmp1 != tmp3) - - self.assertTrue(tmp1 < tmp3) - self.assertTrue(tmp1 <= tmp3) - - self.assertTrue(tmp3 > tmp1) - self.assertTrue(tmp3 >= tmp1) - - def test_push_pop_back(self): + tmp3.push_back(1) + tmp3.push_back(3) + + self.assertTrue(tmp1 == tmp2) + self.assertTrue(tmp1 != tmp3) + + self.assertTrue(tmp1 < tmp3) + self.assertTrue(tmp1 <= tmp3) + + self.assertTrue(tmp3 > tmp1) + self.assertTrue(tmp3 >= tmp1) + + def test_push_pop_back(self): cdef TList[int] tmp - self.assertEqual(tmp.size(), 0) - - tmp.push_back(42) - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.push_back(77) - self.assertEqual(tmp.size(), 2) - self.assertEqual(tmp.back(), 77) - - tmp.pop_back() - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.pop_back() - self.assertEqual(tmp.size(), 0) - - def test_front(self): + self.assertEqual(tmp.size(), 0) + + tmp.push_back(42) + self.assertEqual(tmp.size(), 1) + self.assertEqual(tmp.back(), 42) + + tmp.push_back(77) + self.assertEqual(tmp.size(), 2) + self.assertEqual(tmp.back(), 77) + + tmp.pop_back() + self.assertEqual(tmp.size(), 1) + self.assertEqual(tmp.back(), 42) + + tmp.pop_back() + self.assertEqual(tmp.size(), 0) + + def test_front(self): cdef TList[int] tmp - tmp.push_back(42) - self.assertEqual(tmp.front(), 42) - - def test_empty(self): + tmp.push_back(42) + self.assertEqual(tmp.front(), 42) + + def test_empty(self): cdef TList[int] tmp - self.assertTrue(tmp.empty()) - tmp.push_back(42) - self.assertFalse(tmp.empty()) - - def test_max_size(self): + self.assertTrue(tmp.empty()) + tmp.push_back(42) + self.assertFalse(tmp.empty()) + + def test_max_size(self): cdef TList[int] tmp - self.assertTrue(tmp.max_size() > 0) - + self.assertTrue(tmp.max_size() > 0) + def test_resize(self): cdef TList[int] tmp - - tmp.resize(100, 42) - self.assertEqual(tmp.size(), 100) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_iter(self): + + tmp.resize(100, 42) + self.assertEqual(tmp.size(), 100) + self.assertEqual(tmp.front(), 42) + self.assertEqual(tmp.back(), 42) + + def test_iter(self): cdef TList[int] tmp - tmp.push_back(1) - tmp.push_back(20) - tmp.push_back(300) - - self.assertEqual([i for i in tmp], [1, 20, 300]) - - def test_iterator(self): + tmp.push_back(1) + tmp.push_back(20) + tmp.push_back(300) + + self.assertEqual([i for i in tmp], [1, 20, 300]) + + def test_iterator(self): cdef TList[int] tmp - - self.assertTrue(tmp.begin() == tmp.end()) - self.assertTrue(tmp.rbegin() == tmp.rend()) - self.assertTrue(tmp.const_begin() == tmp.const_end()) - self.assertTrue(tmp.const_rbegin() == tmp.const_rend()) - - tmp.push_back(1) - - self.assertTrue(tmp.begin() != tmp.end()) - self.assertTrue(tmp.rbegin() != tmp.rend()) - self.assertTrue(tmp.const_begin() != tmp.const_end()) - self.assertTrue(tmp.const_rbegin() != tmp.const_rend()) - + + self.assertTrue(tmp.begin() == tmp.end()) + self.assertTrue(tmp.rbegin() == tmp.rend()) + self.assertTrue(tmp.const_begin() == tmp.const_end()) + self.assertTrue(tmp.const_rbegin() == tmp.const_rend()) + + tmp.push_back(1) + + self.assertTrue(tmp.begin() != tmp.end()) + self.assertTrue(tmp.rbegin() != tmp.rend()) + self.assertTrue(tmp.const_begin() != tmp.const_end()) + self.assertTrue(tmp.const_rbegin() != tmp.const_rend()) + self.assertTrue(preincrement(tmp.begin()) == tmp.end()) self.assertTrue(preincrement(tmp.rbegin()) == tmp.rend()) self.assertTrue(preincrement(tmp.const_begin()) == tmp.const_end()) self.assertTrue(preincrement(tmp.const_rbegin()) == tmp.const_rend()) - - def test_assign(self): + + def test_assign(self): cdef TList[int] tmp - - tmp.assign(10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_insert(self): + + tmp.assign(10, 42) + self.assertEqual(tmp.size(), 10) + self.assertEqual(tmp.front(), 42) + self.assertEqual(tmp.back(), 42) + + def test_insert(self): cdef TList[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - - tmp.insert(tmp.begin(), 8) - self.assertEqual([i for i in tmp], [8, 1, 2, 3]) - - tmp.insert(tmp.begin(), 2, 6) - self.assertEqual([i for i in tmp], [6, 6, 8, 1, 2, 3]) - - def test_erase(self): + tmp.push_back(1) + tmp.push_back(2) + tmp.push_back(3) + + tmp.insert(tmp.begin(), 8) + self.assertEqual([i for i in tmp], [8, 1, 2, 3]) + + tmp.insert(tmp.begin(), 2, 6) + self.assertEqual([i for i in tmp], [6, 6, 8, 1, 2, 3]) + + def test_erase(self): cdef TList[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - tmp.push_back(4) - + tmp.push_back(1) + tmp.push_back(2) + tmp.push_back(3) + tmp.push_back(4) + tmp.erase(preincrement(tmp.begin())) - self.assertEqual([i for i in tmp], [1, 3, 4]) - + self.assertEqual([i for i in tmp], [1, 3, 4]) + tmp.erase(tmp.begin(), preincrement(preincrement(tmp.begin()))) - self.assertEqual([i for i in tmp], [4]) + self.assertEqual([i for i in tmp], [4]) diff --git a/util/generic/maybe.pxd b/util/generic/maybe.pxd index 736f44c5e6..15161dd204 100644 --- a/util/generic/maybe.pxd +++ b/util/generic/maybe.pxd @@ -1,35 +1,35 @@ -cdef extern from "<util/generic/maybe.h>" nogil: - cdef cppclass TNothing: - pass - - cdef TNothing Nothing() - - cdef cppclass TMaybe[T]: - TMaybe(...) except + - - TMaybe& operator=(...) except + - - void ConstructInPlace(...) except + - void Clear() except + - - bint Defined() - bint Empty() - - void CheckDefined() except + - - T* Get() except + - T& GetRef() except + - - T GetOrElse(T&) except + - TMaybe OrElse(TMaybe&) except + - - TMaybe[U] Cast[U]() except + - - void Swap(TMaybe& other) except + - - bint operator ==[U](U&) except + - bint operator !=[U](U&) except + - bint operator <[U](U&) except + - bint operator >[U](U&) except + - bint operator <=[U](U&) except + - bint operator >=[U](U&) except + +cdef extern from "<util/generic/maybe.h>" nogil: + cdef cppclass TNothing: + pass + + cdef TNothing Nothing() + + cdef cppclass TMaybe[T]: + TMaybe(...) except + + + TMaybe& operator=(...) except + + + void ConstructInPlace(...) except + + void Clear() except + + + bint Defined() + bint Empty() + + void CheckDefined() except + + + T* Get() except + + T& GetRef() except + + + T GetOrElse(T&) except + + TMaybe OrElse(TMaybe&) except + + + TMaybe[U] Cast[U]() except + + + void Swap(TMaybe& other) except + + + bint operator ==[U](U&) except + + bint operator !=[U](U&) except + + bint operator <[U](U&) except + + bint operator >[U](U&) except + + bint operator <=[U](U&) except + + bint operator >=[U](U&) except + diff --git a/util/generic/maybe_ut.pyx b/util/generic/maybe_ut.pyx index c983f11053..2de185c807 100644 --- a/util/generic/maybe_ut.pyx +++ b/util/generic/maybe_ut.pyx @@ -1,185 +1,185 @@ -from util.generic.maybe cimport TMaybe, Nothing - -import pytest -import unittest - - -def _check_from_py(TMaybe[int] x): - return x.Defined() - - -def _check_to_py_value(): - cdef TMaybe[int] tmp = TMaybe[int](42) - return tmp - - -def _check_to_py_nothing(): - cdef TMaybe[int] tmp = Nothing() - return tmp - - -class TestMaybe(unittest.TestCase): - - def test_ctor1(self): - cdef TMaybe[int] tmp = TMaybe[int]() - self.assertFalse(tmp.Defined()) - - def test_ctor2(self): - cdef TMaybe[int] tmp = TMaybe[int](42) - self.assertTrue(tmp.Defined()) - self.assertEquals(tmp.GetRef(), 42) - - def test_ctor3(self): - cdef TMaybe[int] tmp = Nothing() - self.assertFalse(tmp.Defined()) - - def test_operator_assign(self): - cdef TMaybe[int] tmp - tmp = 42 - self.assertTrue(tmp.Defined()) - self.assertEquals(tmp.GetRef(), 42) - - def test_compare(self): - cdef TMaybe[int] tmp1 = 17 - cdef TMaybe[int] tmp2 = 42 - cdef TMaybe[int] nothing - - # == - self.assertTrue(tmp1 == 17) - self.assertTrue(tmp1 == tmp1) - self.assertTrue(nothing == nothing) - - self.assertFalse(tmp1 == 16) - self.assertFalse(tmp1 == tmp2) - self.assertFalse(tmp1 == nothing) - - # != - self.assertTrue(tmp1 != 16) - self.assertTrue(tmp1 != tmp2) - self.assertTrue(tmp1 != nothing) - - self.assertFalse(tmp1 != 17) - self.assertFalse(tmp1 != tmp1) - self.assertFalse(nothing != nothing) - - # < - self.assertTrue(nothing < tmp1) - self.assertTrue(nothing < tmp2) - self.assertTrue(tmp1 < tmp2) - self.assertTrue(nothing < 0) - self.assertTrue(tmp1 < 18) - - self.assertFalse(nothing < nothing) - self.assertFalse(tmp1 < tmp1) - self.assertFalse(tmp2 < tmp1) - self.assertFalse(tmp1 < 16) - - # <= - self.assertTrue(nothing <= nothing) - self.assertTrue(nothing <= tmp1) - self.assertTrue(nothing <= tmp2) - self.assertTrue(tmp1 <= tmp1) - self.assertTrue(tmp1 <= tmp2) - self.assertTrue(nothing <= 0) - self.assertTrue(tmp1 <= 18) - - self.assertFalse(tmp2 <= tmp1) - self.assertFalse(tmp1 <= 16) - - # > - self.assertTrue(tmp1 > nothing) - self.assertTrue(tmp2 > nothing) - self.assertTrue(tmp2 > tmp1) - self.assertTrue(tmp1 > 16) - - self.assertFalse(nothing > nothing) - self.assertFalse(nothing > 0) - self.assertFalse(tmp1 > tmp1) - self.assertFalse(tmp1 > tmp2) - self.assertFalse(tmp1 > 18) - - # >= - self.assertTrue(nothing >= nothing) - self.assertTrue(tmp1 >= nothing) - self.assertTrue(tmp2 >= nothing) - self.assertTrue(tmp2 >= tmp1) - self.assertTrue(tmp1 >= tmp1) - self.assertTrue(tmp1 >= 16) - - self.assertFalse(nothing >= 0) - self.assertFalse(tmp1 >= tmp2) - self.assertFalse(tmp1 >= 18) - - def test_construct_in_place(self): - cdef TMaybe[int] tmp - tmp.ConstructInPlace(42) - self.assertTrue(tmp.Defined()) - self.assertEquals(tmp.GetRef(), 42) - - def test_clear(self): - cdef TMaybe[int] tmp = 42 - tmp.Clear() - self.assertFalse(tmp.Defined()) - - def test_defined(self): - cdef TMaybe[int] tmp - self.assertFalse(tmp.Defined()) - self.assertTrue(tmp.Empty()) - tmp = 42 - self.assertTrue(tmp.Defined()) - self.assertFalse(tmp.Empty()) - - def test_check_defined(self): - cdef TMaybe[int] tmp - with pytest.raises(RuntimeError): - tmp.CheckDefined() - tmp = 42 - tmp.CheckDefined() - - def test_get(self): - cdef TMaybe[int] tmp = 42 - cdef int* p = tmp.Get() - self.assertTrue(p != NULL) - self.assertEquals(p[0], 42) - - def test_get_ref(self): - cdef TMaybe[int] tmp = 42 - self.assertTrue(tmp.Defined()) - self.assertEquals(tmp.GetRef(), 42) - - def test_get_or_else(self): - cdef TMaybe[int] tmp = 42 - self.assertEquals(tmp.GetOrElse(13), 42) - tmp.Clear() - self.assertEquals(tmp.GetOrElse(13), 13) - - def test_or_else(self): - cdef TMaybe[int] tmp = 42 - cdef TMaybe[int] nothing - self.assertFalse(nothing.OrElse(nothing).Defined()) - self.assertEquals(tmp.OrElse(nothing).GetRef(), 42) - self.assertEquals(nothing.OrElse(tmp).GetRef(), 42) - self.assertEquals(tmp.OrElse(tmp).GetRef(), 42) - - def test_cast(self): - cdef TMaybe[int] tmp = 42 - cdef TMaybe[char] tmp2 = tmp.Cast[char]() - self.assertEquals(tmp2.GetRef(), 42) - - def test_swap(self): - cdef TMaybe[int] tmp1 = 42 - cdef TMaybe[int] tmp2 - tmp2.Swap(tmp1) - self.assertFalse(tmp1.Defined()) - self.assertEquals(tmp2.GetRef(), 42) - - def test_from_py(self): - self.assertTrue(_check_from_py(42)) - self.assertFalse(_check_from_py(None)) - - with self.assertRaises(TypeError): - _check_from_py("ttt") - - def test_to_py(self): - self.assertEquals(_check_to_py_value(), 42) - self.assertEquals(_check_to_py_nothing(), None) +from util.generic.maybe cimport TMaybe, Nothing + +import pytest +import unittest + + +def _check_from_py(TMaybe[int] x): + return x.Defined() + + +def _check_to_py_value(): + cdef TMaybe[int] tmp = TMaybe[int](42) + return tmp + + +def _check_to_py_nothing(): + cdef TMaybe[int] tmp = Nothing() + return tmp + + +class TestMaybe(unittest.TestCase): + + def test_ctor1(self): + cdef TMaybe[int] tmp = TMaybe[int]() + self.assertFalse(tmp.Defined()) + + def test_ctor2(self): + cdef TMaybe[int] tmp = TMaybe[int](42) + self.assertTrue(tmp.Defined()) + self.assertEquals(tmp.GetRef(), 42) + + def test_ctor3(self): + cdef TMaybe[int] tmp = Nothing() + self.assertFalse(tmp.Defined()) + + def test_operator_assign(self): + cdef TMaybe[int] tmp + tmp = 42 + self.assertTrue(tmp.Defined()) + self.assertEquals(tmp.GetRef(), 42) + + def test_compare(self): + cdef TMaybe[int] tmp1 = 17 + cdef TMaybe[int] tmp2 = 42 + cdef TMaybe[int] nothing + + # == + self.assertTrue(tmp1 == 17) + self.assertTrue(tmp1 == tmp1) + self.assertTrue(nothing == nothing) + + self.assertFalse(tmp1 == 16) + self.assertFalse(tmp1 == tmp2) + self.assertFalse(tmp1 == nothing) + + # != + self.assertTrue(tmp1 != 16) + self.assertTrue(tmp1 != tmp2) + self.assertTrue(tmp1 != nothing) + + self.assertFalse(tmp1 != 17) + self.assertFalse(tmp1 != tmp1) + self.assertFalse(nothing != nothing) + + # < + self.assertTrue(nothing < tmp1) + self.assertTrue(nothing < tmp2) + self.assertTrue(tmp1 < tmp2) + self.assertTrue(nothing < 0) + self.assertTrue(tmp1 < 18) + + self.assertFalse(nothing < nothing) + self.assertFalse(tmp1 < tmp1) + self.assertFalse(tmp2 < tmp1) + self.assertFalse(tmp1 < 16) + + # <= + self.assertTrue(nothing <= nothing) + self.assertTrue(nothing <= tmp1) + self.assertTrue(nothing <= tmp2) + self.assertTrue(tmp1 <= tmp1) + self.assertTrue(tmp1 <= tmp2) + self.assertTrue(nothing <= 0) + self.assertTrue(tmp1 <= 18) + + self.assertFalse(tmp2 <= tmp1) + self.assertFalse(tmp1 <= 16) + + # > + self.assertTrue(tmp1 > nothing) + self.assertTrue(tmp2 > nothing) + self.assertTrue(tmp2 > tmp1) + self.assertTrue(tmp1 > 16) + + self.assertFalse(nothing > nothing) + self.assertFalse(nothing > 0) + self.assertFalse(tmp1 > tmp1) + self.assertFalse(tmp1 > tmp2) + self.assertFalse(tmp1 > 18) + + # >= + self.assertTrue(nothing >= nothing) + self.assertTrue(tmp1 >= nothing) + self.assertTrue(tmp2 >= nothing) + self.assertTrue(tmp2 >= tmp1) + self.assertTrue(tmp1 >= tmp1) + self.assertTrue(tmp1 >= 16) + + self.assertFalse(nothing >= 0) + self.assertFalse(tmp1 >= tmp2) + self.assertFalse(tmp1 >= 18) + + def test_construct_in_place(self): + cdef TMaybe[int] tmp + tmp.ConstructInPlace(42) + self.assertTrue(tmp.Defined()) + self.assertEquals(tmp.GetRef(), 42) + + def test_clear(self): + cdef TMaybe[int] tmp = 42 + tmp.Clear() + self.assertFalse(tmp.Defined()) + + def test_defined(self): + cdef TMaybe[int] tmp + self.assertFalse(tmp.Defined()) + self.assertTrue(tmp.Empty()) + tmp = 42 + self.assertTrue(tmp.Defined()) + self.assertFalse(tmp.Empty()) + + def test_check_defined(self): + cdef TMaybe[int] tmp + with pytest.raises(RuntimeError): + tmp.CheckDefined() + tmp = 42 + tmp.CheckDefined() + + def test_get(self): + cdef TMaybe[int] tmp = 42 + cdef int* p = tmp.Get() + self.assertTrue(p != NULL) + self.assertEquals(p[0], 42) + + def test_get_ref(self): + cdef TMaybe[int] tmp = 42 + self.assertTrue(tmp.Defined()) + self.assertEquals(tmp.GetRef(), 42) + + def test_get_or_else(self): + cdef TMaybe[int] tmp = 42 + self.assertEquals(tmp.GetOrElse(13), 42) + tmp.Clear() + self.assertEquals(tmp.GetOrElse(13), 13) + + def test_or_else(self): + cdef TMaybe[int] tmp = 42 + cdef TMaybe[int] nothing + self.assertFalse(nothing.OrElse(nothing).Defined()) + self.assertEquals(tmp.OrElse(nothing).GetRef(), 42) + self.assertEquals(nothing.OrElse(tmp).GetRef(), 42) + self.assertEquals(tmp.OrElse(tmp).GetRef(), 42) + + def test_cast(self): + cdef TMaybe[int] tmp = 42 + cdef TMaybe[char] tmp2 = tmp.Cast[char]() + self.assertEquals(tmp2.GetRef(), 42) + + def test_swap(self): + cdef TMaybe[int] tmp1 = 42 + cdef TMaybe[int] tmp2 + tmp2.Swap(tmp1) + self.assertFalse(tmp1.Defined()) + self.assertEquals(tmp2.GetRef(), 42) + + def test_from_py(self): + self.assertTrue(_check_from_py(42)) + self.assertFalse(_check_from_py(None)) + + with self.assertRaises(TypeError): + _check_from_py("ttt") + + def test_to_py(self): + self.assertEquals(_check_to_py_value(), 42) + self.assertEquals(_check_to_py_nothing(), None) diff --git a/util/generic/overloaded.h b/util/generic/overloaded.h index eb9b58cad4..96a97e44bc 100644 --- a/util/generic/overloaded.h +++ b/util/generic/overloaded.h @@ -1,52 +1,52 @@ #pragma once -/** +/** * Construct an ad-hoc object with an overloaded `operator()`. - * + * * Typically used with lambdas to construct type-matching visitors for e.g. std::variant: - * ``` + * ``` * std::variant<int, void*, TString> var; - * Visit(TOverloaded{ - * [](int val) { Cerr << "int: " << val; }, - * [](void* val) { Cerr << "ptr: " << val; }, - * [](const TString& val) { Cerr << "str: " << val; }, - * }, var); - * ``` - * - * *** IMPORTANT NOTE (IMPLICIT ARGUMENT CONVERSIONS) *** - * - * Since the resulting objects use regular overloaded method resolution rules, - * methods may be called by inexact matches (causing implicit casts), hence this - * implementation does not guarantee exhaustiveness of cases. - * - * For example, the following code will compile and run by casting all values to - * double: - * ``` + * Visit(TOverloaded{ + * [](int val) { Cerr << "int: " << val; }, + * [](void* val) { Cerr << "ptr: " << val; }, + * [](const TString& val) { Cerr << "str: " << val; }, + * }, var); + * ``` + * + * *** IMPORTANT NOTE (IMPLICIT ARGUMENT CONVERSIONS) *** + * + * Since the resulting objects use regular overloaded method resolution rules, + * methods may be called by inexact matches (causing implicit casts), hence this + * implementation does not guarantee exhaustiveness of cases. + * + * For example, the following code will compile and run by casting all values to + * double: + * ``` * std::variant<int, double, char> var; - * Visit(TOverloaded{ - * [](double val) { Cerr << "dbl: " << val; }, - * }, var); - * ``` - * - * If cases may be ambigous or specific type-matching logic is required, - * a verbose `if constexpr`-based version would be preferred: - * ``` + * Visit(TOverloaded{ + * [](double val) { Cerr << "dbl: " << val; }, + * }, var); + * ``` + * + * If cases may be ambigous or specific type-matching logic is required, + * a verbose `if constexpr`-based version would be preferred: + * ``` * std::variant<int, double, char> var; - * Visit([](auto&& val) { - * using T = std::decay_t<decltype(val)>; - * if constexpr (std::is_same_v<T, int>) { - * Cerr << "int: " << val; - * } else if constexpr (std::is_same_v<T, double>) { - * Cerr << "dbl: " << val; - * } else if constexpr (std::is_same_v<T, char>) { - * Cerr << "chr: " << val; - * } else { - * static_assert(TDependentFalse<T>, "unexpected type"); - * } - * }, var); - * ``` - */ - + * Visit([](auto&& val) { + * using T = std::decay_t<decltype(val)>; + * if constexpr (std::is_same_v<T, int>) { + * Cerr << "int: " << val; + * } else if constexpr (std::is_same_v<T, double>) { + * Cerr << "dbl: " << val; + * } else if constexpr (std::is_same_v<T, char>) { + * Cerr << "chr: " << val; + * } else { + * static_assert(TDependentFalse<T>, "unexpected type"); + * } + * }, var); + * ``` + */ + template <class... Fs> struct TOverloaded: Fs... { using Fs::operator()...; diff --git a/util/generic/overloaded_ut.cpp b/util/generic/overloaded_ut.cpp index bf32daf475..f3d73895ad 100644 --- a/util/generic/overloaded_ut.cpp +++ b/util/generic/overloaded_ut.cpp @@ -1,4 +1,4 @@ -#include <util/generic/overloaded.h> +#include <util/generic/overloaded.h> #include <library/cpp/testing/unittest/registar.h> @@ -35,7 +35,7 @@ Y_UNIT_TEST_SUITE(TOverloadedTest) { [&](double) { res = -1; }, [&](TType1) { res = -1; }}, v); - UNIT_ASSERT_VALUES_EQUAL(res, 5); + UNIT_ASSERT_VALUES_EQUAL(res, 5); } Y_UNIT_TEST(TupleTest) { @@ -48,35 +48,35 @@ Y_UNIT_TEST_SUITE(TOverloadedTest) { [&](bool val) { res += "(bool) " + ToString(val) + ' '; }, }); - UNIT_ASSERT_VALUES_EQUAL(res, "(int) 5 (double) 3.14 (bool) 1 (int) 20 "); + UNIT_ASSERT_VALUES_EQUAL(res, "(int) 5 (double) 3.14 (bool) 1 (int) 20 "); } - - Y_UNIT_TEST(ImplicitConversionsTest) { + + Y_UNIT_TEST(ImplicitConversionsTest) { using TTestVariant = std::variant<int, double, char>; - - // Purposefully exhibit inexact overload matched with implicit type - // conversions - - // All cases implicitly cast to int - auto matchAsInt = [](TTestVariant var) { + + // Purposefully exhibit inexact overload matched with implicit type + // conversions + + // All cases implicitly cast to int + auto matchAsInt = [](TTestVariant var) { return std::visit(TOverloaded{ [](int val) { return val; }, }, var); - }; - - UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{17.77}), 17); - UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{12345}), 12345); - UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{'X'}), 88); - - // All cases implicitly cast to double - auto matchAsDouble = [](TTestVariant var) { + }; + + UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{17.77}), 17); + UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{12345}), 12345); + UNIT_ASSERT_VALUES_EQUAL(matchAsInt(TTestVariant{'X'}), 88); + + // All cases implicitly cast to double + auto matchAsDouble = [](TTestVariant var) { return std::visit(TOverloaded{ [](double val) { return val; }, }, var); - }; - - UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{17.77}), 17.77); - UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{12345}), 12345.0); - UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{'X'}), 88.0); - } + }; + + UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{17.77}), 17.77); + UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{12345}), 12345.0); + UNIT_ASSERT_VALUES_EQUAL(matchAsDouble(TTestVariant{'X'}), 88.0); + } } diff --git a/util/generic/ptr.h b/util/generic/ptr.h index 2ef6a9ad72..19db0e3ec5 100644 --- a/util/generic/ptr.h +++ b/util/generic/ptr.h @@ -411,37 +411,37 @@ private: C Counter_; }; -/** - * Atomically reference-counted base with a virtual destructor. - * - * @note Plays well with inheritance, should be used for refcounted base classes. - */ +/** + * Atomically reference-counted base with a virtual destructor. + * + * @note Plays well with inheritance, should be used for refcounted base classes. + */ struct TThrRefBase: public TRefCounted<TThrRefBase, TAtomicCounter> { virtual ~TThrRefBase(); }; -/** - * Atomically reference-counted base. - * - * Deletes refcounted object as type T. - * - * @warning Additional care should be taken with regard to inheritance. If used - * as a base class, @p T should either declare a virtual destructor, or be +/** + * Atomically reference-counted base. + * + * Deletes refcounted object as type T. + * + * @warning Additional care should be taken with regard to inheritance. If used + * as a base class, @p T should either declare a virtual destructor, or be * derived from @p TThrRefBase instead. Otherwise, only destructor of class @p T - * would be called, potentially slicing the object and creating memory leaks. - * - * @note To avoid accidental inheritance when it is not originally intended, - * class @p T should be marked as final. - */ + * would be called, potentially slicing the object and creating memory leaks. + * + * @note To avoid accidental inheritance when it is not originally intended, + * class @p T should be marked as final. + */ template <class T, class D = TDelete> using TAtomicRefCount = TRefCounted<T, TAtomicCounter, D>; -/** - * Non-atomically reference-counted base. - * - * @warning Not thread-safe. Use with great care. If in doubt, use @p ThrRefBase - * or @p TAtomicRefCount instead. - */ +/** + * Non-atomically reference-counted base. + * + * @warning Not thread-safe. Use with great care. If in doubt, use @p ThrRefBase + * or @p TAtomicRefCount instead. + */ template <class T, class D = TDelete> using TSimpleRefCount = TRefCounted<T, TSimpleCounter, D>; diff --git a/util/generic/strbuf_ut.cpp b/util/generic/strbuf_ut.cpp index 245bc3175e..69cde785af 100644 --- a/util/generic/strbuf_ut.cpp +++ b/util/generic/strbuf_ut.cpp @@ -28,15 +28,15 @@ Y_UNIT_TEST_SUITE(TStrBufTest) { } Y_UNIT_TEST(TestConstExpr) { - static constexpr TStringBuf str1("qwe\0rty", 7); + static constexpr TStringBuf str1("qwe\0rty", 7); static constexpr TStringBuf str2(str1.data(), str1.size()); static constexpr TStringBuf str3 = "qwe\0rty"sv; - + UNIT_ASSERT_VALUES_EQUAL(str1.size(), 7); - - UNIT_ASSERT_VALUES_EQUAL(str1, str2); - UNIT_ASSERT_VALUES_EQUAL(str2, str3); - UNIT_ASSERT_VALUES_EQUAL(str1, str3); + + UNIT_ASSERT_VALUES_EQUAL(str1, str2); + UNIT_ASSERT_VALUES_EQUAL(str2, str3); + UNIT_ASSERT_VALUES_EQUAL(str1, str3); static constexpr std::string_view view1(str1); UNIT_ASSERT_VALUES_EQUAL(str1, view1); @@ -47,8 +47,8 @@ Y_UNIT_TEST_SUITE(TStrBufTest) { UNIT_ASSERT_VALUES_EQUAL(str1, str4); static_assert(str1.data() == str4.data()); static_assert(str1.size() == str4.size()); - } - + } + Y_UNIT_TEST(TestAfter) { TStringBuf str("qwerty"); diff --git a/util/generic/string.h b/util/generic/string.h index de41fa7c0c..8cd8aa6917 100644 --- a/util/generic/string.h +++ b/util/generic/string.h @@ -79,11 +79,11 @@ struct TStdString: public TRefCountHolder, public B { } static TStdString* NullStr() noexcept { - #ifdef _LIBCPP_VERSION + #ifdef _LIBCPP_VERSION return (TStdString*)NULL_STRING_REPR; - #else + #else return Singleton<TStdString>(); - #endif + #endif } private: diff --git a/util/generic/string.pxd b/util/generic/string.pxd index 917c4e5a2a..c25f7392a1 100644 --- a/util/generic/string.pxd +++ b/util/generic/string.pxd @@ -1,8 +1,8 @@ -from libcpp.string cimport string as _std_string +from libcpp.string cimport string as _std_string -cdef extern from "<util/generic/strbuf.h>" nogil: - - cdef cppclass TStringBuf: +cdef extern from "<util/generic/strbuf.h>" nogil: + + cdef cppclass TStringBuf: TStringBuf() except + TStringBuf(const char*) except + TStringBuf(const char*, size_t) except + @@ -10,14 +10,14 @@ cdef extern from "<util/generic/strbuf.h>" nogil: char* Data() size_t size() size_t Size() - - + + cdef extern from "<util/generic/string.h>" nogil: size_t npos "TString::npos" - # Inheritance is bogus, but it's safe to assume TString is-a TStringBuf via implicit cast - cdef cppclass TString(TStringBuf): + # Inheritance is bogus, but it's safe to assume TString is-a TStringBuf via implicit cast + cdef cppclass TString(TStringBuf): TString() except + TString(TString&) except + TString(_std_string&) except + @@ -28,34 +28,34 @@ cdef extern from "<util/generic/string.h>" nogil: # as a TString formed by a repetition of character c, n times. TString(size_t, char) except + TString(char*, char*) except + - TString(TStringBuf&) except + - TString(TStringBuf&, TStringBuf&) except + - TString(TStringBuf&, TStringBuf&, TStringBuf&) except + + TString(TStringBuf&) except + + TString(TStringBuf&, TStringBuf&) except + + TString(TStringBuf&, TStringBuf&, TStringBuf&) except + const char* c_str() size_t max_size() size_t length() - void resize(size_t) except + - void resize(size_t, char c) except + + void resize(size_t) except + + void resize(size_t, char c) except + size_t capacity() - void reserve(size_t) except + - void clear() except + + void reserve(size_t) except + + void clear() except + bint empty() char& at(size_t) char& operator[](size_t) - int compare(TStringBuf&) + int compare(TStringBuf&) - TString& append(TStringBuf&) except + - TString& append(TStringBuf&, size_t, size_t) except + + TString& append(TStringBuf&) except + + TString& append(TStringBuf&, size_t, size_t) except + TString& append(char *) except + TString& append(char *, size_t) except + TString& append(size_t, char) except + - void push_back(char c) except + + void push_back(char c) except + - TString& assign(TStringBuf&) except + - TString& assign(TStringBuf&, size_t, size_t) except + + TString& assign(TStringBuf&) except + + TString& assign(TStringBuf&, size_t, size_t) except + TString& assign(char *) except + TString& assign(char *, size_t) except + @@ -65,54 +65,54 @@ cdef extern from "<util/generic/string.h>" nogil: TString& insert(size_t, char* s, size_t) except + TString& insert(size_t, size_t, char c) except + - size_t copy(char *, size_t) except + - size_t copy(char *, size_t, size_t) except + + size_t copy(char *, size_t) except + + size_t copy(char *, size_t, size_t) except + - size_t find(TStringBuf&) - size_t find(TStringBuf&, size_t pos) - size_t find(char) + size_t find(TStringBuf&) + size_t find(TStringBuf&, size_t pos) + size_t find(char) size_t find(char, size_t pos) - size_t rfind(TStringBuf&) - size_t rfind(TStringBuf&, size_t pos) - size_t rfind(char) - size_t rfind(char, size_t pos) + size_t rfind(TStringBuf&) + size_t rfind(TStringBuf&, size_t pos) + size_t rfind(char) + size_t rfind(char, size_t pos) size_t find_first_of(char c) - size_t find_first_of(char c, size_t pos) - size_t find_first_of(TStringBuf& set) - size_t find_first_of(TStringBuf& set, size_t pos) + size_t find_first_of(char c, size_t pos) + size_t find_first_of(TStringBuf& set) + size_t find_first_of(TStringBuf& set, size_t pos) size_t find_first_not_of(char c) - size_t find_first_not_of(char c, size_t pos) - size_t find_first_not_of(TStringBuf& set) - size_t find_first_not_of(TStringBuf& set, size_t pos) + size_t find_first_not_of(char c, size_t pos) + size_t find_first_not_of(TStringBuf& set) + size_t find_first_not_of(TStringBuf& set, size_t pos) size_t find_last_of(char c) - size_t find_last_of(char c, size_t pos) - size_t find_last_of(TStringBuf& set) - size_t find_last_of(TStringBuf& set, size_t pos) + size_t find_last_of(char c, size_t pos) + size_t find_last_of(TStringBuf& set) + size_t find_last_of(TStringBuf& set, size_t pos) TString substr(size_t pos) except + TString substr(size_t pos, size_t n) except + - TString operator+(TStringBuf& rhs) except + + TString operator+(TStringBuf& rhs) except + TString operator+(char* rhs) except + - bint operator==(TStringBuf&) + bint operator==(TStringBuf&) bint operator==(char*) - bint operator!=(TStringBuf&) - bint operator!=(char*) + bint operator!=(TStringBuf&) + bint operator!=(char*) - bint operator<(TStringBuf&) - bint operator<(char*) + bint operator<(TStringBuf&) + bint operator<(char*) - bint operator>(TStringBuf&) - bint operator>(char*) + bint operator>(TStringBuf&) + bint operator>(char*) - bint operator<=(TStringBuf&) - bint operator<=(char*) + bint operator<=(TStringBuf&) + bint operator<=(char*) - bint operator>=(TStringBuf&) - bint operator>=(char*) + bint operator>=(TStringBuf&) + bint operator>=(char*) diff --git a/util/generic/string_ut.cpp b/util/generic/string_ut.cpp index 0d47f2d022..ac82e9091d 100644 --- a/util/generic/string_ut.cpp +++ b/util/generic/string_ut.cpp @@ -107,7 +107,7 @@ protected: // Non-shared behaviour - never shrink s.reserve(256); - #ifndef TSTRING_IS_STD_STRING + #ifndef TSTRING_IS_STD_STRING const auto* data = s.data(); UNIT_ASSERT(s.capacity() >= 256); @@ -115,18 +115,18 @@ protected: s.reserve(128); UNIT_ASSERT(s.capacity() >= 256 && s.data() == data); - #endif + #endif s.resize(64, 'x'); s.reserve(10); - #ifdef TSTRING_IS_STD_STRING + #ifdef TSTRING_IS_STD_STRING UNIT_ASSERT(s.capacity() >= 64); - #else + #else UNIT_ASSERT(s.capacity() >= 256 && s.data() == data); - #endif + #endif - #ifndef TSTRING_IS_STD_STRING + #ifndef TSTRING_IS_STD_STRING // Shared behaviour - always reallocate, just as much as requisted TStringType holder = s; @@ -146,7 +146,7 @@ protected: UNIT_ASSERT(s.capacity() >= 64 && s.capacity() < 128 && s.data() != data); UNIT_ASSERT(s.IsDetached()); - #endif + #endif #endif } diff --git a/util/generic/string_ut.pyx b/util/generic/string_ut.pyx index 0aafffd1d1..5407f5b4c1 100644 --- a/util/generic/string_ut.pyx +++ b/util/generic/string_ut.pyx @@ -1,225 +1,225 @@ # cython: c_string_type=str, c_string_encoding=utf8 -from libcpp.string cimport string as std_string +from libcpp.string cimport string as std_string from util.generic.string cimport TString, npos - -import pytest -import unittest - + +import pytest +import unittest + import sys - -class TestStroka(unittest.TestCase): + +class TestStroka(unittest.TestCase): def test_unicode(self): cdef TString x = "привет" self.assertEquals(x, "привет") - - def test_ctor1(self): + + def test_ctor1(self): cdef TString tmp = TString() cdef TString tmp2 = TString(tmp) self.assertEquals(tmp2, "") - - def test_ctor2(self): - cdef std_string tmp = b"hello" + + def test_ctor2(self): + cdef std_string tmp = b"hello" cdef TString tmp2 = TString(tmp) self.assertEquals(tmp2, "hello") - - def test_ctor3(self): - cdef TString tmp = b"hello" + + def test_ctor3(self): + cdef TString tmp = b"hello" cdef TString tmp2 = TString(tmp, 0, 4) self.assertEquals(tmp2, "hell") - - def test_ctor4(self): + + def test_ctor4(self): cdef TString tmp = TString(<char*>b"hello") self.assertEquals(tmp, "hello") - - def test_ctor5(self): + + def test_ctor5(self): cdef TString tmp = TString(<char*>b"hello", 4) self.assertEquals(tmp, "hell") - - def test_ctor6(self): + + def test_ctor6(self): cdef TString tmp = TString(<char*>b"hello", 1, 3) self.assertEquals(tmp, "ell") - - def test_ctor7(self): + + def test_ctor7(self): cdef TString tmp = TString(3, <char>'x') self.assertEquals(tmp, "xxx") - - def test_ctor8(self): - cdef bytes tmp = b"hello" + + def test_ctor8(self): + cdef bytes tmp = b"hello" cdef TString tmp2 = TString(<char*>tmp, <char*>tmp + 4) self.assertEquals(tmp2, "hell") - - def test_compare(self): - cdef TString tmp1 = b"abacab" - cdef TString tmp2 = b"abacab" - cdef TString tmp3 = b"abacac" - - self.assertTrue(tmp1.compare(tmp2) == 0) - self.assertTrue(tmp1.compare(tmp3) < 0) - self.assertTrue(tmp3.compare(tmp1) > 0) - - self.assertTrue(tmp1 == tmp2) - self.assertTrue(tmp1 != tmp3) - - self.assertTrue(tmp1 < tmp3) - self.assertTrue(tmp1 <= tmp3) - - self.assertTrue(tmp3 > tmp1) - self.assertTrue(tmp3 >= tmp1) - - def test_operator_assign(self): - cdef TString tmp = b"hello" + + def test_compare(self): + cdef TString tmp1 = b"abacab" + cdef TString tmp2 = b"abacab" + cdef TString tmp3 = b"abacac" + + self.assertTrue(tmp1.compare(tmp2) == 0) + self.assertTrue(tmp1.compare(tmp3) < 0) + self.assertTrue(tmp3.compare(tmp1) > 0) + + self.assertTrue(tmp1 == tmp2) + self.assertTrue(tmp1 != tmp3) + + self.assertTrue(tmp1 < tmp3) + self.assertTrue(tmp1 <= tmp3) + + self.assertTrue(tmp3 > tmp1) + self.assertTrue(tmp3 >= tmp1) + + def test_operator_assign(self): + cdef TString tmp = b"hello" cdef TString tmp2 = tmp self.assertEquals(tmp2, "hello") - - def test_operator_plus(self): - cdef TString tmp = TString(b"hello ") + TString(b"world") + + def test_operator_plus(self): + cdef TString tmp = TString(b"hello ") + TString(b"world") self.assertEquals(tmp, "hello world") - - def test_c_str(self): - cdef TString tmp = b"hello" + + def test_c_str(self): + cdef TString tmp = b"hello" if sys.version_info.major == 2: self.assertEquals(bytes(tmp.c_str()), b"hello") else: self.assertEquals(bytes(tmp.c_str(), 'utf8'), b"hello") - - def test_length(self): - cdef TString tmp = b"hello" - self.assertEquals(tmp.size(), tmp.length()) - - def test_index(self): - cdef TString tmp = b"hello" - - self.assertEquals(<bytes>tmp[0], b'h') - self.assertEquals(<bytes>tmp.at(0), b'h') - - self.assertEquals(<bytes>tmp[4], b'o') - self.assertEquals(<bytes>tmp.at(4), b'o') - + + def test_length(self): + cdef TString tmp = b"hello" + self.assertEquals(tmp.size(), tmp.length()) + + def test_index(self): + cdef TString tmp = b"hello" + + self.assertEquals(<bytes>tmp[0], b'h') + self.assertEquals(<bytes>tmp.at(0), b'h') + + self.assertEquals(<bytes>tmp[4], b'o') + self.assertEquals(<bytes>tmp.at(4), b'o') + # Actually, TString::at() is noexcept - # with pytest.raises(IndexError): - # tmp.at(100) - - def test_append(self): + # with pytest.raises(IndexError): + # tmp.at(100) + + def test_append(self): cdef TString tmp - cdef TString tmp2 = b"fuu" - - tmp.append(tmp2) + cdef TString tmp2 = b"fuu" + + tmp.append(tmp2) self.assertEquals(tmp, "fuu") - - tmp.append(tmp2, 1, 2) + + tmp.append(tmp2, 1, 2) self.assertEquals(tmp, "fuuuu") - - tmp.append(<char*>"ll ") + + tmp.append(<char*>"ll ") self.assertEquals(tmp, "fuuuull ") - - tmp.append(<char*>"of greatness", 4) + + tmp.append(<char*>"of greatness", 4) self.assertEquals(tmp, "fuuuull of g") - - tmp.append(2, <char>b'o') + + tmp.append(2, <char>b'o') self.assertEquals(tmp, "fuuuull of goo") - - tmp.push_back(b'z') + + tmp.push_back(b'z') self.assertEquals(tmp, "fuuuull of gooz") - - def test_assign(self): + + def test_assign(self): cdef TString tmp - - tmp.assign(b"one") + + tmp.assign(b"one") self.assertEquals(tmp, "one") - - tmp.assign(b"two hundred", 0, 3) + + tmp.assign(b"two hundred", 0, 3) self.assertEquals(tmp, "two") - - tmp.assign(<char*>b"three") + + tmp.assign(<char*>b"three") self.assertEquals(tmp, "three") - - tmp.assign(<char*>b"three fiddy", 5) + + tmp.assign(<char*>b"three fiddy", 5) self.assertEquals(tmp, "three") - - def test_insert(self): + + def test_insert(self): cdef TString tmp - - tmp = b"xx" - tmp.insert(1, b"foo") + + tmp = b"xx" + tmp.insert(1, b"foo") self.assertEquals(tmp, "xfoox") - - tmp = b"xx" - tmp.insert(1, b"haxor", 1, 3) + + tmp = b"xx" + tmp.insert(1, b"haxor", 1, 3) self.assertEquals(tmp, "xaxox") - - tmp = b"xx" - tmp.insert(1, <char*>b"foo") + + tmp = b"xx" + tmp.insert(1, <char*>b"foo") self.assertEquals(tmp, "xfoox") - - tmp = b"xx" - tmp.insert(1, <char*>b"foozzy", 3) + + tmp = b"xx" + tmp.insert(1, <char*>b"foozzy", 3) self.assertEquals(tmp, "xfoox") - - tmp = b"xx" - tmp.insert(1, 2, <char>b'u') + + tmp = b"xx" + tmp.insert(1, 2, <char>b'u') self.assertEquals(tmp, "xuux") - - def test_copy(self): - cdef char buf[16] - cdef TString tmp = b"hello" - tmp.copy(buf, 5, 0) + + def test_copy(self): + cdef char buf[16] + cdef TString tmp = b"hello" + tmp.copy(buf, 5, 0) self.assertEquals(buf[:5], "hello") - - def test_find(self): - cdef TString haystack = b"whole lotta bytes" + + def test_find(self): + cdef TString haystack = b"whole lotta bytes" cdef TString needle = "hole" - - self.assertEquals(haystack.find(needle), 1) - self.assertEquals(haystack.find(needle, 3), npos) - - self.assertEquals(haystack.find(<char>b'h'), 1) - self.assertEquals(haystack.find(<char>b'h', 3), npos) - - def test_rfind(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString needle = b"hole" - - self.assertEquals(haystack.rfind(needle), 1) - self.assertEquals(haystack.rfind(needle, 0), npos) - - self.assertEquals(haystack.rfind(<char>b'h'), 1) - self.assertEquals(haystack.rfind(<char>b'h', 0), npos) - - def test_find_first_of(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString cset = b"hxz" - - self.assertEquals(haystack.find_first_of(<char>b'h'), 1) - self.assertEquals(haystack.find_first_of(<char>b'h', 3), npos) - - self.assertEquals(haystack.find_first_of(cset), 1) - self.assertEquals(haystack.find_first_of(cset, 3), npos) - - def test_first_not_of(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString cset = b"wxz" - - self.assertEquals(haystack.find_first_not_of(<char>b'w'), 1) - self.assertEquals(haystack.find_first_not_of(<char>b'w', 3), 3) - - self.assertEquals(haystack.find_first_not_of(cset), 1) - self.assertEquals(haystack.find_first_not_of(cset, 3), 3) - - def test_find_last_of(self): - cdef TString haystack = b"whole lotta bytes" - cdef TString cset = b"hxz" - - self.assertEquals(haystack.find_last_of(<char>b'h'), 1) - self.assertEquals(haystack.find_last_of(<char>b'h', 0), npos) - - self.assertEquals(haystack.find_last_of(cset), 1) - self.assertEquals(haystack.find_last_of(cset, 0), npos) - - def test_substr(self): - cdef TString tmp = b"foobar" - + + self.assertEquals(haystack.find(needle), 1) + self.assertEquals(haystack.find(needle, 3), npos) + + self.assertEquals(haystack.find(<char>b'h'), 1) + self.assertEquals(haystack.find(<char>b'h', 3), npos) + + def test_rfind(self): + cdef TString haystack = b"whole lotta bytes" + cdef TString needle = b"hole" + + self.assertEquals(haystack.rfind(needle), 1) + self.assertEquals(haystack.rfind(needle, 0), npos) + + self.assertEquals(haystack.rfind(<char>b'h'), 1) + self.assertEquals(haystack.rfind(<char>b'h', 0), npos) + + def test_find_first_of(self): + cdef TString haystack = b"whole lotta bytes" + cdef TString cset = b"hxz" + + self.assertEquals(haystack.find_first_of(<char>b'h'), 1) + self.assertEquals(haystack.find_first_of(<char>b'h', 3), npos) + + self.assertEquals(haystack.find_first_of(cset), 1) + self.assertEquals(haystack.find_first_of(cset, 3), npos) + + def test_first_not_of(self): + cdef TString haystack = b"whole lotta bytes" + cdef TString cset = b"wxz" + + self.assertEquals(haystack.find_first_not_of(<char>b'w'), 1) + self.assertEquals(haystack.find_first_not_of(<char>b'w', 3), 3) + + self.assertEquals(haystack.find_first_not_of(cset), 1) + self.assertEquals(haystack.find_first_not_of(cset, 3), 3) + + def test_find_last_of(self): + cdef TString haystack = b"whole lotta bytes" + cdef TString cset = b"hxz" + + self.assertEquals(haystack.find_last_of(<char>b'h'), 1) + self.assertEquals(haystack.find_last_of(<char>b'h', 0), npos) + + self.assertEquals(haystack.find_last_of(cset), 1) + self.assertEquals(haystack.find_last_of(cset, 0), npos) + + def test_substr(self): + cdef TString tmp = b"foobar" + self.assertEquals(tmp.substr(1), "oobar") self.assertEquals(tmp.substr(1, 4), "ooba") diff --git a/util/generic/ut/ya.make b/util/generic/ut/ya.make index 61f5ed02a7..6eaf24cc5f 100644 --- a/util/generic/ut/ya.make +++ b/util/generic/ut/ya.make @@ -32,7 +32,7 @@ SRCS( generic/maybe_ut.cpp generic/mem_copy_ut.cpp generic/objects_counter_ut.cpp - generic/overloaded_ut.cpp + generic/overloaded_ut.cpp generic/ptr_ut.cpp generic/queue_ut.cpp generic/serialized_enum_ut.cpp diff --git a/util/generic/vector.pxd b/util/generic/vector.pxd index 1675abb609..99dde95d48 100644 --- a/util/generic/vector.pxd +++ b/util/generic/vector.pxd @@ -1,4 +1,4 @@ -cdef extern from "<util/generic/vector.h>" nogil: +cdef extern from "<util/generic/vector.h>" nogil: cdef cppclass TVector[T]: cppclass iterator: T& operator*() @@ -12,67 +12,67 @@ cdef extern from "<util/generic/vector.h>" nogil: bint operator>(iterator) bint operator<=(iterator) bint operator>=(iterator) - + cppclass reverse_iterator: T& operator*() - reverse_iterator operator++() - reverse_iterator operator--() - reverse_iterator operator+(size_t) - reverse_iterator operator-(size_t) + reverse_iterator operator++() + reverse_iterator operator--() + reverse_iterator operator+(size_t) + reverse_iterator operator-(size_t) bint operator==(reverse_iterator) bint operator!=(reverse_iterator) bint operator<(reverse_iterator) bint operator>(reverse_iterator) bint operator<=(reverse_iterator) bint operator>=(reverse_iterator) - + cppclass const_iterator(iterator): pass - + cppclass const_reverse_iterator(reverse_iterator): pass - + TVector() except + TVector(TVector&) except + TVector(size_t) except + TVector(size_t, T&) except + - + bint operator==(TVector&) bint operator!=(TVector&) bint operator<(TVector&) bint operator>(TVector&) bint operator<=(TVector&) bint operator>=(TVector&) - - void assign(size_t, const T&) except + + + void assign(size_t, const T&) except + void assign[input_iterator](input_iterator, input_iterator) except + - + T& at(size_t) except + - T& operator[](size_t) - + T& operator[](size_t) + T& back() iterator begin() const_iterator const_begin "begin"() size_t capacity() - void clear() except + + void clear() except + bint empty() iterator end() const_iterator const_end "end"() - iterator erase(iterator) except + - iterator erase(iterator, iterator) except + + iterator erase(iterator) except + + iterator erase(iterator, iterator) except + T& front() iterator insert(iterator, const T&) except + void insert(iterator, size_t, const T&) except + void insert[Iter](iterator, Iter, Iter) except + size_t max_size() - void pop_back() except + + void pop_back() except + void push_back(T&) except + void emplace_back(...) except + reverse_iterator rbegin() const_reverse_iterator const_rbegin "rbegin"() reverse_iterator rend() const_reverse_iterator const_rend "rend"() - void reserve(size_t) except + + void reserve(size_t) except + void resize(size_t) except + void resize(size_t, T&) except + size_t size() @@ -80,4 +80,4 @@ cdef extern from "<util/generic/vector.h>" nogil: # C++11 methods T* data() - void shrink_to_fit() except + + void shrink_to_fit() except + diff --git a/util/generic/vector_ut.pyx b/util/generic/vector_ut.pyx index 1aa7af2427..904e67733b 100644 --- a/util/generic/vector_ut.pyx +++ b/util/generic/vector_ut.pyx @@ -1,221 +1,221 @@ # cython: c_string_type=str, c_string_encoding=utf8 from util.generic.vector cimport TVector -from util.generic.string cimport TString - -import pytest -import unittest - - +from util.generic.string cimport TString + +import pytest +import unittest + + def _check_convert(TVector[TString] x): - return x - - -class TestVector(unittest.TestCase): - - def test_ctor1(self): + return x + + +class TestVector(unittest.TestCase): + + def test_ctor1(self): cdef TVector[int] tmp = TVector[int]() - self.assertEqual(tmp.size(), 0) - - def test_ctor2(self): + self.assertEqual(tmp.size(), 0) + + def test_ctor2(self): cdef TVector[int] tmp = TVector[int](10) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp[0], 0) - - def test_ctor3(self): + self.assertEqual(tmp.size(), 10) + self.assertEqual(tmp[0], 0) + + def test_ctor3(self): cdef TVector[int] tmp = TVector[int](10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp[0], 42) - - def test_ctor4(self): + self.assertEqual(tmp.size(), 10) + self.assertEqual(tmp[0], 42) + + def test_ctor4(self): cdef TVector[int] tmp = TVector[int](10, 42) cdef TVector[int] tmp2 = TVector[int](tmp) - self.assertEqual(tmp2.size(), 10) - self.assertEqual(tmp2[0], 42) - - def test_operator_assign(self): + self.assertEqual(tmp2.size(), 10) + self.assertEqual(tmp2[0], 42) + + def test_operator_assign(self): cdef TVector[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - + tmp2.push_back(1) + tmp2.push_back(2) + cdef TVector[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - + tmp3.push_back(1) + tmp3.push_back(3) + self.assertEqual(tmp2[1], 2) self.assertEqual(tmp3[1], 3) - tmp3 = tmp2 - + tmp3 = tmp2 + self.assertEqual(tmp2[1], 2) self.assertEqual(tmp3[1], 2) - def test_compare(self): + def test_compare(self): cdef TVector[int] tmp1 - tmp1.push_back(1) - tmp1.push_back(2) - + tmp1.push_back(1) + tmp1.push_back(2) + cdef TVector[int] tmp2 - tmp2.push_back(1) - tmp2.push_back(2) - + tmp2.push_back(1) + tmp2.push_back(2) + cdef TVector[int] tmp3 - tmp3.push_back(1) - tmp3.push_back(3) - - self.assertTrue(tmp1 == tmp2) - self.assertTrue(tmp1 != tmp3) - - self.assertTrue(tmp1 < tmp3) - self.assertTrue(tmp1 <= tmp3) - - self.assertTrue(tmp3 > tmp1) - self.assertTrue(tmp3 >= tmp1) - - def test_index(self): + tmp3.push_back(1) + tmp3.push_back(3) + + self.assertTrue(tmp1 == tmp2) + self.assertTrue(tmp1 != tmp3) + + self.assertTrue(tmp1 < tmp3) + self.assertTrue(tmp1 <= tmp3) + + self.assertTrue(tmp3 > tmp1) + self.assertTrue(tmp3 >= tmp1) + + def test_index(self): cdef TVector[int] tmp = TVector[int](10, 42) - - self.assertEqual(tmp[0], 42) - self.assertEqual(tmp[5], 42) - - self.assertEqual(tmp.data()[0], 42) - self.assertEqual(tmp.data()[5], 42) - - self.assertEqual(tmp.at(0), 42) - self.assertEqual(tmp.at(5), 42) - - with pytest.raises(IndexError): - tmp.at(100) - - def test_push_pop_back(self): + + self.assertEqual(tmp[0], 42) + self.assertEqual(tmp[5], 42) + + self.assertEqual(tmp.data()[0], 42) + self.assertEqual(tmp.data()[5], 42) + + self.assertEqual(tmp.at(0), 42) + self.assertEqual(tmp.at(5), 42) + + with pytest.raises(IndexError): + tmp.at(100) + + def test_push_pop_back(self): cdef TVector[int] tmp - self.assertEqual(tmp.size(), 0) - - tmp.push_back(42) - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.push_back(77) - self.assertEqual(tmp.size(), 2) - self.assertEqual(tmp.back(), 77) - - tmp.pop_back() - self.assertEqual(tmp.size(), 1) - self.assertEqual(tmp.back(), 42) - - tmp.pop_back() - self.assertEqual(tmp.size(), 0) - - def test_front(self): + self.assertEqual(tmp.size(), 0) + + tmp.push_back(42) + self.assertEqual(tmp.size(), 1) + self.assertEqual(tmp.back(), 42) + + tmp.push_back(77) + self.assertEqual(tmp.size(), 2) + self.assertEqual(tmp.back(), 77) + + tmp.pop_back() + self.assertEqual(tmp.size(), 1) + self.assertEqual(tmp.back(), 42) + + tmp.pop_back() + self.assertEqual(tmp.size(), 0) + + def test_front(self): cdef TVector[int] tmp - tmp.push_back(42) - self.assertEqual(tmp.front(), 42) - - def test_empty(self): + tmp.push_back(42) + self.assertEqual(tmp.front(), 42) + + def test_empty(self): cdef TVector[int] tmp - self.assertTrue(tmp.empty()) - tmp.push_back(42) - self.assertFalse(tmp.empty()) - - def test_max_size(self): + self.assertTrue(tmp.empty()) + tmp.push_back(42) + self.assertFalse(tmp.empty()) + + def test_max_size(self): cdef TVector[int] tmp - self.assertTrue(tmp.max_size() > 0) - - def test_reserve_resize(self): + self.assertTrue(tmp.max_size() > 0) + + def test_reserve_resize(self): cdef TVector[int] tmp - tmp.reserve(1000) - self.assertEqual(tmp.capacity(), 1000) - - tmp.resize(100) - self.assertEqual(tmp.size(), 100) - self.assertEqual(tmp.front(), 0) - self.assertEqual(tmp.back(), 0) - - tmp.shrink_to_fit() - tmp.clear() - - tmp.resize(100, 42) - self.assertEqual(tmp.size(), 100) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_iter(self): + tmp.reserve(1000) + self.assertEqual(tmp.capacity(), 1000) + + tmp.resize(100) + self.assertEqual(tmp.size(), 100) + self.assertEqual(tmp.front(), 0) + self.assertEqual(tmp.back(), 0) + + tmp.shrink_to_fit() + tmp.clear() + + tmp.resize(100, 42) + self.assertEqual(tmp.size(), 100) + self.assertEqual(tmp.front(), 42) + self.assertEqual(tmp.back(), 42) + + def test_iter(self): cdef TVector[int] tmp - tmp.push_back(1) - tmp.push_back(20) - tmp.push_back(300) - - self.assertEqual([i for i in tmp], [1, 20, 300]) - - def test_iterator(self): + tmp.push_back(1) + tmp.push_back(20) + tmp.push_back(300) + + self.assertEqual([i for i in tmp], [1, 20, 300]) + + def test_iterator(self): cdef TVector[int] tmp - - self.assertTrue(tmp.begin() == tmp.end()) - self.assertTrue(tmp.rbegin() == tmp.rend()) - self.assertTrue(tmp.const_begin() == tmp.const_end()) - self.assertTrue(tmp.const_rbegin() == tmp.const_rend()) - - tmp.push_back(1) - - self.assertTrue(tmp.begin() != tmp.end()) - self.assertTrue(tmp.rbegin() != tmp.rend()) - self.assertTrue(tmp.const_begin() != tmp.const_end()) - self.assertTrue(tmp.const_rbegin() != tmp.const_rend()) - - self.assertTrue(tmp.begin() < tmp.end()) - self.assertTrue(tmp.rbegin() < tmp.rend()) - self.assertTrue(tmp.const_begin() < tmp.const_end()) - self.assertTrue(tmp.const_rbegin() < tmp.const_rend()) - - self.assertTrue(tmp.begin() + 1 == tmp.end()) - self.assertTrue(tmp.rbegin() + 1 == tmp.rend()) - self.assertTrue(tmp.const_begin() + 1 == tmp.const_end()) - self.assertTrue(tmp.const_rbegin() + 1 == tmp.const_rend()) - - def test_assign(self): + + self.assertTrue(tmp.begin() == tmp.end()) + self.assertTrue(tmp.rbegin() == tmp.rend()) + self.assertTrue(tmp.const_begin() == tmp.const_end()) + self.assertTrue(tmp.const_rbegin() == tmp.const_rend()) + + tmp.push_back(1) + + self.assertTrue(tmp.begin() != tmp.end()) + self.assertTrue(tmp.rbegin() != tmp.rend()) + self.assertTrue(tmp.const_begin() != tmp.const_end()) + self.assertTrue(tmp.const_rbegin() != tmp.const_rend()) + + self.assertTrue(tmp.begin() < tmp.end()) + self.assertTrue(tmp.rbegin() < tmp.rend()) + self.assertTrue(tmp.const_begin() < tmp.const_end()) + self.assertTrue(tmp.const_rbegin() < tmp.const_rend()) + + self.assertTrue(tmp.begin() + 1 == tmp.end()) + self.assertTrue(tmp.rbegin() + 1 == tmp.rend()) + self.assertTrue(tmp.const_begin() + 1 == tmp.const_end()) + self.assertTrue(tmp.const_rbegin() + 1 == tmp.const_rend()) + + def test_assign(self): cdef TVector[int] tmp - - tmp.assign(10, 42) - self.assertEqual(tmp.size(), 10) - self.assertEqual(tmp.front(), 42) - self.assertEqual(tmp.back(), 42) - - def test_insert(self): + + tmp.assign(10, 42) + self.assertEqual(tmp.size(), 10) + self.assertEqual(tmp.front(), 42) + self.assertEqual(tmp.back(), 42) + + def test_insert(self): cdef TVector[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - + tmp.push_back(1) + tmp.push_back(2) + tmp.push_back(3) + cdef TVector[int] tmp2 - tmp2.push_back(7) - tmp2.push_back(9) - - tmp.insert(tmp.begin(), 8) - self.assertEqual([i for i in tmp], [8, 1, 2, 3]) - - tmp.insert(tmp.begin(), 2, 6) - self.assertEqual([i for i in tmp], [6, 6, 8, 1, 2, 3]) - - tmp.insert(tmp.begin(), tmp2.begin(), tmp2.end()) - self.assertEqual([i for i in tmp], [7, 9, 6, 6, 8, 1, 2, 3]) - - def test_erase(self): + tmp2.push_back(7) + tmp2.push_back(9) + + tmp.insert(tmp.begin(), 8) + self.assertEqual([i for i in tmp], [8, 1, 2, 3]) + + tmp.insert(tmp.begin(), 2, 6) + self.assertEqual([i for i in tmp], [6, 6, 8, 1, 2, 3]) + + tmp.insert(tmp.begin(), tmp2.begin(), tmp2.end()) + self.assertEqual([i for i in tmp], [7, 9, 6, 6, 8, 1, 2, 3]) + + def test_erase(self): cdef TVector[int] tmp - tmp.push_back(1) - tmp.push_back(2) - tmp.push_back(3) - tmp.push_back(4) - - tmp.erase(tmp.begin() + 1) - self.assertEqual([i for i in tmp], [1, 3, 4]) - - tmp.erase(tmp.begin(), tmp.begin() + 2) - self.assertEqual([i for i in tmp], [4]) - - def test_convert(self): - src = ['foo', 'bar', 'baz'] - self.assertEqual(_check_convert(src), src) - - bad_src = ['foo', 42] - with self.assertRaises(TypeError): - _check_convert(bad_src) + tmp.push_back(1) + tmp.push_back(2) + tmp.push_back(3) + tmp.push_back(4) + + tmp.erase(tmp.begin() + 1) + self.assertEqual([i for i in tmp], [1, 3, 4]) + + tmp.erase(tmp.begin(), tmp.begin() + 2) + self.assertEqual([i for i in tmp], [4]) + + def test_convert(self): + src = ['foo', 'bar', 'baz'] + self.assertEqual(_check_convert(src), src) + + bad_src = ['foo', 42] + with self.assertRaises(TypeError): + _check_convert(bad_src) |