1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#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, std::string>>
{ };
TEST_P(TWriteVarIntTest, Serialization)
{
ui64 value = std::get<0>(GetParam());
std::string rightAnswer = std::get<1>(GetParam());
TStringStream outputStream;
WriteVarUint64(&outputStream, value);
EXPECT_EQ(rightAnswer, outputStream.Str());
}
////////////////////////////////////////////////////////////////////////////////
class TReadVarIntTest: public ::testing::TestWithParam<std::tuple<ui64, std::string> >
{ };
TEST_P(TReadVarIntTest, Serialization)
{
ui64 rightAnswer = std::get<0>(GetParam());
auto input = TString(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, std::string("\x00", 1)),
std::make_tuple(0x1ull, std::string("\x01", 1)),
std::make_tuple(0x2ull, std::string("\x02", 1)),
std::make_tuple(0x3ull, std::string("\x03", 1)),
std::make_tuple(0x4ull, std::string("\x04", 1)),
// The following "magic numbers" are critical points for varint encoding.
std::make_tuple((1ull << 7) - 1, std::string("\x7f", 1)),
std::make_tuple((1ull << 7), std::string("\x80\x01", 2)),
std::make_tuple((1ull << 14) - 1, std::string("\xff\x7f", 2)),
std::make_tuple((1ull << 14), std::string("\x80\x80\x01", 3)),
std::make_tuple((1ull << 21) - 1, std::string("\xff\xff\x7f", 3)),
std::make_tuple((1ull << 21), std::string("\x80\x80\x80\x01", 4)),
std::make_tuple((1ull << 28) - 1, std::string("\xff\xff\xff\x7f", 4)),
std::make_tuple((1ull << 28), std::string("\x80\x80\x80\x80\x01", 5)),
std::make_tuple((1ull << 35) - 1, std::string("\xff\xff\xff\xff\x7f", 5)),
std::make_tuple((1ull << 35), std::string("\x80\x80\x80\x80\x80\x01", 6)),
std::make_tuple((1ull << 42) - 1, std::string("\xff\xff\xff\xff\xff\x7f", 6)),
std::make_tuple((1ull << 42), std::string("\x80\x80\x80\x80\x80\x80\x01", 7)),
std::make_tuple((1ull << 49) - 1, std::string("\xff\xff\xff\xff\xff\xff\x7f", 7)),
std::make_tuple((1ull << 49), std::string("\x80\x80\x80\x80\x80\x80\x80\x01", 8)),
std::make_tuple((1ull << 56) - 1, std::string("\xff\xff\xff\xff\xff\xff\xff\x7f", 8)),
std::make_tuple((1ull << 56), std::string("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9)),
std::make_tuple((1ull << 63) - 1, std::string("\xff\xff\xff\xff\xff\xff\xff\xff\x7f", 9)),
std::make_tuple((1ull << 63), std::string("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10)),
// Boundary case.
std::make_tuple(static_cast<ui64>(-1), std::string("\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(TVarUint32Test, BoundaryValues)
{
// UINT32_MAX encoded as a 5-byte varint: should succeed.
const char maxBytes[] = "\xff\xff\xff\xff\x0f";
ui32 value = 0;
EXPECT_EQ(5, ReadVarUint32(maxBytes, &value));
EXPECT_EQ(std::numeric_limits<ui32>::max(), value);
// 2^32 encoded as a 5-byte varint: should throw.
const char overflowBytes[] = "\x80\x80\x80\x80\x10";
EXPECT_THROW(ReadVarUint32(overflowBytes, &value), TSimpleException);
// Max 5-byte varint (2^35 - 1): should throw.
const char maxFiveByteBytes[] = "\xff\xff\xff\xff\x7f";
EXPECT_THROW(ReadVarUint32(maxFiveByteBytes, &value), TSimpleException);
}
////////////////////////////////////////////////////////////////////////////////
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
|