1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <unordered_map>
11 
12 // template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>,
13 //           class Alloc = allocator<pair<const Key, T>>>
14 // class unordered_map
15 
16 // iterator       begin()        {return __table_.begin();}
17 // iterator       end()          {return __table_.end();}
18 // const_iterator begin()  const {return __table_.begin();}
19 // const_iterator end()    const {return __table_.end();}
20 // const_iterator cbegin() const {return __table_.begin();}
21 // const_iterator cend()   const {return __table_.end();}
22 
23 #include <unordered_map>
24 #include <string>
25 #include <cassert>
26 #include <cstddef>
27 
28 #include "test_macros.h"
29 #include "min_allocator.h"
30 
main()31 int main()
32 {
33     {
34         typedef std::unordered_map<int, std::string> C;
35         typedef std::pair<int, std::string> P;
36         P a[] =
37         {
38             P(1, "one"),
39             P(2, "two"),
40             P(3, "three"),
41             P(4, "four"),
42             P(1, "four"),
43             P(2, "four"),
44         };
45         C c(a, a + sizeof(a)/sizeof(a[0]));
46         assert(c.bucket_count() >= 5);
47         assert(c.size() == 4);
48         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
49         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
50         C::iterator i;
51     }
52     {
53         typedef std::unordered_map<int, std::string> C;
54         typedef std::pair<int, std::string> P;
55         P a[] =
56         {
57             P(1, "one"),
58             P(2, "two"),
59             P(3, "three"),
60             P(4, "four"),
61             P(1, "four"),
62             P(2, "four"),
63         };
64         const C c(a, a + sizeof(a)/sizeof(a[0]));
65         assert(c.bucket_count() >= 5);
66         assert(c.size() == 4);
67         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
68         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
69         C::const_iterator i;
70     }
71 #if TEST_STD_VER >= 11
72     {
73         typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
74                             min_allocator<std::pair<const int, std::string>>> C;
75         typedef std::pair<int, std::string> P;
76         P a[] =
77         {
78             P(1, "one"),
79             P(2, "two"),
80             P(3, "three"),
81             P(4, "four"),
82             P(1, "four"),
83             P(2, "four"),
84         };
85         C c(a, a + sizeof(a)/sizeof(a[0]));
86         assert(c.bucket_count() >= 5);
87         assert(c.size() == 4);
88         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
89         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
90         C::iterator i;
91     }
92     {
93         typedef std::unordered_map<int, std::string, std::hash<int>, std::equal_to<int>,
94                             min_allocator<std::pair<const int, std::string>>> C;
95         typedef std::pair<int, std::string> P;
96         P a[] =
97         {
98             P(1, "one"),
99             P(2, "two"),
100             P(3, "three"),
101             P(4, "four"),
102             P(1, "four"),
103             P(2, "four"),
104         };
105         const C c(a, a + sizeof(a)/sizeof(a[0]));
106         assert(c.bucket_count() >= 5);
107         assert(c.size() == 4);
108         assert(static_cast<std::size_t>(std::distance(c.begin(), c.end())) == c.size());
109         assert(static_cast<std::size_t>(std::distance(c.cbegin(), c.cend())) == c.size());
110         C::const_iterator i;
111     }
112 #endif
113 #if TEST_STD_VER > 11
114     { // N3644 testing
115         typedef std::unordered_map<int,double> C;
116         C::iterator ii1{}, ii2{};
117         C::iterator ii4 = ii1;
118         C::const_iterator cii{};
119         assert ( ii1 == ii2 );
120         assert ( ii1 == ii4 );
121 
122         assert (!(ii1 != ii2 ));
123 
124         assert ( (ii1 == cii ));
125         assert ( (cii == ii1 ));
126         assert (!(ii1 != cii ));
127         assert (!(cii != ii1 ));
128     }
129 #endif
130 }
131