128 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_multimap14 15// iterator 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 = typename M::iterator;32 M m;33 std::same_as<R> decltype(auto) r = m.insert(P(2, 2));34 assert(r == m.begin());35 assert(m.size() == 1);36 assert(r->first == 2);37 assert(r->second == 2);38 39 r = m.insert(P(1, 1));40 assert(r == m.begin());41 assert(m.size() == 2);42 assert(r->first == 1);43 assert(r->second == 1);44 45 r = m.insert(P(3, 3));46 assert(r == std::ranges::prev(m.end()));47 assert(m.size() == 3);48 assert(r->first == 3);49 assert(r->second == 3);50 51 r = m.insert(P(3, 3));52 assert(r == std::ranges::prev(m.end()));53 assert(m.size() == 4);54 assert(r->first == 3);55 assert(r->second == 3);56}57 58template <class KeyContainer, class ValueContainer>59constexpr void test() {60 using Key = typename KeyContainer::value_type;61 using Value = typename ValueContainer::value_type;62 using M = std::flat_multimap<Key, Value, TransparentComparator, KeyContainer, ValueContainer>;63 64 using P = std::pair<Key, Value>;65 using CP = std::pair<const Key, Value>;66 67 do_insert_rv_test<M, P>();68 do_insert_rv_test<M, CP>();69}70 71constexpr bool test() {72 test<std::vector<int>, std::vector<MoveOnly>>();73#ifndef __cpp_lib_constexpr_deque74 if (!TEST_IS_CONSTANT_EVALUATED)75#endif76 test<std::deque<int>, std::vector<MoveOnly>>();77 test<MinSequenceContainer<int>, MinSequenceContainer<MoveOnly>>();78 test<std::vector<int, min_allocator<int>>, std::vector<MoveOnly, min_allocator<MoveOnly>>>();79 80 {81 using M = std::flat_multimap<int, MoveOnly>;82 using R = M::iterator;83 M m;84 R r = m.insert({2, MoveOnly(2)});85 assert(r == m.begin());86 assert(m.size() == 1);87 assert(r->first == 2);88 assert(r->second == 2);89 90 r = m.insert({1, MoveOnly(1)});91 assert(r == m.begin());92 assert(m.size() == 2);93 assert(r->first == 1);94 assert(r->second == 1);95 96 r = m.insert({3, MoveOnly(3)});97 assert(r == std::ranges::prev(m.end()));98 assert(m.size() == 3);99 assert(r->first == 3);100 assert(r->second == 3);101 102 r = m.insert({3, MoveOnly(3)});103 assert(r == std::ranges::prev(m.end()));104 assert(m.size() == 4);105 assert(r->first == 3);106 assert(r->second == 3);107 }108 if (!TEST_IS_CONSTANT_EVALUATED) {109 auto insert_func = [](auto& m, auto key_arg, auto value_arg) {110 using FlatMap = std::decay_t<decltype(m)>;111 using value_type = typename FlatMap::value_type;112 value_type p(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg));113 m.insert(std::move(p));114 };115 test_emplace_exception_guarantee(insert_func);116 }117 return true;118}119 120int main(int, char**) {121 test();122#if TEST_STD_VER >= 26123 static_assert(test());124#endif125 126 return 0;127}128