| 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 <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 assignments (assigning an {empty/one-element/mid-sized/long range} to an |
| 23 | // {empty/one-element/full} container); |
| 24 | // - an exception is thrown when allocating new elements. |
| 25 | constexpr bool test() { |
| 26 | static_assert(test_constraints_assign_range<std::vector, bool, char>()); |
| 27 | |
| 28 | for_all_iterators_and_allocators<bool, const int*>([]<class Iter, class Sent, class Alloc>() { |
| 29 | test_sequence_assign_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 assignment -- make sure to test both cases. |
| 36 | { // Ensure reallocation happens. Note that `vector<bool>` typically reserves a lot of capacity. |
| 37 | constexpr int N = 255; |
| 38 | bool in[N] = {}; |
| 39 | std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0}; |
| 40 | assert(v.capacity() < v.size() + std::ranges::size(in)); |
| 41 | |
| 42 | v.assign_range(in); |
| 43 | assert(std::ranges::equal(v, in)); |
| 44 | } |
| 45 | |
| 46 | { // Ensure no reallocation happens. |
| 47 | bool in[] = {1, 1, 0, 1, 1}; |
| 48 | std::vector<bool> v = {0, 0, 0, 1, 1, 0, 0, 0}; |
| 49 | |
| 50 | v.assign_range(in); |
| 51 | assert(std::ranges::equal(v, in)); |
| 52 | } |
| 53 | |
| 54 | { // Ensure input-only sized ranges are accepted. |
| 55 | using input_iter = cpp20_input_iterator<const bool*>; |
| 56 | const bool in[]{true, true, false, true}; |
| 57 | std::vector<bool> v; |
| 58 | v.assign_range(std::views::counted(input_iter{std::ranges::begin(in)}, std::ranges::ssize(in))); |
| 59 | assert(std::ranges::equal(v, std::vector<bool>{true, true, false, true})); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | #ifndef TEST_HAS_NO_LOCALIZATION |
| 67 | void test_counted_istream_view() { |
| 68 | std::istringstream is{"1 1 0 1" }; |
| 69 | auto vals = std::views::istream<bool>(is); |
| 70 | std::vector<bool> v; |
| 71 | v.assign_range(std::views::counted(vals.begin(), 3)); |
| 72 | assert(v == (std::vector{true, true, false})); |
| 73 | } |
| 74 | #endif |
| 75 | |
| 76 | int main(int, char**) { |
| 77 | test(); |
| 78 | static_assert(test()); |
| 79 | |
| 80 | // Note: `test_assign_range_exception_safety_throwing_copy` doesn't apply because copying booleans cannot throw. |
| 81 | test_assign_range_exception_safety_throwing_allocator<std::vector, bool>(); |
| 82 | |
| 83 | #ifndef TEST_HAS_NO_LOCALIZATION |
| 84 | test_counted_istream_view(); |
| 85 | #endif |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |