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// <set>
10
11// class set
12
13// set& operator=(const set& s);
14
15#include <set>
16#include <cassert>
17#include <iterator>
18
19#include "test_macros.h"
20#include "../../../test_compare.h"
21#include "test_allocator.h"
22
23int main(int, char**) {
24 {
25 typedef int V;
26 V ar[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
27 typedef test_less<int> C;
28 typedef test_allocator<V> A;
29 std::set<int, C, A> mo(ar, ar + sizeof(ar) / sizeof(ar[0]), C(5), A(2));
30 std::set<int, C, A> m(ar, ar + sizeof(ar) / sizeof(ar[0]) / 2, C(3), A(7));
31 m = mo;
32 assert(m.get_allocator() == A(7));
33 assert(m.key_comp() == C(5));
34 assert(m.size() == 3);
35 assert(std::distance(m.begin(), m.end()) == 3);
36 assert(*m.begin() == 1);
37 assert(*std::next(m.begin()) == 2);
38 assert(*std::next(m.begin(), 2) == 3);
39
40 assert(mo.get_allocator() == A(2));
41 assert(mo.key_comp() == C(5));
42 assert(mo.size() == 3);
43 assert(std::distance(mo.begin(), mo.end()) == 3);
44 assert(*mo.begin() == 1);
45 assert(*std::next(mo.begin()) == 2);
46 assert(*std::next(mo.begin(), 2) == 3);
47 }
48 {
49 typedef int V;
50 const V ar[] = {1, 2, 3};
51 std::set<int> m(ar, ar + sizeof(ar) / sizeof(ar[0]));
52 std::set<int>* p = &m;
53 m = *p;
54
55 assert(m.size() == 3);
56 assert(std::equal(m.begin(), m.end(), ar));
57 }
58 {
59 typedef int V;
60 V ar[] = {1, 1, 1, 2, 2, 2, 3, 3, 3};
61 typedef test_less<int> C;
62 typedef other_allocator<V> A;
63 std::set<int, C, A> mo(ar, ar + sizeof(ar) / sizeof(ar[0]), C(5), A(2));
64 std::set<int, C, A> m(ar, ar + sizeof(ar) / sizeof(ar[0]) / 2, C(3), A(7));
65 m = mo;
66 assert(m.get_allocator() == A(2));
67 assert(m.key_comp() == C(5));
68 assert(m.size() == 3);
69 assert(std::distance(m.begin(), m.end()) == 3);
70 assert(*m.begin() == 1);
71 assert(*std::next(m.begin()) == 2);
72 assert(*std::next(m.begin(), 2) == 3);
73
74 assert(mo.get_allocator() == A(2));
75 assert(mo.key_comp() == C(5));
76 assert(mo.size() == 3);
77 assert(std::distance(mo.begin(), mo.end()) == 3);
78 assert(*mo.begin() == 1);
79 assert(*std::next(mo.begin()) == 2);
80 assert(*std::next(mo.begin(), 2) == 3);
81 }
82
83 { // Test with std::pair, since we have some special handling for pairs inside __tree
84 std::pair<int, int> arr[] = {
85 std::make_pair(x: 1, y: 2), std::make_pair(x: 2, y: 3), std::make_pair(x: 3, y: 4), std::make_pair(x: 4, y: 5)};
86 std::set<std::pair<int, int> > a(arr, arr + 4);
87 std::set<std::pair<int, int> > b;
88
89 b = a;
90 assert(a == b);
91 }
92
93 return 0;
94}
95

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