brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.6 KiB · a0f9a15 Raw
111 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_map>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_map>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 TransparentMap    = std::flat_map<int, double, TransparentComparator>;32using NonTransparentMap = std::flat_map<int, double, NonTransparentComparator>;33static_assert(CanFind<TransparentMap>);34static_assert(CanFind<const TransparentMap>);35static_assert(!CanFind<NonTransparentMap>);36static_assert(!CanFind<const NonTransparentMap>);37 38template <class KeyContainer, class ValueContainer>39constexpr void test() {40  using Key   = typename KeyContainer::value_type;41  using Value = typename ValueContainer::value_type;42  using M     = std::flat_map<Key, Value, TransparentComparator, KeyContainer, ValueContainer>;43 44  M m            = {{"alpha", 1}, {"beta", 2}, {"epsilon", 3}, {"eta", 4}, {"gamma", 5}};45  const auto& cm = m;46  ASSERT_SAME_TYPE(decltype(m.find(Transparent<std::string>{"abc"})), typename M::iterator);47  ASSERT_SAME_TYPE(decltype(std::as_const(m).find(Transparent<std::string>{"b"})), typename M::const_iterator);48 49  auto test_find = [&](auto&& map, const std::string& expected_key, long expected_offset) {50    auto iter = map.find(Transparent<std::string>{expected_key});51    assert(iter - map.begin() == expected_offset);52  };53 54  test_find(m, "alpha", 0);55  test_find(m, "beta", 1);56  test_find(m, "epsilon", 2);57  test_find(m, "eta", 3);58  test_find(m, "gamma", 4);59  test_find(m, "charlie", 5);60  test_find(m, "aaa", 5);61  test_find(m, "zzz", 5);62  test_find(cm, "alpha", 0);63  test_find(cm, "beta", 1);64  test_find(cm, "epsilon", 2);65  test_find(cm, "eta", 3);66  test_find(cm, "gamma", 4);67  test_find(cm, "charlie", 5);68  test_find(cm, "aaa", 5);69  test_find(cm, "zzz", 5);70}71 72constexpr bool test() {73  test<std::vector<std::string>, std::vector<int>>();74#ifndef __cpp_lib_constexpr_deque75  if (!TEST_IS_CONSTANT_EVALUATED)76#endif77  {78    test<std::deque<std::string>, std::vector<int>>();79  }80  test<MinSequenceContainer<std::string>, MinSequenceContainer<int>>();81  test<std::vector<std::string, min_allocator<std::string>>, std::vector<int, min_allocator<int>>>();82 83  {84    bool transparent_used = false;85    TransparentComparator c(transparent_used);86    std::flat_map<int, int, TransparentComparator> m(std::sorted_unique, {{1, 1}, {2, 2}, {3, 3}}, c);87    assert(!transparent_used);88    auto it = m.find(Transparent<int>{3});89    assert(it != m.end());90    assert(transparent_used);91  }92  {93    // LWG4239 std::string and C string literal94    using M = std::flat_map<std::string, int, std::less<>>;95    M m{{"alpha", 1}, {"beta", 2}, {"epsilon", 1}, {"eta", 3}, {"gamma", 3}};96    auto it = m.find("beta");97    assert(it == m.begin() + 1);98  }99 100  return true;101}102 103int main(int, char**) {104  test();105#if TEST_STD_VER >= 26106  static_assert(test());107#endif108 109  return 0;110}111