Skip to content

Commit f467af6

Browse files
authored
[libc++][test] add test coverage for flat_map::emplace_hint (#113773)
Not all the code path has been exercised by the tests for `flat_map::emplace_hint` Adding more test coverage. At the same time, adding more test cases for `flat_map::emplace`
1 parent 01a103b commit f467af6

File tree

2 files changed

+169
-25
lines changed

2 files changed

+169
-25
lines changed

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace.pass.cpp

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,81 @@ static_assert(!CanEmplace<Map, Emplaceable>);
3939
static_assert(!CanEmplace<Map, int, double>);
4040

4141
template <class KeyContainer, class ValueContainer>
42-
void test_simple() {
42+
void test() {
4343
using Key = typename KeyContainer::value_type;
4444
using Value = typename ValueContainer::value_type;
4545
using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;
4646
using R = std::pair<typename M::iterator, bool>;
47-
M m;
48-
ASSERT_SAME_TYPE(decltype(m.emplace()), R);
49-
R r = m.emplace(typename M::value_type(2, 3.5));
50-
assert(r.second);
51-
assert(r.first == m.begin());
52-
assert(m.size() == 1);
53-
assert(m.begin()->first == 2);
54-
assert(m.begin()->second == 3.5);
47+
{
48+
// was empty
49+
M m;
50+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 3.5));
51+
assert(r.second);
52+
assert(r.first == m.begin());
53+
assert(m.size() == 1);
54+
assert(r.first->first == 2);
55+
assert(r.first->second == 3.5);
56+
}
57+
{
58+
// key does not exist and inserted at the begin
59+
M m = {{3, 4.0}, {5, 3.0}, {6, 1.0}, {7, 0.0}};
60+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));
61+
assert(r.second);
62+
assert(r.first == m.begin());
63+
assert(m.size() == 5);
64+
assert(r.first->first == 2);
65+
assert(r.first->second == 2.0);
66+
}
67+
{
68+
// key does not exist and inserted in the middle
69+
M m = {{0, 4.0}, {1, 3.0}, {3, 1.0}, {4, 0.0}};
70+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));
71+
assert(r.second);
72+
assert(r.first == m.begin() + 2);
73+
assert(m.size() == 5);
74+
assert(r.first->first == 2);
75+
assert(r.first->second == 2.0);
76+
}
77+
{
78+
// key does not exist and inserted at the end
79+
M m = {{0, 4.0}, {1, 3.0}};
80+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));
81+
assert(r.second);
82+
assert(r.first == m.begin() + 2);
83+
assert(m.size() == 3);
84+
assert(r.first->first == 2);
85+
assert(r.first->second == 2.0);
86+
}
87+
{
88+
// key already exists and original at the begin
89+
M m = {{2, 4.0}, {3, 3.0}, {5, 1.0}, {6, 0.0}};
90+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));
91+
assert(!r.second);
92+
assert(r.first == m.begin());
93+
assert(m.size() == 4);
94+
assert(r.first->first == 2);
95+
assert(r.first->second == 4.0);
96+
}
97+
{
98+
// key already exists and original in the middle
99+
M m = {{0, 4.0}, {2, 3.0}, {3, 1.0}, {4, 0.0}};
100+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));
101+
assert(!r.second);
102+
assert(r.first == m.begin() + 1);
103+
assert(m.size() == 4);
104+
assert(r.first->first == 2);
105+
assert(r.first->second == 3.0);
106+
}
107+
{
108+
// key already exists and original at the end
109+
M m = {{0, 4.0}, {1, 3.0}, {2, 1.0}};
110+
std::same_as<R> decltype(auto) r = m.emplace(typename M::value_type(2, 2.0));
111+
assert(!r.second);
112+
assert(r.first == m.begin() + 2);
113+
assert(m.size() == 3);
114+
assert(r.first->first == 2);
115+
assert(r.first->second == 1.0);
116+
}
55117
}
56118

57119
template <class KeyContainer, class ValueContainer>
@@ -82,10 +144,10 @@ void test_emplaceable() {
82144
}
83145

