119 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++17, c++2010 11// <flat_set>12 13// template<class K> iterator find(const K& x);14// template<class K> const_iterator find(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.29template <class M>30concept CanFind = requires(M m, Transparent<int> k) { m.find(k); };31using TransparentSet = std::flat_multiset<int, TransparentComparator>;32using NonTransparentSet = std::flat_multiset<int, NonTransparentComparator>;33static_assert(CanFind<TransparentSet>);34static_assert(CanFind<const TransparentSet>);35static_assert(!CanFind<NonTransparentSet>);36static_assert(!CanFind<const NonTransparentSet>);37 38template <class KeyContainer>39constexpr 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", "alpha", "beta", "epsilon", "epsilon", "eta", "gamma", "gamma"};45 46 const auto& cm = m;47 ASSERT_SAME_TYPE(decltype(m.find(Transparent<std::string>{"abc"})), typename M::iterator);48 ASSERT_SAME_TYPE(decltype(std::as_const(m).find(Transparent<std::string>{"b"})), typename M::const_iterator);49 50 auto test_find = [](auto&& set, const std::string& expected_key, long expected_offset) {51 auto iter = set.find(Transparent<std::string>{expected_key});52 assert(iter - set.begin() == expected_offset);53 };54 55 test_find(m, "alpha", 0);56 test_find(m, "beta", 3);57 test_find(m, "epsilon", 4);58 test_find(m, "eta", 6);59 test_find(m, "gamma", 7);60 test_find(m, "charlie", 9);61 test_find(m, "aaa", 9);62 test_find(m, "zzz", 9);63 test_find(cm, "alpha", 0);64 test_find(cm, "beta", 3);65 test_find(cm, "epsilon", 4);66 test_find(cm, "eta", 6);67 test_find(cm, "gamma", 7);68 test_find(cm, "charlie", 9);69 test_find(cm, "aaa", 9);70 test_find(cm, "zzz", 9);71 }72 {73 // empty74 M m;75 auto iter = m.find(Transparent<std::string>{"a"});76 assert(iter == m.end());77 }78}79 80constexpr bool test() {81 test_one<std::vector<std::string>>();82#ifndef __cpp_lib_constexpr_deque83 if (!TEST_IS_CONSTANT_EVALUATED)84#endif85 test_one<std::deque<std::string>>();86 test_one<MinSequenceContainer<std::string>>();87 test_one<std::vector<std::string, min_allocator<std::string>>>();88 89 {90 bool transparent_used = false;91 TransparentComparator c(transparent_used);92 std::flat_multiset<int, TransparentComparator> m(std::sorted_equivalent, {1, 2, 2, 2, 3, 3}, c);93 assert(!transparent_used);94 auto it = m.find(Transparent<int>{3});95 assert(it != m.end());96 assert(transparent_used);97 }98 {99 // std::string and C string literal100 using M = std::flat_multiset<std::string, std::less<>>;101 M m = {"alpha", "beta", "beta", "epsilon", "eta", "gamma"};102 auto it = m.find("beta");103 assert(it == m.begin() + 1);104 auto it2 = m.find("charlie");105 assert(it2 == m.end());106 }107 108 return true;109}110 111int main(int, char**) {112 test();113#if TEST_STD_VER >= 26114 static_assert(test());115#endif116 117 return 0;118}119