148 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_set>12 13// template <class... Args>14// iterator emplace(Args&&... args);15 16#include <flat_set>17#include <cassert>18#include <deque>19#include <tuple>20#include <functional>21#include <vector>22 23#include "MinSequenceContainer.h"24#include "../helpers.h"25#include "test_macros.h"26#include "../../../Emplaceable.h"27#include "DefaultOnly.h"28#include "min_allocator.h"29 30template <class KeyContainer>31constexpr void test_one() {32 using Key = typename KeyContainer::value_type;33 using M = std::flat_multiset<Key, std::less<Key>, KeyContainer>;34 using R = typename M::iterator;35 {36 // was empty37 M m;38 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));39 assert(r == m.begin());40 assert(m.size() == 1);41 assert(*r == 2);42 }43 {44 // key does not exist and inserted at the begin45 M m = {3, 3, 3, 7};46 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));47 assert(r == m.begin());48 assert(m.size() == 5);49 assert(*r == 2);50 }51 {52 // key does not exist and inserted in the middle53 M m = {1, 1, 3, 4};54 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));55 assert(r == m.begin() + 2);56 assert(m.size() == 5);57 assert(*r == 2);58 }59 {60 // key does not exist and inserted at the end61 M m = {1, 1};62 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));63 assert(r == m.begin() + 2);64 assert(m.size() == 3);65 assert(*r == 2);66 }67 {68 // key already exists and original at the begin69 M m = {2, 2, 5, 6};70 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));71 assert(r == m.begin() + 2);72 assert(m.size() == 5);73 assert(*r == 2);74 }75 {76 // key already exists and original in the middle77 M m = {0, 2, 2, 4};78 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));79 assert(r == m.begin() + 3);80 assert(m.size() == 5);81 assert(*r == 2);82 }83 {84 // key already exists and original at the end85 M m = {0, 1, 2};86 std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));87 assert(r == m.begin() + 3);88 assert(m.size() == 4);89 assert(*r == 2);90 }91}92 93template <class KeyContainer>94constexpr void test_emplaceable() {95 using M = std::flat_multiset<Emplaceable, std::less<Emplaceable>, KeyContainer>;96 using R = typename M::iterator;97 98 M m;99 ASSERT_SAME_TYPE(decltype(m.emplace()), R);100 R r = m.emplace(2, 0.0);101 assert(r == m.begin());102 assert(m.size() == 1);103 assert(*r == Emplaceable(2, 0.0));104 r = m.emplace(1, 3.5);105 assert(r == m.begin());106 assert(m.size() == 2);107 assert(*r == Emplaceable(1, 3.5));108 r = m.emplace(1, 3.5);109 assert(r == m.begin() + 1);110 assert(m.size() == 3);111 assert(*r == Emplaceable(1, 3.5));112}113 114constexpr bool test() {115 test_one<std::vector<int>>();116#ifndef __cpp_lib_constexpr_deque117 if (!TEST_IS_CONSTANT_EVALUATED)118#endif119 test_one<std::deque<int>>();120 test_one<MinSequenceContainer<int>>();121 test_one<std::vector<int, min_allocator<int>>>();122 123 test_emplaceable<std::vector<Emplaceable>>();124#ifndef __cpp_lib_constexpr_deque125 if (!TEST_IS_CONSTANT_EVALUATED)126#endif127 test_emplaceable<std::deque<Emplaceable>>();128 test_emplaceable<MinSequenceContainer<Emplaceable>>();129 test_emplaceable<std::vector<Emplaceable, min_allocator<Emplaceable>>>();130 131 return true;132}133 134void test_exception() {135 auto emplace_func = [](auto& m, auto key_arg) { m.emplace(key_arg); };136 test_emplace_exception_guarantee(emplace_func);137}138 139int main(int, char**) {140 test();141#if TEST_STD_VER >= 26142 static_assert(test());143#endif144 test_exception();145 146 return 0;147}148