99 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// pair<iterator, bool> insert(const value_type& v);14 15#include <flat_map>16#include <deque>17#include <cassert>18#include <functional>19 20#include "MinSequenceContainer.h"21#include "test_macros.h"22#include "../helpers.h"23#include "min_allocator.h"24 25template <class KeyContainer, class ValueContainer>26constexpr void test() {27 using Key = typename KeyContainer::value_type;28 using Value = typename ValueContainer::value_type;29 using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;30 using R = std::pair<typename M::iterator, bool>;31 using VT = typename M::value_type;32 M m;33 34 const VT v1(2, 2.5);35 std::same_as<R> decltype(auto) r = m.insert(v1);36 assert(r.second);37 assert(r.first == m.begin());38 assert(m.size() == 1);39 assert(r.first->first == 2);40 assert(r.first->second == 2.5);41 42 const VT v2(1, 1.5);43 r = m.insert(v2);44 assert(r.second);45 assert(r.first == m.begin());46 assert(m.size() == 2);47 assert(r.first->first == 1);48 assert(r.first->second == 1.5);49 50 const VT v3(3, 3.5);51 r = m.insert(v3);52 assert(r.second);53 assert(r.first == std::ranges::prev(m.end()));54 assert(m.size() == 3);55 assert(r.first->first == 3);56 assert(r.first->second == 3.5);57 58 const VT v4(3, 4.5);59 r = m.insert(v4);60 assert(!r.second);61 assert(r.first == std::ranges::prev(m.end()));62 assert(m.size() == 3);63 assert(r.first->first == 3);64 assert(r.first->second == 3.5);65}66 67constexpr bool test() {68 test<std::vector<int>, std::vector<double>>();69#ifndef __cpp_lib_constexpr_deque70 if (!TEST_IS_CONSTANT_EVALUATED)71#endif72 {73 test<std::deque<int>, std::vector<double>>();74 }75 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();76 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();77 78 if (!TEST_IS_CONSTANT_EVALUATED) {79 auto insert_func = [](auto& m, auto key_arg, auto value_arg) {80 using FlatMap = std::decay_t<decltype(m)>;81 using value_type = typename FlatMap::value_type;82 const value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg));83 m.insert(p);84 };85 test_emplace_exception_guarantee(insert_func);86 }87 88 return true;89}90 91int main(int, char**) {92 test();93#if TEST_STD_VER >= 2694 static_assert(test());95#endif96 97 return 0;98}99