d: Merge dmd, druntime c7902293d7, phobos 03aeafd20
[gcc.git] / libstdc++-v3 / testsuite / 23_containers / flat_map / 1.cc
blob111cafaa6ba1de79db7f1ff439108cd5784ca078
1 // { dg-do run { target c++23 } }
3 #include <flat_map>
5 #if __cpp_lib_flat_map != 202207L
6 # error "Feature-test macro __cpp_lib_flat_map has wrong value in <flat_map>"
7 #endif
9 #include <deque>
10 #include <ranges>
11 #include <vector>
12 #include <testsuite_allocator.h>
13 #include <testsuite_hooks.h>
15 template<template<typename> class KeyContainer, template<typename> class MappedContainer>
16 void
17 test01()
19 std::flat_map<int, int, std::less<int>, KeyContainer<int>, MappedContainer<int>> m;
20 static_assert( std::ranges::random_access_range<decltype(m)> );
22 m.insert({1,-1});
23 m.insert({2,-2});
24 m.insert({3,-3});
25 m.insert({1,-4});
26 m.insert({2,-5});
27 m.insert({3,-6});
28 m.insert({0, 0});
29 VERIFY( m.size() == 4 );
30 VERIFY( std::ranges::equal(m.keys(), (int[]){0, 1, 2, 3}) );
31 VERIFY( std::ranges::equal(m.values(), (int[]){0, -1, -2, -3}) );
33 for (int i = 0; i < 4; i++)
35 m.clear();
37 int j = i;
38 m.insert(m.end(), {j,-j});
39 j = (j + 1) % 4;
40 m.insert(m.end(), {j,-j});
41 j = (j + 1) % 4;
42 m.insert(m.end(), {j,-j});
43 j = (j + 1) % 4;
44 m.insert(m.end(), {j,-j});
46 m.insert(m.begin() + i, {1,-4});
47 m.insert(m.begin() + i, {2,-5});
48 m.insert(m.begin() + i, {3,-6});
49 m.insert(m.begin() + i, {0,-7});
50 VERIFY( std::ranges::equal(m.keys(), (int[]){0, 1, 2, 3}) );
51 VERIFY( std::ranges::equal(m.values(), (int[]){0, -1, -2, -3}) );
54 m.clear();
55 m = {{10,0},{10,1}};
56 VERIFY( m.size() == 1 );
57 m.insert({{11,2},{12,3},{11,4}});
58 VERIFY( m.size() == 3 );
59 VERIFY( m[10] == 0 );
60 VERIFY( m[11] == 2 );
61 VERIFY( m[12] == 3 );
62 m[20] = 42;
63 VERIFY( m[20] == 42 );
64 VERIFY( m.end()[-1] == std::pair(20,42) );
67 void
68 test02()
70 std::flat_map<int, int, std::greater<int>> m;
71 static_assert( std::ranges::random_access_range<decltype(m)> );
73 auto r = m.insert({1,-1});
74 VERIFY( r.first->first == 1 && r.first->second == -1 && r.second );
75 r = m.insert({2,-2});
76 VERIFY( r.first->first == 2 && r.first->second == -2 && r.second );
77 r = m.insert({3,-3});
78 VERIFY( r.first->first == 3 && r.first->second == -3 && r.second );
79 r = m.insert({1,-4});
80 VERIFY( r.first->first == 1 && r.first->second == -1 && !r.second );
81 r = m.insert({2,-5});
82 VERIFY( r.first->first == 2 && r.first->second == -2 && !r.second );
83 r = m.insert({3,-6});
84 VERIFY( r.first->first == 3 && r.first->second == -3 && !r.second );
85 r = m.insert_or_assign(0, 0);
86 VERIFY( r.first->first == 0 && r.first->second == 0 && r.second );
87 r = m.insert_or_assign(0, 1);
88 VERIFY( r.first->first == 0 && r.first->second == 1 && !r.second );
89 VERIFY( *m.insert_or_assign(m.end(), 0, 2) == std::pair(0, 2) );
90 VERIFY( m.size() == 4 );
91 VERIFY( std::ranges::equal(m.keys(), (int[]){3, 2, 1, 0}) );
92 VERIFY( std::ranges::equal(m.values(), (int[]){-3, -2, -1, 2}) );
94 VERIFY( m.contains(3) && !m.contains(7) );
95 VERIFY( m.count(3) == 1 );
98 void
99 test03()
101 std::flat_map<int, int> m;
102 m = {std::pair(1, 2), {3, 4}, {5, 6}};
103 m.insert({std::pair(7, 8), {9, 10}});
105 auto it = m.find(0);
106 VERIFY( it == m.end() );
107 it = m.find(9);
108 VERIFY( it->second == 10 );
110 const auto n = m;
111 VERIFY( m == m );
112 VERIFY( m == n );
114 m.erase(m.begin());
115 m.erase(5);
116 m.erase(m.end()-2, m.end());
117 VERIFY( std::ranges::equal(m, (std::pair<int, int>[]){{3, 4}}) );
118 VERIFY( m != n );
119 VERIFY( n < m );
121 m = n;
122 erase_if(m, [](const auto& x) { auto [k, v] = x; return k < 5 || k > 5; });
123 VERIFY( std::ranges::equal(m, (std::pair<int, int>[]){{5, 6}}) );
126 void
127 test04()
129 using vector = std::vector<int, __gnu_test::uneq_allocator<int>>;
130 vector v1 = {1, 2, 3};
131 vector v2 = {4, 5, 6};
132 __gnu_test::uneq_allocator<int> alloc(42);
134 using flat_map = std::flat_map<int, int, std::less<int>, vector, vector>;
135 flat_map m1(alloc);
136 VERIFY( m1.keys().get_allocator().get_personality() == 42 );
137 VERIFY( m1.values().get_allocator().get_personality() == 42 );
139 flat_map m2(v1, v2, alloc);
140 VERIFY( m2.keys().get_allocator().get_personality() == 42 );
141 VERIFY( m2.values().get_allocator().get_personality() == 42 );
143 flat_map m3(std::sorted_unique_t{}, v1, v2, alloc);
144 VERIFY( m2.keys().get_allocator().get_personality() == 42 );
145 VERIFY( m2.values().get_allocator().get_personality() == 42 );
147 alloc = __gnu_test::uneq_allocator<int>(43);
148 flat_map m4(m3, alloc);
149 VERIFY( m4.keys().get_allocator().get_personality() == 43 );
150 VERIFY( m4.values().get_allocator().get_personality() == 43 );
152 alloc = __gnu_test::uneq_allocator<int>(44);
153 flat_map m5(std::move(m4), alloc);
154 VERIFY( m5.keys().get_allocator().get_personality() == 44 );
155 VERIFY( m5.values().get_allocator().get_personality() == 44 );
158 void
159 test05()
161 std::vector<std::pair<int, int>> v = {{2, -2}, {3,-3}, {1,-1}, {5,-5}, {4,-4}};
162 std::flat_map<int, int> m = {std::from_range, v};
163 VERIFY( std::ranges::equal(m | std::views::keys, (int[]){1, 2, 3, 4, 5}) );
164 VERIFY( std::ranges::equal(m | std::views::values, (int[]){-1, -2, -3, -4, -5}) );
168 main()
170 test01<std::vector, std::vector>();
171 test01<std::deque, std::deque>();
172 test01<std::vector, std::deque>();
173 test01<std::deque, std::vector>();
174 test02();
175 test03();
176 test04();
177 test05();