| 1 |
// { dg-do run { target c++20 } } |
| 2 |
// { dg-add-options no_pch } |
| 3 |
|
| 4 |
// C++20 20.11.3.7 shared_ptr Creation [util.smartptr.shared.create] |
| 5 |
|
| 6 |
#include <memory> |
| 7 |
|
| 8 |
#ifndef __cpp_lib_shared_ptr_arrays |
| 9 |
# error "Feature-test macro for make_shared arrays missing in <memory>" |
| 10 |
#elif __cpp_lib_shared_ptr_arrays < 201707L |
| 11 |
# error "Feature-test macro for make_shared arrays has wrong value in <memory>" |
| 12 |
#endif |
| 13 |
|
| 14 |
#include <testsuite_hooks.h> |
| 15 |
|
| 16 |
int counter = 0; |
| 17 |
|
| 18 |
template<typename T> |
| 19 |
struct Alloc : std::allocator<T> |
| 20 |
{ |
| 21 |
Alloc() = default; |
| 22 |
|
| 23 |
template<typename U> |
| 24 |
Alloc(const Alloc<U>&) { } |
| 25 |
|
| 26 |
T* allocate(std::size_t n) |
| 27 |
{ |
| 28 |
++counter; |
| 29 |
return std::allocator<T>::allocate(n); |
| 30 |
} |
| 31 |
}; |
| 32 |
|
| 33 |
void |
| 34 |
test01() |
| 35 |
{ |
| 36 |
Alloc<int> a; |
| 37 |
|
| 38 |
std::shared_ptr<int[]> p1 = std::allocate_shared<int[]>(a, 24); |
| 39 |
VERIFY( counter == 1 ); |
| 40 |
VERIFY( p1[23] == 0 ); |
| 41 |
std::shared_ptr<int[48]> p2 = std::allocate_shared<int[48]>(a); |
| 42 |
VERIFY( counter == 2 ); |
| 43 |
VERIFY( p2[47] == 0 ); |
| 44 |
|
| 45 |
std::shared_ptr<int[][12]> p3 = std::allocate_shared<int[][12]>(a, 3); |
| 46 |
VERIFY( counter == 3 ); |
| 47 |
VERIFY( p3[2][11] == 0 ); |
| 48 |
std::shared_ptr<int[4][5]> p4 = std::allocate_shared<int[4][5]>(a); |
| 49 |
VERIFY( counter == 4 ); |
| 50 |
VERIFY( p4[3][4] == 0 ); |
| 51 |
} |
| 52 |
|
| 53 |
void |
| 54 |
test02() |
| 55 |
{ |
| 56 |
std::shared_ptr<int[]> p1 = std::make_shared<int[]>(24); |
| 57 |
VERIFY( p1[23] == 0 ); |
| 58 |
std::shared_ptr<int[48]> p2 = std::make_shared<int[48]>(); |
| 59 |
VERIFY( p2[47] == 0 ); |
| 60 |
|
| 61 |
std::shared_ptr<int[][12]> p3 = std::make_shared<int[][12]>(3); |
| 62 |
VERIFY( p3[2][11] == 0 ); |
| 63 |
std::shared_ptr<int[4][5]> p4 = std::make_shared<int[4][5]>(); |
| 64 |
VERIFY( p4[3][4] == 0 ); |
| 65 |
} |
| 66 |
|
| 67 |
#include <vector> |
| 68 |
#include <cstdint> |
| 69 |
|
| 70 |
std::vector<std::uintptr_t> addresses; |
| 71 |
|
| 72 |
void |
| 73 |
test03() |
| 74 |
{ |
| 75 |
// Verify construction and destruction order |
| 76 |
struct Addressed |
| 77 |
{ |
| 78 |
Addressed() { addresses.push_back(me()); } |
| 79 |
~Addressed() { VERIFY( addresses.back() == me() ); addresses.pop_back(); } |
| 80 |
|
| 81 |
std::uintptr_t me() const { return reinterpret_cast<std::uintptr_t>(this); } |
| 82 |
}; |
| 83 |
|
| 84 |
auto check = [](auto shptr) { |
| 85 |
std::uintptr_t last = 0; |
| 86 |
for (auto a : addresses) |
| 87 |
{ |
| 88 |
VERIFY( a > last ); |
| 89 |
last = a; |
| 90 |
} |
| 91 |
shptr.reset(); |
| 92 |
return addresses.empty(); |
| 93 |
}; |
| 94 |
|
| 95 |
VERIFY( check(std::make_shared<Addressed[][2]>(3)) ); |
| 96 |
VERIFY( check(std::make_shared<Addressed[4][2]>()) ); |
| 97 |
} |
| 98 |
|
| 99 |
void |
| 100 |
test04() |
| 101 |
{ |
| 102 |
// Verify initial value |
| 103 |
auto p1 = std::make_shared<int[]>(3, 9); |
| 104 |
VERIFY( p1[0] == 9 && p1[1] == 9 && p1[2] == 9 ); |
| 105 |
|
| 106 |
auto p2 = std::make_shared<int[2]>(4); |
| 107 |
VERIFY( p2[0] == 4 && p2[1] == 4 ); |
| 108 |
|
| 109 |
auto p3 = std::make_shared<int[][3]>(10, {1,2,3}); |
| 110 |
const auto& p3_0 = p3[0]; |
| 111 |
VERIFY( p3_0[0] == 1 && p3_0[1] == 2 && p3_0[2] == 3 ); |
| 112 |
for (int i = 1; i < 10; ++i) |
| 113 |
for (int j = 0; j < 3; ++j) |
| 114 |
VERIFY( p3[i][j] == p3_0[j] ); |
| 115 |
|
| 116 |
auto p4 = std::make_shared<int[10][3]>({4,5,6}); |
| 117 |
const auto& p4_0 = p4[0]; |
| 118 |
VERIFY( p4_0[0] == 4 && p4_0[1] == 5 && p4_0[2] == 6 ); |
| 119 |
for (int i = 1; i < 10; ++i) |
| 120 |
for (int j = 0; j < 3; ++j) |
| 121 |
VERIFY( p4[i][j] == p4_0[j] ); |
| 122 |
|
| 123 |
auto p5 = std::make_shared<int[][3][2]>(10, {{1,2},{3,4},{5,6}}); |
| 124 |
const auto& p5_0 = p5[0]; |
| 125 |
VERIFY( p5_0[0][0] == 1 && p5_0[0][1] == 2 ); |
| 126 |
VERIFY( p5_0[1][0] == 3 && p5_0[1][1] == 4 ); |
| 127 |
VERIFY( p5_0[2][0] == 5 && p5_0[2][1] == 6 ); |
| 128 |
for (int i = 1; i < 10; ++i) |
| 129 |
for (int j = 0; j < 3; ++j) |
| 130 |
for (int k = 0; k < 2; ++k) |
| 131 |
VERIFY( p5[i][j][k] == p5_0[j][k] ); |
| 132 |
|
| 133 |
auto p6 = std::make_shared<int[4][3][2]>({{7,8},{9,10},{11,12}}); |
| 134 |
const auto& p6_0 = p6[0]; |
| 135 |
VERIFY( p6_0[0][0] == 7 && p6_0[0][1] == 8 ); |
| 136 |
VERIFY( p6_0[1][0] == 9 && p6_0[1][1] == 10 ); |
| 137 |
VERIFY( p6_0[2][0] == 11 && p6_0[2][1] == 12 ); |
| 138 |
for (int i = 1; i < 4; ++i) |
| 139 |
for (int j = 0; j < 3; ++j) |
| 140 |
for (int k = 0; k < 2; ++k) |
| 141 |
VERIFY( p6[i][j][k] == p6_0[j][k] ); |
| 142 |
} |
| 143 |
|
| 144 |
void |
| 145 |
test05() |
| 146 |
{ |
| 147 |
// Examples from the standard |
| 148 |
using namespace std; |
| 149 |
|
| 150 |
// Example 2 |
| 151 |
{ |
| 152 |
shared_ptr<double[]> p = make_shared<double[]>(1024); |
| 153 |
// shared_ptr to a value-initialized double[1024] |
| 154 |
for (int i = 0; i < 1024; ++i) |
| 155 |
VERIFY( p[i] == 0.0 ); |
| 156 |
|
| 157 |
shared_ptr<double[][2][2]> q = make_shared<double[][2][2]>(6); |
| 158 |
// shared_ptr to a value-initialized double[6][2][2] |
| 159 |
for (int i = 0; i < 6; ++i) |
| 160 |
for (auto& j : q[i]) |
| 161 |
for (auto& k : j) |
| 162 |
VERIFY( k == 0.0 ); |
| 163 |
} |
| 164 |
|
| 165 |
// Example 3 |
| 166 |
{ |
| 167 |
shared_ptr<double[1024]> p = make_shared<double[1024]>(); |
| 168 |
// shared_ptr to a value-initialized double[1024] |
| 169 |
for (int i = 0; i < 1024; ++i) |
| 170 |
VERIFY( p[i] == 0.0 ); |
| 171 |
|
| 172 |
shared_ptr<double[6][2][2]> q = make_shared<double[6][2][2]>(); |
| 173 |
// shared_ptr to a value-initialized double[6][2][2] |
| 174 |
for (int i = 0; i < 6; ++i) |
| 175 |
for (auto& j : q[i]) |
| 176 |
for (auto& k : j) |
| 177 |
VERIFY( k == 0.0 ); |
| 178 |
} |
| 179 |
|
| 180 |
// Example 4 |
| 181 |
{ |
| 182 |
shared_ptr<double[]> p = make_shared<double[]>(1024, 1.0); |
| 183 |
// shared_ptr to a double[1024], where each element is 1.0 |
| 184 |
for (int i = 0; i < 1024; ++i) |
| 185 |
VERIFY( p[i] == 1.0 ); |
| 186 |
|
| 187 |
shared_ptr<double[][2]> q = make_shared<double[][2]>(6, {1.0, 0.0}); |
| 188 |
// shared_ptr to a double[6][2], where each double[2] element is {1.0, 0.0} |
| 189 |
for (int i = 0; i < 6; ++i) |
| 190 |
VERIFY( q[i][0] == 1.0 && q[i][1] == 0.0 ); |
| 191 |
|
| 192 |
shared_ptr<vector<int>[]> r = make_shared<vector<int>[]>(4, {1, 2}); |
| 193 |
// shared_ptr to a vector<int>[4], where each vector has contents {1, 2} |
| 194 |
for (int i = 0; i < 4; ++i) |
| 195 |
VERIFY( r[i] == vector<int>({1, 2}) ); |
| 196 |
} |
| 197 |
|
| 198 |
// Example 5 |
| 199 |
{ |
| 200 |
shared_ptr<double[1024]> p = make_shared<double[1024]>(1.0); |
| 201 |
// shared_ptr to a double[1024], where each element is 1.0 |
| 202 |
for (int i = 0; i < 1024; ++i) |
| 203 |
VERIFY( p[i] == 1.0 ); |
| 204 |
|
| 205 |
shared_ptr<double[6][2]> q = make_shared<double[6][2]>({1.0, 0.0}); |
| 206 |
// shared_ptr to a double[6][2], where each double[2] element is {1.0, 0.0} |
| 207 |
for (int i = 0; i < 6; ++i) |
| 208 |
VERIFY( q[i][0] == 1.0 && q[i][1] == 0.0 ); |
| 209 |
|
| 210 |
shared_ptr<vector<int>[4]> r = make_shared<vector<int>[4]>({1, 2}); |
| 211 |
// shared_ptr to a vector<int>[4], where each vector has contents {1, 2} |
| 212 |
for (int i = 0; i < 4; ++i) |
| 213 |
VERIFY( r[i] == vector<int>({1, 2}) ); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
int |
| 218 |
main() |
| 219 |
{ |
| 220 |
test01(); |
| 221 |
test02(); |
| 222 |
test03(); |
| 223 |
test04(); |
| 224 |
test05(); |
| 225 |
} |