]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_multiset/operations/1.cc
libstdc++: Fix missing/misplaced { dg-options "-std=gnu++20" } in tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multiset / operations / 1.cc
1 // Copyright (C) 2021-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++20" }
19 // { dg-do run { target c++20 } }
20
21 #include <unordered_set>
22 #include <testsuite_hooks.h>
23
24 struct Equal
25 {
26 typedef void is_transparent;
27
28 bool operator()(int i, long l) const { return i == l; }
29 bool operator()(long l, int i) const { return l == i; }
30 bool operator()(int i, int j) const { ++count; return i == j; }
31
32 static int count;
33 };
34
35 int Equal::count = 0;
36
37 struct Hash
38 {
39 typedef void is_transparent;
40
41 std::size_t operator()(int i) const { ++count; return i; }
42 std::size_t operator()(long l) const { return l; }
43
44 static int count;
45 };
46
47 int Hash::count = 0;
48
49 using test_type = std::unordered_multiset<int, Hash, Equal>;
50
51 test_type x{ 1, 3, 3, 5 };
52 const test_type& cx = x;
53
54 void
55 test01()
56 {
57 Hash::count = 0;
58 Equal::count = 0;
59
60 VERIFY( x.contains(1L) );
61
62 auto it = x.find(1L);
63 VERIFY( it != x.end() && *it == 1 );
64 it = x.find(2L);
65 VERIFY( it == x.end() );
66
67 auto cit = cx.find(3L);
68 VERIFY( cit != cx.end() && *cit == 3 );
69 cit = cx.find(2L);
70 VERIFY( cit == cx.end() );
71
72 VERIFY( Hash::count == 0 );
73 VERIFY( Equal::count == 0 );
74
75 static_assert(std::is_same<decltype(it), test_type::iterator>::value,
76 "find returns iterator");
77 static_assert(std::is_same<decltype(cit), test_type::const_iterator>::value,
78 "const find returns const_iterator");
79 }
80
81 void
82 test02()
83 {
84 Hash::count = 0;
85 Equal::count = 0;
86
87 auto n = x.count(1L);
88 VERIFY( n == 1 );
89 n = x.count(2L);
90 VERIFY( n == 0 );
91
92 auto cn = cx.count(3L);
93 VERIFY( cn == 2 );
94 cn = cx.count(2L);
95 VERIFY( cn == 0 );
96
97 VERIFY( Hash::count == 0 );
98 VERIFY( Equal::count == 0 );
99 }
100
101 void
102 test03()
103 {
104 Hash::count = 0;
105 Equal::count = 0;
106
107 auto it = x.equal_range(1L);
108 VERIFY( it.first != it.second && *it.first == 1 );
109 it = x.equal_range(2L);
110 VERIFY( it.first == it.second && it.first == x.end() );
111
112 auto cit = cx.equal_range(3L);
113 VERIFY( cit.first != cit.second && *cit.first == 3 );
114 VERIFY( std::distance(cit.first, cit.second) == 2 );
115 cit = cx.equal_range(2L);
116 VERIFY( cit.first == cit.second && cit.first == cx.end() );
117
118 VERIFY( Hash::count == 0 );
119 VERIFY( Equal::count == 0 );
120
121 using pair = std::pair<test_type::iterator, test_type::iterator>;
122 static_assert(std::is_same<decltype(it), pair>::value,
123 "equal_range returns pair<iterator, iterator>");
124 using cpair = std::pair<test_type::const_iterator, test_type::const_iterator>;
125 static_assert(std::is_same<decltype(cit), cpair>::value,
126 "const equal_range returns pair<const_iterator, const_iterator>");
127 }
128
129
130 int
131 main()
132 {
133 test01();
134 test02();
135 test03();
136 }