xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/append_range.pass.cpp (revision 346a29908e0a0401073169ea94c17be72a9c83db)
117bbb224Svarconst //===----------------------------------------------------------------------===//
217bbb224Svarconst //
317bbb224Svarconst // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
417bbb224Svarconst // See https://llvm.org/LICENSE.txt for license information.
517bbb224Svarconst // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
617bbb224Svarconst //
717bbb224Svarconst //===----------------------------------------------------------------------===//
817bbb224Svarconst 
917bbb224Svarconst // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
1017bbb224Svarconst // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
1117bbb224Svarconst 
1217bbb224Svarconst // template<container-compatible-range<bool> R>
1317bbb224Svarconst //   constexpr void append_range(R&& rg); // C++23
1417bbb224Svarconst 
1517bbb224Svarconst #include <vector>
1617bbb224Svarconst 
1717bbb224Svarconst #include "../insert_range_sequence_containers.h"
1817bbb224Svarconst #include "test_macros.h"
1917bbb224Svarconst 
2017bbb224Svarconst // Tested cases:
2117bbb224Svarconst // - different kinds of insertions (appending an {empty/one-element/mid-sized/long range} into an
2217bbb224Svarconst //   {empty/one-element/full} container);
2317bbb224Svarconst // - an exception is thrown when allocating new elements.
test()2417bbb224Svarconst constexpr bool test() {
2517bbb224Svarconst   static_assert(test_constraints_append_range<std::vector, bool, char>());
2617bbb224Svarconst 
2717bbb224Svarconst   for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() {
28*346a2990SStephan T. Lavavej     test_sequence_append_range<std::vector<bool, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
2917bbb224Svarconst       LIBCPP_ASSERT(c.__invariants());
3017bbb224Svarconst       // `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
3117bbb224Svarconst     });
3217bbb224Svarconst   });
3317bbb224Svarconst 
3417bbb224Svarconst   { // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
3517bbb224Svarconst     { // Ensure reallocation happens.
3617bbb224Svarconst       constexpr int N = 255;
3717bbb224Svarconst       bool in[N] = {};
3817bbb224Svarconst       std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0};
3917bbb224Svarconst       auto initial = v;
4017bbb224Svarconst       assert(v.capacity() < v.size() + std::ranges::size(in));
4117bbb224Svarconst 
4217bbb224Svarconst       v.append_range(in);
4317bbb224Svarconst       // Because `in` is very large (it has to be to exceed the large capacity that `vector<bool>` allocates), it is
4417bbb224Svarconst       // impractical to have the expected value as a literal.
4517bbb224Svarconst       assert(v.size() == initial.size() + N);
4617bbb224Svarconst       assert(std::ranges::equal(v.begin(), v.begin() + initial.size(), initial.begin(), initial.end()));
4717bbb224Svarconst       assert(std::ranges::equal(v.begin() + initial.size(), v.end(), std::ranges::begin(in), std::ranges::end(in)));
4817bbb224Svarconst     }
4917bbb224Svarconst 
5017bbb224Svarconst     { // Ensure no reallocation happens.
5117bbb224Svarconst       bool in[] = {1, 1, 1, 1, 0, 0, 1, 1, 1, 1};
5217bbb224Svarconst       std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0};
5317bbb224Svarconst       v.reserve(v.size() + std::ranges::size(in));
5417bbb224Svarconst       assert(v.capacity() >= v.size() + std::ranges::size(in));
5517bbb224Svarconst 
5617bbb224Svarconst       v.append_range(in);
5717bbb224Svarconst       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}));
5817bbb224Svarconst     }
5917bbb224Svarconst   }
6017bbb224Svarconst 
6117bbb224Svarconst   return true;
6217bbb224Svarconst }
6317bbb224Svarconst 
main(int,char **)6417bbb224Svarconst int main(int, char**) {
6517bbb224Svarconst   test();
6617bbb224Svarconst   static_assert(test());
6717bbb224Svarconst 
6817bbb224Svarconst   // Note: `test_append_range_exception_safety_throwing_copy` doesn't apply because copying booleans cannot throw.
6917bbb224Svarconst   test_append_range_exception_safety_throwing_allocator<std::vector, bool>();
7017bbb224Svarconst 
7117bbb224Svarconst   return 0;
7217bbb224Svarconst }
73