| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <flat_map> |
| 12 | |
| 13 | // class flat_multimap |
| 14 | |
| 15 | // template<class K> iterator lower_bound(const K& x); |
| 16 | // template<class K> const_iterator lower_bound(const K& x) const; |
| 17 | |
| 18 | #include <cassert> |
| 19 | #include <deque> |
| 20 | #include <flat_map> |
| 21 | #include <functional> |
| 22 | #include <string> |
| 23 | #include <utility> |
| 24 | |
| 25 | #include "MinSequenceContainer.h" |
| 26 | #include "../helpers.h" |
| 27 | #include "test_macros.h" |
| 28 | #include "min_allocator.h" |
| 29 | |
| 30 | // Constraints: The qualified-id Compare::is_transparent is valid and denotes a type. |
| 31 | template <class M> |
| 32 | concept CanLowerBound = requires(M m, Transparent<int> k) { m.lower_bound(k); }; |
| 33 | using TransparentMap = std::flat_multimap<int, double, TransparentComparator>; |
| 34 | using NonTransparentMap = std::flat_multimap<int, double, NonTransparentComparator>; |
| 35 | static_assert(CanLowerBound<TransparentMap>); |
| 36 | static_assert(CanLowerBound<const TransparentMap>); |
| 37 | static_assert(!CanLowerBound<NonTransparentMap>); |
| 38 | static_assert(!CanLowerBound<const NonTransparentMap>); |
| 39 | |
| 40 | template <class KeyContainer, class ValueContainer> |
| 41 | void test() { |
| 42 | using Key = typename KeyContainer::value_type; |
| 43 | using Value = typename ValueContainer::value_type; |
| 44 | using M = std::flat_multimap<Key, Value, TransparentComparator, KeyContainer, ValueContainer>; |
| 45 | |
| 46 | M m = {{"alpha" , 1}, |
| 47 | {"alpha" , 2}, |
| 48 | {"alpha" , 3}, |
| 49 | {"beta" , 2}, |
| 50 | {"epsilon" , 3}, |
| 51 | {"epsilon" , 4}, |
| 52 | {"eta" , 4}, |
| 53 | {"gamma" , 5}, |
| 54 | {"gamma" , 5}, |
| 55 | {"gamma" , 5}, |
| 56 | {"gamma" , 5}}; |
| 57 | const auto& cm = m; |
| 58 | ASSERT_SAME_TYPE(decltype(m.lower_bound(Transparent<std::string>{"abc" })), typename M::iterator); |
| 59 | ASSERT_SAME_TYPE(decltype(std::as_const(m).lower_bound(Transparent<std::string>{"b" })), typename M::const_iterator); |
| 60 | |
| 61 | auto test_lower_bound = [&](auto&& map, const std::string& expected_key, long expected_offset) { |
| 62 | auto iter = map.lower_bound(Transparent<std::string>{.t: expected_key}); |
| 63 | assert(iter - map.begin() == expected_offset); |
| 64 | }; |
| 65 | |
| 66 | test_lower_bound(m, "abc" , 0); |
| 67 | test_lower_bound(m, "alpha" , 0); |
| 68 | test_lower_bound(m, "beta" , 3); |
| 69 | test_lower_bound(m, "bets" , 4); |
| 70 | test_lower_bound(m, "charlie" , 4); |
| 71 | test_lower_bound(m, "echo" , 4); |
| 72 | test_lower_bound(m, "epsilon" , 4); |
| 73 | test_lower_bound(m, "eta" , 6); |
| 74 | test_lower_bound(m, "gamma" , 7); |
| 75 | test_lower_bound(m, "golf" , 11); |
| 76 | test_lower_bound(m, "zzz" , 11); |
| 77 | |
| 78 | test_lower_bound(cm, "abc" , 0); |
| 79 | test_lower_bound(cm, "alpha" , 0); |
| 80 | test_lower_bound(cm, "beta" , 3); |
| 81 | test_lower_bound(cm, "bets" , 4); |
| 82 | test_lower_bound(cm, "charlie" , 4); |
| 83 | test_lower_bound(cm, "echo" , 4); |
| 84 | test_lower_bound(cm, "epsilon" , 4); |
| 85 | test_lower_bound(cm, "eta" , 6); |
| 86 | test_lower_bound(cm, "gamma" , 7); |
| 87 | test_lower_bound(cm, "golf" , 11); |
| 88 | test_lower_bound(cm, "zzz" , 11); |
| 89 | } |
| 90 | |
| 91 | int main(int, char**) { |
| 92 | test<std::vector<std::string>, std::vector<int>>(); |
| 93 | test<std::deque<std::string>, std::vector<int>>(); |
| 94 | test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>(); |
| 95 | test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>(); |
| 96 | |
| 97 | { |
| 98 | bool transparent_used = false; |
| 99 | TransparentComparator c(transparent_used); |
| 100 | std::flat_multimap<int, int, TransparentComparator> m(std::sorted_equivalent, {{1, 1}, {2, 2}, {3, 3}}, c); |
| 101 | assert(!transparent_used); |
| 102 | auto it = m.lower_bound(Transparent<int>{3}); |
| 103 | assert(it != m.end()); |
| 104 | assert(transparent_used); |
| 105 | } |
| 106 | { |
| 107 | // LWG4239 std::string and C string literal |
| 108 | using M = std::flat_multimap<std::string, int, std::less<>>; |
| 109 | M m{{"alpha" , 1}, {"beta" , 2}, {"beta" , 1}, {"eta" , 3}, {"gamma" , 3}}; |
| 110 | auto it = m.lower_bound("charlie" ); |
| 111 | assert(it == m.begin() + 3); |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |