From c06daba33b4ad766da571855708e724618bd99c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Michal?= Date: Wed, 7 Jan 2026 23:20:54 +0100 Subject: [PATCH 01/48] Test that hints given to `flat_multiset::emplace_hint` and `insert` are respected. --- tests/std/tests/P1222R4_flat_set/test.cpp | 62 +++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/std/tests/P1222R4_flat_set/test.cpp b/tests/std/tests/P1222R4_flat_set/test.cpp index 40f7786a12..75fe53b793 100644 --- a/tests/std/tests/P1222R4_flat_set/test.cpp +++ b/tests/std/tests/P1222R4_flat_set/test.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -493,6 +494,65 @@ void test_insert_2() { } } +// Test that hint to emplace/insert is respected, when possible; check returned iterator +template +void test_insert_hint_is_respected() { + using lt = std::less; + + { + flat_multiset a{-1, -1, 1, 1}; + bool problem_seen = false; + auto const assert_inserted_at_position = [&a, &problem_seen]( + const int expected_index, const auto insert_position) { + const auto expected_position = a.begin() + expected_index; + if (expected_position != insert_position) { + println("Wrong insert position: expected {}, actual {}\nContainer after insert {}", expected_index, + insert_position - a.begin(), a); + problem_seen = true; + } + }; + + // hint is greater + assert_all_requirements_and_equals(a, {-1, -1, 1, 1}); + assert_inserted_at_position(2, a.insert(a.end(), 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 1, 1}); + assert_inserted_at_position(3, a.insert(a.find(1), 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 1, 1}); + assert_inserted_at_position(4, a.insert(a.find(1), 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 1, 1}); + assert_inserted_at_position(5, a.insert(a.upper_bound(0), 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 1, 1}); + + // hint is correct + assert_inserted_at_position(5, a.insert(a.upper_bound(0) - 1, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(6, a.insert(a.upper_bound(0) - 1, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(6, a.insert(a.begin() + 6, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(4, a.insert(a.begin() + 4, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(2, a.insert(a.begin() + 2, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(2, a.emplace_hint(a.lower_bound(0), 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + + // hint is less + assert_inserted_at_position(2, a.emplace_hint(a.lower_bound(0) - 1, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(2, a.insert(a.begin() + 1, 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + assert_inserted_at_position(2, a.insert(a.begin(), 0)); + assert_all_requirements_and_equals(a, {-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1}); + + assert(!problem_seen); + + assert(13 == erase_if(a, [](const auto value) { return value == 0; })); + assert_all_requirements_and_equals(a, {-1, -1, 1, 1}); + } +} + + struct key_comparer { const auto& extract_key(const auto& obj) const { if constexpr (requires { obj.key; }) { @@ -1167,6 +1227,8 @@ int main() { test_insert_1>(); test_insert_2>(); test_insert_2>(); + test_insert_hint_is_respected>(); + test_insert_hint_is_respected>(); test_insert_transparent(); test_insert_using_invalid_hint(); test_insert_upper_bound(); From 08f6af53f4625f8a903a0622c99d7c28bbbeda6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vojt=C4=9Bch=20Michal?= Date: Wed, 7 Jan 2026 23:21:27 +0100 Subject: [PATCH 02/48] Test that hints given to `flat_multimap::emplace_hint` and `insert` are respected. --- tests/std/tests/P0429R9_flat_map/test.cpp | 80 +++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/std/tests/P0429R9_flat_map/test.cpp b/tests/std/tests/P0429R9_flat_map/test.cpp index cc5002dca0..a2db0a691e 100644 --- a/tests/std/tests/P0429R9_flat_map/test.cpp +++ b/tests/std/tests/P0429R9_flat_map/test.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,16 @@ bool check_value_content(const T& obj, const typename T::mapped_container_type& return ranges::equal(obj.values(), expected); } +template +bool assert_check_content(const T& obj, const type_identity_t& expected) { + if (!ranges::equal(obj, expected)) { + println(stderr, "Unexpected content!\nExpected {}", expected); + println(stderr, "Actual {}", obj); + return false; + } + return true; +} + enum class subrange_type : bool { equal, permutation, @@ -794,6 +805,71 @@ void test_lookup_call_on_temporaries() { } } +// Test that hint to emplace/insert is respected, when possible; check returned iterator +template