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// <vector>
10
11// explicit vector(size_type n);
12// explicit vector(size_type n, const Allocator& alloc = Allocator());
13
14#include <vector>
15#include <cassert>
16
17#include "test_macros.h"
18#include "DefaultOnly.h"
19#include "min_allocator.h"
20#include "test_allocator.h"
21#include "asan_testing.h"
22
23template <class C>
24TEST_CONSTEXPR_CXX20 void
25test(typename C::size_type n, typename C::allocator_type const& a = typename C::allocator_type()) {
26 (void)a;
27 // Test without a custom allocator
28 {
29 C c(n);
30 LIBCPP_ASSERT(c.__invariants());
31 assert(c.size() == n);
32 assert(c.get_allocator() == typename C::allocator_type());
33 LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
34#if TEST_STD_VER >= 11
35 for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
36 assert(*i == typename C::value_type());
37#endif
38 }
39
40 // Test with a custom allocator
41#if TEST_STD_VER >= 14
42 {
43 C c(n, a);
44 LIBCPP_ASSERT(c.__invariants());
45 assert(c.size() == n);
46 assert(c.get_allocator() == a);
47 LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
48 for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
49 assert(*i == typename C::value_type());
50 }
51#endif
52}
53
54TEST_CONSTEXPR_CXX20 bool tests() {
55 test<std::vector<int> >(0);
56 test<std::vector<int> >(50);
57#if TEST_STD_VER >= 11
58 test<std::vector<int, min_allocator<int>>>(0);
59 test<std::vector<int, min_allocator<int>>>(50);
60 test<std::vector<int, safe_allocator<int>>>(0);
61 test<std::vector<int, safe_allocator<int>>>(50);
62#endif
63
64 return true;
65}
66
67int main(int, char**) {
68 tests();
69#if TEST_STD_VER > 17
70 static_assert(tests());
71#endif
72 test<std::vector<DefaultOnly> >(0);
73 test<std::vector<DefaultOnly> >(500);
74 assert(DefaultOnly::count == 0);
75
76#if TEST_STD_VER >= 11
77 test<std::vector<DefaultOnly, min_allocator<DefaultOnly>>>(0);
78 test<std::vector<DefaultOnly, min_allocator<DefaultOnly>>>(500);
79 test<std::vector<DefaultOnly, safe_allocator<DefaultOnly>>>(0);
80 test<std::vector<DefaultOnly, safe_allocator<DefaultOnly>>>(500);
81 test<std::vector<DefaultOnly, test_allocator<DefaultOnly>>>(0, test_allocator<DefaultOnly>(23));
82 test<std::vector<DefaultOnly, test_allocator<DefaultOnly>>>(100, test_allocator<DefaultOnly>(23));
83 assert(DefaultOnly::count == 0);
84#endif
85
86 return 0;
87}
88

source code of libcxx/test/std/containers/sequences/vector/vector.cons/construct_size.pass.cpp