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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/weak_auto_reset.h"
#include <memory>
#include "base/memory/weak_ptr.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace {
template <class T>
class HasWeakFactory {
public:
HasWeakFactory() = default;
~HasWeakFactory() = default;
// Returns a WeakAutoReset that temporarily sets value_ to `value`.
auto SetValueScoped(T value) {
return WeakAutoReset(factory_.GetWeakPtr(), &HasWeakFactory::value_,
std::move(value));
}
void set_value(T value) { value_ = std::move(value); }
const T& value() const { return value_; }
WeakPtr<HasWeakFactory> GetWeakPtr() { return factory_.GetWeakPtr(); }
private:
T value_ = T();
WeakPtrFactory<HasWeakFactory> factory_{this};
};
} // namespace
TEST(WeakAutoResetTest, DefaultConstructor) {
WeakAutoReset<HasWeakFactory<int>, int> empty;
}
TEST(WeakAutoResetTest, SingleAutoReset) {
HasWeakFactory<int> hwf;
{
WeakAutoReset reset = hwf.SetValueScoped(1);
EXPECT_EQ(1, hwf.value());
}
EXPECT_EQ(0, hwf.value());
}
TEST(WeakAutoResetTest, SingleAutoResetObjectDestroyed) {
auto hwf = std::make_unique<HasWeakFactory<int>>();
WeakAutoReset reset = hwf->SetValueScoped(1);
EXPECT_EQ(1, hwf->value());
hwf.reset();
// ASAN will crash here if we don't correctly detect that hwf has gone away.
}
TEST(WeakAutoResetTest, MultipleNested) {
HasWeakFactory<int> hwf;
{
WeakAutoReset reset = hwf.SetValueScoped(1);
EXPECT_EQ(1, hwf.value());
{
WeakAutoReset reset2 = hwf.SetValueScoped(2);
EXPECT_EQ(2, hwf.value());
}
EXPECT_EQ(1, hwf.value());
}
EXPECT_EQ(0, hwf.value());
}
TEST(WeakAutoResetTest, MultipleNestedObjectDestroyed) {
auto hwf = std::make_unique<HasWeakFactory<int>>();
WeakAutoReset reset = hwf->SetValueScoped(1);
EXPECT_EQ(1, hwf->value());
WeakAutoReset reset2 = hwf->SetValueScoped(2);
EXPECT_EQ(2, hwf->value());
hwf.reset();
// ASAN will crash here if we don't correctly detect that hwf has gone away.
}
TEST(WeakAutoResetTest, MoveAssignmentTransfersOwnership) {
HasWeakFactory<int> hwf;
// Create an auto-reset outside of a scope.
WeakAutoReset reset = hwf.SetValueScoped(1);
{
WeakAutoReset<HasWeakFactory<int>, int> reset2;
EXPECT_EQ(1, hwf.value());
// Move the auto-reset to an instance inside the scope. This should not
// cause the value to reset.
reset2 = std::move(reset);
EXPECT_EQ(1, hwf.value());
}
// Because the active auto-reset went away with the scope, the original value
// should be restored.
EXPECT_EQ(0, hwf.value());
}
TEST(WeakAutoResetTest, MoveAssignmentResetsOldValue) {
HasWeakFactory<int> hwf1;
HasWeakFactory<int> hwf2;
WeakAutoReset reset = hwf1.SetValueScoped(1);
WeakAutoReset reset2 = hwf2.SetValueScoped(2);
EXPECT_EQ(1, hwf1.value());
EXPECT_EQ(2, hwf2.value());
// Overwriting the first with the second should reset the first value, but not
// the second.
reset = std::move(reset2);
EXPECT_EQ(0, hwf1.value());
EXPECT_EQ(2, hwf2.value());
// Overwriting the moved value with a default value should have no effect.
reset2 = WeakAutoReset<HasWeakFactory<int>, int>();
// Overwriting the live auto-reset with a default value should reset the other
// value.
reset = WeakAutoReset<HasWeakFactory<int>, int>();
EXPECT_EQ(0, hwf1.value());
EXPECT_EQ(0, hwf2.value());
}
TEST(WeakAutoResetTest, MoveAssignmentToSelfIsNoOp) {
HasWeakFactory<int> hwf;
{
WeakAutoReset reset = hwf.SetValueScoped(1);
EXPECT_EQ(1, hwf.value());
// Move the auto-reset to itself. This should have no effect. We'll need to
// create an intermediate so that we don't get a compile error.
auto* const reset_ref = &reset;
reset = std::move(*reset_ref);
EXPECT_EQ(1, hwf.value());
}
// The auto-reset goes out of scope, resetting the value.
EXPECT_EQ(0, hwf.value());
}
TEST(WeakAutoResetTest, DeleteTargetObjectAfterMoveIsSafe) {
auto hwf = std::make_unique<HasWeakFactory<int>>();
WeakAutoReset reset = hwf->SetValueScoped(1);
WeakAutoReset reset2 = std::move(reset);
hwf.reset();
// ASAN will crash here if we don't correctly detect that hwf has gone away.
}
using HasWeakFactoryPointer = std::unique_ptr<HasWeakFactory<int>>;
TEST(WeakAutoResetTest, TestSafelyMovesValue) {
// We'll use an object that owns another object while keeping a weak reference
// to the inner object to determine its lifetime.
auto inner = std::make_unique<HasWeakFactory<int>>();
auto weak_ptr = inner->GetWeakPtr();
auto outer = std::make_unique<HasWeakFactory<HasWeakFactoryPointer>>();
outer->set_value(std::move(inner));
ASSERT_TRUE(weak_ptr);
{
// Transfer ownership of the inner object to the auto-reset.
WeakAutoReset reset = outer->SetValueScoped(HasWeakFactoryPointer());
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
}
// Transfer ownership back to the outer object.
EXPECT_TRUE(weak_ptr);
EXPECT_TRUE(outer->value());
// Destroying the outer object destroys the inner object.
outer.reset();
EXPECT_FALSE(weak_ptr);
}
TEST(WeakAutoResetTest, TestSafelyMovesValueAndThenDestroysIt) {
// We'll use an object that owns another object while keeping a weak reference
// to the inner object to determine its lifetime.
auto inner = std::make_unique<HasWeakFactory<int>>();
auto weak_ptr = inner->GetWeakPtr();
auto outer = std::make_unique<HasWeakFactory<HasWeakFactoryPointer>>();
outer->set_value(std::move(inner));
ASSERT_TRUE(weak_ptr);
{
// Transfer ownership of the inner object to the auto-reset.
WeakAutoReset reset = outer->SetValueScoped(HasWeakFactoryPointer());
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
// Destroy the outer object. The auto-reset still owns the old inner object.
outer.reset();
EXPECT_TRUE(weak_ptr);
}
// Onwership can't be transferred back so the inner object is destroyed.
EXPECT_FALSE(weak_ptr);
}
TEST(WeakAutoResetTest, TestMoveConstructorMovesOldValue) {
// We'll use an object that owns another object while keeping a weak reference
// to the inner object to determine its lifetime.
auto inner = std::make_unique<HasWeakFactory<int>>();
auto weak_ptr = inner->GetWeakPtr();
auto outer = std::make_unique<HasWeakFactory<HasWeakFactoryPointer>>();
outer->set_value(std::move(inner));
ASSERT_TRUE(weak_ptr);
{
// Transfer ownership of the inner object to the auto-reset.
WeakAutoReset reset = outer->SetValueScoped(HasWeakFactoryPointer());
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
{
// Move ownership of the old object to a new auto-reset.
WeakAutoReset reset2(std::move(reset));
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
}
// Destroying the second auto-reset transfers ownership back to the outer
// object.
EXPECT_TRUE(weak_ptr);
EXPECT_TRUE(outer->value());
}
}
TEST(WeakAutoResetTest, TestMoveAssignmentMovesOldValue) {
// We'll use an object that owns another object while keeping a weak reference
// to the inner object to determine its lifetime.
auto inner = std::make_unique<HasWeakFactory<int>>();
auto weak_ptr = inner->GetWeakPtr();
auto outer = std::make_unique<HasWeakFactory<HasWeakFactoryPointer>>();
outer->set_value(std::move(inner));
ASSERT_TRUE(weak_ptr);
{
// Create an auto-reset that will receive ownership later.
WeakAutoReset<HasWeakFactory<HasWeakFactoryPointer>, HasWeakFactoryPointer>
reset;
{
// Move ownership of the inner object to an auto-reset.
WeakAutoReset reset2 = outer->SetValueScoped(HasWeakFactoryPointer());
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
// Transfer ownership to the other auto-reset.
reset = std::move(reset2);
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
}
// The auto-reset that initially received the value is gone, but the one
// actually holding the value is still in scope.
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(outer->value());
}
// Now both have gone out of scope, so the inner object should be returned to
// the outer one.
EXPECT_TRUE(weak_ptr);
EXPECT_TRUE(outer->value());
}
TEST(WeakAutoResetTest, TestOldAndNewValuesAreSwapped) {
// We'll use an object that owns another object while keeping a weak reference
// to the inner object to determine its lifetime.
auto inner = std::make_unique<HasWeakFactory<int>>();
auto weak_ptr = inner->GetWeakPtr();
auto outer = std::make_unique<HasWeakFactory<HasWeakFactoryPointer>>();
outer->set_value(std::move(inner));
ASSERT_TRUE(weak_ptr);
// Create a second inner object that we'll swap with the first.
auto replacement = std::make_unique<HasWeakFactory<int>>();
auto weak_ptr2 = replacement->GetWeakPtr();
{
// Swap the values.
WeakAutoReset reset = outer->SetValueScoped(std::move(replacement));
EXPECT_TRUE(weak_ptr);
EXPECT_TRUE(weak_ptr2);
EXPECT_EQ(weak_ptr2.get(), outer->value().get());
}
// Unswap the values. The replacement is discarded.
EXPECT_TRUE(weak_ptr);
EXPECT_FALSE(weak_ptr2);
EXPECT_EQ(weak_ptr.get(), outer->value().get());
}
} // namespace base
|