blob: 6874889918d0ed3fce4ae561a02be95dd8f069fb [file] [log] [blame]
Jiyong Park8fd64c82019-05-31 03:43:34 +09001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "android-base/result.h"
18
19#include "errno.h"
20
21#include <istream>
22#include <string>
23
24#include <gtest/gtest.h>
25
26using namespace std::string_literals;
27
28namespace android {
29namespace base {
30
31TEST(result, result_accessors) {
32 Result<std::string> result = "success";
33 ASSERT_TRUE(result);
34 ASSERT_TRUE(result.has_value());
35
36 EXPECT_EQ("success", *result);
37 EXPECT_EQ("success", result.value());
38
39 EXPECT_EQ('s', result->data()[0]);
40}
41
42TEST(result, result_accessors_rvalue) {
43 ASSERT_TRUE(Result<std::string>("success"));
44 ASSERT_TRUE(Result<std::string>("success").has_value());
45
46 EXPECT_EQ("success", *Result<std::string>("success"));
47 EXPECT_EQ("success", Result<std::string>("success").value());
48
49 EXPECT_EQ('s', Result<std::string>("success")->data()[0]);
50}
51
52TEST(result, result_success) {
53 Result<Success> result = Success();
54 ASSERT_TRUE(result);
55 ASSERT_TRUE(result.has_value());
56
57 EXPECT_EQ(Success(), *result);
58 EXPECT_EQ(Success(), result.value());
59}
60
61TEST(result, result_success_rvalue) {
62 // Success() doesn't actually create a Result<Success> object, but rather an object that can be
63 // implicitly constructed into a Result<Success> object.
64
65 auto MakeRvalueSuccessResult = []() -> Result<Success> { return Success(); };
66 ASSERT_TRUE(MakeRvalueSuccessResult());
67 ASSERT_TRUE(MakeRvalueSuccessResult().has_value());
68
69 EXPECT_EQ(Success(), *MakeRvalueSuccessResult());
70 EXPECT_EQ(Success(), MakeRvalueSuccessResult().value());
71}
72
73TEST(result, result_error) {
74 Result<Success> result = Error() << "failure" << 1;
75 ASSERT_FALSE(result);
76 ASSERT_FALSE(result.has_value());
77
78 EXPECT_EQ(0, result.error().code());
79 EXPECT_EQ("failure1", result.error().message());
80}
81
82TEST(result, result_error_empty) {
83 Result<Success> result = Error();
84 ASSERT_FALSE(result);
85 ASSERT_FALSE(result.has_value());
86
87 EXPECT_EQ(0, result.error().code());
88 EXPECT_EQ("", result.error().message());
89}
90
91TEST(result, result_error_rvalue) {
92 // Error() and ErrnoError() aren't actually used to create a Result<T> object.
93 // Under the hood, they are an intermediate class that can be implicitly constructed into a
94 // Result<T>. This is needed both to create the ostream and because Error() itself, by
95 // definition will not know what the type, T, of the underlying Result<T> object that it would
96 // create is.
97
98 auto MakeRvalueErrorResult = []() -> Result<Success> { return Error() << "failure" << 1; };
99 ASSERT_FALSE(MakeRvalueErrorResult());
100 ASSERT_FALSE(MakeRvalueErrorResult().has_value());
101
102 EXPECT_EQ(0, MakeRvalueErrorResult().error().code());
103 EXPECT_EQ("failure1", MakeRvalueErrorResult().error().message());
104}
105
106TEST(result, result_errno_error) {
107 constexpr int test_errno = 6;
108 errno = test_errno;
109 Result<Success> result = ErrnoError() << "failure" << 1;
110
111 ASSERT_FALSE(result);
112 ASSERT_FALSE(result.has_value());
113
114 EXPECT_EQ(test_errno, result.error().code());
115 EXPECT_EQ("failure1: "s + strerror(test_errno), result.error().message());
116}
117
118TEST(result, result_errno_error_no_text) {
119 constexpr int test_errno = 6;
120 errno = test_errno;
121 Result<Success> result = ErrnoError();
122
123 ASSERT_FALSE(result);
124 ASSERT_FALSE(result.has_value());
125
126 EXPECT_EQ(test_errno, result.error().code());
127 EXPECT_EQ(strerror(test_errno), result.error().message());
128}
129
130TEST(result, result_error_from_other_result) {
131 auto error_text = "test error"s;
132 Result<Success> result = Error() << error_text;
133
134 ASSERT_FALSE(result);
135 ASSERT_FALSE(result.has_value());
136
137 Result<std::string> result2 = result.error();
138
139 ASSERT_FALSE(result2);
140 ASSERT_FALSE(result2.has_value());
141
142 EXPECT_EQ(0, result.error().code());
143 EXPECT_EQ(error_text, result.error().message());
144}
145
146TEST(result, result_error_through_ostream) {
147 auto error_text = "test error"s;
148 Result<Success> result = Error() << error_text;
149
150 ASSERT_FALSE(result);
151 ASSERT_FALSE(result.has_value());
152
153 Result<std::string> result2 = Error() << result.error();
154
155 ASSERT_FALSE(result2);
156 ASSERT_FALSE(result2.has_value());
157
158 EXPECT_EQ(0, result.error().code());
159 EXPECT_EQ(error_text, result.error().message());
160}
161
162TEST(result, result_errno_error_through_ostream) {
163 auto error_text = "test error"s;
164 constexpr int test_errno = 6;
165 errno = 6;
166 Result<Success> result = ErrnoError() << error_text;
167
168 errno = 0;
169
170 ASSERT_FALSE(result);
171 ASSERT_FALSE(result.has_value());
172
173 Result<std::string> result2 = Error() << result.error();
174
175 ASSERT_FALSE(result2);
176 ASSERT_FALSE(result2.has_value());
177
178 EXPECT_EQ(test_errno, result.error().code());
179 EXPECT_EQ(error_text + ": " + strerror(test_errno), result.error().message());
180}
181
182TEST(result, constructor_forwarding) {
183 auto result = Result<std::string>(std::in_place, 5, 'a');
184
185 ASSERT_TRUE(result);
186 ASSERT_TRUE(result.has_value());
187
188 EXPECT_EQ("aaaaa", *result);
189}
190
191struct ConstructorTracker {
192 static size_t constructor_called;
193 static size_t copy_constructor_called;
194 static size_t move_constructor_called;
195 static size_t copy_assignment_called;
196 static size_t move_assignment_called;
197
198 template <typename T>
199 ConstructorTracker(T&& string) : string(string) {
200 ++constructor_called;
201 }
202
203 ConstructorTracker(const ConstructorTracker& ct) {
204 ++copy_constructor_called;
205 string = ct.string;
206 }
207 ConstructorTracker(ConstructorTracker&& ct) noexcept {
208 ++move_constructor_called;
209 string = std::move(ct.string);
210 }
211 ConstructorTracker& operator=(const ConstructorTracker& ct) {
212 ++copy_assignment_called;
213 string = ct.string;
214 return *this;
215 }
216 ConstructorTracker& operator=(ConstructorTracker&& ct) noexcept {
217 ++move_assignment_called;
218 string = std::move(ct.string);
219 return *this;
220 }
221
222 std::string string;
223};
224
225size_t ConstructorTracker::constructor_called = 0;
226size_t ConstructorTracker::copy_constructor_called = 0;
227size_t ConstructorTracker::move_constructor_called = 0;
228size_t ConstructorTracker::copy_assignment_called = 0;
229size_t ConstructorTracker::move_assignment_called = 0;
230
231Result<ConstructorTracker> ReturnConstructorTracker(const std::string& in) {
232 if (in.empty()) {
233 return "literal string";
234 }
235 if (in == "test2") {
236 return ConstructorTracker(in + in + "2");
237 }
238 ConstructorTracker result(in + " " + in);
239 return result;
240};
241
242TEST(result, no_copy_on_return) {
243 // If returning parameters that may be used to implicitly construct the type T of Result<T>,
244 // then those parameters are forwarded to the construction of Result<T>.
245
246 // If returning an prvalue or xvalue, it will be move constructed during the construction of
247 // Result<T>.
248
249 // This check ensures that that is the case, and particularly that no copy constructors
250 // are called.
251
252 auto result1 = ReturnConstructorTracker("");
253 ASSERT_TRUE(result1);
254 EXPECT_EQ("literal string", result1->string);
255 EXPECT_EQ(1U, ConstructorTracker::constructor_called);
256 EXPECT_EQ(0U, ConstructorTracker::copy_constructor_called);
257 EXPECT_EQ(0U, ConstructorTracker::move_constructor_called);
258 EXPECT_EQ(0U, ConstructorTracker::copy_assignment_called);
259 EXPECT_EQ(0U, ConstructorTracker::move_assignment_called);
260
261 auto result2 = ReturnConstructorTracker("test2");
262 ASSERT_TRUE(result2);
263 EXPECT_EQ("test2test22", result2->string);
264 EXPECT_EQ(2U, ConstructorTracker::constructor_called);
265 EXPECT_EQ(0U, ConstructorTracker::copy_constructor_called);
266 EXPECT_EQ(1U, ConstructorTracker::move_constructor_called);
267 EXPECT_EQ(0U, ConstructorTracker::copy_assignment_called);
268 EXPECT_EQ(0U, ConstructorTracker::move_assignment_called);
269
270 auto result3 = ReturnConstructorTracker("test3");
271 ASSERT_TRUE(result3);
272 EXPECT_EQ("test3 test3", result3->string);
273 EXPECT_EQ(3U, ConstructorTracker::constructor_called);
274 EXPECT_EQ(0U, ConstructorTracker::copy_constructor_called);
275 EXPECT_EQ(2U, ConstructorTracker::move_constructor_called);
276 EXPECT_EQ(0U, ConstructorTracker::copy_assignment_called);
277 EXPECT_EQ(0U, ConstructorTracker::move_assignment_called);
278}
279
280// Below two tests require that we do not hide the move constructor with our forwarding reference
281// constructor. This is done with by disabling the forwarding reference constructor if its first
282// and only type is Result<T>.
283TEST(result, result_result_with_success) {
284 auto return_result_result_with_success = []() -> Result<Result<Success>> {
285 return Result<Success>();
286 };
287 auto result = return_result_result_with_success();
288 ASSERT_TRUE(result);
289 ASSERT_TRUE(*result);
290
291 auto inner_result = result.value();
292 ASSERT_TRUE(inner_result);
293}
294
295TEST(result, result_result_with_failure) {
296 auto return_result_result_with_error = []() -> Result<Result<Success>> {
297 return Result<Success>(ResultError("failure string", 6));
298 };
299 auto result = return_result_result_with_error();
300 ASSERT_TRUE(result);
301 ASSERT_FALSE(*result);
302 EXPECT_EQ("failure string", (*result).error().message());
303 EXPECT_EQ(6, (*result).error().code());
304}
305
306// This test requires that we disable the forwarding reference constructor if Result<T> is the
307// *only* type that we are forwarding. In otherwords, if we are forwarding Result<T>, int to
308// construct a Result<T>, then we still need the constructor.
309TEST(result, result_two_parameter_constructor_same_type) {
310 struct TestStruct {
311 TestStruct(int value) : value_(value) {}
312 TestStruct(Result<TestStruct> result, int value) : value_(result->value_ * value) {}
313 int value_;
314 };
315
316 auto return_test_struct = []() -> Result<TestStruct> {
317 return Result<TestStruct>(std::in_place, Result<TestStruct>(std::in_place, 6), 6);
318 };
319
320 auto result = return_test_struct();
321 ASSERT_TRUE(result);
322 EXPECT_EQ(36, result->value_);
323}
324
325TEST(result, die_on_access_failed_result) {
326 Result<std::string> result = Error();
327 ASSERT_DEATH(*result, "");
328}
329
330TEST(result, die_on_get_error_succesful_result) {
331 Result<std::string> result = "success";
332 ASSERT_DEATH(result.error(), "");
333}
334
335template <class CharT>
336std::basic_ostream<CharT>& SetErrnoToTwo(std::basic_ostream<CharT>& ss) {
337 errno = 2;
338 return ss;
339}
340
341TEST(result, preserve_errno) {
342 errno = 1;
343 int old_errno = errno;
344 Result<int> result = Error() << "Failed" << SetErrnoToTwo<char>;
345 ASSERT_FALSE(result);
346 EXPECT_EQ(old_errno, errno);
347
348 errno = 1;
349 old_errno = errno;
350 Result<int> result2 = ErrnoError() << "Failed" << SetErrnoToTwo<char>;
351 ASSERT_FALSE(result2);
352 EXPECT_EQ(old_errno, errno);
353 EXPECT_EQ(old_errno, result2.error().code());
354}
355
356} // namespace base
357} // namespace android