94 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// mapped_type& operator[](const key_type& k);14 15#include <cassert>16#include <deque>17#include <flat_map>18#include <functional>19#include <type_traits>20#include <vector>21 22#include "MinSequenceContainer.h"23#include "../helpers.h"24#include "min_allocator.h"25#include "test_macros.h"26 27// Constraints: is_constructible_v<mapped_type> is true.28template <class M, class Input>29concept CanIndex = requires(M m, Input k) { m[k]; };30 31static_assert(CanIndex<std::flat_map<int, double>, const int&>);32static_assert(!CanIndex<std::flat_map<int, NoDefaultCtr>, const int&>);33 34template <class KeyContainer, class ValueContainer>35constexpr void test() {36 using P = std::pair<int, double>;37 P ar[] = {38 P(1, 1.5),39 P(2, 2.5),40 P(3, 3.5),41 P(4, 4.5),42 P(5, 5.5),43 P(7, 7.5),44 P(8, 8.5),45 };46 const int one = 1;47 std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer> m(ar, ar + sizeof(ar) / sizeof(ar[0]));48 ASSERT_SAME_TYPE(decltype(m[one]), double&);49 assert(m.size() == 7);50 assert(m[one] == 1.5);51 assert(m.size() == 7);52 m[1] = -1.5;53 assert(m[1] == -1.5);54 assert(m.size() == 7);55 assert(m[6] == 0);56 assert(m.size() == 8);57 m[6] = 6.5;58 assert(m[6] == 6.5);59 assert(m.size() == 8);60}61 62constexpr bool test() {63 test<std::vector<int>, std::vector<double>>();64#ifndef __cpp_lib_constexpr_deque65 if (!TEST_IS_CONSTANT_EVALUATED)66#endif67 {68 test<std::deque<int>, std::vector<double>>();69 }70 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();71 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();72 73 if (!TEST_IS_CONSTANT_EVALUATED) {74 auto index_func = [](auto& m, auto key_arg, auto value_arg) {75 using FlatMap = std::decay_t<decltype(m)>;76 const typename FlatMap::key_type key = key_arg;77 const typename FlatMap::mapped_type value = value_arg;78 m[key] = value;79 };80 test_emplace_exception_guarantee(index_func);81 }82 83 return true;84}85 86int main(int, char**) {87 test();88#if TEST_STD_VER >= 2689 static_assert(test());90#endif91 92 return 0;93}94