xref: /llvm-project/libcxx/test/std/containers/unord/unord.multimap/iterators.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
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 // iterator       begin()        {return __table_.begin();}
16 // iterator       end()          {return __table_.end();}
17 // const_iterator begin()  const {return __table_.begin();}
18 // const_iterator end()    const {return __table_.end();}
19 // const_iterator cbegin() const {return __table_.begin();}
20 // const_iterator cend()   const {return __table_.end();}
21 
22 #include <unordered_map>
23 #include <string>
24 #include <cassert>
25 #include <cstddef>
26 
27 #include "test_macros.h"
28 #include "min_allocator.h"
29 
main(int,char **)30 int main(int, char**)
31 {
32     {
33         typedef std::unordered_multimap<int, std::string> C;
34         typedef std::pair<int, std::string> P;
35         P a[] =
36         {
37             P(1, "one"),
38             P(2, "two"),
39             P(3, "three"),
40             P(4, "four"),
41             P(1, "four"),
42             P(2, "four"),
43         };
44         C c(a, a + sizeof(a)/sizeof(a[0]));
45         assert(c.bucket_count() >= 7);
46         assert(c.size() == 6);
47         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
48         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
49         C::iterator i;
50         i = c.begin();
51         i->second = "ONE";
52         assert(i->second == "ONE");
53     }
54     {
55         typedef std::unordered_multimap<int, std::string> C;
56         typedef std::pair<int, std::string> P;
57         P a[] =
58         {
59             P(1, "one"),
60             P(2, "two"),
61             P(3, "three"),
62             P(4, "four"),
63             P(1, "four"),
64             P(2, "four"),
65         };
66         const C c(a, a + sizeof(a)/sizeof(a[0]));
67         assert(c.bucket_count() >= 7);
68         assert(c.size() == 6);
69         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
70         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
71         C::const_iterator i;
72     }
73 #if TEST_STD_VER >= 11
74     {
75         typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
76                             min_allocator<std::pair<const int, std::string>>> C;
77         typedef std::pair<int, std::string> P;
78         P a[] =
79         {
80             P(1, "one"),
81             P(2, "two"),
82             P(3, "three"),
83             P(4, "four"),
84             P(1, "four"),
85             P(2, "four"),
86         };
87         C c(a, a + sizeof(a)/sizeof(a[0]));
88         assert(c.bucket_count() >= 7);
89         assert(c.size() == 6);
90         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
91         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
92         C::iterator i;
93         i = c.begin();
94         i->second = "ONE";
95         assert(i->second == "ONE");
96     }
97     {
98         typedef std::unordered_multimap<int, std::string, std::hash<int>, std::equal_to<int>,
99                             min_allocator<std::pair<const int, std::string>>> C;
100         typedef std::pair<int, std::string> P;
101         P a[] =
102         {
103             P(1, "one"),
104             P(2, "two"),
105             P(3, "three"),
106             P(4, "four"),
107             P(1, "four"),
108             P(2, "four"),
109         };
110         const C c(a, a + sizeof(a)/sizeof(a[0]));
111         assert(c.bucket_count() >= 7);
112         assert(c.size() == 6);
113         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
114         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
115         C::const_iterator i;
116     }
117 #endif
118 #if TEST_STD_VER > 11
119     { // N3644 testing
120         typedef std::unordered_multimap<int,double> C;
121         C::iterator ii1{}, ii2{};
122         C::iterator ii4 = ii1;
123         C::const_iterator cii{};
124         assert ( ii1 == ii2 );
125         assert ( ii1 == ii4 );
126 
127         assert (!(ii1 != ii2 ));
128 
129         assert ( (ii1 == cii ));
130         assert ( (cii == ii1 ));
131         assert (!(ii1 != cii ));
132         assert (!(cii != ii1 ));
133     }
134 #endif
135 
136   return 0;
137 }
138