diff options
author | antoshkka <antoshkka@yandex-team.ru> | 2022-02-10 16:50:14 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:14 +0300 |
commit | 90277959ac43a22ec25e7b78b1a2b4f610530d51 (patch) | |
tree | 83f00e4f33f9a449c5f5a871ad9330211c595f5c /util/generic | |
parent | 9a6b05f93140131e64aa069bad7092698970130d (diff) | |
download | ydb-90277959ac43a22ec25e7b78b1a2b4f610530d51.tar.gz |
Restoring authorship annotation for <antoshkka@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/generic')
-rw-r--r-- | util/generic/algorithm_ut.cpp | 6 | ||||
-rw-r--r-- | util/generic/cast.h | 44 | ||||
-rw-r--r-- | util/generic/cast_ut.cpp | 50 | ||||
-rw-r--r-- | util/generic/strbuf_ut.cpp | 22 |
4 files changed, 61 insertions, 61 deletions
diff --git a/util/generic/algorithm_ut.cpp b/util/generic/algorithm_ut.cpp index 8d732fcc0cf..f1a27863402 100644 --- a/util/generic/algorithm_ut.cpp +++ b/util/generic/algorithm_ut.cpp @@ -12,7 +12,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT(1 == AnyOf(TStringBuf("01"), isOne)); UNIT_ASSERT(1 == AnyOf(TStringBuf("10"), isOne)); UNIT_ASSERT(1 == AnyOf(TStringBuf("11"), isOne)); - UNIT_ASSERT(0 == AnyOf(TStringBuf(), isOne)); + UNIT_ASSERT(0 == AnyOf(TStringBuf(), isOne)); const char array00[]{'0', '0'}; UNIT_ASSERT(0 == AnyOf(array00, isOne)); @@ -25,7 +25,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT(0 == AllOf(TStringBuf("01"), isOne)); UNIT_ASSERT(0 == AllOf(TStringBuf("10"), isOne)); UNIT_ASSERT(1 == AllOf(TStringBuf("11"), isOne)); - UNIT_ASSERT(1 == AllOf(TStringBuf(), isOne)); + UNIT_ASSERT(1 == AllOf(TStringBuf(), isOne)); const char array01[]{'0', '1'}; UNIT_ASSERT(0 == AllOf(array01, isOne)); @@ -37,7 +37,7 @@ Y_UNIT_TEST_SUITE(TAlgorithm) { UNIT_ASSERT(3 == CountIf(TStringBuf("____1________1____1_______"), isOne)); UNIT_ASSERT(5 == CountIf(TStringBuf("1____1________1____1_______1"), isOne)); UNIT_ASSERT(0 == CountIf(TStringBuf("___________"), isOne)); - UNIT_ASSERT(0 == CountIf(TStringBuf(), isOne)); + UNIT_ASSERT(0 == CountIf(TStringBuf(), isOne)); UNIT_ASSERT(1 == CountIf(TStringBuf("1"), isOne)); const char array[] = "____1________1____1_______"; diff --git a/util/generic/cast.h b/util/generic/cast.h index 0d4a41f385b..4c9590e7b8d 100644 --- a/util/generic/cast.h +++ b/util/generic/cast.h @@ -88,37 +88,37 @@ constexpr bool IsNegative(const TType value) noexcept { } namespace NPrivate { - template <class T> - using TUnderlyingTypeOrSelf = typename std::conditional< - std::is_enum<T>::value, - std::underlying_type<T>, // Lazy evaluatuion: do not call ::type here, because underlying_type<T> is undefined if T is not an enum. - std::enable_if<true, T> // Wrapping T in a class, that has member ::type typedef. + template <class T> + using TUnderlyingTypeOrSelf = typename std::conditional< + std::is_enum<T>::value, + std::underlying_type<T>, // Lazy evaluatuion: do not call ::type here, because underlying_type<T> is undefined if T is not an enum. + std::enable_if<true, T> // Wrapping T in a class, that has member ::type typedef. >::type::type; // Left ::type is for std::conditional, right ::type is for underlying_type/enable_if - - template <class TSmall, class TLarge> + + template <class TSmall, class TLarge> struct TSafelyConvertible { - using TSmallInt = TUnderlyingTypeOrSelf<TSmall>; - using TLargeInt = TUnderlyingTypeOrSelf<TLarge>; - - static constexpr bool Result = std::is_integral<TSmallInt>::value && std::is_integral<TLargeInt>::value && - ((std::is_signed<TSmallInt>::value == std::is_signed<TLargeInt>::value && sizeof(TSmallInt) >= sizeof(TLargeInt)) || - (std::is_signed<TSmallInt>::value && sizeof(TSmallInt) > sizeof(TLargeInt))); + using TSmallInt = TUnderlyingTypeOrSelf<TSmall>; + using TLargeInt = TUnderlyingTypeOrSelf<TLarge>; + + static constexpr bool Result = std::is_integral<TSmallInt>::value && std::is_integral<TLargeInt>::value && + ((std::is_signed<TSmallInt>::value == std::is_signed<TLargeInt>::value && sizeof(TSmallInt) >= sizeof(TLargeInt)) || + (std::is_signed<TSmallInt>::value && sizeof(TSmallInt) > sizeof(TLargeInt))); }; } template <class TSmallInt, class TLargeInt> constexpr std::enable_if_t<::NPrivate::TSafelyConvertible<TSmallInt, TLargeInt>::Result, TSmallInt> SafeIntegerCast(TLargeInt largeInt) noexcept { - return static_cast<TSmallInt>(largeInt); + return static_cast<TSmallInt>(largeInt); } -template <class TSmall, class TLarge> +template <class TSmall, class TLarge> inline std::enable_if_t<!::NPrivate::TSafelyConvertible<TSmall, TLarge>::Result, TSmall> SafeIntegerCast(TLarge largeInt) { - using TSmallInt = ::NPrivate::TUnderlyingTypeOrSelf<TSmall>; - using TLargeInt = ::NPrivate::TUnderlyingTypeOrSelf<TLarge>; - + using TSmallInt = ::NPrivate::TUnderlyingTypeOrSelf<TSmall>; + using TLargeInt = ::NPrivate::TUnderlyingTypeOrSelf<TLarge>; + if (std::is_unsigned<TSmallInt>::value && std::is_signed<TLargeInt>::value) { if (IsNegative(largeInt)) { - ythrow TBadCastException() << "Conversion '" << TypeName<TLarge>() << '{' << TLargeInt(largeInt) << "}' to '" + ythrow TBadCastException() << "Conversion '" << TypeName<TLarge>() << '{' << TLargeInt(largeInt) << "}' to '" << TypeName<TSmallInt>() << "', negative value converted to unsigned"; } @@ -128,18 +128,18 @@ inline std::enable_if_t<!::NPrivate::TSafelyConvertible<TSmall, TLarge>::Result, if (std::is_signed<TSmallInt>::value && std::is_unsigned<TLargeInt>::value) { if (IsNegative(smallInt)) { - ythrow TBadCastException() << "Conversion '" << TypeName<TLarge>() << '{' << TLargeInt(largeInt) << "}' to '" + ythrow TBadCastException() << "Conversion '" << TypeName<TLarge>() << '{' << TLargeInt(largeInt) << "}' to '" << TypeName<TSmallInt>() << "', positive value converted to negative"; } } if (TLargeInt(smallInt) != largeInt) { - ythrow TBadCastException() << "Conversion '" << TypeName<TLarge>() << '{' << TLargeInt(largeInt) << "}' to '" + ythrow TBadCastException() << "Conversion '" << TypeName<TLarge>() << '{' << TLargeInt(largeInt) << "}' to '" << TypeName<TSmallInt>() << "', loss of data"; } - return static_cast<TSmall>(smallInt); + return static_cast<TSmall>(smallInt); } template <class TSmallInt, class TLargeInt> diff --git a/util/generic/cast_ut.cpp b/util/generic/cast_ut.cpp index 718a8de79de..9892029eb11 100644 --- a/util/generic/cast_ut.cpp +++ b/util/generic/cast_ut.cpp @@ -6,7 +6,7 @@ class TGenericCastsTest: public TTestBase { UNIT_TEST_SUITE(TGenericCastsTest); UNIT_TEST(TestVerifyDynamicCast) UNIT_TEST(TestIntegralCast) - UNIT_TEST(TestEnumCast) + UNIT_TEST(TestEnumCast) UNIT_TEST(TestToUnderlying) UNIT_TEST(TestBitCast) UNIT_TEST_SUITE_END(); @@ -29,32 +29,32 @@ private: UNIT_ASSERT_EXCEPTION(SafeIntegerCast<ui16>(static_cast<i32>(Max<ui16>() + 10)), TBadCastException); UNIT_ASSERT_EXCEPTION(SafeIntegerCast<ui16>(static_cast<ui32>(Max<ui16>() + 10)), TBadCastException); } - - inline void TestEnumCast() { - enum A { - AM1 = -1 - }; - + + inline void TestEnumCast() { + enum A { + AM1 = -1 + }; + enum B: int { - BM1 = -1 - }; - + BM1 = -1 + }; + enum class C: unsigned short { - CM1 = 1 - }; - - UNIT_ASSERT_EXCEPTION(SafeIntegerCast<unsigned int>(AM1), TBadCastException); - UNIT_ASSERT_EXCEPTION(SafeIntegerCast<unsigned int>(BM1), TBadCastException); - UNIT_ASSERT_EXCEPTION(SafeIntegerCast<C>(AM1), TBadCastException); - UNIT_ASSERT_EXCEPTION(static_cast<int>(SafeIntegerCast<C>(BM1)), TBadCastException); - UNIT_ASSERT(SafeIntegerCast<A>(BM1) == AM1); - UNIT_ASSERT(SafeIntegerCast<B>(AM1) == BM1); - UNIT_ASSERT(SafeIntegerCast<A>(C::CM1) == 1); - UNIT_ASSERT(SafeIntegerCast<B>(C::CM1) == 1); - UNIT_ASSERT(SafeIntegerCast<A>(-1) == AM1); - UNIT_ASSERT(SafeIntegerCast<B>(-1) == BM1); - UNIT_ASSERT(SafeIntegerCast<C>(1) == C::CM1); - } + CM1 = 1 + }; + + UNIT_ASSERT_EXCEPTION(SafeIntegerCast<unsigned int>(AM1), TBadCastException); + UNIT_ASSERT_EXCEPTION(SafeIntegerCast<unsigned int>(BM1), TBadCastException); + UNIT_ASSERT_EXCEPTION(SafeIntegerCast<C>(AM1), TBadCastException); + UNIT_ASSERT_EXCEPTION(static_cast<int>(SafeIntegerCast<C>(BM1)), TBadCastException); + UNIT_ASSERT(SafeIntegerCast<A>(BM1) == AM1); + UNIT_ASSERT(SafeIntegerCast<B>(AM1) == BM1); + UNIT_ASSERT(SafeIntegerCast<A>(C::CM1) == 1); + UNIT_ASSERT(SafeIntegerCast<B>(C::CM1) == 1); + UNIT_ASSERT(SafeIntegerCast<A>(-1) == AM1); + UNIT_ASSERT(SafeIntegerCast<B>(-1) == BM1); + UNIT_ASSERT(SafeIntegerCast<C>(1) == C::CM1); + } void TestToUnderlying() { enum A { diff --git a/util/generic/strbuf_ut.cpp b/util/generic/strbuf_ut.cpp index 69cde785af0..cc392ea0f0f 100644 --- a/util/generic/strbuf_ut.cpp +++ b/util/generic/strbuf_ut.cpp @@ -319,19 +319,19 @@ Y_UNIT_TEST_SUITE(TStrBufTest) { s.SplitAt(pos, l, r); UNIT_ASSERT(l == "abcabc" && r == ""); // modified } - - template <class T> - void PassByConstReference(const T& val) { - // In https://st.yandex-team.ru/IGNIETFERRO-294 was assumed that `const char[]` types are compile time strings + + template <class T> + void PassByConstReference(const T& val) { + // In https://st.yandex-team.ru/IGNIETFERRO-294 was assumed that `const char[]` types are compile time strings // and that CharTraits::Length may not be called for them. Unfortunately that is not true, `char[]` types - // are easily converted to `const char[]` if they are passed to a function accepting `const T&`. - UNIT_ASSERT(TStringBuf(val).size() == 5); - } - + // are easily converted to `const char[]` if they are passed to a function accepting `const T&`. + UNIT_ASSERT(TStringBuf(val).size() == 5); + } + Y_UNIT_TEST(TestPassingArraysByConstReference) { - char data[] = "Hello\0word"; - PassByConstReference(data); - } + char data[] = "Hello\0word"; + PassByConstReference(data); + } Y_UNIT_TEST(TestTruncate) { TStringBuf s = "123"; |