| 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_set> |
| 12 | |
| 13 | // void insert(initializer_list<value_type> il); |
| 14 | |
| 15 | #include <flat_set> |
| 16 | #include <cassert> |
| 17 | #include <functional> |
| 18 | #include <deque> |
| 19 | |
| 20 | #include "MinSequenceContainer.h" |
| 21 | #include "../helpers.h" |
| 22 | #include "test_macros.h" |
| 23 | #include "min_allocator.h" |
| 24 | |
| 25 | template <class KeyContainer> |
| 26 | void test_one() { |
| 27 | using Key = typename KeyContainer::value_type; |
| 28 | using M = std::flat_set<Key, std::less<Key>, KeyContainer>; |
| 29 | using V = typename M::value_type; |
| 30 | |
| 31 | { |
| 32 | M m = {1, 1, 1, 3, 3, 3}; |
| 33 | m.insert({ |
| 34 | 4, |
| 35 | 4, |
| 36 | 4, |
| 37 | 1, |
| 38 | 1, |
| 39 | 1, |
| 40 | 2, |
| 41 | 2, |
| 42 | 2, |
| 43 | }); |
| 44 | assert(m.size() == 4); |
| 45 | assert(std::distance(m.begin(), m.end()) == 4); |
| 46 | assert(*m.begin() == V(1)); |
| 47 | assert(*std::next(m.begin()) == V(2)); |
| 48 | assert(*std::next(m.begin(), 2) == V(3)); |
| 49 | assert(*std::next(m.begin(), 3) == V(4)); |
| 50 | } |
| 51 | { |
| 52 | // was empty |
| 53 | M m; |
| 54 | m.insert({ |
| 55 | 4, |
| 56 | 4, |
| 57 | 4, |
| 58 | 1, |
| 59 | 1, |
| 60 | 1, |
| 61 | 2, |
| 62 | 2, |
| 63 | 2, |
| 64 | }); |
| 65 | M expected = {1, 2, 4}; |
| 66 | assert(m == expected); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void test() { |
| 71 | test_one<std::vector<int>>(); |
| 72 | test_one<std::deque<int>>(); |
| 73 | test_one<MinSequenceContainer<int>>(); |
| 74 | test_one<std::vector<int, min_allocator<int>>>(); |
| 75 | } |
| 76 | |
| 77 | void test_exception() { |
| 78 | auto insert_func = [](auto& m, const auto& newValues) { |
| 79 | using FlatSet = std::decay_t<decltype(m)>; |
| 80 | using value_type = typename FlatSet::value_type; |
| 81 | std::initializer_list<value_type> il = {newValues[0]}; |
| 82 | m.insert(il); |
| 83 | }; |
| 84 | test_insert_range_exception_guarantee(insert_function&: insert_func); |
| 85 | } |
| 86 | |
| 87 | int main(int, char**) { |
| 88 | test(); |
| 89 | test_exception(); |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |