-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy_on_write_test.cc
122 lines (104 loc) · 3.73 KB
/
copy_on_write_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "copy_on_write.h"
#include <memory>
#include <string>
#include <type_traits>
#include "absl/strings/string_view.h"
#include "absl/utility/utility.h"
#include "gtest/gtest.h"
namespace refptr {
namespace {
static_assert(
std::is_same<
std::string,
std::pointer_traits<CopyOnWrite<std::string>>::element_type>::value,
"The element_type of CopyOnWrite is incorrect");
constexpr absl::string_view kText = "Lorem ipsum dolor sit amet";
TEST(CopyOnWriteTest, DefaultConstructs) {
CopyOnWrite<std::string> cow;
EXPECT_TRUE(cow->empty()); // Test operator->.
EXPECT_TRUE(cow.LazyDefault());
EXPECT_EQ(*cow, "");
cow.AsMutable() = std::string(kText);
EXPECT_FALSE(cow->empty()); // Test operator->.
EXPECT_FALSE(cow.LazyDefault());
EXPECT_EQ(*cow, kText);
cow = {};
EXPECT_TRUE(cow.LazyDefault());
EXPECT_EQ(*cow, "");
}
TEST(CopyOnWriteTest, ConstructsInPlace) {
CopyOnWrite<std::string> cow(absl::in_place, kText);
EXPECT_EQ(*cow, kText);
EXPECT_FALSE(cow->empty()); // Test operator->.
EXPECT_EQ(cow.AsMutable(), kText);
}
TEST(CopyOnWriteTest, Moves) {
CopyOnWrite<std::string> original(absl::in_place, kText);
CopyOnWrite<std::string>& ref_original = original;
CopyOnWrite<std::string> cow = std::move(original);
EXPECT_EQ(*cow, kText);
EXPECT_EQ(cow.AsMutable(), kText);
EXPECT_TRUE(ref_original.LazyDefault())
<< "A moved-out instance should be empty again";
}
TEST(CopyOnWriteTest, CopiesByWithMutation) {
CopyOnWrite<std::string> original(absl::in_place, kText);
CopyOnWrite<std::string> copy =
original.With([](std::string& s) { s = "other"; });
// Original.
EXPECT_EQ(*original, kText);
EXPECT_EQ(original.AsMutable(), kText);
// Copy.
EXPECT_EQ(*copy, "other");
EXPECT_EQ(copy.AsMutable(), "other");
}
// An example of a data message object that exposes the data it manages using a
// protobuf-like interface.
class Message {
public:
Message() = default;
Message(const Message&) = default;
Message(Message&&) = default;
Message& operator=(const Message&) = default;
Message& operator=(Message&&) = default;
absl::string_view value() const { return *value_; }
std::string& mutable_value() { return value_.AsMutable(); }
bool has_value() const { return !value_.LazyDefault(); }
void clear_value() { value_ = {}; }
const Message& nested() const { return *nested_; }
Message& mutable_nested() { return nested_.AsMutable(); }
bool has_nested() const { return !nested_.LazyDefault(); }
void clear_nested() { nested_ = {}; }
private:
CopyOnWrite<std::string> value_;
CopyOnWrite<Message> nested_;
};
TEST(CopyOnWriteTest, MessagesExampleWorks) {
Message message;
EXPECT_FALSE(message.has_value());
message.mutable_value() = "foo";
EXPECT_TRUE(message.has_value());
EXPECT_EQ(message.value(), "foo");
EXPECT_FALSE(message.has_nested());
message.mutable_nested().mutable_value() = "bar";
EXPECT_TRUE(message.has_nested());
EXPECT_EQ(message.nested().value(), "bar");
EXPECT_FALSE(message.nested().has_nested());
EXPECT_EQ(message.nested().nested().value(), "");
EXPECT_FALSE(message.nested().nested().has_value());
}
} // namespace
} // namespace refptr