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