139 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// class flat_map14 15// pair<iterator, bool> insert( value_type&& v);16 17#include <flat_map>18#include <cassert>19#include <deque>20 21#include "MinSequenceContainer.h"22#include "MoveOnly.h"23#include "min_allocator.h"24#include "test_macros.h"25#include "../helpers.h"26 27template <class Container, class Pair>28constexpr void do_insert_rv_test() {29 using M = Container;30 using P = Pair;31 using R = std::pair<typename M::iterator, bool>;32 M m;33 std::same_as<R> decltype(auto) r = m.insert(P(2, 2));34 assert(r.second);35 assert(r.first == m.begin());36 assert(m.size() == 1);37 assert(r.first->first == 2);38 assert(r.first->second == 2);39 40 r = m.insert(P(1, 1));41 assert(r.second);42 assert(r.first == m.begin());43 assert(m.size() == 2);44 assert(r.first->first == 1);45 assert(r.first->second == 1);46 47 r = m.insert(P(3, 3));48 assert(r.second);49 assert(r.first == std::ranges::prev(m.end()));50 assert(m.size() == 3);51 assert(r.first->first == 3);52 assert(r.first->second == 3);53 54 r = m.insert(P(3, 3));55 assert(!r.second);56 assert(r.first == std::ranges::prev(m.end()));57 assert(m.size() == 3);58 assert(r.first->first == 3);59 assert(r.first->second == 3);60}61 62template <class KeyContainer, class ValueContainer>63constexpr void test() {64 using Key = typename KeyContainer::value_type;65 using Value = typename ValueContainer::value_type;66 using M = std::flat_map<Key, Value, TransparentComparator, KeyContainer, ValueContainer>;67 68 using P = std::pair<Key, Value>;69 using CP = std::pair<const Key, Value>;70 71 do_insert_rv_test<M, P>();72 do_insert_rv_test<M, CP>();73}74 75constexpr bool test() {76 test<std::vector<int>, std::vector<MoveOnly>>();77#ifndef __cpp_lib_constexpr_deque78 if (!TEST_IS_CONSTANT_EVALUATED)79#endif80 {81 test<std::deque<int>, std::vector<MoveOnly>>();82 }83 test<MinSequenceContainer<int>, MinSequenceContainer<MoveOnly>>();84 test<std::vector<int, min_allocator<int>>, std::vector<MoveOnly, min_allocator<MoveOnly>>>();85 86 {87 using M = std::flat_map<int, MoveOnly>;88 using R = std::pair<M::iterator, bool>;89 M m;90 R r = m.insert({2, MoveOnly(2)});91 assert(r.second);92 assert(r.first == m.begin());93 assert(m.size() == 1);94 assert(r.first->first == 2);95 assert(r.first->second == 2);96 97 r = m.insert({1, MoveOnly(1)});98 assert(r.second);99 assert(r.first == m.begin());100 assert(m.size() == 2);101 assert(r.first->first == 1);102 assert(r.first->second == 1);103 104 r = m.insert({3, MoveOnly(3)});105 assert(r.second);106 assert(r.first == std::ranges::prev(m.end()));107 assert(m.size() == 3);108 assert(r.first->first == 3);109 assert(r.first->second == 3);110 111 r = m.insert({3, MoveOnly(3)});112 assert(!r.second);113 assert(r.first == std::ranges::prev(m.end()));114 assert(m.size() == 3);115 assert(r.first->first == 3);116 assert(r.first->second == 3);117 }118 if (!TEST_IS_CONSTANT_EVALUATED) {119 auto insert_func = [](auto& m, auto key_arg, auto value_arg) {120 using FlatMap = std::decay_t<decltype(m)>;121 using value_type = typename FlatMap::value_type;122 value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg));123 m.insert(std::move(p));124 };125 test_emplace_exception_guarantee(insert_func);126 }127 128 return true;129}130 131int main(int, char**) {132 test();133#if TEST_STD_VER >= 26134 static_assert(test());135#endif136 137 return 0;138}139