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 void assign_range(R&& rg); // C++23 14 15 #include <vector> 16 17 #include "../insert_range_sequence_containers.h" 18 #include "test_macros.h" 19 20 // Tested cases: 21 // - different kinds of assignments (assigning an {empty/one-element/mid-sized/long range} to an 22 // {empty/one-element/full} container); 23 // - an exception is thrown when allocating new elements. 24 constexpr bool test() { 25 static_assert(test_constraints_assign_range<std::vector, bool, char>()); 26 27 for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() { 28 test_sequence_assign_range<std::vector<bool, Alloc>, Iter, Sent>([](auto&& c) { 29 LIBCPP_ASSERT(c.__invariants()); 30 // `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`. 31 }); 32 }); 33 34 { // Vector may or may not need to reallocate because of the assignment -- make sure to test both cases. 35 { // Ensure reallocation happens. Note that `vector<bool>` typically reserves a lot of capacity. 36 constexpr int N = 255; 37 bool in[N] = {}; 38 std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0}; 39 assert(v.capacity() < v.size() + std::ranges::size(in)); 40 41 v.assign_range(in); 42 assert(std::ranges::equal(v, in)); 43 } 44 45 { // Ensure no reallocation happens. 46 bool in[] = {1, 1, 0, 1, 1}; 47 std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0}; 48 49 v.assign_range(in); 50 assert(std::ranges::equal(v, in)); 51 } 52 } 53 54 return true; 55 } 56 57 int main(int, char**) { 58 test(); 59 static_assert(test()); 60 61 // Note: `test_assign_range_exception_safety_throwing_copy` doesn't apply because copying booleans cannot throw. 62 test_assign_range_exception_safety_throwing_allocator<std::vector, bool>(); 63 64 return 0; 65 } 66