xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/insert_range.pass.cpp (revision 733a98db4a264f474564cc2064b8862dedd8458f)
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 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
11 
12 // template<container-compatible-range<bool> R>
13 //   constexpr iterator insert_range(const_iterator position, R&& rg); // C++23
14 
15 #include <sstream>
16 #include <vector>
17 
18 #include "../insert_range_sequence_containers.h"
19 #include "test_macros.h"
20 
21 // Tested cases:
22 // - different kinds of insertions (inserting an {empty/one-element/mid-sized/long range} into an
23 //   {empty/one-element/full} container at the {beginning/middle/end});
24 // - an exception is thrown when allocating new elements.
25 constexpr bool test() {
26   static_assert(test_constraints_insert_range<std::vector, bool, char>());
27 
28   for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() {
29     test_sequence_insert_range<std::vector<bool, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
30       LIBCPP_ASSERT(c.__invariants());
31       // `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
32     });
33   });
34 
35   { // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
36     { // Ensure reallocation happens.
37       constexpr int N = 255;
38       bool in[N] = {};
39       std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0};
40       auto initial = v;
41       assert(v.capacity() < v.size() + std::ranges::size(in));
42 
43       v.insert_range(v.end(), in);
44       // Because `in` is very large (it has to be to exceed the large capacity that `vector<bool>` allocates), it is
45       // impractical to have the expected value as a literal.
46       assert(v.size() == initial.size() + N);
47       assert(std::ranges::equal(v.begin(), v.begin() + initial.size(), initial.begin(), initial.end()));
48       assert(std::ranges::equal(v.begin() + initial.size(), v.end(), std::ranges::begin(in), std::ranges::end(in)));
49     }
50 
51     { // Ensure no reallocation happens.
52       bool in[] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
53       std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0};
54       v.reserve(v.size() + std::ranges::size(in));
55       assert(v.capacity() >= v.size() + std::ranges::size(in));
56 
57       v.insert_range(v.end(), in);
58       assert(std::ranges::equal(v, std::vector<bool>{0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1}));
59     }
60 
61     { // Ensure input-only sized ranges are accepted.
62       using input_iter = cpp20_input_iterator<const bool*>;
63       const bool in[]{true, true, false, true};
64       std::vector<bool> v{true, false};
65       v.insert_range(v.begin(), std::views::counted(input_iter{std::ranges::begin(in)}, std::ranges::ssize(in)));
66       assert(std::ranges::equal(v, std::vector<bool>{true, true, false, true, true, false}));
67     }
68   }
69 
70   return true;
71 }
72 
73 #ifndef TEST_HAS_NO_LOCALIZATION
74 void test_counted_istream_view() {
75   std::istringstream is{"1 1 0 1"};
76   auto vals = std::views::istream<bool>(is);
77   std::vector<bool> v;
78   v.insert_range(v.end(), std::views::counted(vals.begin(), 3));
79   assert(v == (std::vector{true, true, false}));
80 }
81 #endif
82 
83 int main(int, char**) {
84   test();
85   static_assert(test());
86 
87   // Note: `test_insert_range_exception_safety_throwing_copy` doesn't apply because copying booleans cannot throw.
88   test_insert_range_exception_safety_throwing_allocator<std::vector, bool>();
89 
90 #ifndef TEST_HAS_NO_LOCALIZATION
91   test_counted_istream_view();
92 #endif
93 
94   return 0;
95 }
96