1 // { dg-do run { target c++23 } }
5 #if __cpp_lib_flat_map != 202207L
6 # error "Feature-test macro __cpp_lib_flat_map has wrong value in <flat_map>"
12 #include <testsuite_allocator.h>
13 #include <testsuite_hooks.h>
15 template<template<typename
> class KeyContainer
, template<typename
> class MappedContainer
>
19 std::flat_map
<int, int, std::less
<int>, KeyContainer
<int>, MappedContainer
<int>> m
;
20 static_assert( std::ranges::random_access_range
<decltype(m
)> );
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
++)
38 m
.insert(m
.end(), {j
,-j
});
40 m
.insert(m
.end(), {j
,-j
});
42 m
.insert(m
.end(), {j
,-j
});
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}) );
56 VERIFY( m
.size() == 1 );
57 m
.insert({{11,2},{12,3},{11,4}});
58 VERIFY( m
.size() == 3 );
63 VERIFY( m
[20] == 42 );
64 VERIFY( m
.end()[-1] == std::pair(20,42) );
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
);
76 VERIFY( r
.first
->first
== 2 && r
.first
->second
== -2 && r
.second
);
78 VERIFY( r
.first
->first
== 3 && r
.first
->second
== -3 && r
.second
);
80 VERIFY( r
.first
->first
== 1 && r
.first
->second
== -1 && !r
.second
);
82 VERIFY( r
.first
->first
== 2 && r
.first
->second
== -2 && !r
.second
);
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 );
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}});
106 VERIFY( it
== m
.end() );
108 VERIFY( it
->second
== 10 );
116 m
.erase(m
.end()-2, m
.end());
117 VERIFY( std::ranges::equal(m
, (std::pair
<int, int>[]){{3, 4}}) );
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}}) );
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
>;
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 );
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}) );
170 test01
<std::vector
, std::vector
>();
171 test01
<std::deque
, std::deque
>();
172 test01
<std::vector
, std::deque
>();
173 test01
<std::deque
, std::vector
>();