xref: /llvm-project/libcxx/test/std/containers/unord/unord.multimap/equal_range_non_const.pass.cpp (revision 3cd4531b9ba421d1d096e746d787fe3039a546bb)
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 // <unordered_map>
10 
11 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
12 //           class Alloc = allocator<pair<const Key, T>>>
13 // class unordered_multimap
14 
15 // pair<iterator, iterator> equal_range(const key_type& k);
16 
17 #include <unordered_map>
18 #include <string>
19 #include <set>
20 #include <cassert>
21 #include <iterator>
22 
23 #include "test_macros.h"
24 #include "min_allocator.h"
25 
main(int,char **)26 int main(int, char**)
27 {
28     {
29         typedef std::unordered_multimap<int, std::string> C;
30         typedef C::iterator I;
31         typedef std::pair<int, std::string> P;
32         P a[] =
33         {
34             P(10, "ten"),
35             P(20, "twenty"),
36             P(30, "thirty"),
37             P(40, "forty"),
38             P(50, "fifty"),
39             P(50, "fiftyA"),
40             P(50, "fiftyB"),
41             P(60, "sixty"),
42             P(70, "seventy"),
43             P(80, "eighty"),
44         };
45         C c(std::begin(a), std::end(a));
46         std::pair<I, I> r = c.equal_range(30);
47         assert(std::distance(r.first, r.second) == 1);
48         assert(r.first->first == 30);
49         assert(r.first->second == "thirty");
50         r = c.equal_range(5);
51         assert(std::distance(r.first, r.second) == 0);
52         r = c.equal_range(50);
53         std::set<std::string> s;
54         s.insert("fifty");
55         s.insert("fiftyA");
56         s.insert("fiftyB");
57         for ( int i = 0; i < 3; ++i )
58         {
59             assert(r.first->first == 50);
60             assert(s.find(r.first->second) != s.end());
61             s.erase(s.find(r.first->second));
62             ++r.first;
63         }
64     }
65 #if TEST_STD_VER >= 11
66     {
67         typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
68                             min_allocator<std::pair<const int, std::string>>> C;
69         typedef C::iterator I;
70         typedef std::pair<int, std::string> P;
71         P a[] =
72         {
73             P(10, "ten"),
74             P(20, "twenty"),
75             P(30, "thirty"),
76             P(40, "forty"),
77             P(50, "fifty"),
78             P(50, "fiftyA"),
79             P(50, "fiftyB"),
80             P(60, "sixty"),
81             P(70, "seventy"),
82             P(80, "eighty"),
83         };
84         C c(std::begin(a), std::end(a));
85         std::pair<I, I> r = c.equal_range(30);
86         assert(std::distance(r.first, r.second) == 1);
87         assert(r.first->first == 30);
88         assert(r.first->second == "thirty");
89         r = c.equal_range(5);
90         assert(std::distance(r.first, r.second) == 0);
91         r = c.equal_range(50);
92         std::set<std::string> s;
93         s.insert("fifty");
94         s.insert("fiftyA");
95         s.insert("fiftyB");
96         for ( int i = 0; i < 3; ++i )
97         {
98             assert(r.first->first == 50);
99             assert(s.find(r.first->second) != s.end());
100             s.erase(s.find(r.first->second));
101             ++r.first;
102         }
103     }
104 #endif
105 
106   return 0;
107 }
108