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

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