/Users/deen/code/yugabyte-db/src/yb/util/byte_buffer-test.cc
Line | Count | Source |
1 | | // Copyright (c) YugaByte, Inc. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
4 | | // in compliance with the License. You may obtain a copy of the License at |
5 | | // |
6 | | // http://www.apache.org/licenses/LICENSE-2.0 |
7 | | // |
8 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
9 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
10 | | // or implied. See the License for the specific language governing permissions and limitations |
11 | | // under the License. |
12 | | // |
13 | | #include <string> |
14 | | #include <unordered_map> |
15 | | |
16 | | #include <gtest/gtest.h> |
17 | | |
18 | | #include "yb/util/byte_buffer.h" |
19 | | #include "yb/util/random_util.h" |
20 | | |
21 | | using namespace std::literals; |
22 | | |
23 | | namespace yb { |
24 | | |
25 | | using Buffer = ByteBuffer<8>; |
26 | | |
27 | 1 | TEST(ByteBufferTest, Assign) { |
28 | 1 | std::string str = "source_string"s; |
29 | 1 | Buffer buffer(str); |
30 | 1 | Buffer copy(buffer); |
31 | 1 | ASSERT_EQ(buffer, copy); |
32 | 1 | copy = buffer; |
33 | 1 | Buffer moved(std::move(buffer)); |
34 | 1 | ASSERT_TRUE(buffer.empty()); |
35 | 1 | ASSERT_EQ(moved, copy); |
36 | 1 | moved = std::move(copy); |
37 | 1 | ASSERT_TRUE(copy.empty()); |
38 | 1 | buffer.Assign(str); |
39 | 1 | ASSERT_EQ(buffer, moved); |
40 | 1 | } |
41 | | |
42 | 1 | TEST(ByteBufferTest, Append) { |
43 | 1 | constexpr int kStringLen = 128; |
44 | 1 | constexpr int kIterations = 1000; |
45 | 1 | std::string str = RandomHumanReadableString(kStringLen); |
46 | 1.00k | for (int i = kIterations; i-- > 0;) { |
47 | 1.00k | Buffer buffer; |
48 | 6.74k | while (buffer.size() < str.size()) { |
49 | 5.74k | size_t len = RandomUniformInt<size_t>(0, str.size() - buffer.size()); |
50 | 5.74k | buffer.append(Slice(str.c_str() + buffer.size(), len)); |
51 | 5.74k | ASSERT_EQ(buffer.AsSlice(), Slice(str).Prefix(buffer.size())); |
52 | 5.74k | if (len == 0) { |
53 | 756 | buffer.PushBack(str[buffer.size()]); |
54 | 756 | ASSERT_EQ(buffer.AsSlice(), Slice(str).Prefix(buffer.size())); |
55 | 756 | } |
56 | 5.74k | } |
57 | 1.00k | } |
58 | 1 | } |
59 | | |
60 | | template <class Map> |
61 | 2 | void TestMap() { |
62 | 2 | std::vector<std::pair<std::string, int>> entries = { |
63 | 2 | { "one"s, 1 }, |
64 | 2 | { "two"s, 2 }, |
65 | 2 | { "three"s, 3 }, |
66 | 2 | }; |
67 | | |
68 | 2 | Map m; |
69 | 6 | for (const auto& p : entries) { |
70 | 6 | ASSERT_TRUE(m.emplace(Buffer(p.first), p.second).second); |
71 | 6 | } |
72 | | |
73 | 6 | for (const auto& p : entries) { |
74 | 6 | ASSERT_EQ(m[Buffer(p.first)], p.second); |
75 | 6 | } |
76 | 2 | } _ZN2yb7TestMapINSt3__13mapINS_10ByteBufferILm8EEEiNS1_4lessIS4_EENS1_9allocatorINS1_4pairIKS4_iEEEEEEEEvv Line | Count | Source | 61 | 1 | void TestMap() { | 62 | 1 | std::vector<std::pair<std::string, int>> entries = { | 63 | 1 | { "one"s, 1 }, | 64 | 1 | { "two"s, 2 }, | 65 | 1 | { "three"s, 3 }, | 66 | 1 | }; | 67 | | | 68 | 1 | Map m; | 69 | 3 | for (const auto& p : entries) { | 70 | 3 | ASSERT_TRUE(m.emplace(Buffer(p.first), p.second).second); | 71 | 3 | } | 72 | | | 73 | 3 | for (const auto& p : entries) { | 74 | 3 | ASSERT_EQ(m[Buffer(p.first)], p.second); | 75 | 3 | } | 76 | 1 | } |
_ZN2yb7TestMapINSt3__113unordered_mapINS_10ByteBufferILm8EEEiNS_14ByteBufferHashENS1_8equal_toIS4_EENS1_9allocatorINS1_4pairIKS4_iEEEEEEEEvv Line | Count | Source | 61 | 1 | void TestMap() { | 62 | 1 | std::vector<std::pair<std::string, int>> entries = { | 63 | 1 | { "one"s, 1 }, | 64 | 1 | { "two"s, 2 }, | 65 | 1 | { "three"s, 3 }, | 66 | 1 | }; | 67 | | | 68 | 1 | Map m; | 69 | 3 | for (const auto& p : entries) { | 70 | 3 | ASSERT_TRUE(m.emplace(Buffer(p.first), p.second).second); | 71 | 3 | } | 72 | | | 73 | 3 | for (const auto& p : entries) { | 74 | 3 | ASSERT_EQ(m[Buffer(p.first)], p.second); | 75 | 3 | } | 76 | 1 | } |
|
77 | | |
78 | 1 | TEST(ByteBufferTest, Map) { |
79 | 1 | TestMap<std::map<Buffer, int>>(); |
80 | 1 | TestMap<std::unordered_map<Buffer, int, ByteBufferHash>>(); |
81 | 1 | } |
82 | | |
83 | | } // namespace yb |