]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/23_containers/unordered_multimap/insert/hint.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 23_containers / unordered_multimap / insert / hint.cc
1 // Copyright (C) 2013-2016 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++11" }
19
20 // Insert with hint, specific to this library implementation.
21
22 #include <iterator>
23 #include <unordered_map>
24 #include <testsuite_hooks.h>
25
26 void test01()
27 {
28 bool test __attribute__((unused)) = true;
29
30 typedef std::unordered_multimap<int, int> Map;
31 typedef typename Map::value_type Pair;
32
33 Map m;
34
35 auto it1 = m.insert(Pair(0, 0));
36 VERIFY( it1 != m.end() );
37 VERIFY( it1->first == 0 );
38 VERIFY( it1->second == 0 );
39
40 auto it2 = m.insert(it1, Pair(0, 2));
41 VERIFY( it2 != m.end() );
42 VERIFY( it2 != it1 );
43 VERIFY( it2->second == 2 );
44 VERIFY( it2 == std::next(it1) );
45
46 Pair p(0, 1);
47 it2 = m.insert(it1, p);
48 VERIFY( it2 == std::next(it1) );
49 }
50
51 struct hasher
52 {
53 std::size_t operator()(int val) const
54 { return val / 2; }
55 };
56
57 void test02()
58 {
59 bool test __attribute__((unused)) = true;
60
61 typedef std::unordered_multimap<int, int, hasher> Map;
62 typedef typename Map::value_type Pair;
63
64 Map m;
65
66 auto it1 = m.insert(Pair(0, 0));
67 auto it2 = m.insert(it1, Pair(1, 0));
68 VERIFY( m.bucket(it1->first) == m.bucket(it2->first) );
69 VERIFY( m.bucket_size(m.bucket(it1->first)) == 2 );
70
71 auto it3 = m.insert(it2, Pair(2, 0));
72 VERIFY( m.bucket(it3->first) != m.bucket(it2->first) );
73 VERIFY( m.bucket_size(m.bucket(it3->first)) == 1 );
74
75 auto it4 = m.insert(it1, Pair(0, 1));
76 VERIFY( it4 == std::next(it1) );
77 VERIFY( m.bucket_size(m.bucket(it1->first)) == 3 );
78 VERIFY( m.bucket_size(m.bucket(it3->first)) == 1 );
79
80 Pair p(1, 1);
81 it4 = m.insert(it2, p);
82 VERIFY( it4 == std::next(it2) );
83 VERIFY( m.bucket_size(m.bucket(it1->first)) == 4 );
84 auto range = m.equal_range(0);
85 VERIFY( std::distance(range.first, range.second) == 2 );
86 range = m.equal_range(1);
87 VERIFY( std::distance(range.first, range.second) == 2 );
88 }
89
90 void test03()
91 {
92 bool test __attribute__((unused)) = true;
93
94 typedef std::unordered_multimap<int, int> Map;
95 typedef typename Map::value_type Pair;
96
97 Map m;
98
99 auto it1 = m.insert(Pair(0, 0));
100 VERIFY( it1 != m.end() );
101 VERIFY( it1->first == 0 );
102 VERIFY( it1->second == 0 );
103
104 auto it2 = m.emplace_hint(it1, std::piecewise_construct,
105 std::make_tuple(0),
106 std::make_tuple(2));
107 VERIFY( it2 != m.end() );
108 VERIFY( it2 != it1 );
109 VERIFY( it2->second == 2 );
110 VERIFY( it2 == std::next(it1) );
111
112 Pair p(0, 1);
113 it2 = m.emplace_hint(it1, p);
114 VERIFY( it2 == std::next(it1) );
115 }
116
117 int main()
118 {
119 test01();
120 test02();
121 test03();
122 return 0;
123 }