diff options
author | nkmakarov <nkmakarov@yandex-team.ru> | 2022-02-10 16:49:06 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:06 +0300 |
commit | f1803fa4ac9e2ee6cbfde317571ec330013392ff (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /util | |
parent | 324348a37ed08cf66897faefb0ec4bebfe7804e1 (diff) | |
download | ydb-f1803fa4ac9e2ee6cbfde317571ec330013392ff.tar.gz |
Restoring authorship annotation for <nkmakarov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util')
-rw-r--r-- | util/draft/date.cpp | 22 | ||||
-rw-r--r-- | util/draft/date.h | 16 | ||||
-rw-r--r-- | util/draft/date_ut.cpp | 14 | ||||
-rw-r--r-- | util/draft/enum.h | 58 | ||||
-rw-r--r-- | util/generic/algorithm.h | 68 | ||||
-rw-r--r-- | util/generic/algorithm_ut.cpp | 230 | ||||
-rw-r--r-- | util/string/cast.h | 66 | ||||
-rw-r--r-- | util/string/cast_ut.cpp | 58 |
8 files changed, 266 insertions, 266 deletions
diff --git a/util/draft/date.cpp b/util/draft/date.cpp index 2ad2989c9e5..a290c460505 100644 --- a/util/draft/date.cpp +++ b/util/draft/date.cpp @@ -17,23 +17,23 @@ time_t GetDateStart(time_t ts) { return mktime(&dateTm); } -static time_t ParseDate(const char* date, const char* format) { +static time_t ParseDate(const char* date, const char* format) { tm dateTm; memset(&dateTm, 0, sizeof(tm)); if (!strptime(date, format, &dateTm)) { - ythrow yexception() << "Invalid date string and format: " << date << ", " << format; + ythrow yexception() << "Invalid date string and format: " << date << ", " << format; } return mktime(&dateTm); } -static time_t ParseDate(const char* dateStr) { +static time_t ParseDate(const char* dateStr) { if (strlen(dateStr) != 8) { - ythrow yexception() << "Invalid date string: " << dateStr; + ythrow yexception() << "Invalid date string: " << dateStr; } - - return ParseDate(dateStr, "%Y%m%d"); -} - + + return ParseDate(dateStr, "%Y%m%d"); +} + template <> TDate FromStringImpl<TDate>(const char* data, size_t len) { return TDate(ParseDate(TString(data, len).data())); @@ -56,9 +56,9 @@ TDate::TDate(time_t ts) TDate::TDate(const TString& date, const TString& format) : Timestamp(GetDateStart(ParseDate(date.data(), format.data()))) -{ -} - +{ +} + TDate::TDate(unsigned year, unsigned month, unsigned monthDay) { tm dateTm; Zero(dateTm); diff --git a/util/draft/date.h b/util/draft/date.h index 2a9cbc39971..e3eb616fe5d 100644 --- a/util/draft/date.h +++ b/util/draft/date.h @@ -3,7 +3,7 @@ #include <util/stream/output.h> #include <util/stream/input.h> #include <util/generic/string.h> -#include <util/datetime/constants.h> +#include <util/datetime/constants.h> #include <ctime> @@ -43,31 +43,31 @@ public: TString ToStroka(const char* format = "%Y%m%d") const; TDate& operator++() { - Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2)); + Timestamp = GetDateStart(Timestamp + 3 * (SECONDS_IN_DAY / 2)); return *this; } TDate& operator--() { - Timestamp = GetDateStart(Timestamp - SECONDS_IN_DAY / 2); + Timestamp = GetDateStart(Timestamp - SECONDS_IN_DAY / 2); return *this; } TDate& operator+=(unsigned days) { - Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + Timestamp = GetDateStart(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); return *this; } TDate& operator-=(unsigned days) { - Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + Timestamp = GetDateStart(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); return *this; } TDate operator+(unsigned days) const { - return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + return TDate(Timestamp + days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); } TDate operator-(unsigned days) const { - return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); + return TDate(Timestamp - days * SECONDS_IN_DAY + SECONDS_IN_DAY / 2); } unsigned GetWeekDay() const; // days since Sunday @@ -113,7 +113,7 @@ inline int operator-(const TDate& left, const TDate& right) { if (left < right) { return -(right - left); } - return static_cast<int>((left.Timestamp + SECONDS_IN_DAY / 2 - right.Timestamp) / SECONDS_IN_DAY); + return static_cast<int>((left.Timestamp + SECONDS_IN_DAY / 2 - right.Timestamp) / SECONDS_IN_DAY); } inline IInputStream& operator>>(IInputStream& left, TDate& right) { diff --git a/util/draft/date_ut.cpp b/util/draft/date_ut.cpp index 223fa4e41c0..8c33a6c1cfd 100644 --- a/util/draft/date_ut.cpp +++ b/util/draft/date_ut.cpp @@ -24,13 +24,13 @@ Y_UNIT_TEST_SUITE(TDateTest) { UNIT_ASSERT_EQUAL(d.ToStroka("____%Y__%m____%d"), "____2005__06____03"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1117756800); } - { - TDate d("2011-02-15", "%Y-%m-%d"); - UNIT_ASSERT_EQUAL(d.GetYear(), 2011); - UNIT_ASSERT_EQUAL(d.GetMonth(), 2); - UNIT_ASSERT_EQUAL(d.GetMonthDay(), 15); - UNIT_ASSERT_EQUAL(d.ToStroka("%Y%m%d"), "20110215"); + { + TDate d("2011-02-15", "%Y-%m-%d"); + UNIT_ASSERT_EQUAL(d.GetYear(), 2011); + UNIT_ASSERT_EQUAL(d.GetMonth(), 2); + UNIT_ASSERT_EQUAL(d.GetMonthDay(), 15); + UNIT_ASSERT_EQUAL(d.ToStroka("%Y%m%d"), "20110215"); UNIT_ASSERT_EQUAL(d.GetStartUTC(), 1297728000); - } + } } } diff --git a/util/draft/enum.h b/util/draft/enum.h index c65f54357c4..18002b7df20 100644 --- a/util/draft/enum.h +++ b/util/draft/enum.h @@ -3,7 +3,7 @@ #include <bitset> #include <util/generic/strbuf.h> -#include <util/stream/str.h> +#include <util/stream/str.h> #include <util/string/cast.h> #include <util/string/split.h> #include <utility> @@ -12,11 +12,11 @@ class TEnumNotFoundException: public yexception { }; #define EnumFromString(key, entries) EnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) -#define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size) +#define EnumFromStringWithSize(key, entries, size) EnumFromStringImpl(key, entries, size) #define FindEnumFromString(key, entries) FindEnumFromStringImpl(key, entries, Y_ARRAY_SIZE(entries)) -#define FindEnumFromStringWithSize(key, entries, size) FindEnumFromStringImpl(key, entries, size) +#define FindEnumFromStringWithSize(key, entries, size) FindEnumFromStringImpl(key, entries, size) #define EnumToString(key, entries) EnumToStringImpl(key, entries, Y_ARRAY_SIZE(entries)) -#define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size) +#define EnumToStringWithSize(key, entries, size) EnumToStringImpl(key, entries, size) #define PrintEnumItems(entries) PrintEnumItemsImpl(entries, Y_ARRAY_SIZE(entries)) template <class K1, class K2, class V> @@ -80,39 +80,39 @@ const K* EnumToStringImpl(V value, const std::pair<K, V>* entries, size_t arrayS /////////////////////////////////// -template <class B> -inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { - if (allIfEmpty) { - flags.set(); - } else { - flags.reset(); - } -} - +template <class B> +inline void SetEnumFlagsForEmptySpec(B& flags, bool allIfEmpty) { + if (allIfEmpty) { + flags.set(); + } else { + flags.reset(); + } +} + // all set by default template <class E, size_t N, size_t B> inline void SetEnumFlags(const std::pair<const char*, E> (&str2Enum)[N], TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) { if (optSpec.empty()) { - SetEnumFlagsForEmptySpec(flags, allIfEmpty); - } else { - flags.reset(); - for (const auto& it : StringSplitter(optSpec).Split(',')) { + SetEnumFlagsForEmptySpec(flags, allIfEmpty); + } else { + flags.reset(); + for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, N); - flags.set(e); + flags.set(e); } - } -} - -template <class E, size_t B> + } +} + +template <class E, size_t B> inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf optSpec, - std::bitset<B>& flags, const size_t size, + std::bitset<B>& flags, const size_t size, bool allIfEmpty = true) { - if (optSpec.empty()) { - SetEnumFlagsForEmptySpec(flags, allIfEmpty); + if (optSpec.empty()) { + SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { flags.reset(); - for (const auto& it : StringSplitter(optSpec).Split(',')) { + for (const auto& it : StringSplitter(optSpec).Split(',')) { E e = *EnumFromStringImpl(ToString(it.Token()).data(), str2Enum, size); flags.set(e); } @@ -123,13 +123,13 @@ inline void SetEnumFlags(const std::pair<const char*, E>* str2Enum, TStringBuf o template <class E, size_t B> inline void SetEnumFlags(TStringBuf optSpec, std::bitset<B>& flags, bool allIfEmpty = true) { if (optSpec.empty()) { - SetEnumFlagsForEmptySpec(flags, allIfEmpty); + SetEnumFlagsForEmptySpec(flags, allIfEmpty); } else { flags.reset(); - for (const auto& it : StringSplitter(optSpec).Split(',')) { + for (const auto& it : StringSplitter(optSpec).Split(',')) { E e; if (!TryFromString(it.Token(), e)) - ythrow yexception() << "Unknown enum value '" << it.Token() << "'"; + ythrow yexception() << "Unknown enum value '" << it.Token() << "'"; flags.set((size_t)e); } } diff --git a/util/generic/algorithm.h b/util/generic/algorithm.h index 4e87544109d..badfb889933 100644 --- a/util/generic/algorithm.h +++ b/util/generic/algorithm.h @@ -694,25 +694,25 @@ static inline std::pair<I1, I2> Mismatch(I1 b1, I1 e1, I2 b2, I2 e2, P p) { return std::make_pair(b1, b2); } -template <class It, class Val> -static inline bool BinarySearch(It begin, It end, const Val& val) { +template <class It, class Val> +static inline bool BinarySearch(It begin, It end, const Val& val) { return std::binary_search(begin, end, val); -} +} -template <class It, class Val, class Comp> -static inline bool BinarySearch(It begin, It end, const Val& val, Comp comp) { +template <class It, class Val, class Comp> +static inline bool BinarySearch(It begin, It end, const Val& val, Comp comp) { return std::binary_search(begin, end, val, comp); -} - -template <class It, class Val> +} + +template <class It, class Val> static inline std::pair<It, It> EqualRange(It begin, It end, const Val& val) { return std::equal_range(begin, end, val); -} - -template <class It, class Val, class Comp> +} + +template <class It, class Val, class Comp> static inline std::pair<It, It> EqualRange(It begin, It end, const Val& val, Comp comp) { return std::equal_range(begin, end, val, comp); -} +} template <class ForwardIt> bool IsSorted(ForwardIt begin, ForwardIt end) { @@ -723,36 +723,36 @@ template <class ForwardIt, class Compare> bool IsSorted(ForwardIt begin, ForwardIt end, Compare comp) { return std::is_sorted(begin, end, comp); } - + template <class TIterator, typename TGetKey> bool IsSortedBy(TIterator begin, TIterator end, const TGetKey& getKey) { return IsSorted(begin, end, [&](auto&& left, auto&& right) { return getKey(left) < getKey(right); }); } -template <class It, class Val> -void Iota(It begin, It end, Val val) { +template <class It, class Val> +void Iota(It begin, It end, Val val) { std::iota(begin, end, val); -} - -template <class TI, class TO, class S> -TO CopyN(TI from, S s, TO to) { - return std::copy_n(from, s, to); -} - -template <class TI, class TO, class P> -TO CopyIf(TI begin, TI end, TO to, P pred) { - return std::copy_if(begin, end, to, pred); -} - -template <class T> +} + +template <class TI, class TO, class S> +TO CopyN(TI from, S s, TO to) { + return std::copy_n(from, s, to); +} + +template <class TI, class TO, class P> +TO CopyIf(TI begin, TI end, TO to, P pred) { + return std::copy_if(begin, end, to, pred); +} + +template <class T> std::pair<const T&, const T&> MinMax(const T& first, const T& second) { - return std::minmax(first, second); -} - -template <class It> + return std::minmax(first, second); +} + +template <class It> std::pair<It, It> MinMaxElement(It first, It last) { - return std::minmax_element(first, last); -} + return std::minmax_element(first, last); +} template <class TIterator, class TGenerator> void Generate(TIterator first, TIterator last, TGenerator generator) { diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index 1c73ab255a2..8d732fcc0cf 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -347,74 +347,74 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { } } } - + Y_UNIT_TEST(BinarySearchTest) { - { + { TVector<TString> v; - bool test = BinarySearch(v.begin(), v.end(), "test"); - UNIT_ASSERT_EQUAL(test, false); - } - - { - int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - + bool test = BinarySearch(v.begin(), v.end(), "test"); + UNIT_ASSERT_EQUAL(test, false); + } + + { + int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + bool test = BinarySearch(data, data + Y_ARRAY_SIZE(data), 2); - UNIT_ASSERT_EQUAL(test, true); - + UNIT_ASSERT_EQUAL(test, true); + test = BinarySearch(data, data + Y_ARRAY_SIZE(data), 10); - UNIT_ASSERT_EQUAL(test, false); - } - - { + UNIT_ASSERT_EQUAL(test, false); + } + + { TVector<size_t> data = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; - + bool test = BinarySearch(data.begin(), data.end(), (size_t)9, TGreater<size_t>()); - UNIT_ASSERT_EQUAL(test, true); - + UNIT_ASSERT_EQUAL(test, true); + test = BinarySearch(data.begin(), data.end(), (size_t)11, TGreater<size_t>()); - UNIT_ASSERT_EQUAL(test, false); - + UNIT_ASSERT_EQUAL(test, false); + test = BinarySearch(data.rbegin(), data.rend(), (size_t)1); - UNIT_ASSERT_EQUAL(test, true); - } - } - + UNIT_ASSERT_EQUAL(test, true); + } + } + Y_UNIT_TEST(EqualRangeTest) { - { + { TVector<TString> v; using PairOfVector = std::pair<TVector<TString>::iterator, TVector<TString>::iterator>; - PairOfVector tmp = EqualRange(v.begin(), v.end(), "tmp"); - - UNIT_ASSERT_EQUAL(tmp.first, tmp.second); - UNIT_ASSERT_EQUAL(tmp.first, v.end()); - } - - { - int data[] = {1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5}; + PairOfVector tmp = EqualRange(v.begin(), v.end(), "tmp"); + + UNIT_ASSERT_EQUAL(tmp.first, tmp.second); + UNIT_ASSERT_EQUAL(tmp.first, v.end()); + } + + { + int data[] = {1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 5}; using PairOfInt = std::pair<int*, int*>; PairOfInt tmp = EqualRange(data, data + Y_ARRAY_SIZE(data), 3); - - UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 4); - UNIT_ASSERT_EQUAL(tmp.first - data, 7); + + UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 4); + UNIT_ASSERT_EQUAL(tmp.first - data, 7); UNIT_ASSERT_EQUAL(data + Y_ARRAY_SIZE(data) - tmp.second, 2); - } - - { + } + + { TVector<size_t> data = {9, 9, 8, 8, 8, 5, 4, 3, 3, 0, 0}; - + using PairOfVector = std::pair<TVector<size_t>::iterator, TVector<size_t>::iterator>; - PairOfVector tmp = EqualRange(data.begin(), data.end(), 8, TGreater<size_t>()); - - UNIT_ASSERT_EQUAL(tmp.first - data.begin(), 2); - UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 3); - + PairOfVector tmp = EqualRange(data.begin(), data.end(), 8, TGreater<size_t>()); + + UNIT_ASSERT_EQUAL(tmp.first - data.begin(), 2); + UNIT_ASSERT_EQUAL(tmp.second - tmp.first, 3); + using PairOfVectorReverse = std::pair<TVector<size_t>::reverse_iterator, TVector<size_t>::reverse_iterator>; PairOfVectorReverse tmpR = EqualRange(data.rbegin(), data.rend(), (size_t)0); - - UNIT_ASSERT_EQUAL(tmpR.first, data.rbegin()); - UNIT_ASSERT_EQUAL(tmpR.second - tmpR.first, 2); - } - } + + UNIT_ASSERT_EQUAL(tmpR.first, data.rbegin()); + UNIT_ASSERT_EQUAL(tmpR.second - tmpR.first, 2); + } + } Y_UNIT_TEST(IsSortedTest) { TVector<int> v0; @@ -507,7 +507,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { TVector<int> expected = {10, 7, 2}; UNIT_ASSERT_VALUES_EQUAL(collection, expected); } - + Y_UNIT_TEST(StableSortByTest) { TVector<int> collection = {404, 101, 106, 203, 102, 205, 401}; StableSortBy(collection, [](int x) { return x / 100; }); @@ -531,86 +531,86 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { Y_UNIT_TEST(IotaTest) { TVector<int> v(10); - - Iota(v.begin(), v.end(), 0); - UNIT_ASSERT_VALUES_EQUAL(v[0], 0); - UNIT_ASSERT_VALUES_EQUAL(v[5], 5); - UNIT_ASSERT_VALUES_EQUAL(v[9], 9); - - Iota(v.begin() + 2, v.begin() + 5, 162); - UNIT_ASSERT_VALUES_EQUAL(v[0], 0); - UNIT_ASSERT_VALUES_EQUAL(v[3], 163); - UNIT_ASSERT_VALUES_EQUAL(v[9], 9); - } - + + Iota(v.begin(), v.end(), 0); + UNIT_ASSERT_VALUES_EQUAL(v[0], 0); + UNIT_ASSERT_VALUES_EQUAL(v[5], 5); + UNIT_ASSERT_VALUES_EQUAL(v[9], 9); + + Iota(v.begin() + 2, v.begin() + 5, 162); + UNIT_ASSERT_VALUES_EQUAL(v[0], 0); + UNIT_ASSERT_VALUES_EQUAL(v[3], 163); + UNIT_ASSERT_VALUES_EQUAL(v[9], 9); + } + Y_UNIT_TEST(CopyNTest) { int data[] = {1, 2, 3, 4, 8, 7, 6, 5}; - const size_t vSize = 10; + const size_t vSize = 10; TVector<int> result(10, 0); - size_t toCopy = 5; - + size_t toCopy = 5; + TVector<int>::iterator iter = CopyN(data, toCopy, result.begin()); - UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), toCopy); + UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), toCopy); UNIT_ASSERT_VALUES_EQUAL(result.size(), 10); - for (size_t idx = 0; idx < toCopy; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(data[idx], result[idx]); - } - for (size_t idx = toCopy; idx < vSize; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); - } - - toCopy = 8; - const size_t start = 1; - result.assign(vSize, 0); - iter = CopyN(data, toCopy, result.begin() + start); - UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), start + toCopy); - for (size_t idx = 0; idx < start; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); - } - for (size_t idx = 0; idx < toCopy; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[start + idx], data[idx]); - } - for (size_t idx = start + toCopy; idx < vSize; ++idx) { - UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); - } - } - + for (size_t idx = 0; idx < toCopy; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(data[idx], result[idx]); + } + for (size_t idx = toCopy; idx < vSize; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); + } + + toCopy = 8; + const size_t start = 1; + result.assign(vSize, 0); + iter = CopyN(data, toCopy, result.begin() + start); + UNIT_ASSERT_VALUES_EQUAL(iter - result.begin(), start + toCopy); + for (size_t idx = 0; idx < start; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); + } + for (size_t idx = 0; idx < toCopy; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[start + idx], data[idx]); + } + for (size_t idx = start + toCopy; idx < vSize; ++idx) { + UNIT_ASSERT_VALUES_EQUAL(result[idx], 0); + } + } + Y_UNIT_TEST(CopyIfTest) { - const size_t count = 9; - int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; - const size_t vSize = 10; + const size_t count = 9; + int data[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; + const size_t vSize = 10; TVector<int> v(vSize, 0); - + TVector<int>::iterator iter = CopyIf(data, data + count, v.begin(), [](int x) { return !(x % 3); }); UNIT_ASSERT_VALUES_EQUAL(v.size(), vSize); - UNIT_ASSERT_VALUES_EQUAL(iter - v.begin(), 3); - v.resize(iter - v.begin()); - for (size_t idx = 0; idx < v.size(); ++idx) { - UNIT_ASSERT_VALUES_EQUAL(v[idx], 3 * (idx + 1)); - } - } - + UNIT_ASSERT_VALUES_EQUAL(iter - v.begin(), 3); + v.resize(iter - v.begin()); + for (size_t idx = 0; idx < v.size(); ++idx) { + UNIT_ASSERT_VALUES_EQUAL(v[idx], 3 * (idx + 1)); + } + } + Y_UNIT_TEST(MinMaxElementTest) { TVector<int> v(10); - Iota(v.begin(), v.end(), 0); - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, 0); - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 9); - - v[3] = -2; - v[7] = 11; - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, -2); - UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 11); - } - + Iota(v.begin(), v.end(), 0); + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, 0); + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 9); + + v[3] = -2; + v[7] = 11; + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).first, -2); + UNIT_ASSERT_EQUAL(*MinMaxElement(v.begin(), v.end()).second, 11); + } + Y_UNIT_TEST(MinMaxTest) { std::pair<int, int> p1 = MinMax(5, 12); - UNIT_ASSERT_EQUAL(p1.first, 5); - UNIT_ASSERT_EQUAL(p1.second, 12); - + UNIT_ASSERT_EQUAL(p1.first, 5); + UNIT_ASSERT_EQUAL(p1.second, 12); + std::pair<TString, TString> p2 = MinMax(TString("test"), TString("data")); UNIT_ASSERT_EQUAL(p2.first, TString("data")); UNIT_ASSERT_EQUAL(p2.second, TString("test")); - } + } Y_UNIT_TEST(TestMaxElementBy) { const int array[] = {1, 2, 5, 3, 4, 5}; diff --git a/util/string/cast.h b/util/string/cast.h index de44a873701..90e925c194c 100644 --- a/util/string/cast.h +++ b/util/string/cast.h @@ -236,15 +236,15 @@ inline bool TryFromString(const TChar* data, T& result) { return TryFromString<T>(data, std::char_traits<TChar>::length(data), result); } -template <class T, class TChar> -inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) { - if (TryFromString<T>(data, len, result)) { - return true; - } - result = def; - return false; -} - +template <class T, class TChar> +inline bool TryFromString(const TChar* data, const size_t len, T& result, const T& def) { + if (TryFromString<T>(data, len, result)) { + return true; + } + result = def; + return false; +} + template <class T> inline bool TryFromString(const TStringBuf& s, T& result) { return TryFromString<T>(s.data(), s.size(), result); @@ -275,39 +275,39 @@ inline bool TryFromStringWithDefault(const TStringType& s, T& result, const T& d return TryFromString<T>(s.data(), s.size(), result, def); } -template <class T> -inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { - return TryFromStringWithDefault<T>(TStringBuf(s), result, def); -} - +template <class T> +inline bool TryFromStringWithDefault(const char* s, T& result, const T& def) { + return TryFromStringWithDefault<T>(TStringBuf(s), result, def); +} + template <class T, class TStringType> inline bool TryFromStringWithDefault(const TStringType& s, T& result) { - return TryFromStringWithDefault<T>(s, result, T()); -} - -// FromString methods with default value if data is invalid -template <class T, class TChar> -inline T FromString(const TChar* data, const size_t len, const T& def) { - T result; - TryFromString<T>(data, len, result, def); - return result; -} - + return TryFromStringWithDefault<T>(s, result, T()); +} + +// FromString methods with default value if data is invalid +template <class T, class TChar> +inline T FromString(const TChar* data, const size_t len, const T& def) { + T result; + TryFromString<T>(data, len, result, def); + return result; +} + template <class T, class TStringType> inline T FromStringWithDefault(const TStringType& s, const T& def) { return FromString<T>(s.data(), s.size(), def); } -template <class T> -inline T FromStringWithDefault(const char* s, const T& def) { - return FromStringWithDefault<T>(TStringBuf(s), def); -} - +template <class T> +inline T FromStringWithDefault(const char* s, const T& def) { + return FromStringWithDefault<T>(TStringBuf(s), def); +} + template <class T, class TStringType> inline T FromStringWithDefault(const TStringType& s) { - return FromStringWithDefault<T>(s, T()); -} - + return FromStringWithDefault<T>(s, T()); +} + double StrToD(const char* b, char** se); double StrToD(const char* b, const char* e, char** se); diff --git a/util/string/cast_ut.cpp b/util/string/cast_ut.cpp index edb6df2771b..033450c38c4 100644 --- a/util/string/cast_ut.cpp +++ b/util/string/cast_ut.cpp @@ -2,7 +2,7 @@ #include <library/cpp/testing/unittest/registar.h> -#include <util/charset/wide.h> +#include <util/charset/wide.h> #include <util/system/defaults.h> #include <limits> @@ -372,71 +372,71 @@ Y_UNIT_TEST_SUITE(TCastTest) { } Y_UNIT_TEST(TestDefault) { - size_t res = 0; - const size_t def1 = 42; - + size_t res = 0; + const size_t def1 = 42; + TString s1("100500"); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res, def1), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s1, res), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100500", res, def1), true); UNIT_ASSERT_VALUES_EQUAL(res, 100500); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s1, def1), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s1, def1), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s1, def1), 100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s1), 100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault("100500", def1), 100500); - + TString s2("100q500"); UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s2, res, def1), false); UNIT_ASSERT_VALUES_EQUAL(res, def1); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100q500", res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault("100 500", res), false); UNIT_ASSERT_VALUES_EQUAL(res, size_t()); - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s2, def1), yexception); - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault("100q500", def1), yexception); + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s2, def1), yexception); + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault("100q500", def1), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s2, def1), def1); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s2), size_t()); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>("100q500"), size_t()); - UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s2), TFromStringException); - - int res2 = 0; - const int def2 = -6; - + UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s2), TFromStringException); + + int res2 = 0; + const int def2 = -6; + TUtf16String s3 = u"-100500"; UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2, def2), true); UNIT_ASSERT_VALUES_EQUAL(res2, -100500); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s3, res2), true); UNIT_ASSERT_VALUES_EQUAL(res2, -100500); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s3, def1), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s3, def1), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s3, def2), -100500); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s3), size_t()); - + TUtf16String s4 = u"-f100500"; UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2, def2), false); UNIT_ASSERT_VALUES_EQUAL(res2, def2); - + UNIT_ASSERT_VALUES_EQUAL(TryFromStringWithDefault(s4, res2), false); UNIT_ASSERT_VALUES_EQUAL(res2, size_t()); - - UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); + + UNIT_CHECK_GENERATED_NO_EXCEPTION(FromStringWithDefault(s4, def2), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault(s4, def2), def2); - UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); + UNIT_CHECK_GENERATED_EXCEPTION(FromString<size_t>(s4), yexception); UNIT_ASSERT_VALUES_EQUAL(FromStringWithDefault<size_t>(s4), size_t()); - } - + } + Y_UNIT_TEST(TestBool) { // True cases UNIT_ASSERT_VALUES_EQUAL(FromString<bool>("yes"), true); |