84146
int main(int, char**) {
85-
test_simple<std::vector<int>, std::vector<double>>();
86-
test_simple<std::deque<int>, std::vector<double>>();
87-
test_simple<MinSequenceContainer<int>, MinSequenceContainer<double>>();
88-
test_simple<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
147+
test<std::vector<int>, std::vector<double>>();
148+
test<std::deque<int>, std::vector<double>>();
149+
test<MinSequenceContainer<int>, MinSequenceContainer<double>>();
150+
test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
89151

90152
test_emplaceable<std::vector<int>, std::vector<Emplaceable>>();
91153
test_emplaceable<std::deque<int>, std::vector<Emplaceable>>();

libcxx/test/std/containers/container.adaptors/flat.map/flat.map.modifiers/emplace_hint.pass.cpp

Lines changed: 94 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,100 @@ static_assert(!CanEmplaceHint<Map, int, double>);
4242
#endif
4343

4444
template <class KeyContainer, class ValueContainer>
45-
void test_simple() {
45+
void test() {
4646
using Key = typename KeyContainer::value_type;
4747
using Value = typename ValueContainer::value_type;
4848
using M = std::flat_map<Key, Value, std::less<Key>, KeyContainer, ValueContainer>;
4949
using R = M::iterator;
50-
M m;
51-
ASSERT_SAME_TYPE(decltype(m.emplace_hint(m.cbegin())), R);
52-
R r = m.emplace_hint(m.end(), typename M::value_type(2, 3.5));
53-
assert(r == m.begin());
54-
assert(m.size() == 1);
55-
assert(m.begin()->first == 2);
56-
assert(m.begin()->second == 3.5);
50+
{
51+
// was empty
52+
M m;
53+
std::same_as<R> decltype(auto) r = m.emplace_hint(m.end(), typename M::value_type(2, 3.5));
54+
assert(r == m.begin());
55+
assert(m.size() == 1);
56+
assert(r->first == 2);
57+
assert(r->second == 3.5);
58+
}
59+
{
60+
// hints correct at the begin
61+
M m = {{3, 3.0}, {4, 4.0}};
62+
auto hint = m.begin();
63+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
64+
assert(r == m.begin());
65+
assert(m.size() == 3);
66+
assert(r->first == 2);
67+
assert(r->second == 2.0);
68+
}
69+
{
70+
// hints correct in the middle
71+
M m = {{0, 0.0}, {1, 1.0}, {3, 3.0}, {4, 4.0}};
72+
auto hint = m.begin() + 2;
73+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
74+
assert(r == m.begin() + 2);
75+
assert(m.size() == 5);
76+
assert(r->first == 2);
77+
assert(r->second == 2.0);
78+
}
79+
{
80+
// hints correct at the end
81+
M m = {{0, 0.0}, {1, 1.0}};
82+
auto hint = m.end();
83+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
84+
assert(r == m.begin() + 2);
85+
assert(m.size() == 3);
86+
assert(r->first == 2);
87+
assert(r->second == 2.0);
88+
}
89+
{
90+
// hints correct but key already exists
91+
M m = {{0, 0.0}, {1, 1.0}, {2, 1.9}, {3, 3.0}, {4, 4.0}};
92+
auto hint = m.begin() + 2;
93+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
94+
assert(r == m.begin() + 2);
95+
assert(m.size() == 5);
96+
assert(r->first == 2);
97+
assert(r->second == 1.9);
98+
}
99+
{
100+
// hints incorrectly at the begin
101+
M m = {{1, 1.0}, {4, 4.0}};
102+
auto hint = m.begin();
103+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
104+
assert(r == m.begin() + 1);
105+
assert(m.size() == 3);
106+
assert(r->first == 2);
107+
assert(r->second == 2.0);
108+
}
109+
{
110+
// hints incorrectly in the middle
111+
M m = {{0, 0.0}, {1, 1.0}, {3, 3.0}, {4, 4.0}};
112+
auto hint = m.begin() + 1;
113+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
114+
assert(r == m.begin() + 2);
115+
assert(m.size() == 5);
116+
assert(r->first == 2);
117+
assert(r->second == 2.0);
118+
}
119+
{
120+
// hints incorrectly at the end
121+
M m = {{0, 0.0}, {3, 3.0}};
122+
auto hint = m.end();
123+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
124+
assert(r == m.begin() + 1);
125+
assert(m.size() == 3);
126+
assert(r->first == 2);
127+
assert(r->second == 2.0);
128+
}
129+
{
130+
// hints incorrect and key already exists
131+
M m = {{0, 0.0}, {1, 1.0}, {2, 1.9}, {3, 3.0}, {4, 4.0}};
132+
auto hint = m.begin();
133+
std::same_as<R> decltype(auto) r = m.emplace_hint(hint, typename M::value_type(2, 2.0));
134+
assert(r == m.begin() + 2);
135+
assert(m.size() == 5);
136+
assert(r->first == 2);
137+
assert(r->second == 1.9);
138+
}
57139
}
58140

59141
template <class KeyContainer, class ValueContainer>
@@ -81,10 +163,10 @@ void test_emplaceable() {
81163
}
82164

83165
int main(int, char**) {
84-
test_simple<std::vector<int>, std::vector<double>>();
85-
test_simple<std::deque<int>, std::vector<double>>();
86-
test_simple<MinSequenceContainer<int>, MinSequenceContainer<double>>();
87-
test_simple<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
166+
test<std::vector<int>, std::vector<double>>();
167+
test<std::deque<int>, std::vector<double>>();
168+
test<MinSequenceContainer<int>, MinSequenceContainer<double>>();
169+
test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>();
88170

89171
test_emplaceable<std::vector<int>, std::vector<Emplaceable>>();
90172
test_emplaceable<std::deque<int>, std::vector<Emplaceable>>();

0 commit comments

Comments
 (0)