summaryrefslogtreecommitdiff
path: root/util/hash_test.cc
diff options
context:
space:
mode:
authorPeter Dillinger <peterd@fb.com>2020-10-23 14:08:52 -0700
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>2020-10-23 14:11:15 -0700
commita16d1b2fd36a2fe036b0aed1008f60570c981773 (patch)
tree8e4d327c55d53377e98d71a27e2724450176e662 /util/hash_test.cc
parentb1cdb8cc86f18e7cad7b219c4c496ab967044383 (diff)
Add Encode/DecodeFixedGeneric, coding_lean.h (#7587)
Summary: To minimize dependencies for Ribbon filter code in progress, core part of coding.h for fixed sizes has been moved to coding_lean.h. Also, generic versions of these functions have been added to math128.h (since the generic versions are likely only to be used along with Unsigned128). Pull Request resolved: https://github.com/facebook/rocksdb/pull/7587 Test Plan: Unit tests added for new functions Reviewed By: jay-zhuang Differential Revision: D24486718 Pulled By: pdillinger fbshipit-source-id: a69768f742379689442135fa52237c01dfe2647e
Diffstat (limited to 'util/hash_test.cc')
-rw-r--r--util/hash_test.cc44
1 files changed, 42 insertions, 2 deletions
diff --git a/util/hash_test.cc b/util/hash_test.cc
index da253bdd4..a6530b686 100644
--- a/util/hash_test.cc
+++ b/util/hash_test.cc
@@ -396,7 +396,9 @@ using ROCKSDB_NAMESPACE::BitParity;
using ROCKSDB_NAMESPACE::BitsSetToOne;
using ROCKSDB_NAMESPACE::CountTrailingZeroBits;
using ROCKSDB_NAMESPACE::DecodeFixed128;
+using ROCKSDB_NAMESPACE::DecodeFixedGeneric;
using ROCKSDB_NAMESPACE::EncodeFixed128;
+using ROCKSDB_NAMESPACE::EncodeFixedGeneric;
using ROCKSDB_NAMESPACE::FloorLog2;
using ROCKSDB_NAMESPACE::Lower64of128;
using ROCKSDB_NAMESPACE::Multiply64to128;
@@ -534,9 +536,10 @@ TEST(MathTest, Math128) {
TEST(MathTest, Coding128) {
const char *in = "_1234567890123456";
+ // Note: in + 1 is likely unaligned
Unsigned128 decoded = DecodeFixed128(in + 1);
- EXPECT_EQ(Lower64of128(decoded), 4050765991979987505U);
- EXPECT_EQ(Upper64of128(decoded), 3906085646303834169U);
+ EXPECT_EQ(Lower64of128(decoded), 0x3837363534333231U);
+ EXPECT_EQ(Upper64of128(decoded), 0x3635343332313039U);
char out[18];
out[0] = '_';
EncodeFixed128(out + 1, decoded);
@@ -544,6 +547,43 @@ TEST(MathTest, Coding128) {
EXPECT_EQ(std::string(in), std::string(out));
}
+TEST(MathTest, CodingGeneric) {
+ const char *in = "_1234567890123456";
+ // Decode
+ // Note: in + 1 is likely unaligned
+ Unsigned128 decoded128 = DecodeFixedGeneric<Unsigned128>(in + 1);
+ EXPECT_EQ(Lower64of128(decoded128), 0x3837363534333231U);
+ EXPECT_EQ(Upper64of128(decoded128), 0x3635343332313039U);
+
+ uint64_t decoded64 = DecodeFixedGeneric<uint64_t>(in + 1);
+ EXPECT_EQ(decoded64, 0x3837363534333231U);
+
+ uint32_t decoded32 = DecodeFixedGeneric<uint32_t>(in + 1);
+ EXPECT_EQ(decoded32, 0x34333231U);
+
+ uint16_t decoded16 = DecodeFixedGeneric<uint16_t>(in + 1);
+ EXPECT_EQ(decoded16, 0x3231U);
+
+ // Encode
+ char out[18];
+ out[0] = '_';
+ memset(out + 1, '\0', 17);
+ EncodeFixedGeneric(out + 1, decoded128);
+ EXPECT_EQ(std::string(in), std::string(out));
+
+ memset(out + 1, '\0', 9);
+ EncodeFixedGeneric(out + 1, decoded64);
+ EXPECT_EQ(std::string("_12345678"), std::string(out));
+
+ memset(out + 1, '\0', 5);
+ EncodeFixedGeneric(out + 1, decoded32);
+ EXPECT_EQ(std::string("_1234"), std::string(out));
+
+ memset(out + 1, '\0', 3);
+ EncodeFixedGeneric(out + 1, decoded16);
+ EXPECT_EQ(std::string("_12"), std::string(out));
+}
+
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);