-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathOptionalMemorySafetyTest.cpp
333 lines (289 loc) · 12.7 KB
/
OptionalMemorySafetyTest.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/Api.h>
#include <aws/crt/Optional.h>
#include <aws/testing/aws_test_harness.h>
const char *s_test_str = "This is a string, that should be long enough to avoid small string optimizations";
static int s_OptionalCopySafety(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Optional<Aws::Crt::String> str1(s_test_str);
Aws::Crt::Optional<Aws::Crt::String> strCpyAssigned = str1;
Aws::Crt::Optional<Aws::Crt::String> strCpyConstructedOptional(strCpyAssigned);
Aws::Crt::Optional<Aws::Crt::String> strCpyConstructedValue(*strCpyAssigned);
// now force data access just to check there's not a segfault hiding somewhere.
ASSERT_STR_EQUALS(s_test_str, str1->c_str());
ASSERT_STR_EQUALS(s_test_str, strCpyAssigned->c_str());
ASSERT_STR_EQUALS(s_test_str, strCpyConstructedOptional->c_str());
ASSERT_STR_EQUALS(s_test_str, strCpyConstructedValue->c_str());
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(OptionalCopySafety, s_OptionalCopySafety)
static int s_OptionalMoveSafety(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Optional<Aws::Crt::String> str1(s_test_str);
Aws::Crt::Optional<Aws::Crt::String> strMoveAssigned = std::move(str1);
ASSERT_STR_EQUALS(s_test_str, strMoveAssigned->c_str());
Aws::Crt::Optional<Aws::Crt::String> strMoveValueAssigned = std::move(*strMoveAssigned);
ASSERT_STR_EQUALS(s_test_str, strMoveValueAssigned->c_str());
Aws::Crt::Optional<Aws::Crt::String> strMoveConstructed(std::move(strMoveValueAssigned));
ASSERT_STR_EQUALS(s_test_str, strMoveConstructed->c_str());
Aws::Crt::Optional<Aws::Crt::String> strMoveValueConstructed(std::move(*strMoveConstructed));
ASSERT_STR_EQUALS(s_test_str, strMoveValueConstructed->c_str());
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(OptionalMoveSafety, s_OptionalMoveSafety)
class EmplaceTester
{
public:
int a;
static size_t ctorCallCount;
static size_t dtorCallCount;
EmplaceTester(int val) : a(val) { ctorCallCount += 1; }
~EmplaceTester()
{
a = -1337;
dtorCallCount += 1;
}
EmplaceTester(const EmplaceTester &) = delete;
EmplaceTester(EmplaceTester &&) = delete;
};
size_t EmplaceTester::ctorCallCount = 0;
size_t EmplaceTester::dtorCallCount = 0;
static int s_OptionalEmplace(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
Aws::Crt::Optional<Aws::Crt::String> str1{Aws::Crt::InPlace, s_test_str};
ASSERT_STR_EQUALS(s_test_str, str1->c_str());
ASSERT_INT_EQUALS(0, EmplaceTester::ctorCallCount);
ASSERT_INT_EQUALS(0, EmplaceTester::dtorCallCount);
// Aws::Crt::Optional<MyTestClass> opt1(MyTestClass(5)); // error: call to deleted constructor of 'MyTestClass'
Aws::Crt::Optional<EmplaceTester> opt1{Aws::Crt::InPlace, 5}; // but this is allowed
ASSERT_INT_EQUALS(5, opt1->a);
ASSERT_INT_EQUALS(1, EmplaceTester::ctorCallCount);
ASSERT_INT_EQUALS(0, EmplaceTester::dtorCallCount);
opt1.emplace(100);
ASSERT_INT_EQUALS(100, opt1->a);
// If optional already contains a value before the call, the contained value is destroyed.
ASSERT_INT_EQUALS(2, EmplaceTester::ctorCallCount);
ASSERT_INT_EQUALS(1, EmplaceTester::dtorCallCount);
}
ASSERT_INT_EQUALS(2, EmplaceTester::dtorCallCount);
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(OptionalEmplace, s_OptionalEmplace)
class CopyMoveTester
{
public:
struct Initer
{
};
CopyMoveTester() = default;
explicit CopyMoveTester(const Initer &) : m_initer_copied(true) {}
explicit CopyMoveTester(Initer &&) : m_initer_moved(true) {}
CopyMoveTester(const CopyMoveTester &) : m_copied(true), m_moved(false) {}
CopyMoveTester(CopyMoveTester &&) : m_copied(false), m_moved(true) {}
CopyMoveTester &operator=(const CopyMoveTester &)
{
m_copied = true;
m_moved = false;
m_initer_copied = false;
m_initer_moved = false;
return *this;
}
CopyMoveTester &operator=(CopyMoveTester &&)
{
m_copied = false;
m_moved = true;
m_initer_copied = false;
m_initer_moved = false;
return *this;
}
CopyMoveTester &operator=(const Initer &)
{
m_copied = false;
m_moved = false;
m_initer_copied = true;
m_initer_moved = false;
return *this;
}
CopyMoveTester &operator=(Initer &&)
{
m_copied = false;
m_moved = false;
m_initer_copied = false;
m_initer_moved = true;
return *this;
}
~CopyMoveTester() {}
bool m_copied = false;
bool m_moved = false;
bool m_initer_copied = false;
bool m_initer_moved = false;
};
static int s_OptionalCopyAndMoveSemantics(struct aws_allocator *allocator, void *ctx)
{
(void)ctx;
{
Aws::Crt::ApiHandle apiHandle(allocator);
CopyMoveTester initialItem;
ASSERT_FALSE(initialItem.m_copied);
ASSERT_FALSE(initialItem.m_moved);
{
// Optional(const U&), where U == T
Aws::Crt::Optional<CopyMoveTester> copyConstructedValue(initialItem);
ASSERT_TRUE(copyConstructedValue->m_copied);
ASSERT_FALSE(copyConstructedValue->m_moved);
// Optional(const Optional<U>&), where U == T
Aws::Crt::Optional<CopyMoveTester> copyConstructedOptional(copyConstructedValue);
ASSERT_TRUE(copyConstructedOptional->m_copied);
ASSERT_FALSE(copyConstructedOptional->m_moved);
}
{
// operator=(const U&), where U == T
Aws::Crt::Optional<CopyMoveTester> copyAssignedValue;
// Assignment to empty Optional.
copyAssignedValue = initialItem;
ASSERT_TRUE(copyAssignedValue->m_copied);
ASSERT_FALSE(copyAssignedValue->m_moved);
// Assignment to non-empty Optional.
copyAssignedValue = initialItem;
ASSERT_TRUE(copyAssignedValue->m_copied);
ASSERT_FALSE(copyAssignedValue->m_moved);
}
{
// operator=(const U&), where U != T
Aws::Crt::Optional<CopyMoveTester> copyAssignedOtherValue;
CopyMoveTester::Initer copyIniter;
// Assignment to empty Optional.
copyAssignedOtherValue = copyIniter;
ASSERT_FALSE(copyAssignedOtherValue->m_copied);
ASSERT_FALSE(copyAssignedOtherValue->m_moved);
ASSERT_TRUE(copyAssignedOtherValue->m_initer_copied);
ASSERT_FALSE(copyAssignedOtherValue->m_initer_moved);
// Assignment to non-empty Optional.
copyAssignedOtherValue = copyIniter;
ASSERT_FALSE(copyAssignedOtherValue->m_copied);
ASSERT_FALSE(copyAssignedOtherValue->m_moved);
ASSERT_TRUE(copyAssignedOtherValue->m_initer_copied);
ASSERT_FALSE(copyAssignedOtherValue->m_initer_moved);
}
{
// operator=(const Optional<U>&), where U == T
Aws::Crt::Optional<CopyMoveTester> copyAssignedOptional;
Aws::Crt::Optional<CopyMoveTester> tester = CopyMoveTester();
// Assignment to empty Optional.
copyAssignedOptional = tester;
ASSERT_TRUE(copyAssignedOptional->m_copied);
ASSERT_FALSE(copyAssignedOptional->m_moved);
// Assignment to non-empty Optional.
copyAssignedOptional = tester;
ASSERT_TRUE(copyAssignedOptional->m_copied);
ASSERT_FALSE(copyAssignedOptional->m_moved);
}
{
// operator=(const Optional<U>&), where U != T
Aws::Crt::Optional<CopyMoveTester> copyAssignedOtherOptional;
Aws::Crt::Optional<CopyMoveTester::Initer> copyIniterOptional = CopyMoveTester::Initer();
// Assignment to empty Optional.
copyAssignedOtherOptional = copyIniterOptional;
ASSERT_FALSE(copyAssignedOtherOptional->m_copied);
ASSERT_FALSE(copyAssignedOtherOptional->m_moved);
ASSERT_TRUE(copyAssignedOtherOptional->m_initer_copied);
ASSERT_FALSE(copyAssignedOtherOptional->m_initer_moved);
// Assignment to non-empty Optional.
copyAssignedOtherOptional = copyIniterOptional;
ASSERT_FALSE(copyAssignedOtherOptional->m_copied);
ASSERT_FALSE(copyAssignedOtherOptional->m_moved);
ASSERT_TRUE(copyAssignedOtherOptional->m_initer_copied);
ASSERT_FALSE(copyAssignedOtherOptional->m_initer_moved);
}
{
// Optional(U&&), where U == T
Aws::Crt::Optional<CopyMoveTester> moveConstructedValue(std::move(initialItem));
ASSERT_FALSE(moveConstructedValue->m_copied);
ASSERT_TRUE(moveConstructedValue->m_moved);
// Optional(Optional<U>&&), where U == T
Aws::Crt::Optional<CopyMoveTester> moveConstructedOptional(std::move(moveConstructedValue));
ASSERT_FALSE(moveConstructedOptional->m_copied);
ASSERT_TRUE(moveConstructedOptional->m_moved);
}
{
// operator=(U&&), where U == T
Aws::Crt::Optional<CopyMoveTester> moveAssignedValue;
CopyMoveTester tester;
// Assignment to empty Optional.
moveAssignedValue = std::move(tester);
ASSERT_FALSE(moveAssignedValue->m_copied);
ASSERT_TRUE(moveAssignedValue->m_moved);
// Assignment to non-empty Optional.
tester = CopyMoveTester();
moveAssignedValue = std::move(tester);
ASSERT_FALSE(moveAssignedValue->m_copied);
ASSERT_TRUE(moveAssignedValue->m_moved);
}
{
// operator=(U&&), where U != T
Aws::Crt::Optional<CopyMoveTester> moveAssignedOtherValue;
CopyMoveTester::Initer moveIniter;
// Assignment to empty Optional.
moveAssignedOtherValue = std::move(moveIniter);
ASSERT_FALSE(moveAssignedOtherValue->m_copied);
ASSERT_FALSE(moveAssignedOtherValue->m_moved);
ASSERT_FALSE(moveAssignedOtherValue->m_initer_copied);
ASSERT_TRUE(moveAssignedOtherValue->m_initer_moved);
// Assignment to non-empty Optional.
moveIniter = CopyMoveTester::Initer();
moveAssignedOtherValue = std::move(moveIniter);
ASSERT_FALSE(moveAssignedOtherValue->m_copied);
ASSERT_FALSE(moveAssignedOtherValue->m_moved);
ASSERT_FALSE(moveAssignedOtherValue->m_initer_copied);
ASSERT_TRUE(moveAssignedOtherValue->m_initer_moved);
}
{
// operator=(Optional<U>&&), where U == T
Aws::Crt::Optional<CopyMoveTester> moveAssignedOptional;
Aws::Crt::Optional<CopyMoveTester> tester = CopyMoveTester();
// Assignment to empty Optional.
moveAssignedOptional = std::move(tester);
ASSERT_FALSE(moveAssignedOptional->m_copied);
ASSERT_TRUE(moveAssignedOptional->m_moved);
// Assignment to non-empty Optional.
tester = CopyMoveTester();
moveAssignedOptional = std::move(tester);
ASSERT_FALSE(moveAssignedOptional->m_copied);
ASSERT_TRUE(moveAssignedOptional->m_moved);
}
{
// operator=(Optional<U>&&), where U != T
Aws::Crt::Optional<CopyMoveTester> moveAssignedOtherOptional;
Aws::Crt::Optional<CopyMoveTester::Initer> moveIniterOptional = CopyMoveTester::Initer();
// Assignment to empty Optional.
moveAssignedOtherOptional = std::move(moveIniterOptional);
ASSERT_FALSE(moveAssignedOtherOptional->m_copied);
ASSERT_FALSE(moveAssignedOtherOptional->m_moved);
ASSERT_FALSE(moveAssignedOtherOptional->m_initer_copied);
ASSERT_TRUE(moveAssignedOtherOptional->m_initer_moved);
// Assignment to non-empty Optional.
moveIniterOptional = CopyMoveTester::Initer();
moveAssignedOtherOptional = std::move(moveIniterOptional);
ASSERT_FALSE(moveAssignedOtherOptional->m_copied);
ASSERT_FALSE(moveAssignedOtherOptional->m_moved);
ASSERT_FALSE(moveAssignedOtherOptional->m_initer_copied);
ASSERT_TRUE(moveAssignedOtherOptional->m_initer_moved);
}
}
return AWS_OP_SUCCESS;
}
AWS_TEST_CASE(OptionalCopyAndMoveSemantics, s_OptionalCopyAndMoveSemantics)