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 | // void push_back(const value_type& x); |
12 | |
13 | #include <vector> |
14 | #include <cassert> |
15 | #include <cstddef> |
16 | #include "test_macros.h" |
17 | #include "test_allocator.h" |
18 | #include "min_allocator.h" |
19 | #include "asan_testing.h" |
20 | |
21 | TEST_CONSTEXPR_CXX20 bool tests() |
22 | { |
23 | { |
24 | std::vector<int> c; |
25 | c.push_back(x: 0); |
26 | assert(c.size() == 1); |
27 | assert(is_contiguous_container_asan_correct(c)); |
28 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
29 | assert(c[j] == j); |
30 | c.push_back(x: 1); |
31 | assert(c.size() == 2); |
32 | assert(is_contiguous_container_asan_correct(c)); |
33 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
34 | assert(c[j] == j); |
35 | c.push_back(x: 2); |
36 | assert(c.size() == 3); |
37 | assert(is_contiguous_container_asan_correct(c)); |
38 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
39 | assert(c[j] == j); |
40 | c.push_back(x: 3); |
41 | assert(c.size() == 4); |
42 | assert(is_contiguous_container_asan_correct(c)); |
43 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
44 | assert(c[j] == j); |
45 | c.push_back(x: 4); |
46 | assert(c.size() == 5); |
47 | assert(is_contiguous_container_asan_correct(c)); |
48 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
49 | assert(c[j] == j); |
50 | } |
51 | { |
52 | // libc++ needs 15 because it grows by 2x (1 + 2 + 4 + 8). |
53 | // Use 17 for implementations that dynamically allocate a container proxy |
54 | // and grow by 1.5x (1 for proxy + 1 + 2 + 3 + 4 + 6). |
55 | std::vector<int, limited_allocator<int, 17> > c; |
56 | c.push_back(0); |
57 | assert(c.size() == 1); |
58 | assert(is_contiguous_container_asan_correct(c)); |
59 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
60 | assert(c[j] == j); |
61 | c.push_back(1); |
62 | assert(c.size() == 2); |
63 | assert(is_contiguous_container_asan_correct(c)); |
64 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
65 | assert(c[j] == j); |
66 | c.push_back(2); |
67 | assert(c.size() == 3); |
68 | assert(is_contiguous_container_asan_correct(c)); |
69 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
70 | assert(c[j] == j); |
71 | c.push_back(3); |
72 | assert(c.size() == 4); |
73 | assert(is_contiguous_container_asan_correct(c)); |
74 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
75 | assert(c[j] == j); |
76 | c.push_back(4); |
77 | assert(c.size() == 5); |
78 | assert(is_contiguous_container_asan_correct(c)); |
79 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
80 | assert(c[j] == j); |
81 | } |
82 | #if TEST_STD_VER >= 11 |
83 | { |
84 | std::vector<int, min_allocator<int>> c; |
85 | c.push_back(0); |
86 | assert(c.size() == 1); |
87 | assert(is_contiguous_container_asan_correct(c)); |
88 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
89 | assert(c[j] == j); |
90 | c.push_back(1); |
91 | assert(c.size() == 2); |
92 | assert(is_contiguous_container_asan_correct(c)); |
93 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
94 | assert(c[j] == j); |
95 | c.push_back(2); |
96 | assert(c.size() == 3); |
97 | assert(is_contiguous_container_asan_correct(c)); |
98 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
99 | assert(c[j] == j); |
100 | c.push_back(3); |
101 | assert(c.size() == 4); |
102 | assert(is_contiguous_container_asan_correct(c)); |
103 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
104 | assert(c[j] == j); |
105 | c.push_back(4); |
106 | assert(c.size() == 5); |
107 | assert(is_contiguous_container_asan_correct(c)); |
108 | for (int j = 0; static_cast<std::size_t>(j) < c.size(); ++j) |
109 | assert(c[j] == j); |
110 | } |
111 | #endif |
112 | |
113 | return true; |
114 | } |
115 | |
116 | int main(int, char**) |
117 | { |
118 | tests(); |
119 | #if TEST_STD_VER > 17 |
120 | static_assert(tests()); |
121 | #endif |
122 | return 0; |
123 | } |
124 | |