xref: /llvm-project/libcxx/test/std/containers/sequences/vector.bool/construct_from_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 
11 #include <sstream>
12 #include <vector>
13 
14 #include "../from_range_sequence_containers.h"
15 #include "test_macros.h"
16 
17 // template<container-compatible-range<T> R>
18 //   vector(from_range_t, R&& rg, const Allocator& = Allocator()); // C++23
19 
20 constexpr bool test() {
21   for_all_iterators_and_allocators<bool>([]<class Iter, class Sent, class Alloc>() {
22     test_vector_bool<Iter, Sent, Alloc>([]([[maybe_unused]] const auto& c) {
23       LIBCPP_ASSERT(c.__invariants());
24       // `is_contiguous_container_asan_correct` doesn't work on `vector<bool>`.
25     });
26   });
27 
28   { // Ensure input-only sized ranges are accepted.
29     using input_iter = cpp20_input_iterator<const bool*>;
30     const bool in[]{true, true, false, true};
31     std::vector v(std::from_range, std::views::counted(input_iter{std::ranges::begin(in)}, std::ranges::ssize(in)));
32     assert(std::ranges::equal(v, std::vector<bool>{true, true, false, true}));
33   }
34 
35   return true;
36 }
37 
38 #ifndef TEST_HAS_NO_LOCALIZATION
39 void test_counted_istream_view() {
40   std::istringstream is{"1 1 0 1"};
41   auto vals = std::views::istream<bool>(is);
42   std::vector v(std::from_range, std::views::counted(vals.begin(), 3));
43   assert(v == (std::vector{true, true, false}));
44 }
45 #endif
46 
47 int main(int, char**) {
48   test();
49   static_assert(test());
50 
51   static_assert(test_constraints<std::vector, bool, char>());
52 
53   // Note: test_exception_safety_throwing_copy doesn't apply because copying a boolean cannot throw.
54   test_exception_safety_throwing_allocator<std::vector, bool>();
55 
56 #ifndef TEST_HAS_NO_LOCALIZATION
57   test_counted_istream_view();
58 #endif
59 
60   return 0;
61 }
62