1 /*
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/match.h>
18 #include <gtest/gtest.h>
19
20 #include <chrono>
21 #include <string>
22 #include <variant>
23
24 namespace android::test {
25
26 // Keep in sync with example usage in header file.
TEST(Match,Example)27 TEST(Match, Example) {
28 using namespace std::chrono;
29 using namespace std::chrono_literals;
30 using namespace std::string_literals;
31
32 std::variant<seconds, minutes, hours> duration = 119min;
33
34 // Mutable match.
35 ftl::match(duration, [](auto& d) { ++d; });
36
37 // Immutable match. Exhaustive due to minutes being convertible to seconds.
38 EXPECT_EQ("2 hours"s,
39 ftl::match(
40 duration,
41 [](const seconds& s) {
42 const auto h = duration_cast<hours>(s);
43 return std::to_string(h.count()) + " hours"s;
44 },
45 [](const hours& h) { return std::to_string(h.count() / 24) + " days"s; }));
46 }
47
48 } // namespace android::test
49