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