Dominik Laskowski | 9bb429a | 2024-01-28 15:20:47 -0500 | [diff] [blame] | 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> |
Dominik Laskowski | 189d182 | 2024-05-03 17:30:26 -0400 | [diff] [blame^] | 18 | #include <ftl/optional.h> |
| 19 | #include <ftl/unit.h> |
Dominik Laskowski | 9bb429a | 2024-01-28 15:20:47 -0500 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
Dominik Laskowski | 189d182 | 2024-05-03 17:30:26 -0400 | [diff] [blame^] | 22 | #include <cctype> |
Dominik Laskowski | 9bb429a | 2024-01-28 15:20:47 -0500 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
Dominik Laskowski | 189d182 | 2024-05-03 17:30:26 -0400 | [diff] [blame^] | 80 | namespace { |
| 81 | |
| 82 | IntExp increment(IntExp exp) { |
| 83 | const int i = FTL_TRY(exp); |
| 84 | return IntExp(i + 1); |
| 85 | } |
| 86 | |
| 87 | StringExp repeat(StringExp exp) { |
| 88 | const std::string str = FTL_TRY(exp); |
| 89 | return StringExp(str + str); |
| 90 | } |
| 91 | |
| 92 | void uppercase(char& c, ftl::Optional<char> opt) { |
| 93 | c = std::toupper(FTL_TRY(std::move(opt).ok_or(ftl::Unit()))); |
| 94 | } |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | // Keep in sync with example usage in header file. |
| 99 | TEST(Expected, Try) { |
| 100 | EXPECT_EQ(IntExp(100), increment(IntExp(99))); |
| 101 | EXPECT_TRUE(repeat(ftl::Unexpected(std::errc::value_too_large)).has_error([](std::errc e) { |
| 102 | return e == std::errc::value_too_large; |
| 103 | })); |
| 104 | |
| 105 | EXPECT_EQ(StringExp("haha"s), repeat(StringExp("ha"s))); |
| 106 | EXPECT_TRUE(repeat(ftl::Unexpected(std::errc::bad_message)).has_error([](std::errc e) { |
| 107 | return e == std::errc::bad_message; |
| 108 | })); |
| 109 | |
| 110 | char c = '?'; |
| 111 | uppercase(c, std::nullopt); |
| 112 | EXPECT_EQ(c, '?'); |
| 113 | |
| 114 | uppercase(c, 'a'); |
| 115 | EXPECT_EQ(c, 'A'); |
| 116 | } |
| 117 | |
Dominik Laskowski | 9bb429a | 2024-01-28 15:20:47 -0500 | [diff] [blame] | 118 | } // namespace android::test |