182 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// template <class... Args>14// pair<iterator, bool> emplace(Args&&... args);15 16#include <flat_map>17#include <cassert>18#include <deque>19#include <tuple>20#include <functional>21#include <type_traits>22#include <vector>23 24#include "MinSequenceContainer.h"25#include "../helpers.h"26#include "test_macros.h"27#include "../../../Emplaceable.h"28#include "DefaultOnly.h"29#include "min_allocator.h"30 31// Constraints: is_constructible_v<pair<key_type, mapped_type>, Args...> is true.32template <class M, class... Args>33concept CanEmplace = requires(M m, Args&&... args) { m.emplace(std::forward<Args>(args)...); };34 35using Map = std::flat_map<Emplaceable, Emplaceable>;36static_assert(CanEmplace<Map>);37static_assert(CanEmplace<Map, Emplaceable, Emplaceable>);38static_assert(CanEmplace<Map, std::piecewise_construct_t, std::tuple<int, double>, std::tuple<int, double>>);39static_assert(!CanEmplace<Map, Emplaceable>);40static_assert(!CanEmplace<Map, int, double>);41 42template <class KeyContainer, class ValueContainer>43constexpr void test() {44 using Key = typename KeyContainer::value_type;45 using Value = typename ValueContainer::value_type;46 using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;47 using R = std::pair<typename M::iterator, bool>;48 {49 // was empty50 M m;51 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 3.5));52 assert(r.second);53 assert(r.first == m.begin());54 assert(m.size() == 1);55 assert(r.first->first == 2);56 assert(r.first->second == 3.5);57 }58 {59 // key does not exist and inserted at the begin60 M m = {{3, 4.0}, {5, 3.0}, {6, 1.0}, {7, 0.0}};61 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));62 assert(r.second);63 assert(r.first == m.begin());64 assert(m.size() == 5);65 assert(r.first->first == 2);66 assert(r.first->second == 2.0);67 }68 {69 // key does not exist and inserted in the middle70 M m = {{0, 4.0}, {1, 3.0}, {3, 1.0}, {4, 0.0}};71 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));72 assert(r.second);73 assert(r.first == m.begin() + 2);74 assert(m.size() == 5);75 assert(r.first->first == 2);76 assert(r.first->second == 2.0);77 }78 {79 // key does not exist and inserted at the end80 M m = {{0, 4.0}, {1, 3.0}};81 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));82 assert(r.second);83 assert(r.first == m.begin() + 2);84 assert(m.size() == 3);85 assert(r.first->first == 2);86 assert(r.first->second == 2.0);87 }88 {89 // key already exists and original at the begin90 M m = {{2, 4.0}, {3, 3.0}, {5, 1.0}, {6, 0.0}};91 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));92 assert(!r.second);93 assert(r.first == m.begin());94 assert(m.size() == 4);95 assert(r.first->first == 2);96 assert(r.first->second == 4.0);97 }98 {99 // key already exists and original in the middle100 M m = {{0, 4.0}, {2, 3.0}, {3, 1.0}, {4, 0.0}};101 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));102 assert(!r.second);103 assert(r.first == m.begin() + 1);104 assert(m.size() == 4);105 assert(r.first->first == 2);106 assert(r.first->second == 3.0);107 }108 {109 // key already exists and original at the end110 M m = {{0, 4.0}, {1, 3.0}, {2, 1.0}};111 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));112 assert(!r.second);113 assert(r.first == m.begin() + 2);114 assert(m.size() == 3);115 assert(r.first->first == 2);116 assert(r.first->second == 1.0);117 }118}119 120template <class KeyContainer, class ValueContainer>121constexpr void test_emplaceable() {122 using M = std::flat_map<int, Emplaceable, std::less<int>, KeyContainer, ValueContainer>;123 using R = std::pair<typename M::iterator, bool>;124 125 M m;126 ASSERT_SAME_TYPE(decltype(m.emplace()), R);127 R r = m.emplace(std::piecewise_construct, std::forward_as_tuple(2), std::forward_as_tuple());128 assert(r.second);129 assert(r.first == m.begin());130 assert(m.size() == 1);131 assert(m.begin()->first == 2);132 assert(m.begin()->second == Emplaceable());133 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5));134 assert(r.second);135 assert(r.first == m.begin());136 assert(m.size() == 2);137 assert(m.begin()->first == 1);138 assert(m.begin()->second == Emplaceable(2, 3.5));139 r = m.emplace(std::piecewise_construct, std::forward_as_tuple(1), std::forward_as_tuple(2, 3.5));140 assert(!r.second);141 assert(r.first == m.begin());142 assert(m.size() == 2);143 assert(m.begin()->first == 1);144 assert(m.begin()->second == Emplaceable(2, 3.5));145}146 147constexpr bool test() {148 test<std::vector<int>, std::vector<double>>();149 test<MinSequenceContainer<int>, MinSequenceContainer<double>>();150 test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();151 152 test_emplaceable<std::vector<int>, std::vector<Emplaceable>>();153 test_emplaceable<MinSequenceContainer<int>, MinSequenceContainer<Emplaceable>>();154 test_emplaceable<std::vector<int, min_allocator<int>>, std::vector<Emplaceable, min_allocator<Emplaceable>>>();155 156#ifndef __cpp_lib_constexpr_deque157 if (!TEST_IS_CONSTANT_EVALUATED)158#endif159 {160 test<std::deque<int>, std::vector<double>>();161 test_emplaceable<std::deque<int>, std::vector<Emplaceable>>();162 }163 164 if (!TEST_IS_CONSTANT_EVALUATED) {165 auto emplace_func = [](auto& m, auto key_arg, auto value_arg) {166 m.emplace(std::piecewise_construct, std::tuple(key_arg), std::tuple(value_arg));167 };168 test_emplace_exception_guarantee(emplace_func);169 }170 171 return true;172}173 174int main(int, char**) {175 test();176#if TEST_STD_VER >= 26177 static_assert(test());178#endif179 180 return 0;181}182