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