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
155
156
157
158
159
160
161
162
163
164
|
// Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
// This source code is licensed under both the GPLv2 (found in the
// COPYING file in the root directory) and Apache 2.0 License
// (found in the LICENSE.Apache file in the root directory).
#include "test_util/testharness.h"
#include "utilities/cassandra/serialize.h"
namespace ROCKSDB_NAMESPACE {
namespace cassandra {
TEST(SerializeTest, SerializeI64) {
std::string dest;
Serialize<int64_t>(0, &dest);
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x00'}),
dest);
dest.clear();
Serialize<int64_t>(1, &dest);
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x01'}),
dest);
dest.clear();
Serialize<int64_t>(-1, &dest);
EXPECT_EQ(std::string({'\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff',
'\xff'}),
dest);
dest.clear();
Serialize<int64_t>(9223372036854775807, &dest);
EXPECT_EQ(std::string({'\x7f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff',
'\xff'}),
dest);
dest.clear();
Serialize<int64_t>(-9223372036854775807, &dest);
EXPECT_EQ(std::string({'\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
'\x01'}),
dest);
}
TEST(SerializeTest, DeserializeI64) {
std::string dest;
std::size_t offset = dest.size();
Serialize<int64_t>(0, &dest);
EXPECT_EQ(0, Deserialize<int64_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int64_t>(1, &dest);
EXPECT_EQ(1, Deserialize<int64_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int64_t>(-1, &dest);
EXPECT_EQ(-1, Deserialize<int64_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int64_t>(-9223372036854775807, &dest);
EXPECT_EQ(-9223372036854775807, Deserialize<int64_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int64_t>(9223372036854775807, &dest);
EXPECT_EQ(9223372036854775807, Deserialize<int64_t>(dest.c_str(), offset));
}
TEST(SerializeTest, SerializeI32) {
std::string dest;
Serialize<int32_t>(0, &dest);
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x00'}), dest);
dest.clear();
Serialize<int32_t>(1, &dest);
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x01'}), dest);
dest.clear();
Serialize<int32_t>(-1, &dest);
EXPECT_EQ(std::string({'\xff', '\xff', '\xff', '\xff'}), dest);
dest.clear();
Serialize<int32_t>(2147483647, &dest);
EXPECT_EQ(std::string({'\x7f', '\xff', '\xff', '\xff'}), dest);
dest.clear();
Serialize<int32_t>(-2147483648LL, &dest);
EXPECT_EQ(std::string({'\x80', '\x00', '\x00', '\x00'}), dest);
}
TEST(SerializeTest, DeserializeI32) {
std::string dest;
std::size_t offset = dest.size();
Serialize<int32_t>(0, &dest);
EXPECT_EQ(0, Deserialize<int32_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int32_t>(1, &dest);
EXPECT_EQ(1, Deserialize<int32_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int32_t>(-1, &dest);
EXPECT_EQ(-1, Deserialize<int32_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int32_t>(2147483647, &dest);
EXPECT_EQ(2147483647, Deserialize<int32_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int32_t>(-2147483648LL, &dest);
EXPECT_EQ(-2147483648LL, Deserialize<int32_t>(dest.c_str(), offset));
}
TEST(SerializeTest, SerializeI8) {
std::string dest;
Serialize<int8_t>(0, &dest);
EXPECT_EQ(std::string({'\x00'}), dest);
dest.clear();
Serialize<int8_t>(1, &dest);
EXPECT_EQ(std::string({'\x01'}), dest);
dest.clear();
Serialize<int8_t>(-1, &dest);
EXPECT_EQ(std::string({'\xff'}), dest);
dest.clear();
Serialize<int8_t>(127, &dest);
EXPECT_EQ(std::string({'\x7f'}), dest);
dest.clear();
Serialize<int8_t>(-128, &dest);
EXPECT_EQ(std::string({'\x80'}), dest);
}
TEST(SerializeTest, DeserializeI8) {
std::string dest;
std::size_t offset = dest.size();
Serialize<int8_t>(0, &dest);
EXPECT_EQ(0, Deserialize<int8_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int8_t>(1, &dest);
EXPECT_EQ(1, Deserialize<int8_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int8_t>(-1, &dest);
EXPECT_EQ(-1, Deserialize<int8_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int8_t>(127, &dest);
EXPECT_EQ(127, Deserialize<int8_t>(dest.c_str(), offset));
offset = dest.size();
Serialize<int8_t>(-128, &dest);
EXPECT_EQ(-128, Deserialize<int8_t>(dest.c_str(), offset));
}
} // namespace cassandra
} // namespace ROCKSDB_NAMESPACE
int main(int argc, char** argv) {
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
|