]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 "port/port.h" | |
7 | #include "port/stack_trace.h" | |
8 | #include "rocksdb/slice.h" | |
9 | #include "test_util/testharness.h" | |
10 | #include "test_util/testutil.h" | |
11 | ||
12 | namespace ROCKSDB_NAMESPACE { | |
13 | ||
14 | // Use this to keep track of the cleanups that were actually performed | |
15 | void Multiplier(void* arg1, void* arg2) { | |
16 | int* res = reinterpret_cast<int*>(arg1); | |
17 | int* num = reinterpret_cast<int*>(arg2); | |
18 | *res *= *num; | |
19 | } | |
20 | ||
21 | class PinnableSliceTest : public testing::Test { | |
22 | public: | |
23 | void AssertSameData(const std::string& expected, | |
24 | const PinnableSlice& slice) { | |
25 | std::string got; | |
26 | got.assign(slice.data(), slice.size()); | |
27 | ASSERT_EQ(expected, got); | |
28 | } | |
29 | }; | |
30 | ||
31 | // Test that the external buffer is moved instead of being copied. | |
32 | TEST_F(PinnableSliceTest, MoveExternalBuffer) { | |
33 | Slice s("123"); | |
34 | std::string buf; | |
35 | PinnableSlice v1(&buf); | |
36 | v1.PinSelf(s); | |
37 | ||
38 | PinnableSlice v2(std::move(v1)); | |
39 | ASSERT_EQ(buf.data(), v2.data()); | |
40 | ASSERT_EQ(&buf, v2.GetSelf()); | |
41 | ||
42 | PinnableSlice v3; | |
43 | v3 = std::move(v2); | |
44 | ASSERT_EQ(buf.data(), v3.data()); | |
45 | ASSERT_EQ(&buf, v3.GetSelf()); | |
46 | } | |
47 | ||
48 | TEST_F(PinnableSliceTest, Move) { | |
49 | int n2 = 2; | |
50 | int res = 1; | |
51 | const std::string const_str1 = "123"; | |
52 | const std::string const_str2 = "ABC"; | |
53 | Slice slice1(const_str1); | |
54 | Slice slice2(const_str2); | |
55 | ||
56 | { | |
57 | // Test move constructor on a pinned slice. | |
58 | res = 1; | |
59 | PinnableSlice v1; | |
60 | v1.PinSlice(slice1, Multiplier, &res, &n2); | |
61 | PinnableSlice v2(std::move(v1)); | |
62 | ||
63 | // Since v1's Cleanable has been moved to v2, | |
64 | // no cleanup should happen in Reset. | |
65 | v1.Reset(); | |
66 | ASSERT_EQ(1, res); | |
67 | ||
68 | AssertSameData(const_str1, v2); | |
69 | } | |
70 | // v2 is cleaned up. | |
71 | ASSERT_EQ(2, res); | |
72 | ||
73 | { | |
74 | // Test move constructor on an unpinned slice. | |
75 | PinnableSlice v1; | |
76 | v1.PinSelf(slice1); | |
77 | PinnableSlice v2(std::move(v1)); | |
78 | ||
79 | AssertSameData(const_str1, v2); | |
80 | } | |
81 | ||
82 | { | |
83 | // Test move assignment from a pinned slice to | |
84 | // another pinned slice. | |
85 | res = 1; | |
86 | PinnableSlice v1; | |
87 | v1.PinSlice(slice1, Multiplier, &res, &n2); | |
88 | PinnableSlice v2; | |
89 | v2.PinSlice(slice2, Multiplier, &res, &n2); | |
90 | v2 = std::move(v1); | |
91 | ||
92 | // v2's Cleanable will be Reset before moving | |
93 | // anything from v1. | |
94 | ASSERT_EQ(2, res); | |
95 | // Since v1's Cleanable has been moved to v2, | |
96 | // no cleanup should happen in Reset. | |
97 | v1.Reset(); | |
98 | ASSERT_EQ(2, res); | |
99 | ||
100 | AssertSameData(const_str1, v2); | |
101 | } | |
102 | // The Cleanable moved from v1 to v2 will be Reset. | |
103 | ASSERT_EQ(4, res); | |
104 | ||
105 | { | |
106 | // Test move assignment from a pinned slice to | |
107 | // an unpinned slice. | |
108 | res = 1; | |
109 | PinnableSlice v1; | |
110 | v1.PinSlice(slice1, Multiplier, &res, &n2); | |
111 | PinnableSlice v2; | |
112 | v2.PinSelf(slice2); | |
113 | v2 = std::move(v1); | |
114 | ||
115 | // Since v1's Cleanable has been moved to v2, | |
116 | // no cleanup should happen in Reset. | |
117 | v1.Reset(); | |
118 | ASSERT_EQ(1, res); | |
119 | ||
120 | AssertSameData(const_str1, v2); | |
121 | } | |
122 | // The Cleanable moved from v1 to v2 will be Reset. | |
123 | ASSERT_EQ(2, res); | |
124 | ||
125 | { | |
126 | // Test move assignment from an upinned slice to | |
127 | // another unpinned slice. | |
128 | PinnableSlice v1; | |
129 | v1.PinSelf(slice1); | |
130 | PinnableSlice v2; | |
131 | v2.PinSelf(slice2); | |
132 | v2 = std::move(v1); | |
133 | ||
134 | AssertSameData(const_str1, v2); | |
135 | } | |
136 | ||
137 | { | |
138 | // Test move assignment from an upinned slice to | |
139 | // a pinned slice. | |
140 | res = 1; | |
141 | PinnableSlice v1; | |
142 | v1.PinSelf(slice1); | |
143 | PinnableSlice v2; | |
144 | v2.PinSlice(slice2, Multiplier, &res, &n2); | |
145 | v2 = std::move(v1); | |
146 | ||
147 | // v2's Cleanable will be Reset before moving | |
148 | // anything from v1. | |
149 | ASSERT_EQ(2, res); | |
150 | ||
151 | AssertSameData(const_str1, v2); | |
152 | } | |
153 | // No Cleanable is moved from v1 to v2, so no more cleanup. | |
154 | ASSERT_EQ(2, res); | |
155 | } | |
156 | ||
157 | } // namespace ROCKSDB_NAMESPACE | |
158 | ||
159 | int main(int argc, char** argv) { | |
160 | ROCKSDB_NAMESPACE::port::InstallStackTraceHandler(); | |
161 | ::testing::InitGoogleTest(&argc, argv); | |
162 | return RUN_ALL_TESTS(); | |
163 | } |