]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/util_merge_operators_test.cc
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / rocksdb / utilities / util_merge_operators_test.cc
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5
6 #include "test_util/testharness.h"
7 #include "test_util/testutil.h"
8 #include "utilities/merge_operators.h"
9
10 namespace ROCKSDB_NAMESPACE {
11
12 class UtilMergeOperatorTest : public testing::Test {
13 public:
14 UtilMergeOperatorTest() {}
15
16 std::string FullMergeV2(std::string existing_value,
17 std::vector<std::string> operands,
18 std::string key = "") {
19 std::string result;
20 Slice result_operand(nullptr, 0);
21
22 Slice existing_value_slice(existing_value);
23 std::vector<Slice> operands_slice(operands.begin(), operands.end());
24
25 const MergeOperator::MergeOperationInput merge_in(
26 key, &existing_value_slice, operands_slice, nullptr);
27 MergeOperator::MergeOperationOutput merge_out(result, result_operand);
28 merge_operator_->FullMergeV2(merge_in, &merge_out);
29
30 if (result_operand.data()) {
31 result.assign(result_operand.data(), result_operand.size());
32 }
33 return result;
34 }
35
36 std::string FullMergeV2(std::vector<std::string> operands,
37 std::string key = "") {
38 std::string result;
39 Slice result_operand(nullptr, 0);
40
41 std::vector<Slice> operands_slice(operands.begin(), operands.end());
42
43 const MergeOperator::MergeOperationInput merge_in(key, nullptr,
44 operands_slice, nullptr);
45 MergeOperator::MergeOperationOutput merge_out(result, result_operand);
46 merge_operator_->FullMergeV2(merge_in, &merge_out);
47
48 if (result_operand.data()) {
49 result.assign(result_operand.data(), result_operand.size());
50 }
51 return result;
52 }
53
54 std::string PartialMerge(std::string left, std::string right,
55 std::string key = "") {
56 std::string result;
57
58 merge_operator_->PartialMerge(key, left, right, &result, nullptr);
59 return result;
60 }
61
62 std::string PartialMergeMulti(std::deque<std::string> operands,
63 std::string key = "") {
64 std::string result;
65 std::deque<Slice> operands_slice(operands.begin(), operands.end());
66
67 merge_operator_->PartialMergeMulti(key, operands_slice, &result, nullptr);
68 return result;
69 }
70
71 protected:
72 std::shared_ptr<MergeOperator> merge_operator_;
73 };
74
75 TEST_F(UtilMergeOperatorTest, MaxMergeOperator) {
76 merge_operator_ = MergeOperators::CreateMaxOperator();
77
78 EXPECT_EQ("B", FullMergeV2("B", {"A"}));
79 EXPECT_EQ("B", FullMergeV2("A", {"B"}));
80 EXPECT_EQ("", FullMergeV2({"", "", ""}));
81 EXPECT_EQ("A", FullMergeV2({"A"}));
82 EXPECT_EQ("ABC", FullMergeV2({"ABC"}));
83 EXPECT_EQ("Z", FullMergeV2({"ABC", "Z", "C", "AXX"}));
84 EXPECT_EQ("ZZZ", FullMergeV2({"ABC", "CC", "Z", "ZZZ"}));
85 EXPECT_EQ("a", FullMergeV2("a", {"ABC", "CC", "Z", "ZZZ"}));
86
87 EXPECT_EQ("z", PartialMergeMulti({"a", "z", "efqfqwgwew", "aaz", "hhhhh"}));
88
89 EXPECT_EQ("b", PartialMerge("a", "b"));
90 EXPECT_EQ("z", PartialMerge("z", "azzz"));
91 EXPECT_EQ("a", PartialMerge("a", ""));
92 }
93
94 } // namespace ROCKSDB_NAMESPACE
95
96 int main(int argc, char** argv) {
97 ::testing::InitGoogleTest(&argc, argv);
98 return RUN_ALL_TESTS();
99 }