]>
Commit | Line | Data |
---|---|---|
e06cde87 VV |
1 | // { dg-options "-std=gnu++2a" } |
2 | // { dg-do run { target c++2a } } | |
3 | ||
83ffe9cd | 4 | // Copyright (C) 2020-2023 Free Software Foundation, Inc. |
e06cde87 VV |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 3, or (at your option) | |
10 | // any later version. | |
11 | // | |
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | // | |
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING3. If not see | |
19 | // <http://www.gnu.org/licenses/>. | |
20 | ||
21 | #include <unordered_map> | |
22 | #include <testsuite_hooks.h> | |
23 | ||
24 | struct aggressive_aggregate | |
25 | { | |
26 | int a; | |
27 | int b; | |
28 | }; | |
29 | ||
30 | void test_emplace() | |
31 | { | |
32 | std::unordered_map<int, aggressive_aggregate> x; | |
33 | auto emplaced = x.emplace(std::piecewise_construct, | |
34 | std::tuple(0), std::tuple(1, 2)); | |
35 | VERIFY(emplaced.first->second.a == 1); | |
36 | VERIFY(emplaced.first->second.b == 2); | |
37 | emplaced = x.emplace(std::piecewise_construct, | |
38 | std::tuple(1), std::tuple(1)); | |
39 | VERIFY(emplaced.first->second.a == 1); | |
40 | VERIFY(emplaced.first->second.b == 0); | |
41 | emplaced = x.emplace(std::piecewise_construct, | |
42 | std::tuple(2), std::tuple()); | |
43 | VERIFY(emplaced.first->second.a == 0); | |
44 | VERIFY(emplaced.first->second.b == 0); | |
45 | } | |
46 | ||
47 | void test_emplace_hint() | |
48 | { | |
49 | std::unordered_map<int, aggressive_aggregate> x; | |
50 | auto it = x.emplace_hint(x.begin(), | |
51 | std::piecewise_construct, | |
52 | std::tuple(3), std::tuple(1, 2)); | |
53 | VERIFY(it->second.a == 1); | |
54 | VERIFY(it->second.b == 2); | |
55 | it = x.emplace_hint(x.begin(), | |
56 | std::piecewise_construct, | |
57 | std::tuple(4), std::tuple(1)); | |
58 | VERIFY(it->second.a == 1); | |
59 | VERIFY(it->second.b == 0); | |
60 | it = x.emplace_hint(x.begin(), | |
61 | std::piecewise_construct, | |
62 | std::tuple(5), std::tuple()); | |
63 | VERIFY(it->second.a == 0); | |
64 | VERIFY(it->second.b == 0); | |
65 | } | |
66 | ||
67 | void test_try_emplace_rvalue() | |
68 | { | |
69 | std::unordered_map<int, aggressive_aggregate> x; | |
70 | auto emplaced = x.try_emplace(6, 1, 2); | |
71 | VERIFY(emplaced.first->second.a == 1); | |
72 | VERIFY(emplaced.first->second.b == 2); | |
73 | emplaced = x.try_emplace(7, 1); | |
74 | VERIFY(emplaced.first->second.a == 1); | |
75 | VERIFY(emplaced.first->second.b == 0); | |
76 | emplaced = x.try_emplace(8); | |
77 | VERIFY(emplaced.first->second.a == 0); | |
78 | VERIFY(emplaced.first->second.b == 0); | |
79 | } | |
80 | ||
81 | void test_try_emplace_lvalue() | |
82 | { | |
83 | std::unordered_map<int, aggressive_aggregate> x; | |
84 | int key = 9; | |
85 | auto emplaced = x.try_emplace(key, 1, 2); | |
86 | VERIFY(emplaced.first->second.a == 1); | |
87 | VERIFY(emplaced.first->second.b == 2); | |
88 | key = 10; | |
89 | emplaced = x.try_emplace(key, 1); | |
90 | VERIFY(emplaced.first->second.a == 1); | |
91 | VERIFY(emplaced.first->second.b == 0); | |
92 | key = 11; | |
93 | emplaced = x.try_emplace(key); | |
94 | VERIFY(emplaced.first->second.a == 0); | |
95 | VERIFY(emplaced.first->second.b == 0); | |
96 | } | |
97 | ||
98 | void test_try_emplace_hint_rvalue() | |
99 | { | |
100 | std::unordered_map<int, aggressive_aggregate> x; | |
101 | auto it = x.try_emplace(x.begin(), 12, 1, 2); | |
102 | VERIFY(it->second.a == 1); | |
103 | VERIFY(it->second.b == 2); | |
104 | it = x.try_emplace(x.begin(), 13, 1); | |
105 | VERIFY(it->second.a == 1); | |
106 | VERIFY(it->second.b == 0); | |
107 | it = x.try_emplace(x.begin(), 14); | |
108 | VERIFY(it->second.a == 0); | |
109 | VERIFY(it->second.b == 0); | |
110 | } | |
111 | ||
112 | void test_try_emplace_hint_lvalue() | |
113 | { | |
114 | std::unordered_map<int, aggressive_aggregate> x; | |
115 | int key = 15; | |
116 | auto it = x.try_emplace(x.begin(), key, 1, 2); | |
117 | VERIFY(it->second.a == 1); | |
118 | VERIFY(it->second.b == 2); | |
119 | key = 16; | |
120 | it = x.try_emplace(x.begin(), key, 1); | |
121 | VERIFY(it->second.a == 1); | |
122 | VERIFY(it->second.b == 0); | |
123 | key = 17; | |
124 | it = x.try_emplace(x.begin(), key); | |
125 | VERIFY(it->second.a == 0); | |
126 | VERIFY(it->second.b == 0); | |
127 | } | |
128 | ||
129 | int main() | |
130 | { | |
131 | test_emplace(); | |
132 | test_emplace_hint(); | |
133 | test_try_emplace_rvalue(); | |
134 | test_try_emplace_lvalue(); | |
135 | test_try_emplace_hint_rvalue(); | |
136 | test_try_emplace_hint_lvalue(); | |
137 | } |