File: result_test.cpp

package info (click to toggle)
android-platform-system-core 1%3A10.0.0%2Br36-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 123,760 kB
  • sloc: cpp: 197,034; ansic: 18,211; asm: 3,606; sh: 3,180; python: 2,671; java: 693; xml: 266; makefile: 237
file content (333 lines) | stat: -rw-r--r-- 10,820 bytes parent folder | download
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 (C) 2017 The Android Open Source Project
 *
 * 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 "result.h"

#include "errno.h"

#include <string>

#include <gtest/gtest.h>

using namespace std::string_literals;

namespace android {
namespace init {

TEST(result, result_accessors) {
    Result<std::string> result = "success";
    ASSERT_TRUE(result);
    ASSERT_TRUE(result.has_value());

    EXPECT_EQ("success", *result);
    EXPECT_EQ("success", result.value());

    EXPECT_EQ('s', result->data()[0]);
}

TEST(result, result_accessors_rvalue) {
    ASSERT_TRUE(Result<std::string>("success"));
    ASSERT_TRUE(Result<std::string>("success").has_value());

    EXPECT_EQ("success", *Result<std::string>("success"));
    EXPECT_EQ("success", Result<std::string>("success").value());

    EXPECT_EQ('s', Result<std::string>("success")->data()[0]);
}

TEST(result, result_success) {
    Result<Success> result = Success();
    ASSERT_TRUE(result);
    ASSERT_TRUE(result.has_value());

    EXPECT_EQ(Success(), *result);
    EXPECT_EQ(Success(), result.value());
}

TEST(result, result_success_rvalue) {
    // Success() doesn't actually create a Result<Success> object, but rather an object that can be
    // implicitly constructed into a Result<Success> object.

    auto MakeRvalueSuccessResult = []() -> Result<Success> { return Success(); };
    ASSERT_TRUE(MakeRvalueSuccessResult());
    ASSERT_TRUE(MakeRvalueSuccessResult().has_value());

    EXPECT_EQ(Success(), *MakeRvalueSuccessResult());
    EXPECT_EQ(Success(), MakeRvalueSuccessResult().value());
}

TEST(result, result_error) {
    Result<Success> result = Error() << "failure" << 1;
    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    EXPECT_EQ(0, result.error_errno());
    EXPECT_EQ("failure1", result.error_string());
}

TEST(result, result_error_empty) {
    Result<Success> result = Error();
    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    EXPECT_EQ(0, result.error_errno());
    EXPECT_EQ("", result.error_string());
}

TEST(result, result_error_rvalue) {
    // Error() and ErrnoError() aren't actually used to create a Result<T> object.
    // Under the hood, they are an intermediate class that can be implicitly constructed into a
    // Result<T>.  This is needed both to create the ostream and because Error() itself, by
    // definition will not know what the type, T, of the underlying Result<T> object that it would
    // create is.

    auto MakeRvalueErrorResult = []() -> Result<Success> { return Error() << "failure" << 1; };
    ASSERT_FALSE(MakeRvalueErrorResult());
    ASSERT_FALSE(MakeRvalueErrorResult().has_value());

    EXPECT_EQ(0, MakeRvalueErrorResult().error_errno());
    EXPECT_EQ("failure1", MakeRvalueErrorResult().error_string());
}

TEST(result, result_errno_error) {
    constexpr int test_errno = 6;
    errno = test_errno;
    Result<Success> result = ErrnoError() << "failure" << 1;

    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    EXPECT_EQ(test_errno, result.error_errno());
    EXPECT_EQ("failure1: "s + strerror(test_errno), result.error_string());
}

TEST(result, result_errno_error_no_text) {
    constexpr int test_errno = 6;
    errno = test_errno;
    Result<Success> result = ErrnoError();

    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    EXPECT_EQ(test_errno, result.error_errno());
    EXPECT_EQ(strerror(test_errno), result.error_string());
}

TEST(result, result_error_from_other_result) {
    auto error_text = "test error"s;
    Result<Success> result = Error() << error_text;

    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    Result<std::string> result2 = result.error();

    ASSERT_FALSE(result2);
    ASSERT_FALSE(result2.has_value());

    EXPECT_EQ(0, result.error_errno());
    EXPECT_EQ(error_text, result.error_string());
}

TEST(result, result_error_through_ostream) {
    auto error_text = "test error"s;
    Result<Success> result = Error() << error_text;

    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    Result<std::string> result2 = Error() << result.error();

    ASSERT_FALSE(result2);
    ASSERT_FALSE(result2.has_value());

    EXPECT_EQ(0, result.error_errno());
    EXPECT_EQ(error_text, result.error_string());
}

TEST(result, result_errno_error_through_ostream) {
    auto error_text = "test error"s;
    constexpr int test_errno = 6;
    errno = 6;
    Result<Success> result = ErrnoError() << error_text;

    errno = 0;

    ASSERT_FALSE(result);
    ASSERT_FALSE(result.has_value());

    Result<std::string> result2 = Error() << result.error();

    ASSERT_FALSE(result2);
    ASSERT_FALSE(result2.has_value());

    EXPECT_EQ(test_errno, result.error_errno());
    EXPECT_EQ(error_text + ": " + strerror(test_errno), result.error_string());
}

TEST(result, constructor_forwarding) {
    auto result = Result<std::string>(5, 'a');

    ASSERT_TRUE(result);
    ASSERT_TRUE(result.has_value());

    EXPECT_EQ("aaaaa", *result);
}

struct ConstructorTracker {
    static size_t constructor_called;
    static size_t copy_constructor_called;
    static size_t move_constructor_called;
    static size_t copy_assignment_called;
    static size_t move_assignment_called;

