diff options
| author | pkalinnikov <[email protected]> | 2022-02-10 16:50:15 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:50:15 +0300 |
| commit | 9e33e026829d561d6fd46d72b88c367952e08075 (patch) | |
| tree | 2af190fca83ac522e9a7e29de1daae32582064b4 /library/cpp/packedtypes | |
| parent | ba5325cc01aabb81effc91ff1bdbb461313cbd00 (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/packedtypes')
| -rw-r--r-- | library/cpp/packedtypes/longs.h | 16 | ||||
| -rw-r--r-- | library/cpp/packedtypes/longs_ut.cpp | 86 | ||||
| -rw-r--r-- | library/cpp/packedtypes/ut/ya.make | 2 | ||||
| -rw-r--r-- | library/cpp/packedtypes/ya.make | 2 | ||||
| -rw-r--r-- | library/cpp/packedtypes/zigzag.h | 24 | ||||
| -rw-r--r-- | library/cpp/packedtypes/zigzag_ut.cpp | 70 |
6 files changed, 100 insertions, 100 deletions
diff --git a/library/cpp/packedtypes/longs.h b/library/cpp/packedtypes/longs.h index 084098d705e..a91d5df1785 100644 --- a/library/cpp/packedtypes/longs.h +++ b/library/cpp/packedtypes/longs.h @@ -91,7 +91,7 @@ struct mem_traits { #define MY_14(x) ((ui16)(x) < PACK1LIM ? 1 : 2) #define MY_28(x) ((ui32)(x) < PACK2LIM ? MY_14(x) : ((ui32)(x) < PACK3LIM ? 3 : 4)) - + #define MY_32(x) ((ui32)(x) < PACK4LIM ? MY_28(x) : 5) #define MY_64(x) ((ui64)(x) < PACK4LIM ? MY_28(x) : ((ui64)(x) < PACK6LIM ? ((ui64)(x) < PACK5LIM ? 5 : 6) : ((ui64)(x) < PACK7LIM ? 7 : ((ui64)(x) < PACK8LIM ? 8 : 9)))) @@ -141,7 +141,7 @@ struct mem_traits { (ret) = 5; \ } \ MACRO_END - + #define PACK_64(x, buf, how, ret) \ MACRO_BEGIN \ if ((ui64)(x) < PACK4LIM) { \ @@ -227,13 +227,13 @@ struct mem_traits { (ret) = 5; \ } \ MACRO_END - + #define UNPACK_32(x, buf, how, ret) \ MACRO_BEGIN \ ui8 firstByte = how::get_8(buf); \ DO_UNPACK_32(firstByte, x, buf, how, ret); \ MACRO_END - + #define DO_UNPACK_64(firstByte, x, buf, how, ret) \ MACRO_BEGIN \ if (firstByte < 0xF0) { \ @@ -292,13 +292,13 @@ inline int len_long(const i64& longVal) { inline int in_long(i32& longVal, const char* ptrBuf) { int ret; - UNPACK_32(longVal, ptrBuf, mem_traits, ret); + UNPACK_32(longVal, ptrBuf, mem_traits, ret); return ret; } inline int out_long(const i32& longVal, char* ptrBuf) { int ret; - PACK_32(longVal, ptrBuf, mem_traits, ret); + PACK_32(longVal, ptrBuf, mem_traits, ret); return ret; } @@ -311,7 +311,7 @@ inline const C* Unpack32(T& x, const C* src) { int pkLen = 0; const char* c = reinterpret_cast<const char*>(src); Y_UNUSED(pkLen); - UNPACK_32(x, c, mem_traits, pkLen); + UNPACK_32(x, c, mem_traits, pkLen); Y_ASSERT(pkLen); return reinterpret_cast<const C*>(c); } @@ -331,7 +331,7 @@ inline C* Pack32(const T& x, C* dest) { int pkLen = 0; Y_UNUSED(pkLen); char* c = reinterpret_cast<char*>(dest); - PACK_32(x, c, mem_traits, pkLen); + PACK_32(x, c, mem_traits, pkLen); Y_ASSERT(pkLen); return reinterpret_cast<C*>(c); } diff --git a/library/cpp/packedtypes/longs_ut.cpp b/library/cpp/packedtypes/longs_ut.cpp index 8b06c934d2f..ae5fc54c253 100644 --- a/library/cpp/packedtypes/longs_ut.cpp +++ b/library/cpp/packedtypes/longs_ut.cpp @@ -9,60 +9,60 @@ Y_UNIT_TEST_SUITE(TLongsTest) { Y_UNIT_TEST(TestLongs) { - i16 x16 = 40; - i64 x64 = 40; - i64 y64; + i16 x16 = 40; + i64 x64 = 40; + i64 y64; TString s; - s += Sprintf("x16=0x%x\n", (int)x16); + s += Sprintf("x16=0x%x\n", (int)x16); s += Sprintf("LO_8(x16)=0x%x HI_8(x16)=0x%x\n\n", (int)Lo8(x16), (int)Hi8(x16)); - - char buf[100]; - memset(buf, 0, 100); + + char buf[100]; + memset(buf, 0, 100); char* p = buf; - int l = out_long(x64, buf); - s += Sprintf("x64=0x%" PRIi64 "\n", x64); + int l = out_long(x64, buf); + s += Sprintf("x64=0x%" PRIi64 "\n", x64); s += Sprintf("LO_32(x64)=0x%" PRIu32 " HI_32(x64)=0x%" PRIu32 "\n", (ui32)Lo32(x64), (ui32)Hi32(x64)); - s += Sprintf("buf=%s, l=%d: ", buf, l); - for (int i = 0; i < l; i++) { - s += Sprintf("0x%02x ", buf[i]); - } - s += Sprintf("\n"); + s += Sprintf("buf=%s, l=%d: ", buf, l); + for (int i = 0; i < l; i++) { + s += Sprintf("0x%02x ", buf[i]); + } + s += Sprintf("\n"); - p = buf; - in_long(y64, p); - s += Sprintf("x=0x%" PRIi64 " y=0x%" PRIi64 "\n", x64, y64); - if (x64 != y64) { - s += Sprintf("Error: y64 != x64\n"); - } else { - s += Sprintf("OK\n"); - } + p = buf; + in_long(y64, p); + s += Sprintf("x=0x%" PRIi64 " y=0x%" PRIi64 "\n", x64, y64); + if (x64 != y64) { + s += Sprintf("Error: y64 != x64\n"); + } else { + s += Sprintf("OK\n"); + } UNIT_ASSERT_EQUAL(Crc<ui64>(s.data(), s.size()), 7251624297500315779ULL); // WTF? - } - - template <typename TSignedInt> - void TestOneValue(TSignedInt value) { - char buffer[sizeof(TSignedInt) + 1]; - auto bytes = out_long(value, buffer); - TSignedInt readValue = 0; - auto readBytes = in_long(readValue, buffer); - UNIT_ASSERT_EQUAL(bytes, readBytes); - UNIT_ASSERT_EQUAL(bytes, len_long(value)); - UNIT_ASSERT_EQUAL(value, readValue); - } - - template <typename TSignedInt> - void TestCornerCasesImpl(int maxPow) { - for (int i = 0; i <= maxPow; ++i) { + } + + template <typename TSignedInt> + void TestOneValue(TSignedInt value) { + char buffer[sizeof(TSignedInt) + 1]; + auto bytes = out_long(value, buffer); + TSignedInt readValue = 0; + auto readBytes = in_long(readValue, buffer); + UNIT_ASSERT_EQUAL(bytes, readBytes); + UNIT_ASSERT_EQUAL(bytes, len_long(value)); + UNIT_ASSERT_EQUAL(value, readValue); + } + + template <typename TSignedInt> + void TestCornerCasesImpl(int maxPow) { + for (int i = 0; i <= maxPow; ++i) { TestOneValue<TSignedInt>((TSignedInt)(1ull << i)); TestOneValue<TSignedInt>((TSignedInt)((1ull << i) - 1)); TestOneValue<TSignedInt>((TSignedInt)((1ull << i) + 1)); } - } + } Y_UNIT_TEST(TestCornerCases) { - TestCornerCasesImpl<i32>(31); - TestCornerCasesImpl<i64>(63); - } -} + TestCornerCasesImpl<i32>(31); + TestCornerCasesImpl<i64>(63); + } +} diff --git a/library/cpp/packedtypes/ut/ya.make b/library/cpp/packedtypes/ut/ya.make index a203115e71e..357b03bdd41 100644 --- a/library/cpp/packedtypes/ut/ya.make +++ b/library/cpp/packedtypes/ut/ya.make @@ -13,7 +13,7 @@ SRCS( longs_ut.cpp packed_ut.cpp packedfloat_ut.cpp - zigzag_ut.cpp + zigzag_ut.cpp ) END() diff --git a/library/cpp/packedtypes/ya.make b/library/cpp/packedtypes/ya.make index 4c2c9506195..5f72331c7d5 100644 --- a/library/cpp/packedtypes/ya.make +++ b/library/cpp/packedtypes/ya.make @@ -15,7 +15,7 @@ SRCS( packed.h packedfloat.cpp packedfloat.h - zigzag.h + zigzag.h ) END() diff --git a/library/cpp/packedtypes/zigzag.h b/library/cpp/packedtypes/zigzag.h index 548403f8384..2adc7159027 100644 --- a/library/cpp/packedtypes/zigzag.h +++ b/library/cpp/packedtypes/zigzag.h @@ -1,21 +1,21 @@ -#pragma once - -#include <util/generic/typetraits.h> - +#pragma once + +#include <util/generic/typetraits.h> + #include <limits.h> -//! Convert signed values to unsigned. Convenient for further varint encoding. -//! See https://developers.google.com/protocol-buffers/docs/encoding#types for details. - -template <typename TSignedInt> +//! Convert signed values to unsigned. Convenient for further varint encoding. +//! See https://developers.google.com/protocol-buffers/docs/encoding#types for details. + +template <typename TSignedInt> inline auto ZigZagEncode(TSignedInt n) -> std::make_unsigned_t<TSignedInt> { static_assert(std::is_signed<TSignedInt>::value && std::is_integral<TSignedInt>::value, "Expected signed integral type."); auto un = static_cast<std::make_unsigned_t<TSignedInt>>(n); return (un << 1) ^ (n >> (CHAR_BIT * sizeof(TSignedInt) - 1)); -} - -template <typename TUnsignedInt> +} + +template <typename TUnsignedInt> inline auto ZigZagDecode(TUnsignedInt n) -> std::make_signed_t<TUnsignedInt> { static_assert(std::is_unsigned<TUnsignedInt>::value, "Expected unsigned integral type."); return (n >> 1) ^ -static_cast<std::make_signed_t<TUnsignedInt>>(n & 1); -} +} diff --git a/library/cpp/packedtypes/zigzag_ut.cpp b/library/cpp/packedtypes/zigzag_ut.cpp index 13b78b7481d..cd3284d55e1 100644 --- a/library/cpp/packedtypes/zigzag_ut.cpp +++ b/library/cpp/packedtypes/zigzag_ut.cpp @@ -1,38 +1,38 @@ -#include "zigzag.h" - +#include "zigzag.h" + #include <library/cpp/testing/unittest/registar.h> - + Y_UNIT_TEST_SUITE(TZigZagTest) { - template <typename T> - void TestEncodeDecode(T value) { - auto encoded = ZigZagEncode(value); - UNIT_ASSERT(encoded >= 0); - auto decoded = ZigZagDecode(encoded); - UNIT_ASSERT(decoded == value); - static_assert(sizeof(value) == sizeof(decoded), "sizeof mismatch"); - static_assert(sizeof(value) == sizeof(encoded), "sizeof mismatch"); - } - - template <typename TSignedInt> - void TestImpl() { - static const int bits = CHAR_BIT * sizeof(TSignedInt); - for (int p = 0; p + 1 < bits; ++p) { - TSignedInt value = 1; - value <<= p; - - TestEncodeDecode(value); - TestEncodeDecode(-value); - TestEncodeDecode(value - 1); - TestEncodeDecode(-value + 1); - } - - TestEncodeDecode((TSignedInt)123); - TestEncodeDecode((TSignedInt)-123); - } - + template <typename T> + void TestEncodeDecode(T value) { + auto encoded = ZigZagEncode(value); + UNIT_ASSERT(encoded >= 0); + auto decoded = ZigZagDecode(encoded); + UNIT_ASSERT(decoded == value); + static_assert(sizeof(value) == sizeof(decoded), "sizeof mismatch"); + static_assert(sizeof(value) == sizeof(encoded), "sizeof mismatch"); + } + + template <typename TSignedInt> + void TestImpl() { + static const int bits = CHAR_BIT * sizeof(TSignedInt); + for (int p = 0; p + 1 < bits; ++p) { + TSignedInt value = 1; + value <<= p; + + TestEncodeDecode(value); + TestEncodeDecode(-value); + TestEncodeDecode(value - 1); + TestEncodeDecode(-value + 1); + } + + TestEncodeDecode((TSignedInt)123); + TestEncodeDecode((TSignedInt)-123); + } + Y_UNIT_TEST(TestSigned) { - TestImpl<i16>(); - TestImpl<i32>(); - TestImpl<i64>(); - } -} + TestImpl<i16>(); + TestImpl<i32>(); + TestImpl<i64>(); + } +} |
