Dominik Laskowski | e6c5e6e | 2022-08-03 12:58:28 -0700 | [diff] [blame] | 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/algorithm.h> |
| 18 | #include <ftl/small_map.h> |
| 19 | #include <ftl/static_vector.h> |
| 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include <string_view> |
| 23 | |
| 24 | namespace android::test { |
| 25 | |
| 26 | // Keep in sync with example usage in header file. |
| 27 | TEST(Algorithm, FindIf) { |
| 28 | using namespace std::string_view_literals; |
| 29 | |
| 30 | const ftl::StaticVector vector = {"upside"sv, "down"sv, "cake"sv}; |
| 31 | EXPECT_EQ(ftl::find_if(vector, [](const auto& str) { return str.front() == 'c'; }), "cake"sv); |
| 32 | |
| 33 | const ftl::SmallMap map = ftl::init::map<int, ftl::StaticVector<std::string_view, 3>>( |
| 34 | 12, "snow"sv, "cone"sv)(13, "tiramisu"sv)(14, "upside"sv, "down"sv, "cake"sv); |
| 35 | |
| 36 | using Map = decltype(map); |
| 37 | |
| 38 | EXPECT_EQ(14, ftl::find_if(map, [](const auto& pair) { |
| 39 | return pair.second.size() == 3; |
| 40 | }).transform(ftl::to_key<Map>)); |
| 41 | |
| 42 | const auto opt = ftl::find_if(map, [](const auto& pair) { |
| 43 | return pair.second.size() == 1; |
| 44 | }).transform(ftl::to_mapped_ref<Map>); |
| 45 | |
| 46 | ASSERT_TRUE(opt); |
| 47 | EXPECT_EQ(opt->get(), ftl::StaticVector("tiramisu"sv)); |
| 48 | } |
| 49 | |
| 50 | } // namespace android::test |