    template <typename T>
    ConstructorTracker(T&& string) : string(string) {
        ++constructor_called;
    }

    ConstructorTracker(const ConstructorTracker& ct) {
        ++copy_constructor_called;
        string = ct.string;
    }
    ConstructorTracker(ConstructorTracker&& ct) noexcept {
        ++move_constructor_called;
        string = std::move(ct.string);
    }
    ConstructorTracker& operator=(const ConstructorTracker& ct) {
        ++copy_assignment_called;
        string = ct.string;
        return *this;
    }
    ConstructorTracker& operator=(ConstructorTracker&& ct) noexcept {
        ++move_assignment_called;
        string = std::move(ct.string);
        return *this;
    }

    std::string string;
};

size_t ConstructorTracker::constructor_called = 0;
size_t ConstructorTracker::copy_constructor_called = 0;
size_t ConstructorTracker::move_constructor_called = 0;
size_t ConstructorTracker::copy_assignment_called = 0;
size_t ConstructorTracker::move_assignment_called = 0;

Result<ConstructorTracker> ReturnConstructorTracker(const std::string& in) {
    if (in.empty()) {
        return "literal string";
    }
    if (in == "test2") {
        return ConstructorTracker(in + in + "2");
    }
    ConstructorTracker result(in + " " + in);
    return result;
};

TEST(result, no_copy_on_return) {
    // If returning parameters that may be used to implicitly construct the type T of Result<T>,
    // then those parameters are forwarded to the construction of Result<T>.

    // If returning an prvalue or xvalue, it will be move constructed during the construction of
    // Result<T>.

    // This check ensures that that is the case, and particularly that no copy constructors
    // are called.

    auto result1 = ReturnConstructorTracker("");
    ASSERT_TRUE(result1);
    EXPECT_EQ("literal string", result1->string);
    EXPECT_EQ(1U, ConstructorTracker::constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::copy_constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::move_constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::copy_assignment_called);
    EXPECT_EQ(0U, ConstructorTracker::move_assignment_called);

    auto result2 = ReturnConstructorTracker("test2");
    ASSERT_TRUE(result2);
    EXPECT_EQ("test2test22", result2->string);
    EXPECT_EQ(2U, ConstructorTracker::constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::copy_constructor_called);
    EXPECT_EQ(1U, ConstructorTracker::move_constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::copy_assignment_called);
    EXPECT_EQ(0U, ConstructorTracker::move_assignment_called);

    auto result3 = ReturnConstructorTracker("test3");
    ASSERT_TRUE(result3);
    EXPECT_EQ("test3 test3", result3->string);
    EXPECT_EQ(3U, ConstructorTracker::constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::copy_constructor_called);
    EXPECT_EQ(2U, ConstructorTracker::move_constructor_called);
    EXPECT_EQ(0U, ConstructorTracker::copy_assignment_called);
    EXPECT_EQ(0U, ConstructorTracker::move_assignment_called);
}

// Below two tests require that we do not hide the move constructor with our forwarding reference
// constructor.  This is done with by disabling the forwarding reference constructor if its first
// and only type is Result<T>.
TEST(result, result_result_with_success) {
    auto return_result_result_with_success = []() -> Result<Result<Success>> {
        return Result<Success>();
    };
    auto result = return_result_result_with_success();
    ASSERT_TRUE(result);
    ASSERT_TRUE(*result);

    auto inner_result = result.value();
    ASSERT_TRUE(inner_result);
}

TEST(result, result_result_with_failure) {
    auto return_result_result_with_error = []() -> Result<Result<Success>> {
        return Result<Success>(ResultError("failure string", 6));
    };
    auto result = return_result_result_with_error();
    ASSERT_TRUE(result);
    ASSERT_FALSE(*result);
    EXPECT_EQ("failure string", result->error_string());
    EXPECT_EQ(6, result->error_errno());
}

// This test requires that we disable the forwarding reference constructor if Result<T> is the
// *only* type that we are forwarding.  In otherwords, if we are forwarding Result<T>, int to
// construct a Result<T>, then we still need the constructor.
TEST(result, result_two_parameter_constructor_same_type) {
    struct TestStruct {
        TestStruct(int value) : value_(value) {}
        TestStruct(Result<TestStruct> result, int value) : value_(result->value_ * value) {}
        int value_;
    };

    auto return_test_struct = []() -> Result<TestStruct> { return {Result<TestStruct>(6), 6}; };

    auto result = return_test_struct();
    ASSERT_TRUE(result);
    EXPECT_EQ(36, result->value_);
}

TEST(result, die_on_access_failed_result) {
    Result<std::string> result = Error();
    ASSERT_DEATH(*result, "");
}

TEST(result, die_on_get_error_succesful_result) {
    Result<std::string> result = "success";
    ASSERT_DEATH(result.error_string(), "");
}

}  // namespace init
}  // namespace android