brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.3 KiB · 5712493 Raw
108 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// key_compare key_comp() const;14// value_compare value_comp() const;15 16#include <cassert>17#include <flat_map>18#include <functional>19#include <type_traits>20#include <utility>21#include <vector>22 23#include "test_macros.h"24 25constexpr bool test() {26  {27    using M    = std::flat_map<int, char>;28    using Comp = std::less<int>; // the default29    M m        = {};30    ASSERT_SAME_TYPE(M::key_compare, Comp);31    static_assert(!std::is_same_v<M::value_compare, Comp>);32    ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp);33    ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare);34    Comp kc = m.key_comp();35    assert(kc(1, 2));36    assert(!kc(2, 1));37    auto vc = m.value_comp();38    ASSERT_SAME_TYPE(decltype(vc(std::make_pair(1, 2), std::make_pair(1, 2))), bool);39    assert(vc({1, '2'}, {2, '1'}));40    assert(!vc({2, '1'}, {1, '2'}));41  }42  if (!TEST_IS_CONSTANT_EVALUATED) {43    using Comp = std::function<bool(int, int)>;44    using M    = std::flat_map<int, int, Comp>;45    Comp comp  = std::greater<int>();46    M m({}, comp);47    ASSERT_SAME_TYPE(M::key_compare, Comp);48    ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp);49    ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare);50    Comp kc = m.key_comp();51    assert(!kc(1, 2));52    assert(kc(2, 1));53    auto vc = m.value_comp();54    auto a  = std::make_pair(1, 2);55    ASSERT_SAME_TYPE(decltype(vc(a, a)), bool);56    static_assert(!noexcept(vc(a, a)));57    assert(!vc({1, 2}, {2, 1}));58    assert(vc({2, 1}, {1, 2}));59  }60  {61    using Comp = std::less<>;62    using M    = std::flat_map<int, int, Comp>;63    M m        = {};64    ASSERT_SAME_TYPE(M::key_compare, Comp);65    ASSERT_SAME_TYPE(decltype(m.key_comp()), Comp);66    ASSERT_SAME_TYPE(decltype(m.value_comp()), M::value_compare);67    Comp kc = m.key_comp();68    assert(kc(1, 2));69    assert(!kc(2, 1));70    auto vc = m.value_comp();71    auto a  = std::make_pair(1, 2);72    ASSERT_SAME_TYPE(decltype(vc(a, a)), bool);73    assert(vc({1, 2}, {2, 1}));74    assert(!vc({2, 1}, {1, 2}));75  }76  if (!TEST_IS_CONSTANT_EVALUATED) {77    using Comp = std::function<bool(const std::vector<int>&, const std::vector<int>&)>;78    using M    = std::flat_map<std::vector<int>, int, Comp>;79    Comp comp  = [i = 1](const auto& x, const auto& y) { return x[i] < y[i]; };80    M m({}, comp);81    auto vc = m.value_comp();82    static_assert(sizeof(vc) >= sizeof(Comp));83    comp = nullptr;84    m    = M({}, nullptr);85    assert(m.key_comp() == nullptr);86    // At this point, m.key_comp() is disengaged.87    // But the std::function captured by copy inside `vc` remains valid.88    auto a = std::make_pair(std::vector<int>{2, 1, 4}, 42);89    auto b = std::make_pair(std::vector<int>{1, 2, 3}, 42);90    auto c = std::make_pair(std::vector<int>{0, 3, 2}, 42);91    assert(vc(a, b));92    assert(vc(b, c));93    assert(!vc(b, a));94    assert(!vc(c, b));95  }96 97  return true;98}99 100int main(int, char**) {101  test();102#if TEST_STD_VER >= 26103  static_assert(test());104#endif105 106  return 0;107}108