| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 10 | |
| 11 | // <flat_map> |
| 12 | |
| 13 | // template <class InputIterator> |
| 14 | // void insert(InputIterator first, InputIterator last); |
| 15 | |
| 16 | #include <algorithm> |
| 17 | #include <flat_map> |
| 18 | #include <cassert> |
| 19 | #include <functional> |
| 20 | #include <deque> |
| 21 | |
| 22 | #include "MinSequenceContainer.h" |
| 23 | #include "../helpers.h" |
| 24 | #include "test_macros.h" |
| 25 | #include "test_iterators.h" |
| 26 | #include "min_allocator.h" |
| 27 | |
| 28 | // test constraint InputIterator |
| 29 | template <class M, class... Args> |
| 30 | concept CanInsert = requires(M m, Args&&... args) { m.insert(std::forward<Args>(args)...); }; |
| 31 | |
| 32 | using Map = std::flat_map<int, int>; |
| 33 | using Pair = std::pair<int, int>; |
| 34 | |
| 35 | static_assert(CanInsert<Map, Pair*, Pair*>); |
| 36 | static_assert(CanInsert<Map, cpp17_input_iterator<Pair*>, cpp17_input_iterator<Pair*>>); |
| 37 | static_assert(!CanInsert<Map, int, int>); |
| 38 | static_assert(!CanInsert<Map, cpp20_input_iterator<Pair*>, cpp20_input_iterator<Pair*>>); |
| 39 | |
| 40 | template <class KeyContainer, class ValueContainer> |
| 41 | void test() { |
| 42 | using P = std::pair<int, double>; |
| 43 | using M = std::flat_map<int, double, std::less<int>, KeyContainer, ValueContainer>; |
| 44 | |
| 45 | P ar1[] = { |
| 46 | P(2, 1), |
| 47 | P(2, 1.5), |
| 48 | P(2, 2), |
| 49 | P(1, 1), |
| 50 | P(1, 1.5), |
| 51 | P(1, 2), |
| 52 | P(3, 1), |
| 53 | P(3, 1.5), |
| 54 | P(3, 2), |
| 55 | }; |
| 56 | P ar2[] = { |
| 57 | P(4, 1), |
| 58 | P(4, 1.5), |
| 59 | P(4, 2), |
| 60 | P(1, 1), |
| 61 | P(1, 1.5), |
| 62 | P(1, 2), |
| 63 | P(0, 1), |
| 64 | P(0, 1.5), |
| 65 | P(0, 2), |
| 66 | }; |
| 67 | |
| 68 | M m; |
| 69 | m.insert(cpp17_input_iterator<P*>(ar1), cpp17_input_iterator<P*>(ar1 + sizeof(ar1) / sizeof(ar1[0]))); |
| 70 | assert(m.size() == 3); |
| 71 | M expected{{1, 1}, {2, 1}, {3, 1}}; |
| 72 | assert(m == expected); |
| 73 | |
| 74 | m.insert(cpp17_input_iterator<P*>(ar2), cpp17_input_iterator<P*>(ar2 + sizeof(ar2) / sizeof(ar2[0]))); |
| 75 | assert(m.size() == 5); |
| 76 | M expected2{{0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 1}}; |
| 77 | assert(m == expected2); |
| 78 | } |
| 79 | |
| 80 | int main(int, char**) { |
| 81 | test<std::vector<int>, std::vector<double>>(); |
| 82 | test<std::deque<int>, std::vector<double>>(); |
| 83 | test<MinSequenceContainer<int>, MinSequenceContainer<double>>(); |
| 84 | test<std::vector<int, min_allocator<int>>, std::vector<double, min_allocator<double>>>(); |
| 85 | |
| 86 | { |
| 87 | auto insert_func = [](auto& m, const auto& newValues) { m.insert(newValues.begin(), newValues.end()); }; |
| 88 | test_insert_range_exception_guarantee(insert_function&: insert_func); |
| 89 | } |
| 90 | { |
| 91 | std::flat_map<int, int, std::less<int>, SillyReserveVector<int>, SillyReserveVector<int>> m{{1, 1}, {2, 2}}; |
| 92 | std::vector<std::pair<int, int>> v{{3, 3}, {4, 4}}; |
| 93 | m.insert(v.begin(), v.end()); |
| 94 | assert(std::ranges::equal(m, std::vector<std::pair<int, int>>{{1, 1}, {2, 2}, {3, 3}, {4, 4}})); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |