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-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03
10
11// <set>
12
13// class set
14
15// template <class... Args>
16// pair<iterator, bool> emplace(Args&&... args);
17
18#include <set>
19#include <cassert>
20
21#include "test_macros.h"
22#include "../../Emplaceable.h"
23#include "DefaultOnly.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(args: 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
88 return 0;
89}
90

source code of libcxx/test/std/containers/associative/set/emplace.pass.cpp