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// const_pointer data() const;
12
13#include <vector>
14#include <cassert>
15
16#include "test_macros.h"
17#include "min_allocator.h"
18#include "asan_testing.h"
19
20struct Nasty {
21 TEST_CONSTEXPR Nasty() : i_(0) {}
22 TEST_CONSTEXPR Nasty(int i) : i_(i) {}
23 TEST_CONSTEXPR_CXX20 ~Nasty() {}
24
25 Nasty* operator&() const {
26 assert(false);
27 return nullptr;
28 }
29 int i_;
30};
31
32TEST_CONSTEXPR_CXX20 bool tests() {
33 {
34 const std::vector<int> v;
35 assert(v.data() == 0);
36 assert(is_contiguous_container_asan_correct(v));
37 }
38 {
39 const std::vector<int> v(100);
40 assert(v.data() == std::addressof(v.front()));
41 assert(is_contiguous_container_asan_correct(v));
42 }
43 {
44 const std::vector<Nasty> v(100);
45 assert(v.data() == std::addressof(v.front()));
46 assert(is_contiguous_container_asan_correct(v));
47 }
48#if TEST_STD_VER >= 11
49 {
50 const std::vector<int, min_allocator<int>> v;
51 assert(v.data() == 0);
52 assert(is_contiguous_container_asan_correct(v));
53 }
54 {
55 const std::vector<int, min_allocator<int>> v(100);
56 assert(v.data() == &v.front());
57 assert(is_contiguous_container_asan_correct(v));
58 }
59 {
60 const std::vector<Nasty, min_allocator<Nasty>> v(100);
61 assert(v.data() == std::addressof(v.front()));
62 assert(is_contiguous_container_asan_correct(v));
63 }
64 {
65 const std::vector<int, safe_allocator<int>> v;
66 assert(v.data() == 0);
67 assert(is_contiguous_container_asan_correct(v));
68 }
69 {
70 const std::vector<int, safe_allocator<int>> v(100);
71 assert(v.data() == &v.front());
72 assert(is_contiguous_container_asan_correct(v));
73 }
74 {
75 const std::vector<Nasty, safe_allocator<Nasty>> v(100);
76 assert(v.data() == std::addressof(v.front()));
77 assert(is_contiguous_container_asan_correct(v));
78 }
79#endif
80
81 return true;
82}
83
84int main(int, char**) {
85 tests();
86#if TEST_STD_VER > 17
87 static_assert(tests());
88#endif
89 return 0;
90}
91

source code of libcxx/test/std/containers/sequences/vector/vector.data/data_const.pass.cpp