blob: ad8d3cf8ef1ed2b1bb55c1e467e2283ecb1e1407 [file] [log] [blame]
Dominik Laskowskia7e22552022-08-01 08:23:34 -07001/*
2 * Copyright 2022 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/optional.h>
18#include <ftl/static_vector.h>
19#include <ftl/string.h>
Dominik Laskowski54494bd2022-08-02 13:37:14 -070020#include <ftl/unit.h>
Dominik Laskowskia7e22552022-08-01 08:23:34 -070021#include <gtest/gtest.h>
22
Dominik Laskowskia957c1c2022-08-03 12:51:43 -070023#include <cstdlib>
Dominik Laskowskia7e22552022-08-01 08:23:34 -070024#include <functional>
25#include <numeric>
26#include <utility>
27
28using namespace std::placeholders;
29using namespace std::string_literals;
30
31namespace android::test {
32
33using ftl::Optional;
34using ftl::StaticVector;
35
36TEST(Optional, Transform) {
37 // Empty.
38 EXPECT_EQ(std::nullopt, Optional<int>().transform([](int) { return 0; }));
39
40 // By value.
41 EXPECT_EQ(0, Optional(0).transform([](int x) { return x; }));
42 EXPECT_EQ(100, Optional(99).transform([](int x) { return x + 1; }));
43 EXPECT_EQ("0b100"s, Optional(4).transform(std::bind(ftl::to_string<int>, _1, ftl::Radix::kBin)));
44
45 // By reference.
46 {
47 Optional opt = 'x';
48 EXPECT_EQ('z', opt.transform([](char& c) {
49 c = 'y';
50 return 'z';
51 }));
52
53 EXPECT_EQ('y', opt);
54 }
55
56 // By rvalue reference.
57 {
58 std::string out;
59 EXPECT_EQ("xyz"s, Optional("abc"s).transform([&out](std::string&& str) {
60 out = std::move(str);
61 return "xyz"s;
62 }));
63
64 EXPECT_EQ(out, "abc"s);
65 }
66
Dominik Laskowski54494bd2022-08-02 13:37:14 -070067 // No return value.
68 {
69 Optional opt = "food"s;
70 EXPECT_EQ(ftl::unit, opt.transform(ftl::unit_fn([](std::string& str) { str.pop_back(); })));
71 EXPECT_EQ(opt, "foo"s);
72 }
73
Dominik Laskowskia7e22552022-08-01 08:23:34 -070074 // Chaining.
75 EXPECT_EQ(14u, Optional(StaticVector{"upside"s, "down"s})
76 .transform([](StaticVector<std::string, 3>&& v) {
77 v.push_back("cake"s);
78 return v;
79 })
80 .transform([](const StaticVector<std::string, 3>& v) {
81 return std::accumulate(v.begin(), v.end(), std::string());
82 })
83 .transform([](const std::string& s) { return s.length(); }));
84}
85
Dominik Laskowskia957c1c2022-08-03 12:51:43 -070086namespace {
87
88Optional<int> parse_int(const std::string& str) {
89 if (const int i = std::atoi(str.c_str())) return i;
90 return std::nullopt;
91}
92
93} // namespace
94
95TEST(Optional, AndThen) {
96 // Empty.
97 EXPECT_EQ(std::nullopt, Optional<int>().and_then([](int) -> Optional<int> { return 0; }));
98 EXPECT_EQ(std::nullopt, Optional<int>().and_then([](int) { return Optional<int>(); }));
99
100 // By value.
101 EXPECT_EQ(0, Optional(0).and_then([](int x) { return Optional(x); }));
102 EXPECT_EQ(123, Optional("123").and_then(parse_int));
103 EXPECT_EQ(std::nullopt, Optional("abc").and_then(parse_int));
104
105 // By reference.
106 {
107 Optional opt = 'x';
108 EXPECT_EQ('z', opt.and_then([](char& c) {
109 c = 'y';
110 return Optional('z');
111 }));
112
113 EXPECT_EQ('y', opt);
114 }
115
116 // By rvalue reference.
117 {
118 std::string out;
119 EXPECT_EQ("xyz"s, Optional("abc"s).and_then([&out](std::string&& str) {
120 out = std::move(str);
121 return Optional("xyz"s);
122 }));
123
124 EXPECT_EQ(out, "abc"s);
125 }
126
127 // Chaining.
128 using StringVector = StaticVector<std::string, 3>;
129 EXPECT_EQ(14u, Optional(StaticVector{"-"s, "1"s})
130 .and_then([](StringVector&& v) -> Optional<StringVector> {
131 if (v.push_back("4"s)) return v;
132 return {};
133 })
134 .and_then([](const StringVector& v) -> Optional<std::string> {
135 if (v.full()) return std::accumulate(v.begin(), v.end(), std::string());
136 return {};
137 })
138 .and_then(parse_int)
139 .and_then([](int i) {
140 return i > 0 ? std::nullopt : std::make_optional(static_cast<unsigned>(-i));
141 }));
142}
143
Dominik Laskowskia7e22552022-08-01 08:23:34 -0700144} // namespace android::test