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
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/memory/always_valid_pointer.h"
#include <string>
#include "test/gtest.h"
namespace webrtc {
TEST(AlwaysValidPointerTest, DefaultToEmptyValue) {
AlwaysValidPointer<std::string> ptr(nullptr);
EXPECT_EQ(*ptr, "");
}
TEST(AlwaysValidPointerTest, DefaultWithForwardedArgument) {
AlwaysValidPointer<std::string> ptr(nullptr, "test");
EXPECT_EQ(*ptr, "test");
}
TEST(AlwaysValidPointerTest, DefaultToSubclass) {
struct A {
virtual ~A() {}
virtual int f() = 0;
};
struct B : public A {
int b = 0;
explicit B(int val) : b(val) {}
virtual ~B() {}
int f() override { return b; }
};
AlwaysValidPointer<A, B> ptr(nullptr, 3);
EXPECT_EQ(ptr->f(), 3);
EXPECT_EQ((*ptr).f(), 3);
EXPECT_EQ(ptr.get()->f(), 3);
}
TEST(AlwaysValidPointerTest, NonDefaultValue) {
std::string str("keso");
AlwaysValidPointer<std::string> ptr(&str, "test");
EXPECT_EQ(*ptr, "keso");
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipOfInstance) {
std::string str("keso");
std::unique_ptr<std::string> str2 = std::make_unique<std::string>("kent");
AlwaysValidPointer<std::string> ptr(std::move(str2), &str);
EXPECT_EQ(*ptr, "kent");
EXPECT_EQ(str2, nullptr);
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipFallbackOnPointer) {
std::string str("keso");
std::unique_ptr<std::string> str2;
AlwaysValidPointer<std::string> ptr(std::move(str2), &str);
EXPECT_EQ(*ptr, "keso");
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipFallbackOnDefault) {
std::unique_ptr<std::string> str;
std::string* str_ptr = nullptr;
AlwaysValidPointer<std::string> ptr(std::move(str), str_ptr);
EXPECT_EQ(*ptr, "");
}
TEST(AlwaysValidPointerTest,
TakeOverOwnershipFallbackOnDefaultWithForwardedArgument) {
std::unique_ptr<std::string> str2;
AlwaysValidPointer<std::string> ptr(std::move(str2), nullptr, "keso");
EXPECT_EQ(*ptr, "keso");
}
TEST(AlwaysValidPointerTest, TakeOverOwnershipDoesNotForwardDefaultArguments) {
std::unique_ptr<std::string> str = std::make_unique<std::string>("kalle");
std::unique_ptr<std::string> str2 = std::make_unique<std::string>("anka");
AlwaysValidPointer<std::string> ptr(std::move(str), nullptr, *str2);
EXPECT_EQ(*ptr, "kalle");
EXPECT_TRUE(!str);
EXPECT_EQ(*str2, "anka");
}
TEST(AlwaysValidPointerTest, DefaultToLambda) {
AlwaysValidPointer<std::string> ptr(
nullptr, []() { return std::make_unique<std::string>("onkel skrue"); });
EXPECT_EQ(*ptr, "onkel skrue");
}
TEST(AlwaysValidPointerTest, NoDefaultObjectPassValidPointer) {
std::string str("foo");
AlwaysValidPointerNoDefault<std::string> ptr(&str);
EXPECT_EQ(*ptr, "foo");
EXPECT_EQ(ptr, &str);
}
TEST(AlwaysValidPointerTest, NoDefaultObjectWithTakeOverOwnership) {
std::unique_ptr<std::string> str = std::make_unique<std::string>("yum");
AlwaysValidPointerNoDefault<std::string> ptr(std::move(str));
EXPECT_EQ(*ptr, "yum");
std::unique_ptr<std::string> str2 = std::make_unique<std::string>("fun");
AlwaysValidPointerNoDefault<std::string> ptr2(std::move(str), str2.get());
EXPECT_EQ(*ptr2, "fun");
EXPECT_EQ(ptr2, str2.get());
}
#if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
TEST(AlwaysValidPointerTest, NoDefaultObjectPassNullPointer) {
auto pass_null = []() {
AlwaysValidPointerNoDefault<std::string> ptr(nullptr);
};
EXPECT_DEATH(pass_null(), "");
}
TEST(AlwaysValidPointerTest, NoDefaultObjectPassNullUniquePointer) {
auto pass_null = []() {
std::unique_ptr<std::string> str;
AlwaysValidPointerNoDefault<std::string> ptr(std::move(str));
};
EXPECT_DEATH(pass_null(), "");
}
#endif
} // namespace webrtc
|