diff options
| author | babenko <[email protected]> | 2022-02-10 16:49:19 +0300 | 
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:49:19 +0300 | 
| commit | f31097c96270919a1f49360bdaaa69ea4f3fefab (patch) | |
| tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/yt/coding/unittests | |
| parent | cec37806d8847aa3db53bafc9e251d4aaf325c12 (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/yt/coding/unittests')
| -rw-r--r-- | library/cpp/yt/coding/unittests/varint_ut.cpp | 268 | ||||
| -rw-r--r-- | library/cpp/yt/coding/unittests/ya.make | 30 | ||||
| -rw-r--r-- | library/cpp/yt/coding/unittests/zig_zag_ut.cpp | 114 | 
3 files changed, 206 insertions, 206 deletions
diff --git a/library/cpp/yt/coding/unittests/varint_ut.cpp b/library/cpp/yt/coding/unittests/varint_ut.cpp index 8ebfdfbed81..ed83ab5c92b 100644 --- a/library/cpp/yt/coding/unittests/varint_ut.cpp +++ b/library/cpp/yt/coding/unittests/varint_ut.cpp @@ -1,134 +1,134 @@ -#include <library/cpp/testing/gtest/gtest.h>  -  -#include <library/cpp/yt/coding/varint.h>  -  -#include <util/random/random.h>  -  -#include <util/string/escape.h>  -  -#include <tuple>  -  -namespace NYT {  -namespace {  -  -using ::testing::Values;  -  -////////////////////////////////////////////////////////////////////////////////  -  -class TWriteVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, TString> >  -{ };  -  -TEST_P(TWriteVarIntTest, Serialization)  -{  -    ui64 value = std::get<0>(GetParam());  -    TString rightAnswer = std::get<1>(GetParam());  -  -    TStringStream outputStream;  -    WriteVarUint64(&outputStream, value);  -    EXPECT_EQ(rightAnswer, outputStream.Str());  -}  -  -////////////////////////////////////////////////////////////////////////////////  -  -class TReadVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, TString> >  -{ };  -  -TEST_P(TReadVarIntTest, Serialization)  -{  -    ui64 rightAnswer = std::get<0>(GetParam());  -    TString input = std::get<1>(GetParam());  -  -    TStringInput inputStream(input);  -    ui64 value;  -    ReadVarUint64(&inputStream, &value);  -    EXPECT_EQ(rightAnswer, value);  -}  -  -TEST(TReadVarIntTest, Overflow)  -{  -    TString input("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 11);  -    TStringInput inputStream(input);  -    ui64 value;  -    EXPECT_ANY_THROW(ReadVarUint64(&inputStream, &value));  -}  -  -////////////////////////////////////////////////////////////////////////////////  -  -auto ValuesForVarIntTests = Values(  -    // Simple cases.  -    std::make_tuple(0x0ull,                TString("\x00", 1)),  -    std::make_tuple(0x1ull,                TString("\x01", 1)),  -    std::make_tuple(0x2ull,                TString("\x02", 1)),  -    std::make_tuple(0x3ull,                TString("\x03", 1)),  -    std::make_tuple(0x4ull,                TString("\x04", 1)),  -  -    // The following "magic numbers" are critical points for varint encoding.  -    std::make_tuple((1ull << 7) - 1,       TString("\x7f", 1)),  -    std::make_tuple((1ull << 7),           TString("\x80\x01", 2)),  -    std::make_tuple((1ull << 14) - 1,      TString("\xff\x7f", 2)),  -    std::make_tuple((1ull << 14),          TString("\x80\x80\x01", 3)),  -    std::make_tuple((1ull << 21) - 1,      TString("\xff\xff\x7f", 3)),  -    std::make_tuple((1ull << 21),          TString("\x80\x80\x80\x01", 4)),  -    std::make_tuple((1ull << 28) - 1,      TString("\xff\xff\xff\x7f", 4)),  -    std::make_tuple((1ull << 28),          TString("\x80\x80\x80\x80\x01", 5)),  -    std::make_tuple((1ull << 35) - 1,      TString("\xff\xff\xff\xff\x7f", 5)),  -    std::make_tuple((1ull << 35),          TString("\x80\x80\x80\x80\x80\x01", 6)),  -    std::make_tuple((1ull << 42) - 1,      TString("\xff\xff\xff\xff\xff\x7f", 6)),  -    std::make_tuple((1ull << 42),          TString("\x80\x80\x80\x80\x80\x80\x01", 7)),  -    std::make_tuple((1ull << 49) - 1,      TString("\xff\xff\xff\xff\xff\xff\x7f", 7)),  -    std::make_tuple((1ull << 49),          TString("\x80\x80\x80\x80\x80\x80\x80\x01", 8)),  -    std::make_tuple((1ull << 56) - 1,      TString("\xff\xff\xff\xff\xff\xff\xff\x7f", 8)),  -    std::make_tuple((1ull << 56),          TString("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9)),  -    std::make_tuple((1ull << 63) - 1,      TString("\xff\xff\xff\xff\xff\xff\xff\xff\x7f", 9)),  -    std::make_tuple((1ull << 63),          TString("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10)),  -  -    // Boundary case.  -    std::make_tuple(static_cast<ui64>(-1), TString("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", 10))  -);  -  -INSTANTIATE_TEST_SUITE_P(ValueParametrized, TWriteVarIntTest,  -    ValuesForVarIntTests);  -  -INSTANTIATE_TEST_SUITE_P(ValueParametrized, TReadVarIntTest,  -    ValuesForVarIntTests);  -  -////////////////////////////////////////////////////////////////////////////////  -  -TEST(TVarInt32Test, RandomValues)  -{  -    srand(100500); // Set seed  -    const int numberOfValues = 10000;  -  -    TStringStream stream;  -    for (int i = 0; i < numberOfValues; ++i) {  -        i32 expected = static_cast<i32>(RandomNumber<ui32>());  -        WriteVarInt32(&stream, expected);  -        i32 actual;  -        ReadVarInt32(&stream, &actual);  -        EXPECT_EQ(expected, actual)  -            << "Encoded Variant: " << EscapeC(stream.Str());  -    }  -}  -  -////////////////////////////////////////////////////////////////////////////////  -  -TEST(TVarInt64Test, RandomValues)  -{  -    srand(100500); // Set seed  -    const int numberOfValues = 10000;  -  -    TStringStream stream;  -    for (int i = 0; i < numberOfValues; ++i) {  -        i64 expected = static_cast<i64>(RandomNumber<ui64>());  -        WriteVarInt64(&stream, expected);  -        i64 actual;  -        ReadVarInt64(&stream, &actual);  -        EXPECT_EQ(expected, actual)  -            << "Encoded Variant: " << EscapeC(stream.Str());  -    }  -}  -  -////////////////////////////////////////////////////////////////////////////////  -  -} // namespace  -} // namespace NYT  +#include <library/cpp/testing/gtest/gtest.h> + +#include <library/cpp/yt/coding/varint.h> + +#include <util/random/random.h> + +#include <util/string/escape.h> + +#include <tuple> + +namespace NYT { +namespace { + +using ::testing::Values; + +//////////////////////////////////////////////////////////////////////////////// + +class TWriteVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, TString> > +{ }; + +TEST_P(TWriteVarIntTest, Serialization) +{ +    ui64 value = std::get<0>(GetParam()); +    TString rightAnswer = std::get<1>(GetParam()); + +    TStringStream outputStream; +    WriteVarUint64(&outputStream, value); +    EXPECT_EQ(rightAnswer, outputStream.Str()); +} + +//////////////////////////////////////////////////////////////////////////////// + +class TReadVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, TString> > +{ }; + +TEST_P(TReadVarIntTest, Serialization) +{ +    ui64 rightAnswer = std::get<0>(GetParam()); +    TString input = std::get<1>(GetParam()); + +    TStringInput inputStream(input); +    ui64 value; +    ReadVarUint64(&inputStream, &value); +    EXPECT_EQ(rightAnswer, value); +} + +TEST(TReadVarIntTest, Overflow) +{ +    TString input("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 11); +    TStringInput inputStream(input); +    ui64 value; +    EXPECT_ANY_THROW(ReadVarUint64(&inputStream, &value)); +} + +//////////////////////////////////////////////////////////////////////////////// + +auto ValuesForVarIntTests = Values( +    // Simple cases. +    std::make_tuple(0x0ull,                TString("\x00", 1)), +    std::make_tuple(0x1ull,                TString("\x01", 1)), +    std::make_tuple(0x2ull,                TString("\x02", 1)), +    std::make_tuple(0x3ull,                TString("\x03", 1)), +    std::make_tuple(0x4ull,                TString("\x04", 1)), + +    // The following "magic numbers" are critical points for varint encoding. +    std::make_tuple((1ull << 7) - 1,       TString("\x7f", 1)), +    std::make_tuple((1ull << 7),           TString("\x80\x01", 2)), +    std::make_tuple((1ull << 14) - 1,      TString("\xff\x7f", 2)), +    std::make_tuple((1ull << 14),          TString("\x80\x80\x01", 3)), +    std::make_tuple((1ull << 21) - 1,      TString("\xff\xff\x7f", 3)), +    std::make_tuple((1ull << 21),          TString("\x80\x80\x80\x01", 4)), +    std::make_tuple((1ull << 28) - 1,      TString("\xff\xff\xff\x7f", 4)), +    std::make_tuple((1ull << 28),          TString("\x80\x80\x80\x80\x01", 5)), +    std::make_tuple((1ull << 35) - 1,      TString("\xff\xff\xff\xff\x7f", 5)), +    std::make_tuple((1ull << 35),          TString("\x80\x80\x80\x80\x80\x01", 6)), +    std::make_tuple((1ull << 42) - 1,      TString("\xff\xff\xff\xff\xff\x7f", 6)), +    std::make_tuple((1ull << 42),          TString("\x80\x80\x80\x80\x80\x80\x01", 7)), +    std::make_tuple((1ull << 49) - 1,      TString("\xff\xff\xff\xff\xff\xff\x7f", 7)), +    std::make_tuple((1ull << 49),          TString("\x80\x80\x80\x80\x80\x80\x80\x01", 8)), +    std::make_tuple((1ull << 56) - 1,      TString("\xff\xff\xff\xff\xff\xff\xff\x7f", 8)), +    std::make_tuple((1ull << 56),          TString("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9)), +    std::make_tuple((1ull << 63) - 1,      TString("\xff\xff\xff\xff\xff\xff\xff\xff\x7f", 9)), +    std::make_tuple((1ull << 63),          TString("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10)), + +    // Boundary case. +    std::make_tuple(static_cast<ui64>(-1), TString("\xff\xff\xff\xff\xff\xff\xff\xff\xff\x01", 10)) +); + +INSTANTIATE_TEST_SUITE_P(ValueParametrized, TWriteVarIntTest, +    ValuesForVarIntTests); + +INSTANTIATE_TEST_SUITE_P(ValueParametrized, TReadVarIntTest, +    ValuesForVarIntTests); + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TVarInt32Test, RandomValues) +{ +    srand(100500); // Set seed +    const int numberOfValues = 10000; + +    TStringStream stream; +    for (int i = 0; i < numberOfValues; ++i) { +        i32 expected = static_cast<i32>(RandomNumber<ui32>()); +        WriteVarInt32(&stream, expected); +        i32 actual; +        ReadVarInt32(&stream, &actual); +        EXPECT_EQ(expected, actual) +            << "Encoded Variant: " << EscapeC(stream.Str()); +    } +} + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TVarInt64Test, RandomValues) +{ +    srand(100500); // Set seed +    const int numberOfValues = 10000; + +    TStringStream stream; +    for (int i = 0; i < numberOfValues; ++i) { +        i64 expected = static_cast<i64>(RandomNumber<ui64>()); +        WriteVarInt64(&stream, expected); +        i64 actual; +        ReadVarInt64(&stream, &actual); +        EXPECT_EQ(expected, actual) +            << "Encoded Variant: " << EscapeC(stream.Str()); +    } +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT diff --git a/library/cpp/yt/coding/unittests/ya.make b/library/cpp/yt/coding/unittests/ya.make index 23e4387fb25..e0622db22d2 100644 --- a/library/cpp/yt/coding/unittests/ya.make +++ b/library/cpp/yt/coding/unittests/ya.make @@ -1,15 +1,15 @@ -GTEST()  -  -OWNER(g:yt)  -  -SRCS(  -    zig_zag_ut.cpp  -    varint_ut.cpp  -)  -  -PEERDIR(  -    library/cpp/yt/coding  -    library/cpp/testing/gtest  -)  -  -END()  +GTEST() + +OWNER(g:yt) + +SRCS( +    zig_zag_ut.cpp +    varint_ut.cpp +) + +PEERDIR( +    library/cpp/yt/coding +    library/cpp/testing/gtest +) + +END() diff --git a/library/cpp/yt/coding/unittests/zig_zag_ut.cpp b/library/cpp/yt/coding/unittests/zig_zag_ut.cpp index ba092df2879..fae4e63064e 100644 --- a/library/cpp/yt/coding/unittests/zig_zag_ut.cpp +++ b/library/cpp/yt/coding/unittests/zig_zag_ut.cpp @@ -1,57 +1,57 @@ -#include <library/cpp/testing/gtest/gtest.h>  -  -#include <library/cpp/yt/coding/zig_zag.h>  -  -namespace NYT {  -namespace {  -  -////////////////////////////////////////////////////////////////////////////////  -  -TEST(TZigZagTest, Encode32)  -{  -    EXPECT_EQ(0u, ZigZagEncode32( 0));  -    EXPECT_EQ(1u, ZigZagEncode32(-1));  -    EXPECT_EQ(2u, ZigZagEncode32( 1));  -    EXPECT_EQ(3u, ZigZagEncode32(-2));  -    // ...  -    EXPECT_EQ(std::numeric_limits<ui32>::max() - 1, ZigZagEncode32(std::numeric_limits<i32>::max()));  -    EXPECT_EQ(std::numeric_limits<ui32>::max(),     ZigZagEncode32(std::numeric_limits<i32>::min()));  -}  -  -TEST(TZigZagTest, Decode32)  -{  -    EXPECT_EQ( 0, ZigZagDecode32(0));  -    EXPECT_EQ(-1, ZigZagDecode32(1));  -    EXPECT_EQ( 1, ZigZagDecode32(2));  -    EXPECT_EQ(-2, ZigZagDecode32(3));  -    // ...  -    EXPECT_EQ(std::numeric_limits<i32>::max(), ZigZagDecode32(std::numeric_limits<ui32>::max() - 1));  -    EXPECT_EQ(std::numeric_limits<i32>::min(), ZigZagDecode32(std::numeric_limits<ui32>::max()));  -}  -  -TEST(TZigZagTest, Encode64)  -{  -    EXPECT_EQ(0ull, ZigZagEncode64( 0));  -    EXPECT_EQ(1ull, ZigZagEncode64(-1));  -    EXPECT_EQ(2ull, ZigZagEncode64( 1));  -    EXPECT_EQ(3ull, ZigZagEncode64(-2));  -    // ...  -    EXPECT_EQ(std::numeric_limits<ui64>::max() - 1, ZigZagEncode64(std::numeric_limits<i64>::max()));  -    EXPECT_EQ(std::numeric_limits<ui64>::max(),     ZigZagEncode64(std::numeric_limits<i64>::min()));  -}  -  -TEST(TZigZagTest, Decode64)  -{  -    EXPECT_EQ(ZigZagDecode64(0),  0ll);  -    EXPECT_EQ(ZigZagDecode64(1), -1ll);  -    EXPECT_EQ(ZigZagDecode64(2),  1ll);  -    EXPECT_EQ(ZigZagDecode64(3), -2ll);  -    // ...  -    EXPECT_EQ(std::numeric_limits<i64>::max(), ZigZagDecode64(std::numeric_limits<ui64>::max() - 1));  -    EXPECT_EQ(std::numeric_limits<i64>::min(), ZigZagDecode64(std::numeric_limits<ui64>::max()));  -}  -  -////////////////////////////////////////////////////////////////////////////////  -  -} // namespace  -} // namespace NYT  +#include <library/cpp/testing/gtest/gtest.h> + +#include <library/cpp/yt/coding/zig_zag.h> + +namespace NYT { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TZigZagTest, Encode32) +{ +    EXPECT_EQ(0u, ZigZagEncode32( 0)); +    EXPECT_EQ(1u, ZigZagEncode32(-1)); +    EXPECT_EQ(2u, ZigZagEncode32( 1)); +    EXPECT_EQ(3u, ZigZagEncode32(-2)); +    // ... +    EXPECT_EQ(std::numeric_limits<ui32>::max() - 1, ZigZagEncode32(std::numeric_limits<i32>::max())); +    EXPECT_EQ(std::numeric_limits<ui32>::max(),     ZigZagEncode32(std::numeric_limits<i32>::min())); +} + +TEST(TZigZagTest, Decode32) +{ +    EXPECT_EQ( 0, ZigZagDecode32(0)); +    EXPECT_EQ(-1, ZigZagDecode32(1)); +    EXPECT_EQ( 1, ZigZagDecode32(2)); +    EXPECT_EQ(-2, ZigZagDecode32(3)); +    // ... +    EXPECT_EQ(std::numeric_limits<i32>::max(), ZigZagDecode32(std::numeric_limits<ui32>::max() - 1)); +    EXPECT_EQ(std::numeric_limits<i32>::min(), ZigZagDecode32(std::numeric_limits<ui32>::max())); +} + +TEST(TZigZagTest, Encode64) +{ +    EXPECT_EQ(0ull, ZigZagEncode64( 0)); +    EXPECT_EQ(1ull, ZigZagEncode64(-1)); +    EXPECT_EQ(2ull, ZigZagEncode64( 1)); +    EXPECT_EQ(3ull, ZigZagEncode64(-2)); +    // ... +    EXPECT_EQ(std::numeric_limits<ui64>::max() - 1, ZigZagEncode64(std::numeric_limits<i64>::max())); +    EXPECT_EQ(std::numeric_limits<ui64>::max(),     ZigZagEncode64(std::numeric_limits<i64>::min())); +} + +TEST(TZigZagTest, Decode64) +{ +    EXPECT_EQ(ZigZagDecode64(0),  0ll); +    EXPECT_EQ(ZigZagDecode64(1), -1ll); +    EXPECT_EQ(ZigZagDecode64(2),  1ll); +    EXPECT_EQ(ZigZagDecode64(3), -2ll); +    // ... +    EXPECT_EQ(std::numeric_limits<i64>::max(), ZigZagDecode64(std::numeric_limits<ui64>::max() - 1)); +    EXPECT_EQ(std::numeric_limits<i64>::min(), ZigZagDecode64(std::numeric_limits<ui64>::max())); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT  | 
