1 /*
2 * Copyright 2024 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 <ftl/expected.h>
18 #include <ftl/optional.h>
19 #include <ftl/unit.h>
20 #include <gtest/gtest.h>
21
22 #include <cctype>
23 #include <string>
24 #include <system_error>
25
26 namespace android::test {
27
28 using IntExp = ftl::Expected<int, std::errc>;
29 using StringExp = ftl::Expected<std::string, std::errc>;
30
31 using namespace std::string_literals;
32
TEST(Expected,Construct)33 TEST(Expected, Construct) {
34 // Default value.
35 EXPECT_TRUE(IntExp().has_value());
36 EXPECT_EQ(IntExp(), IntExp(0));
37
38 EXPECT_TRUE(StringExp().has_value());
39 EXPECT_EQ(StringExp(), StringExp(""));
40
41 // Value.
42 ASSERT_TRUE(IntExp(42).has_value());
43 EXPECT_EQ(42, IntExp(42).value());
44
45 ASSERT_TRUE(StringExp("test").has_value());
46 EXPECT_EQ("test"s, StringExp("test").value());
47
48 // Error.
49 const auto exp = StringExp(ftl::Unexpected(std::errc::invalid_argument));
50 ASSERT_FALSE(exp.has_value());
51 EXPECT_EQ(std::errc::invalid_argument, exp.error());
52 }
53
TEST(Expected,HasError)54 TEST(Expected, HasError) {
55 EXPECT_FALSE(IntExp(123).has_error([](auto) { return true; }));
56 EXPECT_FALSE(IntExp(ftl::Unexpected(std::errc::io_error)).has_error([](auto) { return false; }));
57
58 EXPECT_TRUE(StringExp(ftl::Unexpected(std::errc::permission_denied)).has_error([](auto e) {
59 return e == std::errc::permission_denied;
60 }));
61 }
62
TEST(Expected,ValueOpt)63 TEST(Expected, ValueOpt) {
64 EXPECT_EQ(ftl::Optional(-1), IntExp(-1).value_opt());
65 EXPECT_EQ(std::nullopt, IntExp(ftl::Unexpected(std::errc::broken_pipe)).value_opt());
66
67 {
68 const StringExp exp("foo"s);
69 EXPECT_EQ(ftl::Optional('f'),
70 exp.value_opt().transform([](const auto& s) { return s.front(); }));
71 EXPECT_EQ("foo"s, exp.value());
72 }
73 {
74 StringExp exp("foobar"s);
75 EXPECT_EQ(ftl::Optional(6), std::move(exp).value_opt().transform(&std::string::length));
76 EXPECT_TRUE(exp.value().empty());
77 }
78 }
79
80 namespace {
81
increment_try(IntExp exp)82 IntExp increment_try(IntExp exp) {
83 const int i = FTL_TRY(exp);
84 return IntExp(i + 1);
85 }
86
increment_expect(IntExp exp,int & out)87 std::errc increment_expect(IntExp exp, int& out) {
88 const int i = FTL_EXPECT(exp);
89 out = i + 1;
90 return std::errc::operation_in_progress;
91 }
92
repeat_try(StringExp exp)93 StringExp repeat_try(StringExp exp) {
94 const std::string str = FTL_TRY(exp);
95 return StringExp(str + str);
96 }
97
repeat_expect(StringExp exp,std::string & out)98 std::errc repeat_expect(StringExp exp, std::string& out) {
99 const std::string str = FTL_EXPECT(exp);
100 out = str + str;
101 return std::errc::operation_in_progress;
102 }
103
uppercase(char & c,ftl::Optional<char> opt)104 void uppercase(char& c, ftl::Optional<char> opt) {
105 c = std::toupper(FTL_TRY(std::move(opt).ok_or(ftl::Unit())));
106 }
107
108 } // namespace
109
110 // Keep in sync with example usage in header file.
TEST(Expected,Try)111 TEST(Expected, Try) {
112 EXPECT_EQ(IntExp(100), increment_try(IntExp(99)));
113 EXPECT_TRUE(increment_try(ftl::Unexpected(std::errc::value_too_large)).has_error([](std::errc e) {
114 return e == std::errc::value_too_large;
115 }));
116
117 EXPECT_EQ(StringExp("haha"s), repeat_try(StringExp("ha"s)));
118 EXPECT_TRUE(repeat_try(ftl::Unexpected(std::errc::bad_message)).has_error([](std::errc e) {
119 return e == std::errc::bad_message;
120 }));
121
122 char c = '?';
123 uppercase(c, std::nullopt);
124 EXPECT_EQ(c, '?');
125
126 uppercase(c, 'a');
127 EXPECT_EQ(c, 'A');
128 }
129
TEST(Expected,Expect)130 TEST(Expected, Expect) {
131 int i = 0;
132 EXPECT_EQ(std::errc::operation_in_progress, increment_expect(IntExp(99), i));
133 EXPECT_EQ(100, i);
134 EXPECT_EQ(std::errc::value_too_large,
135 increment_expect(ftl::Unexpected(std::errc::value_too_large), i));
136 EXPECT_EQ(100, i);
137
138 std::string str;
139 EXPECT_EQ(std::errc::operation_in_progress, repeat_expect(StringExp("ha"s), str));
140 EXPECT_EQ("haha"s, str);
141 EXPECT_EQ(std::errc::bad_message, repeat_expect(ftl::Unexpected(std::errc::bad_message), str));
142 EXPECT_EQ("haha"s, str);
143 }
144
145 } // namespace android::test
146