brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.6 KiB · e5a9f95 Raw
97 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++0310 11// <set>12 13// class set14 15// template <class... Args>16//   pair<iterator, bool> emplace(Args&&... args);17 18#include <cassert>19#include <set>20 21#include "../../Emplaceable.h"22#include "DefaultOnly.h"23#include "MoveOnly.h"24#include "min_allocator.h"25 26int main(int, char**) {27  {28    typedef std::set<DefaultOnly> M;29    typedef std::pair<M::iterator, bool> R;30    M m;31    assert(DefaultOnly::count == 0);32    R r = m.emplace();33    assert(r.second);34    assert(r.first == m.begin());35    assert(m.size() == 1);36    assert(*m.begin() == DefaultOnly());37    assert(DefaultOnly::count == 1);38 39    r = m.emplace();40    assert(!r.second);41    assert(r.first == m.begin());42    assert(m.size() == 1);43    assert(*m.begin() == DefaultOnly());44    assert(DefaultOnly::count == 1);45  }46  assert(DefaultOnly::count == 0);47  {48    typedef std::set<Emplaceable> M;49    typedef std::pair<M::iterator, bool> R;50    M m;51    R r = m.emplace();52    assert(r.second);53    assert(r.first == m.begin());54    assert(m.size() == 1);55    assert(*m.begin() == Emplaceable());56    r = m.emplace(2, 3.5);57    assert(r.second);58    assert(r.first == std::next(m.begin()));59    assert(m.size() == 2);60    assert(*r.first == Emplaceable(2, 3.5));61    r = m.emplace(2, 3.5);62    assert(!r.second);63    assert(r.first == std::next(m.begin()));64    assert(m.size() == 2);65    assert(*r.first == Emplaceable(2, 3.5));66  }67  {68    typedef std::set<int> M;69    typedef std::pair<M::iterator, bool> R;70    M m;71    R r = m.emplace(M::value_type(2));72    assert(r.second);73    assert(r.first == m.begin());74    assert(m.size() == 1);75    assert(*r.first == 2);76  }77  {78    typedef std::set<int, std::less<int>, min_allocator<int>> M;79    typedef std::pair<M::iterator, bool> R;80    M m;81    R r = m.emplace(M::value_type(2));82    assert(r.second);83    assert(r.first == m.begin());84    assert(m.size() == 1);85    assert(*r.first == 2);86  }87  { // We're unwrapping pairs for `{,multi}map`. Make sure we're not trying to do that for set.88    using Set = std::set<std::pair<MoveOnly, MoveOnly>>;89    Set set;90    auto res = set.emplace(std::pair<MoveOnly, MoveOnly>(2, 4));91    assert(std::get<1>(res));92    assert(set.begin() == std::get<0>(res));93  }94 95  return 0;96}97