brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.5 KiB · de8c05c Raw
158 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//   pair<iterator, bool> 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_set<Key, std::less<Key>, KeyContainer>;34  using R   = std::pair<typename M::iterator, bool>;35  {36    // was empty37    M m;38    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));39    assert(r.second);40    assert(r.first == m.begin());41    assert(m.size() == 1);42    assert(*r.first == 2);43  }44  {45    // key does not exist and inserted at the begin46    M m                              = {3, 5, 6, 7};47    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));48    assert(r.second);49    assert(r.first == m.begin());50    assert(m.size() == 5);51    assert(*r.first == 2);52  }53  {54    // key does not exist and inserted in the middle55    M m                              = {0, 1, 3, 4};56    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));57    assert(r.second);58    assert(r.first == m.begin() + 2);59    assert(m.size() == 5);60    assert(*r.first == 2);61  }62  {63    // key does not exist and inserted at the end64    M m                              = {0, 1};65    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));66    assert(r.second);67    assert(r.first == m.begin() + 2);68    assert(m.size() == 3);69    assert(*r.first == 2);70  }71  {72    // key already exists and original at the begin73    M m                              = {2, 3, 5, 6};74    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));75    assert(!r.second);76    assert(r.first == m.begin());77    assert(m.size() == 4);78    assert(*r.first == 2);79  }80  {81    // key already exists and original in the middle82    M m                              = {0, 2, 3, 4};83    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));84    assert(!r.second);85    assert(r.first == m.begin() + 1);86    assert(m.size() == 4);87    assert(*r.first == 2);88  }89  {90    // key already exists and original at the end91    M m                              = {0, 1, 2};92    std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2));93    assert(!r.second);94    assert(r.first == m.begin() + 2);95    assert(m.size() == 3);96    assert(*r.first == 2);97  }98}99 100template <class KeyContainer>101constexpr void test_emplaceable() {102  using M = std::flat_set<Emplaceable, std::less<Emplaceable>, KeyContainer>;103  using R = std::pair<typename M::iterator, bool>;104 105  M m;106  ASSERT_SAME_TYPE(decltype(m.emplace()), R);107  R r = m.emplace(2, 0.0);108  assert(r.second);109  assert(r.first == m.begin());110  assert(m.size() == 1);111  assert(*m.begin() == Emplaceable(2, 0.0));112  r = m.emplace(1, 3.5);113  assert(r.second);114  assert(r.first == m.begin());115  assert(m.size() == 2);116  assert(*m.begin() == Emplaceable(1, 3.5));117  r = m.emplace(1, 3.5);118  assert(!r.second);119  assert(r.first == m.begin());120  assert(m.size() == 2);121  assert(*m.begin() == Emplaceable(1, 3.5));122}123 124constexpr bool test() {125  test_one<std::vector<int>>();126#ifndef __cpp_lib_constexpr_deque127  if (!TEST_IS_CONSTANT_EVALUATED)128#endif129    test_one<std::deque<int>>();130  test_one<MinSequenceContainer<int>>();131  test_one<std::vector<int, min_allocator<int>>>();132 133  test_emplaceable<std::vector<Emplaceable>>();134#ifndef __cpp_lib_constexpr_deque135  if (!TEST_IS_CONSTANT_EVALUATED)136#endif137    test_emplaceable<std::deque<Emplaceable>>();138  test_emplaceable<MinSequenceContainer<Emplaceable>>();139  test_emplaceable<std::vector<Emplaceable, min_allocator<Emplaceable>>>();140 141  return true;142}143 144void test_exception() {145  auto emplace_func = [](auto& m, auto key_arg) { m.emplace(key_arg); };146  test_emplace_exception_guarantee(emplace_func);147}148 149int main(int, char**) {150  test();151  test_exception();152#if TEST_STD_VER >= 26153  static_assert(test());154#endif155 156  return 0;157}158