2017-06-16 21:12:52 +00:00
|
|
|
// Copyright (c) 2017-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// 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).
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2017-07-21 21:42:32 +00:00
|
|
|
#include "utilities/cassandra/serialize.h"
|
2017-06-16 21:12:52 +00:00
|
|
|
|
2024-03-04 18:08:32 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE::cassandra {
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
TEST(SerializeTest, SerializeI64) {
|
|
|
|
std::string dest;
|
|
|
|
Serialize<int64_t>(0, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
|
|
|
|
'\x00'}),
|
|
|
|
dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int64_t>(1, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
|
|
|
|
'\x01'}),
|
|
|
|
dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int64_t>(-1, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\xff', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff',
|
|
|
|
'\xff'}),
|
|
|
|
dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int64_t>(9223372036854775807, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x7f', '\xff', '\xff', '\xff', '\xff', '\xff', '\xff',
|
|
|
|
'\xff'}),
|
|
|
|
dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int64_t>(-9223372036854775807, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x80', '\x00', '\x00', '\x00', '\x00', '\x00', '\x00',
|
|
|
|
'\x01'}),
|
|
|
|
dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x00'}), dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int32_t>(1, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x00', '\x00', '\x00', '\x01'}), dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int32_t>(-1, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\xff', '\xff', '\xff', '\xff'}), dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int32_t>(2147483647, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x7f', '\xff', '\xff', '\xff'}), dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
dest.clear();
|
|
|
|
Serialize<int32_t>(-2147483648LL, &dest);
|
2022-10-24 23:38:09 +00:00
|
|
|
EXPECT_EQ(std::string({'\x80', '\x00', '\x00', '\x00'}), dest);
|
2017-06-16 21:12:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2024-03-04 18:08:32 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE::cassandra
|
2017-06-16 21:12:52 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2017-06-16 21:12:52 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|