| 1 | /* Copyright (c) 2014, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | /* See http://code.google.com/p/googletest/wiki/Primer */ |
| 25 | |
| 26 | #include <gtest/gtest.h> |
| 27 | #include <stddef.h> |
| 28 | #include <memory> |
| 29 | #include <stdexcept> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "my_config.h" |
| 33 | #include "storage/innobase/include/univ.i" |
| 34 | #include "storage/innobase/include/ut0new.h" |
| 35 | |
| 36 | namespace innodb_ut0new_unittest { |
| 37 | |
| 38 | static auto pfs_key = 12345; |
| 39 | |
| 40 | inline bool ptr_is_suitably_aligned(void *ptr) { |
| 41 | #if (defined(LINUX_RHEL6) || (defined(LINUX_RHEL7))) && (SIZEOF_VOIDP == 4) |
| 42 | // This is a "workaround" for 32-bit OL6/7 platform which do not respect |
| 43 | // the alignment requirements. TL;DR 32-bit OL6/7/8 have a violation so that |
| 44 | // the pointer returned by malloc is not suitably aligned |
| 45 | // (ptr % alignof(max_align_t) is _not_ 0). This was found out through the |
| 46 | // suite of ut0new* unit-tests and since then these unit-tests are failing |
| 47 | // on those platforms. |
| 48 | // |
| 49 | // For more details see |
| 50 | // https://mybug.mysql.oraclecorp.com/orabugs/bug.php?id=33137030. |
| 51 | // |
| 52 | // As it stands, issue is identified and confirmed but OL6 and OL7 platforms |
| 53 | // will not receive a backport of a fix that has been deployed to OL8. By |
| 54 | // returning true, we allow the unit-tests to continue running in faith that |
| 55 | // nothing will become broken. |
| 56 | return true; |
| 57 | #else |
| 58 | return reinterpret_cast<std::uintptr_t>(ptr) % alignof(max_align_t) == 0; |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | /* test edge cases */ |
| 63 | TEST(ut0new, edgecases) { |
| 64 | #ifdef UNIV_PFS_MEMORY |
| 65 | auto ptr = ut::new_arr<byte>(ut::Count{0}); |
| 66 | EXPECT_NE(nullptr, ptr); |
| 67 | ut::delete_arr((byte *)ptr); |
| 68 | #endif /* UNIV_PFS_MEMORY */ |
| 69 | } |
| 70 | |
| 71 | struct Pod_type { |
| 72 | Pod_type(int _x, int _y) : x(_x), y(_y) {} |
| 73 | int x; |
| 74 | int y; |
| 75 | }; |
| 76 | |
| 77 | struct My_fancy_sum { |
| 78 | My_fancy_sum(int x, int y) : result(x + y) {} |
| 79 | int result; |
| 80 | }; |
| 81 | struct Non_pod_type { |
| 82 | Non_pod_type(int _x, int _y, std::string _s) |
| 83 | : x(_x), y(_y), s(_s), sum(x, y) { |
| 84 | s_count++; |
| 85 | } |
| 86 | Non_pod_type(const Non_pod_type &other) |
| 87 | : x(other.x), y(other.y), s(other.s), sum(other.sum) { |
| 88 | s_count++; |
| 89 | } |
| 90 | ~Non_pod_type() { s_count--; } |
| 91 | int x; |
| 92 | int y; |
| 93 | std::string s; |
| 94 | My_fancy_sum sum; |
| 95 | static size_t get_count() { return s_count; } |
| 96 | |
| 97 | private: |
| 98 | static size_t s_count; |
| 99 | }; |
| 100 | size_t Non_pod_type::s_count{0}; |
| 101 | |
| 102 | struct Default_constructible_pod { |
| 103 | Default_constructible_pod() : x(0), y(1) {} |
| 104 | int x, y; |
| 105 | }; |
| 106 | struct Default_constructible_non_pod { |
| 107 | Default_constructible_non_pod() : x(0), y(1), s("non-pod-string") {} |
| 108 | int x, y; |
| 109 | std::string s; |
| 110 | }; |
| 111 | |
| 112 | template <typename T, bool With_pfs> |
| 113 | struct Ut0new_test_param_wrapper { |
| 114 | using type = T; |
| 115 | static constexpr bool with_pfs = With_pfs; |
| 116 | }; |
| 117 | |
| 118 | using all_fundamental_types = ::testing::Types< |
| 119 | // with PFS |
| 120 | Ut0new_test_param_wrapper<char, true>, |
| 121 | Ut0new_test_param_wrapper<unsigned char, true>, |
| 122 | Ut0new_test_param_wrapper<wchar_t, true>, |
| 123 | Ut0new_test_param_wrapper<short int, true>, |
| 124 | Ut0new_test_param_wrapper<unsigned short int, true>, |
| 125 | Ut0new_test_param_wrapper<int, true>, |
| 126 | Ut0new_test_param_wrapper<unsigned int, true>, |
| 127 | Ut0new_test_param_wrapper<long int, true>, |
| 128 | Ut0new_test_param_wrapper<unsigned long int, true>, |
| 129 | Ut0new_test_param_wrapper<long long int, true>, |
| 130 | Ut0new_test_param_wrapper<unsigned long long int, true>, |
| 131 | Ut0new_test_param_wrapper<float, true>, |
| 132 | Ut0new_test_param_wrapper<double, true>, |
| 133 | Ut0new_test_param_wrapper<long double, true>, |
| 134 | // no PFS |
| 135 | Ut0new_test_param_wrapper<char, false>, |
| 136 | Ut0new_test_param_wrapper<unsigned char, false>, |
| 137 | Ut0new_test_param_wrapper<wchar_t, false>, |
| 138 | Ut0new_test_param_wrapper<short int, false>, |
| 139 | Ut0new_test_param_wrapper<unsigned short int, false>, |
| 140 | Ut0new_test_param_wrapper<int, false>, |
| 141 | Ut0new_test_param_wrapper<unsigned int, false>, |
| 142 | Ut0new_test_param_wrapper<long int, false>, |
| 143 | Ut0new_test_param_wrapper<unsigned long int, false>, |
| 144 | Ut0new_test_param_wrapper<long long int, false>, |
| 145 | Ut0new_test_param_wrapper<unsigned long long int, false>, |
| 146 | Ut0new_test_param_wrapper<float, false>, |
| 147 | Ut0new_test_param_wrapper<double, false>, |
| 148 | Ut0new_test_param_wrapper<long double, false>>; |
| 149 | |
| 150 | using all_pod_types = ::testing::Types< |
| 151 | // with PFS |
| 152 | Ut0new_test_param_wrapper<Pod_type, true>, |
| 153 | // no PFS |
| 154 | Ut0new_test_param_wrapper<Pod_type, false>>; |
| 155 | |
| 156 | using all_default_constructible_pod_types = ::testing::Types< |
| 157 | // with PFS |
| 158 | Ut0new_test_param_wrapper<Default_constructible_pod, true>, |
| 159 | // no PFS |
| 160 | Ut0new_test_param_wrapper<Default_constructible_pod, false>>; |
| 161 | |
| 162 | using all_non_pod_types = ::testing::Types< |
| 163 | // with PFS |
| 164 | Ut0new_test_param_wrapper<Non_pod_type, true>, |
| 165 | // no PFS |
| 166 | Ut0new_test_param_wrapper<Non_pod_type, false>>; |
| 167 | |
| 168 | using all_default_constructible_non_pod_types = ::testing::Types< |
| 169 | // with PFS |
| 170 | Ut0new_test_param_wrapper<Default_constructible_non_pod, true>, |
| 171 | // no PFS |
| 172 | Ut0new_test_param_wrapper<Default_constructible_non_pod, false>>; |
| 173 | |
| 174 | // malloc/free - fundamental types |
| 175 | template <typename T> |
| 176 | class ut0new_malloc_free_fundamental_types : public ::testing::Test {}; |
| 177 | TYPED_TEST_SUITE_P(ut0new_malloc_free_fundamental_types); |
| 178 | TYPED_TEST_P(ut0new_malloc_free_fundamental_types, fundamental_types) { |
| 179 | using type = typename TypeParam::type; |
| 180 | auto with_pfs = TypeParam::with_pfs; |
| 181 | type *ptr = with_pfs ? static_cast<type *>(ut::malloc_withkey( |
| 182 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 183 | : static_cast<type *>(ut::malloc(sizeof(type))); |
| 184 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 185 | ut::free(ptr); |
| 186 | } |
| 187 | REGISTER_TYPED_TEST_SUITE_P(ut0new_malloc_free_fundamental_types, |
| 188 | fundamental_types); |
| 189 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 190 | ut0new_malloc_free_fundamental_types, |
| 191 | all_fundamental_types); |
| 192 | |
| 193 | // malloc/free - pod types |
| 194 | template <typename T> |
| 195 | class ut0new_malloc_free_pod_types : public ::testing::Test {}; |
| 196 | TYPED_TEST_SUITE_P(ut0new_malloc_free_pod_types); |
| 197 | TYPED_TEST_P(ut0new_malloc_free_pod_types, pod_types) { |
| 198 | using type = typename TypeParam::type; |
| 199 | auto with_pfs = TypeParam::with_pfs; |
| 200 | type *ptr = with_pfs ? static_cast<type *>(ut::malloc_withkey( |
| 201 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 202 | : static_cast<type *>(ut::malloc(sizeof(type))); |
| 203 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 204 | ut::free(ptr); |
| 205 | } |
| 206 | REGISTER_TYPED_TEST_SUITE_P(ut0new_malloc_free_pod_types, pod_types); |
| 207 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, ut0new_malloc_free_pod_types, |
| 208 | all_pod_types); |
| 209 | |
| 210 | // malloc/free - non-pod types |
| 211 | template <typename T> |
| 212 | class ut0new_malloc_free_non_pod_types : public ::testing::Test {}; |
| 213 | TYPED_TEST_SUITE_P(ut0new_malloc_free_non_pod_types); |
| 214 | TYPED_TEST_P(ut0new_malloc_free_non_pod_types, non_pod_types) { |
| 215 | using type = typename TypeParam::type; |
| 216 | auto with_pfs = TypeParam::with_pfs; |
| 217 | type *ptr = with_pfs ? static_cast<type *>(ut::malloc_withkey( |
| 218 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 219 | : static_cast<type *>(ut::malloc(sizeof(type))); |
| 220 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 221 | ut::free(ptr); |
| 222 | } |
| 223 | REGISTER_TYPED_TEST_SUITE_P(ut0new_malloc_free_non_pod_types, non_pod_types); |
| 224 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, ut0new_malloc_free_non_pod_types, |
| 225 | all_non_pod_types); |
| 226 | |
| 227 | // zalloc/free - fundamental types |
| 228 | template <typename T> |
| 229 | class ut0new_zalloc_free_fundamental_types : public ::testing::Test {}; |
| 230 | TYPED_TEST_SUITE_P(ut0new_zalloc_free_fundamental_types); |
| 231 | TYPED_TEST_P(ut0new_zalloc_free_fundamental_types, fundamental_types) { |
| 232 | using type = typename TypeParam::type; |
| 233 | auto with_pfs = TypeParam::with_pfs; |
| 234 | type *ptr = with_pfs ? static_cast<type *>(ut::zalloc_withkey( |
| 235 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 236 | : static_cast<type *>(ut::zalloc(sizeof(type))); |
| 237 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 238 | EXPECT_EQ(*ptr, 0); |
| 239 | ut::free(ptr); |
| 240 | } |
| 241 | REGISTER_TYPED_TEST_SUITE_P(ut0new_zalloc_free_fundamental_types, |
| 242 | fundamental_types); |
| 243 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 244 | ut0new_zalloc_free_fundamental_types, |
| 245 | all_fundamental_types); |
| 246 | |
| 247 | // realloc - fundamental types |
| 248 | template <typename T> |
| 249 | class ut0new_realloc_fundamental_types : public ::testing::Test {}; |
| 250 | TYPED_TEST_SUITE_P(ut0new_realloc_fundamental_types); |
| 251 | TYPED_TEST_P(ut0new_realloc_fundamental_types, fundamental_types) { |
| 252 | using T = typename TypeParam::type; |
| 253 | auto with_pfs = TypeParam::with_pfs; |
| 254 | { |
| 255 | // Allocating through realloc and release through free should work fine |
| 256 | auto p = with_pfs |
| 257 | ? static_cast<T *>(ut::realloc_withkey( |
| 258 | ut::make_psi_memory_key(pfs_key), nullptr, sizeof(T))) |
| 259 | : static_cast<T *>(ut::realloc(nullptr, sizeof(T))); |
| 260 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 261 | ut::free(p); |
| 262 | } |
| 263 | |
| 264 | { |
| 265 | // Allocating through realloc and release through realloc should work fine |
| 266 | auto p = with_pfs |
| 267 | ? static_cast<T *>(ut::realloc_withkey( |
| 268 | ut::make_psi_memory_key(pfs_key), nullptr, sizeof(T))) |
| 269 | : static_cast<T *>(ut::realloc(nullptr, sizeof(T))); |
| 270 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 271 | ut::realloc(p, 0); |
| 272 | } |
| 273 | |
| 274 | { |
| 275 | // Allocating through malloc and then upsizing the memory region through |
| 276 | // realloc should work fine |
| 277 | auto p = with_pfs ? static_cast<T *>(ut::malloc_withkey( |
| 278 | ut::make_psi_memory_key(pfs_key), sizeof(T))) |
| 279 | : static_cast<T *>(ut::malloc(sizeof(T))); |
| 280 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 281 | |
| 282 | // Let's write something into the memory so we can afterwards check if |
| 283 | // ut::realloc_* is handling the copying/moving the element(s) properly |
| 284 | *p = 0xA; |
| 285 | |
| 286 | // Enlarge to 10x through realloc |
| 287 | p = with_pfs ? static_cast<T *>(ut::realloc_withkey( |
| 288 | ut::make_psi_memory_key(pfs_key), p, 10 * sizeof(T))) |
| 289 | : static_cast<T *>(ut::realloc(p, 10 * sizeof(T))); |
| 290 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 291 | |
| 292 | // Make sure that the contents of memory are untouched after reallocation |
| 293 | EXPECT_EQ(p[0], 0xA); |
| 294 | |
| 295 | // Write some more stuff to the memory |
| 296 | for (size_t i = 1; i < 10; i++) p[i] = 0xB; |
| 297 | |
| 298 | // Enlarge to 100x through realloc |
| 299 | p = with_pfs ? static_cast<T *>(ut::realloc_withkey( |
| 300 | ut::make_psi_memory_key(pfs_key), p, 100 * sizeof(T))) |
| 301 | : static_cast<T *>(ut::realloc(p, 100 * sizeof(T))); |
| 302 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 303 | |
| 304 | // Make sure that the contents of memory are untouched after reallocation |
| 305 | EXPECT_EQ(p[0], 0xA); |
| 306 | for (size_t i = 1; i < 10; i++) EXPECT_EQ(p[i], 0xB); |
| 307 | |
| 308 | // Write some more stuff to the memory |
| 309 | for (size_t i = 10; i < 100; i++) p[i] = 0xC; |
| 310 | |
| 311 | // Enlarge to 1000x through realloc |
| 312 | p = with_pfs ? static_cast<T *>(ut::realloc_withkey( |
| 313 | ut::make_psi_memory_key(pfs_key), p, 1000 * sizeof(T))) |
| 314 | : static_cast<T *>(ut::realloc(p, 1000 * sizeof(T))); |
| 315 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 316 | |
| 317 | // Make sure that the contents of memory are untouched after reallocation |
| 318 | EXPECT_EQ(p[0], 0xA); |
| 319 | for (size_t i = 1; i < 10; i++) EXPECT_EQ(p[i], 0xB); |
| 320 | for (size_t i = 10; i < 100; i++) EXPECT_EQ(p[i], 0xC); |
| 321 | |
| 322 | ut::free(p); |
| 323 | } |
| 324 | |
| 325 | { |
| 326 | // Allocating through malloc and then downsizing the memory region through |
| 327 | // realloc should also work fine |
| 328 | auto p = with_pfs ? static_cast<T *>(ut::malloc_withkey( |
| 329 | ut::make_psi_memory_key(pfs_key), 10 * sizeof(T))) |
| 330 | : static_cast<T *>(ut::malloc(10 * sizeof(T))); |
| 331 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 332 | |
| 333 | // Write some stuff to the memory |
| 334 | for (size_t i = 0; i < 10; i++) p[i] = 0xA; |
| 335 | |
| 336 | // Downsize the array to only half of the elements |
| 337 | p = with_pfs ? static_cast<T *>(ut::realloc_withkey( |
| 338 | ut::make_psi_memory_key(pfs_key), p, 5 * sizeof(T))) |
| 339 | : static_cast<T *>(ut::realloc(p, 5 * sizeof(T))); |
| 340 | EXPECT_TRUE(ptr_is_suitably_aligned(p)); |
| 341 | |
| 342 | // Make sure that the respective contents of memory (only half of the |
| 343 | // elements) are untouched after reallocation |
| 344 | for (size_t i = 0; i < 5; i++) EXPECT_EQ(p[i], 0xA); |
| 345 | |
| 346 | ut::free(p); |
| 347 | } |
| 348 | } |
| 349 | REGISTER_TYPED_TEST_SUITE_P(ut0new_realloc_fundamental_types, |
| 350 | fundamental_types); |
| 351 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 352 | ut0new_realloc_fundamental_types, |
| 353 | all_fundamental_types); |
| 354 | |
| 355 | // new/delete - fundamental types |
| 356 | template <typename T> |
| 357 | class ut0new_new_delete_fundamental_types : public ::testing::Test {}; |
| 358 | TYPED_TEST_SUITE_P(ut0new_new_delete_fundamental_types); |
| 359 | TYPED_TEST_P(ut0new_new_delete_fundamental_types, fundamental_types) { |
| 360 | using type = typename TypeParam::type; |
| 361 | auto with_pfs = TypeParam::with_pfs; |
| 362 | type *ptr = with_pfs |
| 363 | ? ut::new_withkey<type>(ut::make_psi_memory_key(pfs_key), 1) |
| 364 | : ut::new_<type>(1); |
| 365 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 366 | EXPECT_EQ(*ptr, 1); |
| 367 | ut::delete_(ptr); |
| 368 | } |
| 369 | REGISTER_TYPED_TEST_SUITE_P(ut0new_new_delete_fundamental_types, |
| 370 | fundamental_types); |
| 371 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_new_delete_fundamental_types, |
| 372 | all_fundamental_types); |
| 373 | |
| 374 | // new/delete - pod types |
| 375 | template <typename T> |
| 376 | class ut0new_new_delete_pod_types : public ::testing::Test {}; |
| 377 | TYPED_TEST_SUITE_P(ut0new_new_delete_pod_types); |
| 378 | TYPED_TEST_P(ut0new_new_delete_pod_types, pod_types) { |
| 379 | using type = typename TypeParam::type; |
| 380 | auto with_pfs = TypeParam::with_pfs; |
| 381 | |
| 382 | type *ptr = |
| 383 | with_pfs ? ut::new_withkey<type>(ut::make_psi_memory_key(pfs_key), 2, 5) |
| 384 | : ut::new_<type>(2, 5); |
| 385 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 386 | EXPECT_EQ(ptr->x, 2); |
| 387 | EXPECT_EQ(ptr->y, 5); |
| 388 | ut::delete_(ptr); |
| 389 | } |
| 390 | REGISTER_TYPED_TEST_SUITE_P(ut0new_new_delete_pod_types, pod_types); |
| 391 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_new_delete_pod_types, all_pod_types); |
| 392 | |
| 393 | // new/delete - non-pod types |
| 394 | template <typename T> |
| 395 | class ut0new_new_delete_non_pod_types : public ::testing::Test {}; |
| 396 | TYPED_TEST_SUITE_P(ut0new_new_delete_non_pod_types); |
| 397 | TYPED_TEST_P(ut0new_new_delete_non_pod_types, non_pod_types) { |
| 398 | using type = typename TypeParam::type; |
| 399 | auto with_pfs = TypeParam::with_pfs; |
| 400 | EXPECT_EQ(type::get_count(), 0); |
| 401 | type *ptr = with_pfs ? ut::new_withkey<type>(ut::make_psi_memory_key(pfs_key), |
| 402 | 2, 5, std::string("non-pod")) |
| 403 | : ut::new_<type>(2, 5, std::string("non-pod")); |
| 404 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 405 | EXPECT_EQ(ptr->x, 2); |
| 406 | EXPECT_EQ(ptr->y, 5); |
| 407 | EXPECT_EQ(ptr->sum.result, 7); |
| 408 | EXPECT_EQ(ptr->s, std::string("non-pod")); |
| 409 | EXPECT_EQ(type::get_count(), 1); |
| 410 | ut::delete_(ptr); |
| 411 | EXPECT_EQ(type::get_count(), 0); |
| 412 | } |
| 413 | REGISTER_TYPED_TEST_SUITE_P(ut0new_new_delete_non_pod_types, non_pod_types); |
| 414 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_new_delete_non_pod_types, |
| 415 | all_non_pod_types); |
| 416 | |
| 417 | // new/delete - array specialization for fundamental types |
| 418 | template <typename T> |
| 419 | class ut0new_new_delete_fundamental_types_arr : public ::testing::Test {}; |
| 420 | TYPED_TEST_SUITE_P(ut0new_new_delete_fundamental_types_arr); |
| 421 | TYPED_TEST_P(ut0new_new_delete_fundamental_types_arr, fundamental_types) { |
| 422 | using type = typename TypeParam::type; |
| 423 | auto with_pfs = TypeParam::with_pfs; |
| 424 | type *ptr = |
| 425 | with_pfs |
| 426 | ? ut::new_arr_withkey<type>( |
| 427 | ut::make_psi_memory_key(pfs_key), |
| 428 | std::forward_as_tuple((type)0), std::forward_as_tuple((type)1), |
| 429 | std::forward_as_tuple((type)2), std::forward_as_tuple((type)3), |
| 430 | std::forward_as_tuple((type)4), std::forward_as_tuple((type)5), |
| 431 | std::forward_as_tuple((type)6), std::forward_as_tuple((type)7), |
| 432 | std::forward_as_tuple((type)8), std::forward_as_tuple((type)9)) |
| 433 | : ut::new_arr<type>( |
| 434 | std::forward_as_tuple((type)0), std::forward_as_tuple((type)1), |
| 435 | std::forward_as_tuple((type)2), std::forward_as_tuple((type)3), |
| 436 | std::forward_as_tuple((type)4), std::forward_as_tuple((type)5), |
| 437 | std::forward_as_tuple((type)6), std::forward_as_tuple((type)7), |
| 438 | std::forward_as_tuple((type)8), std::forward_as_tuple((type)9)); |
| 439 | |
| 440 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 441 | |
| 442 | for (size_t elem = 0; elem < 10; elem++) { |
| 443 | EXPECT_EQ(ptr[elem], elem); |
| 444 | } |
| 445 | ut::delete_arr(ptr); |
| 446 | } |
| 447 | REGISTER_TYPED_TEST_SUITE_P(ut0new_new_delete_fundamental_types_arr, |
| 448 | fundamental_types); |
| 449 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_new_delete_fundamental_types_arr, |
| 450 | all_fundamental_types); |
| 451 | |
| 452 | // new/delete - array specialization for pod types |
| 453 | template <typename T> |
| 454 | class ut0new_new_delete_pod_types_arr : public ::testing::Test {}; |
| 455 | TYPED_TEST_SUITE_P(ut0new_new_delete_pod_types_arr); |
| 456 | TYPED_TEST_P(ut0new_new_delete_pod_types_arr, pod_types) { |
| 457 | using type = typename TypeParam::type; |
| 458 | auto with_pfs = TypeParam::with_pfs; |
| 459 | type *ptr = |
| 460 | with_pfs |
| 461 | ? ut::new_arr_withkey<type>( |
| 462 | ut::make_psi_memory_key(pfs_key), std::forward_as_tuple(0, 1), |
| 463 | std::forward_as_tuple(2, 3), std::forward_as_tuple(4, 5), |
| 464 | std::forward_as_tuple(6, 7), std::forward_as_tuple(8, 9)) |
| 465 | : ut::new_arr<type>( |
| 466 | std::forward_as_tuple(0, 1), std::forward_as_tuple(2, 3), |
| 467 | std::forward_as_tuple(4, 5), std::forward_as_tuple(6, 7), |
| 468 | std::forward_as_tuple(8, 9)); |
| 469 | |
| 470 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 471 | |
| 472 | for (size_t elem = 0; elem < 5; elem++) { |
| 473 | EXPECT_EQ(ptr[elem].x, 2 * elem); |
| 474 | EXPECT_EQ(ptr[elem].y, 2 * elem + 1); |
| 475 | } |
| 476 | ut::delete_arr(ptr); |
| 477 | } |
| 478 | REGISTER_TYPED_TEST_SUITE_P(ut0new_new_delete_pod_types_arr, pod_types); |
| 479 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_new_delete_pod_types_arr, |
| 480 | all_pod_types); |
| 481 | |
| 482 | // new/delete - array specialization for non-pod types |
| 483 | template <typename T> |
| 484 | class ut0new_new_delete_non_pod_types_arr : public ::testing::Test {}; |
| 485 | TYPED_TEST_SUITE_P(ut0new_new_delete_non_pod_types_arr); |
| 486 | TYPED_TEST_P(ut0new_new_delete_non_pod_types_arr, non_pod_types) { |
| 487 | using type = typename TypeParam::type; |
| 488 | auto with_pfs = TypeParam::with_pfs; |
| 489 | EXPECT_EQ(type::get_count(), 0); |
| 490 | type *ptr = with_pfs ? ut::new_arr_withkey<type>( |
| 491 | ut::make_psi_memory_key(pfs_key), |
| 492 | std::forward_as_tuple(1, 2, std::string("a")), |
| 493 | std::forward_as_tuple(3, 4, std::string("b")), |
| 494 | std::forward_as_tuple(5, 6, std::string("c")), |
| 495 | std::forward_as_tuple(7, 8, std::string("d")), |
| 496 | std::forward_as_tuple(9, 10, std::string("e"))) |
| 497 | : ut::new_arr<type>( |
| 498 | std::forward_as_tuple(1, 2, std::string("a")), |
| 499 | std::forward_as_tuple(3, 4, std::string("b")), |
| 500 | std::forward_as_tuple(5, 6, std::string("c")), |
| 501 | std::forward_as_tuple(7, 8, std::string("d")), |
| 502 | std::forward_as_tuple(9, 10, std::string("e"))); |
| 503 | |
| 504 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 505 | |
| 506 | EXPECT_EQ(ptr[0].x, 1); |
| 507 | EXPECT_EQ(ptr[0].y, 2); |
| 508 | EXPECT_TRUE(ptr[0].s == std::string("a")); |
| 509 | EXPECT_EQ(ptr[0].sum.result, 3); |
| 510 | |
| 511 | EXPECT_EQ(ptr[1].x, 3); |
| 512 | EXPECT_EQ(ptr[1].y, 4); |
| 513 | EXPECT_TRUE(ptr[1].s == std::string("b")); |
| 514 | EXPECT_EQ(ptr[1].sum.result, 7); |
| 515 | |
| 516 | EXPECT_EQ(ptr[2].x, 5); |
| 517 | EXPECT_EQ(ptr[2].y, 6); |
| 518 | EXPECT_TRUE(ptr[2].s == std::string("c")); |
| 519 | EXPECT_EQ(ptr[2].sum.result, 11); |
| 520 | |
| 521 | EXPECT_EQ(ptr[3].x, 7); |
| 522 | EXPECT_EQ(ptr[3].y, 8); |
| 523 | EXPECT_TRUE(ptr[3].s == std::string("d")); |
| 524 | EXPECT_EQ(ptr[3].sum.result, 15); |
| 525 | |
| 526 | EXPECT_EQ(ptr[4].x, 9); |
| 527 | EXPECT_EQ(ptr[4].y, 10); |
| 528 | EXPECT_TRUE(ptr[4].s == std::string("e")); |
| 529 | EXPECT_EQ(ptr[4].sum.result, 19); |
| 530 | |
| 531 | EXPECT_EQ(type::get_count(), 5); |
| 532 | |
| 533 | ut::delete_arr(ptr); |
| 534 | EXPECT_EQ(type::get_count(), 0); |
| 535 | } |
| 536 | REGISTER_TYPED_TEST_SUITE_P(ut0new_new_delete_non_pod_types_arr, non_pod_types); |
| 537 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_new_delete_non_pod_types_arr, |
| 538 | all_non_pod_types); |
| 539 | |
| 540 | // new/delete - array specialization for default constructible |
| 541 | // fundamental types |
| 542 | template <typename T> |
| 543 | class ut0new_new_delete_default_constructible_fundamental_types_arr |
| 544 | : public ::testing::Test {}; |
| 545 | TYPED_TEST_SUITE_P( |
| 546 | ut0new_new_delete_default_constructible_fundamental_types_arr); |
| 547 | TYPED_TEST_P(ut0new_new_delete_default_constructible_fundamental_types_arr, |
| 548 | fundamental_types) { |
| 549 | using type = typename TypeParam::type; |
| 550 | auto with_pfs = TypeParam::with_pfs; |
| 551 | constexpr size_t n_elements = 5; |
| 552 | type *ptr = with_pfs |
| 553 | ? ut::new_arr_withkey<type>(ut::make_psi_memory_key(pfs_key), |
| 554 | ut::Count{n_elements}) |
| 555 | : ut::new_arr<type>(ut::Count{n_elements}); |
| 556 | |
| 557 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 558 | |
| 559 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 560 | EXPECT_EQ(ptr[elem], type{}); |
| 561 | } |
| 562 | ut::delete_arr(ptr); |
| 563 | } |
| 564 | REGISTER_TYPED_TEST_SUITE_P( |
| 565 | ut0new_new_delete_default_constructible_fundamental_types_arr, |
| 566 | fundamental_types); |
| 567 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 568 | My, ut0new_new_delete_default_constructible_fundamental_types_arr, |
| 569 | all_fundamental_types); |
| 570 | |
| 571 | // new/delete - array specialization for default constructible pod types |
| 572 | template <typename T> |
| 573 | class ut0new_new_delete_default_constructible_pod_types_arr |
| 574 | : public ::testing::Test {}; |
| 575 | TYPED_TEST_SUITE_P(ut0new_new_delete_default_constructible_pod_types_arr); |
| 576 | TYPED_TEST_P(ut0new_new_delete_default_constructible_pod_types_arr, pod_types) { |
| 577 | using type = typename TypeParam::type; |
| 578 | auto with_pfs = TypeParam::with_pfs; |
| 579 | constexpr size_t n_elements = 5; |
| 580 | type *ptr = with_pfs |
| 581 | ? ut::new_arr_withkey<type>(ut::make_psi_memory_key(pfs_key), |
| 582 | ut::Count{n_elements}) |
| 583 | : ut::new_arr<type>(ut::Count{n_elements}); |
| 584 | |
| 585 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 586 | |
| 587 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 588 | EXPECT_EQ(ptr[elem].x, 0); |
| 589 | EXPECT_EQ(ptr[elem].y, 1); |
| 590 | } |
| 591 | ut::delete_arr(ptr); |
| 592 | } |
| 593 | REGISTER_TYPED_TEST_SUITE_P( |
| 594 | ut0new_new_delete_default_constructible_pod_types_arr, pod_types); |
| 595 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 596 | My, ut0new_new_delete_default_constructible_pod_types_arr, |
| 597 | all_default_constructible_pod_types); |
| 598 | |
| 599 | // new/delete - array specialization for default constructible non-pod |
| 600 | // types |
| 601 | template <typename T> |
| 602 | class ut0new_new_delete_default_constructible_non_pod_types_arr |
| 603 | : public ::testing::Test {}; |
| 604 | TYPED_TEST_SUITE_P(ut0new_new_delete_default_constructible_non_pod_types_arr); |
| 605 | TYPED_TEST_P(ut0new_new_delete_default_constructible_non_pod_types_arr, |
| 606 | pod_types) { |
| 607 | using type = typename TypeParam::type; |
| 608 | auto with_pfs = TypeParam::with_pfs; |
| 609 | constexpr size_t n_elements = 5; |
| 610 | type *ptr = with_pfs |
| 611 | ? ut::new_arr_withkey<type>(ut::make_psi_memory_key(pfs_key), |
| 612 | ut::Count{n_elements}) |
| 613 | : ut::new_arr<type>(ut::Count{n_elements}); |
| 614 | |
| 615 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 616 | |
| 617 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 618 | EXPECT_EQ(ptr[elem].x, 0); |
| 619 | EXPECT_EQ(ptr[elem].y, 1); |
| 620 | EXPECT_TRUE(ptr[elem].s == std::string("non-pod-string")); |
| 621 | } |
| 622 | ut::delete_arr(ptr); |
| 623 | } |
| 624 | REGISTER_TYPED_TEST_SUITE_P( |
| 625 | ut0new_new_delete_default_constructible_non_pod_types_arr, pod_types); |
| 626 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 627 | My, ut0new_new_delete_default_constructible_non_pod_types_arr, |
| 628 | all_default_constructible_non_pod_types); |
| 629 | |
| 630 | TEST(ut0new_new_delete, unique_ptr_demo) { |
| 631 | struct Int_deleter { |
| 632 | void operator()(int *p) { |
| 633 | std::cout << "Hello from custom deleter!\n"; |
| 634 | ut::delete_(p); |
| 635 | } |
| 636 | }; |
| 637 | std::unique_ptr<int, Int_deleter> ptr(ut::new_<int>(1), Int_deleter{}); |
| 638 | } |
| 639 | |
| 640 | TEST(ut0new_new_delete_arr, unique_ptr_demo) { |
| 641 | struct Int_arr_deleter { |
| 642 | void operator()(int *p) { |
| 643 | std::cout << "Hello from custom deleter!\n"; |
| 644 | ut::delete_arr(p); |
| 645 | } |
| 646 | }; |
| 647 | std::unique_ptr<int, Int_arr_deleter> ptr( |
| 648 | ut::new_arr<int>(std::forward_as_tuple(1), std::forward_as_tuple(2), |
| 649 | std::forward_as_tuple(3), std::forward_as_tuple(4), |
| 650 | std::forward_as_tuple(5)), |
| 651 | Int_arr_deleter{}); |
| 652 | } |
| 653 | |
| 654 | TEST(ut0new_new_delete_arr, demo_with_non_default_constructible_types) { |
| 655 | auto ptr = ut::new_arr_withkey<Pod_type>( |
| 656 | ut::make_psi_memory_key(pfs_key), std::forward_as_tuple(1, 2), |
| 657 | std::forward_as_tuple(3, 4), std::forward_as_tuple(5, 6), |
| 658 | std::forward_as_tuple(7, 8), std::forward_as_tuple(9, 10)); |
| 659 | |
| 660 | EXPECT_EQ(ptr[0].x, 1); |
| 661 | EXPECT_EQ(ptr[0].y, 2); |
| 662 | EXPECT_EQ(ptr[1].x, 3); |
| 663 | EXPECT_EQ(ptr[1].y, 4); |
| 664 | EXPECT_EQ(ptr[2].x, 5); |
| 665 | EXPECT_EQ(ptr[2].y, 6); |
| 666 | EXPECT_EQ(ptr[3].x, 7); |
| 667 | EXPECT_EQ(ptr[3].y, 8); |
| 668 | EXPECT_EQ(ptr[4].x, 9); |
| 669 | EXPECT_EQ(ptr[4].y, 10); |
| 670 | |
| 671 | ut::delete_arr(ptr); |
| 672 | } |
| 673 | |
| 674 | TEST(ut0new_new_delete_arr, |
| 675 | demo_with_explicit_N_default_constructible_instances) { |
| 676 | constexpr auto n_elements = 5; |
| 677 | auto ptr = ut::new_arr_withkey<Default_constructible_pod>( |
| 678 | ut::make_psi_memory_key(pfs_key), std::forward_as_tuple(), |
| 679 | std::forward_as_tuple(), std::forward_as_tuple(), std::forward_as_tuple(), |
| 680 | std::forward_as_tuple()); |
| 681 | |
| 682 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 683 | EXPECT_EQ(ptr[elem].x, 0); |
| 684 | EXPECT_EQ(ptr[elem].y, 1); |
| 685 | } |
| 686 | ut::delete_arr(ptr); |
| 687 | } |
| 688 | |
| 689 | TEST(ut0new_new_delete_arr, |
| 690 | demo_with_N_default_constructible_instances_through_ut_count) { |
| 691 | constexpr auto n_elements = 5; |
| 692 | auto ptr = ut::new_arr_withkey<Default_constructible_pod>( |
| 693 | ut::make_psi_memory_key(pfs_key), ut::Count(n_elements)); |
| 694 | |
| 695 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 696 | EXPECT_EQ(ptr[elem].x, 0); |
| 697 | EXPECT_EQ(ptr[elem].y, 1); |
| 698 | } |
| 699 | ut::delete_arr(ptr); |
| 700 | } |
| 701 | |
| 702 | TEST( |
| 703 | ut0new_new_delete_arr, |
| 704 | demo_with_type_that_is_both_default_constructible_and_constructible_through_user_provided_ctr) { |
| 705 | struct Type { |
| 706 | Type() : x(10), y(15) {} |
| 707 | Type(int _x, int _y) : x(_x), y(_y) {} |
| 708 | int x, y; |
| 709 | }; |
| 710 | |
| 711 | auto ptr = ut::new_arr_withkey<Type>( |
| 712 | ut::make_psi_memory_key(pfs_key), std::forward_as_tuple(1, 2), |
| 713 | std::forward_as_tuple(), std::forward_as_tuple(3, 4), |
| 714 | std::forward_as_tuple(5, 6), std::forward_as_tuple()); |
| 715 | |
| 716 | EXPECT_EQ(ptr[0].x, 1); |
| 717 | EXPECT_EQ(ptr[0].y, 2); |
| 718 | EXPECT_EQ(ptr[1].x, 10); |
| 719 | EXPECT_EQ(ptr[1].y, 15); |
| 720 | EXPECT_EQ(ptr[2].x, 3); |
| 721 | EXPECT_EQ(ptr[2].y, 4); |
| 722 | EXPECT_EQ(ptr[3].x, 5); |
| 723 | EXPECT_EQ(ptr[3].y, 6); |
| 724 | EXPECT_EQ(ptr[4].x, 10); |
| 725 | EXPECT_EQ(ptr[4].y, 15); |
| 726 | |
| 727 | ut::delete_arr(ptr); |
| 728 | } |
| 729 | |
| 730 | TEST( |
| 731 | ut0new_new_delete_arr, |
| 732 | destructors_of_successfully_instantiated_trivially_constructible_elements_are_invoked_when_one_of_the_element_constructors_throws) { |
| 733 | static int n_constructors = 0; |
| 734 | static int n_destructors = 0; |
| 735 | |
| 736 | struct Type_that_may_throw { |
| 737 | Type_that_may_throw() { |
| 738 | n_constructors++; |
| 739 | if (n_constructors % 4 == 0) { |
| 740 | throw std::runtime_error("cannot construct"); |
| 741 | } |
| 742 | } |
| 743 | ~Type_that_may_throw() { ++n_destructors; } |
| 744 | }; |
| 745 | |
| 746 | bool exception_thrown_and_caught = false; |
| 747 | try { |
| 748 | auto ptr = ut::new_arr_withkey<Type_that_may_throw>( |
| 749 | ut::make_psi_memory_key(pfs_key), ut::Count(7)); |
| 750 | ASSERT_FALSE(ptr); |
| 751 | } catch (std::runtime_error &) { |
| 752 | exception_thrown_and_caught = true; |
| 753 | } |
| 754 | EXPECT_TRUE(exception_thrown_and_caught); |
| 755 | EXPECT_EQ(n_constructors, 4); |
| 756 | EXPECT_EQ(n_destructors, 3); |
| 757 | } |
| 758 | |
| 759 | TEST( |
| 760 | ut0new_new_delete_arr, |
| 761 | destructors_of_successfully_instantiated_non_trivially_constructible_elements_are_invoked_when_one_of_the_element_constructors_throws) { |
| 762 | static int n_constructors = 0; |
| 763 | static int n_destructors = 0; |
| 764 | |
| 765 | struct Type_that_may_throw { |
| 766 | Type_that_may_throw(int x, int y) : _x(x), _y(y) { |
| 767 | n_constructors++; |
| 768 | if (n_constructors % 4 == 0) { |
| 769 | throw std::runtime_error("cannot construct"); |
| 770 | } |
| 771 | } |
| 772 | ~Type_that_may_throw() { ++n_destructors; } |
| 773 | int _x, _y; |
| 774 | }; |
| 775 | |
| 776 | bool exception_thrown_and_caught = false; |
| 777 | try { |
| 778 | auto ptr = ut::new_arr_withkey<Type_that_may_throw>( |
| 779 | ut::make_psi_memory_key(pfs_key), std::forward_as_tuple(0, 1), |
| 780 | std::forward_as_tuple(2, 3), std::forward_as_tuple(4, 5), |
| 781 | std::forward_as_tuple(6, 7), std::forward_as_tuple(8, 9)); |
| 782 | ASSERT_FALSE(ptr); |
| 783 | } catch (std::runtime_error &) { |
| 784 | exception_thrown_and_caught = true; |
| 785 | } |
| 786 | EXPECT_TRUE(exception_thrown_and_caught); |
| 787 | EXPECT_EQ(n_constructors, 4); |
| 788 | EXPECT_EQ(n_destructors, 3); |
| 789 | } |
| 790 | |
| 791 | TEST( |
| 792 | ut0new_new_delete_arr, |
| 793 | no_destructors_are_invoked_when_first_trivially_constructible_element_constructor_throws) { |
| 794 | static int n_constructors = 0; |
| 795 | static int n_destructors = 0; |
| 796 | |
| 797 | struct Type_that_always_throws { |
| 798 | Type_that_always_throws() { |
| 799 | n_constructors++; |
| 800 | throw std::runtime_error("cannot construct"); |
| 801 | } |
| 802 | ~Type_that_always_throws() { ++n_destructors; } |
| 803 | }; |
| 804 | |
| 805 | bool exception_thrown_and_caught = false; |
| 806 | try { |
| 807 | auto ptr = ut::new_arr_withkey<Type_that_always_throws>( |
| 808 | ut::make_psi_memory_key(pfs_key), ut::Count(7)); |
| 809 | ASSERT_FALSE(ptr); |
| 810 | } catch (std::runtime_error &) { |
| 811 | exception_thrown_and_caught = true; |
| 812 | } |
| 813 | EXPECT_TRUE(exception_thrown_and_caught); |
| 814 | EXPECT_EQ(n_constructors, 1); |
| 815 | EXPECT_EQ(n_destructors, 0); |
| 816 | } |
| 817 | |
| 818 | TEST( |
| 819 | ut0new_new_delete_arr, |
| 820 | no_destructors_are_invoked_when_first_non_trivially_constructible_element_constructor_throws) { |
| 821 | static int n_constructors = 0; |
| 822 | static int n_destructors = 0; |
| 823 | |
| 824 | struct Type_that_always_throws { |
| 825 | Type_that_always_throws(int x, int y) : _x(x), _y(y) { |
| 826 | n_constructors++; |
| 827 | throw std::runtime_error("cannot construct"); |
| 828 | } |
| 829 | ~Type_that_always_throws() { ++n_destructors; } |
| 830 | int _x, _y; |
| 831 | }; |
| 832 | |
| 833 | bool exception_thrown_and_caught = false; |
| 834 | try { |
| 835 | auto ptr = ut::new_arr_withkey<Type_that_always_throws>( |
| 836 | ut::make_psi_memory_key(pfs_key), std::forward_as_tuple(0, 1), |
| 837 | std::forward_as_tuple(2, 3), std::forward_as_tuple(4, 5), |
| 838 | std::forward_as_tuple(6, 7), std::forward_as_tuple(8, 9)); |
| 839 | ASSERT_FALSE(ptr); |
| 840 | } catch (std::runtime_error &) { |
| 841 | exception_thrown_and_caught = true; |
| 842 | } |
| 843 | EXPECT_TRUE(exception_thrown_and_caught); |
| 844 | EXPECT_EQ(n_constructors, 1); |
| 845 | EXPECT_EQ(n_destructors, 0); |
| 846 | } |
| 847 | |
| 848 | TEST(ut0new_new_delete, |
| 849 | no_destructor_is_invoked_when_no_object_is_successfully_constructed) { |
| 850 | static int n_constructors = 0; |
| 851 | static int n_destructors = 0; |
| 852 | |
| 853 | struct Type_that_always_throws { |
| 854 | Type_that_always_throws() { |
| 855 | n_constructors++; |
| 856 | throw std::runtime_error("cannot construct"); |
| 857 | } |
| 858 | ~Type_that_always_throws() { ++n_destructors; } |
| 859 | }; |
| 860 | |
| 861 | bool exception_thrown_and_caught = false; |
| 862 | try { |
| 863 | auto ptr = ut::new_withkey<Type_that_always_throws>( |
| 864 | ut::make_psi_memory_key(pfs_key)); |
| 865 | ASSERT_FALSE(ptr); |
| 866 | } catch (std::runtime_error &) { |
| 867 | exception_thrown_and_caught = true; |
| 868 | } |
| 869 | EXPECT_TRUE(exception_thrown_and_caught); |
| 870 | EXPECT_EQ(n_constructors, 1); |
| 871 | EXPECT_EQ(n_destructors, 0); |
| 872 | } |
| 873 | |
| 874 | TEST(ut0new_new_delete, zero_sized_allocation_returns_valid_ptr) { |
| 875 | auto ptr = ut::new_<byte>(0); |
| 876 | EXPECT_NE(ptr, nullptr); |
| 877 | ut::delete_(ptr); |
| 878 | } |
| 879 | |
| 880 | TEST(ut0new_new_delete_arr, zero_sized_allocation_returns_valid_ptr) { |
| 881 | auto ptr = ut::new_arr<byte>(ut::Count{0}); |
| 882 | EXPECT_NE(ptr, nullptr); |
| 883 | ut::delete_arr(ptr); |
| 884 | } |
| 885 | |
| 886 | TEST(ut0new_new_delete_arr, |
| 887 | reference_types_are_automagically_handled_through_forward_as_tuple) { |
| 888 | struct My_ref { |
| 889 | My_ref(int &ref) : _ref(ref) {} |
| 890 | int &_ref; |
| 891 | }; |
| 892 | |
| 893 | int y = 10; |
| 894 | int x = 20; |
| 895 | auto ptr = ut::new_arr_withkey<My_ref>(ut::make_psi_memory_key(pfs_key), |
| 896 | std::forward_as_tuple(x), |
| 897 | std::forward_as_tuple(y)); |
| 898 | EXPECT_EQ(ptr[0]._ref, x); |
| 899 | EXPECT_EQ(ptr[1]._ref, y); |
| 900 | x = 30; |
| 901 | y = 40; |
| 902 | EXPECT_EQ(ptr[0]._ref, x); |
| 903 | EXPECT_EQ(ptr[1]._ref, y); |
| 904 | |
| 905 | ut::delete_arr(ptr); |
| 906 | } |
| 907 | |
| 908 | TEST(ut0new_new_delete_arr, |
| 909 | reference_types_are_automagically_handled_through_make_tuple_and_ref) { |
| 910 | struct My_ref { |
| 911 | My_ref(int &ref) : _ref(ref) {} |
| 912 | int &_ref; |
| 913 | }; |
| 914 | |
| 915 | int y = 10; |
| 916 | int x = 20; |
| 917 | auto ptr = ut::new_arr_withkey<My_ref>(ut::make_psi_memory_key(pfs_key), |
| 918 | std::make_tuple(std::ref(x)), |
| 919 | std::make_tuple(std::ref(y))); |
| 920 | EXPECT_EQ(ptr[0]._ref, x); |
| 921 | EXPECT_EQ(ptr[1]._ref, y); |
| 922 | x = 30; |
| 923 | y = 40; |
| 924 | EXPECT_EQ(ptr[0]._ref, x); |
| 925 | EXPECT_EQ(ptr[1]._ref, y); |
| 926 | |
| 927 | ut::delete_arr(ptr); |
| 928 | } |
| 929 | |
| 930 | TEST(ut0new_new_delete_arr, proper_overload_resolution_is_selected) { |
| 931 | auto ptr = ut::new_arr_withkey<Pod_type>(ut::make_psi_memory_key(pfs_key), |
| 932 | std::forward_as_tuple(1, 2)); |
| 933 | EXPECT_EQ(ptr[0].x, 1); |
| 934 | EXPECT_EQ(ptr[0].y, 2); |
| 935 | ut::delete_arr(ptr); |
| 936 | } |
| 937 | |
| 938 | // Page-aligned alloc/free - fundamental types |
| 939 | template <typename T> |
| 940 | class ut0new_page_malloc_free_fundamental_types : public ::testing::Test {}; |
| 941 | TYPED_TEST_SUITE_P(ut0new_page_malloc_free_fundamental_types); |
| 942 | TYPED_TEST_P(ut0new_page_malloc_free_fundamental_types, fundamental_types) { |
| 943 | using type = typename TypeParam::type; |
| 944 | auto with_pfs = TypeParam::with_pfs; |
| 945 | type *ptr = with_pfs ? static_cast<type *>(ut::malloc_page_withkey( |
| 946 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 947 | : static_cast<type *>(ut::malloc_page(sizeof(type))); |
| 948 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 949 | EXPECT_GE(ut::page_allocation_size(ptr), sizeof(type)); |
| 950 | ut::free_page(ptr); |
| 951 | } |
| 952 | REGISTER_TYPED_TEST_SUITE_P(ut0new_page_malloc_free_fundamental_types, |
| 953 | fundamental_types); |
| 954 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 955 | ut0new_page_malloc_free_fundamental_types, |
| 956 | all_fundamental_types); |
| 957 | |
| 958 | // Page-aligned alloc/free - pod types |
| 959 | template <typename T> |
| 960 | class ut0new_page_malloc_free_pod_types : public ::testing::Test {}; |
| 961 | TYPED_TEST_SUITE_P(ut0new_page_malloc_free_pod_types); |
| 962 | TYPED_TEST_P(ut0new_page_malloc_free_pod_types, pod_types) { |
| 963 | using type = typename TypeParam::type; |
| 964 | auto with_pfs = TypeParam::with_pfs; |
| 965 | type *ptr = with_pfs ? static_cast<type *>(ut::malloc_page_withkey( |
| 966 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 967 | : static_cast<type *>(ut::malloc_page(sizeof(type))); |
| 968 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 969 | EXPECT_GE(ut::page_allocation_size(ptr), sizeof(type)); |
| 970 | ut::free_page(ptr); |
| 971 | } |
| 972 | REGISTER_TYPED_TEST_SUITE_P(ut0new_page_malloc_free_pod_types, pod_types); |
| 973 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, ut0new_page_malloc_free_pod_types, |
| 974 | all_pod_types); |
| 975 | |
| 976 | // Page-aligned alloc/free - non-pod types |
| 977 | template <typename T> |
| 978 | class ut0new_page_malloc_free_non_pod_types : public ::testing::Test {}; |
| 979 | TYPED_TEST_SUITE_P(ut0new_page_malloc_free_non_pod_types); |
| 980 | TYPED_TEST_P(ut0new_page_malloc_free_non_pod_types, non_pod_types) { |
| 981 | using type = typename TypeParam::type; |
| 982 | auto with_pfs = TypeParam::with_pfs; |
| 983 | type *ptr = with_pfs ? static_cast<type *>(ut::malloc_page_withkey( |
| 984 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 985 | : static_cast<type *>(ut::malloc_page(sizeof(type))); |
| 986 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 987 | EXPECT_GE(ut::page_allocation_size(ptr), sizeof(type)); |
| 988 | // Referencing non-pod type members through returned pointer is UB. |
| 989 | // Solely releasing it is ok. |
| 990 | // |
| 991 | // Using it otherwise is UB because ut::malloc_* functions are raw |
| 992 | // memory management functions which do not invoke constructors neither |
| 993 | // they know which type they are operating with. That is why we would be end |
| 994 | // up accessing memory of not yet instantiated object (UB). |
| 995 | ut::free_page(ptr); |
| 996 | } |
| 997 | REGISTER_TYPED_TEST_SUITE_P(ut0new_page_malloc_free_non_pod_types, |
| 998 | non_pod_types); |
| 999 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, |
| 1000 | ut0new_page_malloc_free_non_pod_types, |
| 1001 | all_non_pod_types); |
| 1002 | |
| 1003 | // Allocating memory backed by huge (large) pages generally requires some prior |
| 1004 | // system admin setup. When there's no such setup, we don't want to make our CI |
| 1005 | // systems red. We skip the test instead and by doing it we are denoting that |
| 1006 | // test was neither failed nor successful with the message explaining possible |
| 1007 | // reasons. Solaris and OSX for example do not require any prior setup whereas |
| 1008 | // Linux and Windows do. |
| 1009 | #define SKIP_TEST_IF_HUGE_PAGE_SUPPORT_IS_NOT_AVAILABLE(ptr) \ |
| 1010 | if (!ptr) \ |
| 1011 | GTEST_SKIP() \ |
| 1012 | << "Huge-page support seems not to be enabled on this " \ |
| 1013 | "platform or underlying huge-page allocation function " \ |
| 1014 | "was not used correctly. Please check. Skipping the test ..."; |
| 1015 | |
| 1016 | // Large-page alloc/free - fundamental types |
| 1017 | template <typename T> |
| 1018 | class ut0new_large_malloc_free_fundamental_types : public ::testing::Test {}; |
| 1019 | TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fundamental_types); |
| 1020 | TYPED_TEST_P(ut0new_large_malloc_free_fundamental_types, fundamental_types) { |
| 1021 | using type = typename TypeParam::type; |
| 1022 | auto with_pfs = TypeParam::with_pfs; |
| 1023 | type *ptr = with_pfs |
| 1024 | ? static_cast<type *>(ut::malloc_large_page_withkey( |
| 1025 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 1026 | : static_cast<type *>(ut::malloc_large_page(sizeof(type))); |
| 1027 | SKIP_TEST_IF_HUGE_PAGE_SUPPORT_IS_NOT_AVAILABLE(ptr) |
| 1028 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 1029 | EXPECT_GE(ut::large_page_allocation_size(ptr), sizeof(type)); |
| 1030 | EXPECT_GT(ut::large_page_low_level_info(ptr).allocation_size, |
| 1031 | ut::large_page_allocation_size(ptr)); |
| 1032 | EXPECT_EQ(reinterpret_cast<std::uintptr_t>( |
| 1033 | ut::large_page_low_level_info(ptr).base_ptr) % |
| 1034 | CPU_PAGE_SIZE, |
| 1035 | 0); |
| 1036 | ut::free_large_page(ptr); |
| 1037 | } |
| 1038 | REGISTER_TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fundamental_types, |
| 1039 | fundamental_types); |
| 1040 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 1041 | ut0new_large_malloc_free_fundamental_types, |
| 1042 | all_fundamental_types); |
| 1043 | |
| 1044 | // Large-page alloc/free - pod types |
| 1045 | template <typename T> |
| 1046 | class ut0new_large_malloc_free_pod_types : public ::testing::Test {}; |
| 1047 | TYPED_TEST_SUITE_P(ut0new_large_malloc_free_pod_types); |
| 1048 | TYPED_TEST_P(ut0new_large_malloc_free_pod_types, pod_types) { |
| 1049 | using type = typename TypeParam::type; |
| 1050 | auto with_pfs = TypeParam::with_pfs; |
| 1051 | type *ptr = with_pfs |
| 1052 | ? static_cast<type *>(ut::malloc_large_page_withkey( |
| 1053 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 1054 | : static_cast<type *>(ut::malloc_large_page(sizeof(type))); |
| 1055 | SKIP_TEST_IF_HUGE_PAGE_SUPPORT_IS_NOT_AVAILABLE(ptr) |
| 1056 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 1057 | EXPECT_GE(ut::large_page_allocation_size(ptr), sizeof(type)); |
| 1058 | EXPECT_GT(ut::large_page_low_level_info(ptr).allocation_size, |
| 1059 | ut::large_page_allocation_size(ptr)); |
| 1060 | EXPECT_EQ(reinterpret_cast<std::uintptr_t>( |
| 1061 | ut::large_page_low_level_info(ptr).base_ptr) % |
| 1062 | CPU_PAGE_SIZE, |
| 1063 | 0); |
| 1064 | ut::free_large_page(ptr); |
| 1065 | } |
| 1066 | REGISTER_TYPED_TEST_SUITE_P(ut0new_large_malloc_free_pod_types, pod_types); |
| 1067 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, ut0new_large_malloc_free_pod_types, |
| 1068 | all_pod_types); |
| 1069 | |
| 1070 | // Large-page alloc/free - non-pod types |
| 1071 | template <typename T> |
| 1072 | class ut0new_large_malloc_free_non_pod_types : public ::testing::Test {}; |
| 1073 | TYPED_TEST_SUITE_P(ut0new_large_malloc_free_non_pod_types); |
| 1074 | TYPED_TEST_P(ut0new_large_malloc_free_non_pod_types, non_pod_types) { |
| 1075 | using type = typename TypeParam::type; |
| 1076 | auto with_pfs = TypeParam::with_pfs; |
| 1077 | type *ptr = with_pfs |
| 1078 | ? static_cast<type *>(ut::malloc_large_page_withkey( |
| 1079 | ut::make_psi_memory_key(pfs_key), sizeof(type))) |
| 1080 | : static_cast<type *>(ut::malloc_large_page(sizeof(type))); |
| 1081 | SKIP_TEST_IF_HUGE_PAGE_SUPPORT_IS_NOT_AVAILABLE(ptr) |
| 1082 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 1083 | EXPECT_GE(ut::large_page_allocation_size(ptr), sizeof(type)); |
| 1084 | EXPECT_GT(ut::large_page_low_level_info(ptr).allocation_size, |
| 1085 | ut::large_page_allocation_size(ptr)); |
| 1086 | EXPECT_EQ(reinterpret_cast<std::uintptr_t>( |
| 1087 | ut::large_page_low_level_info(ptr).base_ptr) % |
| 1088 | CPU_PAGE_SIZE, |
| 1089 | 0); |
| 1090 | // Referencing non-pod type members through returned pointer is UB. |
| 1091 | // Solely releasing it is ok. |
| 1092 | // |
| 1093 | // Using it otherwise is UB because ut::large_malloc_* functions are raw |
| 1094 | // memory management functions which do not invoke constructors neither |
| 1095 | // they know which type they are operating with. That is why we would be end |
| 1096 | // up accessing memory of not yet instantiated object (UB). |
| 1097 | ut::free_large_page(ptr); |
| 1098 | } |
| 1099 | REGISTER_TYPED_TEST_SUITE_P(ut0new_large_malloc_free_non_pod_types, |
| 1100 | non_pod_types); |
| 1101 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, |
| 1102 | ut0new_large_malloc_free_non_pod_types, |
| 1103 | all_non_pod_types); |
| 1104 | |
| 1105 | // Large-page alloc/free with page-aligned fallback - fundamental types |
| 1106 | template <typename T> |
| 1107 | class ut0new_large_malloc_free_fallback_fundamental_types |
| 1108 | : public ::testing::Test {}; |
| 1109 | TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fallback_fundamental_types); |
| 1110 | TYPED_TEST_P(ut0new_large_malloc_free_fallback_fundamental_types, |
| 1111 | fundamental_types) { |
| 1112 | using type = typename TypeParam::type; |
| 1113 | auto with_pfs = TypeParam::with_pfs; |
| 1114 | for (auto default_cfg : {true, false}) { |
| 1115 | auto ret = with_pfs ? ut::malloc_large_page_withkey( |
| 1116 | ut::make_psi_memory_key(pfs_key), sizeof(type), |
| 1117 | ut::fallback_to_normal_page_t{}, default_cfg) |
| 1118 | : ut::malloc_large_page(sizeof(type), |
| 1119 | ut::fallback_to_normal_page_t{}, |
| 1120 | default_cfg); |
| 1121 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ret) % CPU_PAGE_SIZE == 0); |
| 1122 | EXPECT_GE( |
| 1123 | ut::large_page_allocation_size(ret, ut::fallback_to_normal_page_t{}), |
| 1124 | sizeof(type)); |
| 1125 | EXPECT_GT( |
| 1126 | ut::large_page_low_level_info(ret, ut::fallback_to_normal_page_t{}) |
| 1127 | .allocation_size, |
| 1128 | ut::large_page_allocation_size(ret, ut::fallback_to_normal_page_t{})); |
| 1129 | EXPECT_EQ( |
| 1130 | reinterpret_cast<std::uintptr_t>( |
| 1131 | ut::large_page_low_level_info(ret, ut::fallback_to_normal_page_t{}) |
| 1132 | .base_ptr) % |
| 1133 | CPU_PAGE_SIZE, |
| 1134 | 0); |
| 1135 | ut::free_large_page(ret, ut::fallback_to_normal_page_t{}); |
| 1136 | } |
| 1137 | } |
| 1138 | REGISTER_TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fallback_fundamental_types, |
| 1139 | fundamental_types); |
| 1140 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 1141 | FundamentalTypes, ut0new_large_malloc_free_fallback_fundamental_types, |
| 1142 | all_fundamental_types); |
| 1143 | |
| 1144 | // Large-page alloc/free with page-aligned fallback - pod types |
| 1145 | template <typename T> |
| 1146 | class ut0new_large_malloc_free_fallback_pod_types : public ::testing::Test {}; |
| 1147 | TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fallback_pod_types); |
| 1148 | TYPED_TEST_P(ut0new_large_malloc_free_fallback_pod_types, pod_types) { |
| 1149 | using type = typename TypeParam::type; |
| 1150 | auto with_pfs = TypeParam::with_pfs; |
| 1151 | for (auto default_cfg : {true, false}) { |
| 1152 | auto ret = with_pfs ? ut::malloc_large_page_withkey( |
| 1153 | ut::make_psi_memory_key(pfs_key), sizeof(type), |
| 1154 | ut::fallback_to_normal_page_t{}, default_cfg) |
| 1155 | : ut::malloc_large_page(sizeof(type), |
| 1156 | ut::fallback_to_normal_page_t{}, |
| 1157 | default_cfg); |
| 1158 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ret) % CPU_PAGE_SIZE == 0); |
| 1159 | EXPECT_GE( |
| 1160 | ut::large_page_allocation_size(ret, ut::fallback_to_normal_page_t{}), |
| 1161 | sizeof(type)); |
| 1162 | EXPECT_GT( |
| 1163 | ut::large_page_low_level_info(ret, ut::fallback_to_normal_page_t{}) |
| 1164 | .allocation_size, |
| 1165 | ut::large_page_allocation_size(ret, ut::fallback_to_normal_page_t{})); |
| 1166 | EXPECT_EQ( |
| 1167 | reinterpret_cast<std::uintptr_t>( |
| 1168 | ut::large_page_low_level_info(ret, ut::fallback_to_normal_page_t{}) |
| 1169 | .base_ptr) % |
| 1170 | CPU_PAGE_SIZE, |
| 1171 | 0); |
| 1172 | ut::free_large_page(ret, ut::fallback_to_normal_page_t{}); |
| 1173 | } |
| 1174 | } |
| 1175 | REGISTER_TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fallback_pod_types, |
| 1176 | pod_types); |
| 1177 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, |
| 1178 | ut0new_large_malloc_free_fallback_pod_types, |
| 1179 | all_pod_types); |
| 1180 | |
| 1181 | // Large-page alloc/free with page-aligned fallback - non-pod types |
| 1182 | template <typename T> |
| 1183 | class ut0new_large_malloc_free_fallback_non_pod_types : public ::testing::Test { |
| 1184 | }; |
| 1185 | TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fallback_non_pod_types); |
| 1186 | TYPED_TEST_P(ut0new_large_malloc_free_fallback_non_pod_types, non_pod_types) { |
| 1187 | using type = typename TypeParam::type; |
| 1188 | auto with_pfs = TypeParam::with_pfs; |
| 1189 | for (auto default_cfg : {true, false}) { |
| 1190 | auto ret = with_pfs ? ut::malloc_large_page_withkey( |
| 1191 | ut::make_psi_memory_key(pfs_key), sizeof(type), |
| 1192 | ut::fallback_to_normal_page_t{}, default_cfg) |
| 1193 | : ut::malloc_large_page(sizeof(type), |
| 1194 | ut::fallback_to_normal_page_t{}, |
| 1195 | default_cfg); |
| 1196 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ret) % CPU_PAGE_SIZE == 0); |
| 1197 | EXPECT_GE( |
| 1198 | ut::large_page_allocation_size(ret, ut::fallback_to_normal_page_t{}), |
| 1199 | sizeof(type)); |
| 1200 | EXPECT_GT( |
| 1201 | ut::large_page_low_level_info(ret, ut::fallback_to_normal_page_t{}) |
| 1202 | .allocation_size, |
| 1203 | ut::large_page_allocation_size(ret, ut::fallback_to_normal_page_t{})); |
| 1204 | EXPECT_EQ( |
| 1205 | reinterpret_cast<std::uintptr_t>( |
| 1206 | ut::large_page_low_level_info(ret, ut::fallback_to_normal_page_t{}) |
| 1207 | .base_ptr) % |
| 1208 | CPU_PAGE_SIZE, |
| 1209 | 0); |
| 1210 | // Referencing non-pod type members through returned pointer is UB. |
| 1211 | // Solely releasing it is ok. |
| 1212 | // |
| 1213 | // Using it otherwise is UB because ut::large_malloc_* functions are raw |
| 1214 | // memory management functions which do not invoke constructors neither |
| 1215 | // they know which type they are operating with. That is why we would be end |
| 1216 | // up accessing memory of not yet instantiated object (UB). |
| 1217 | ut::free_large_page(ret, ut::fallback_to_normal_page_t{}); |
| 1218 | } |
| 1219 | } |
| 1220 | REGISTER_TYPED_TEST_SUITE_P(ut0new_large_malloc_free_fallback_non_pod_types, |
| 1221 | non_pod_types); |
| 1222 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, |
| 1223 | ut0new_large_malloc_free_fallback_non_pod_types, |
| 1224 | all_non_pod_types); |
| 1225 | |
| 1226 | // aligned alloc/free - fundamental types |
| 1227 | template <typename T> |
| 1228 | class aligned_alloc_free_fundamental_types : public ::testing::Test {}; |
| 1229 | TYPED_TEST_SUITE_P(aligned_alloc_free_fundamental_types); |
| 1230 | TYPED_TEST_P(aligned_alloc_free_fundamental_types, fundamental_types) { |
| 1231 | using type = typename TypeParam::type; |
| 1232 | auto with_pfs = TypeParam::with_pfs; |
| 1233 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1234 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1235 | type *ptr = |
| 1236 | with_pfs |
| 1237 | ? static_cast<type *>(ut::aligned_alloc_withkey( |
| 1238 | ut::make_psi_memory_key(pfs_key), sizeof(type), alignment)) |
| 1239 | : static_cast<type *>(ut::aligned_alloc(sizeof(type), alignment)); |
| 1240 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1241 | ut::aligned_free(ptr); |
| 1242 | } |
| 1243 | } |
| 1244 | REGISTER_TYPED_TEST_SUITE_P(aligned_alloc_free_fundamental_types, |
| 1245 | fundamental_types); |
| 1246 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 1247 | aligned_alloc_free_fundamental_types, |
| 1248 | all_fundamental_types); |
| 1249 | |
| 1250 | // aligned alloc/free - pod types |
| 1251 | template <typename T> |
| 1252 | class aligned_alloc_free_pod_types : public ::testing::Test {}; |
| 1253 | TYPED_TEST_SUITE_P(aligned_alloc_free_pod_types); |
| 1254 | TYPED_TEST_P(aligned_alloc_free_pod_types, pod_types) { |
| 1255 | using type = typename TypeParam::type; |
| 1256 | auto with_pfs = TypeParam::with_pfs; |
| 1257 | auto alignment = 4 * 1024; |
| 1258 | type *ptr = |
| 1259 | with_pfs |
| 1260 | ? static_cast<type *>(ut::aligned_alloc_withkey( |
| 1261 | ut::make_psi_memory_key(pfs_key), sizeof(type), alignment)) |
| 1262 | : static_cast<type *>(ut::aligned_alloc(sizeof(type), alignment)); |
| 1263 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1264 | ut::aligned_free(ptr); |
| 1265 | } |
| 1266 | REGISTER_TYPED_TEST_SUITE_P(aligned_alloc_free_pod_types, pod_types); |
| 1267 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, aligned_alloc_free_pod_types, |
| 1268 | all_pod_types); |
| 1269 | |
| 1270 | // aligned alloc/free - non-pod types |
| 1271 | template <typename T> |
| 1272 | class aligned_alloc_free_non_pod_types : public ::testing::Test {}; |
| 1273 | TYPED_TEST_SUITE_P(aligned_alloc_free_non_pod_types); |
| 1274 | TYPED_TEST_P(aligned_alloc_free_non_pod_types, non_pod_types) { |
| 1275 | using type = typename TypeParam::type; |
| 1276 | auto with_pfs = TypeParam::with_pfs; |
| 1277 | auto alignment = 4 * 1024; |
| 1278 | type *ptr = |
| 1279 | with_pfs |
| 1280 | ? static_cast<type *>(ut::aligned_alloc_withkey( |
| 1281 | ut::make_psi_memory_key(pfs_key), sizeof(type), alignment)) |
| 1282 | : static_cast<type *>(ut::aligned_alloc(sizeof(type), alignment)); |
| 1283 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1284 | // Referencing non-pod type members through returned pointer is UB. |
| 1285 | // Solely releasing it is ok. |
| 1286 | // |
| 1287 | // Using it otherwise is UB because aligned_alloc_* functions are raw |
| 1288 | // memory management functions which do not invoke constructors neither |
| 1289 | // they know which type they are operating with. That is why we would be end |
| 1290 | // up accessing memory of not yet instantiated object (UB). |
| 1291 | ut::aligned_free(ptr); |
| 1292 | } |
| 1293 | REGISTER_TYPED_TEST_SUITE_P(aligned_alloc_free_non_pod_types, non_pod_types); |
| 1294 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, aligned_alloc_free_non_pod_types, |
| 1295 | all_non_pod_types); |
| 1296 | |
| 1297 | // aligned new/delete - fundamental types |
| 1298 | template <typename T> |
| 1299 | class aligned_new_delete_fundamental_types : public ::testing::Test {}; |
| 1300 | TYPED_TEST_SUITE_P(aligned_new_delete_fundamental_types); |
| 1301 | TYPED_TEST_P(aligned_new_delete_fundamental_types, fundamental_types) { |
| 1302 | using type = typename TypeParam::type; |
| 1303 | auto with_pfs = TypeParam::with_pfs; |
| 1304 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1305 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1306 | type *ptr = with_pfs ? ut::aligned_new_withkey<type>( |
| 1307 | ut::make_psi_memory_key(pfs_key), alignment, 1) |
| 1308 | : ut::aligned_new<type>(alignment, 1); |
| 1309 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1310 | EXPECT_EQ(*ptr, 1); |
| 1311 | ut::aligned_delete(ptr); |
| 1312 | } |
| 1313 | } |
| 1314 | REGISTER_TYPED_TEST_SUITE_P(aligned_new_delete_fundamental_types, |
| 1315 | fundamental_types); |
| 1316 | INSTANTIATE_TYPED_TEST_SUITE_P(My, aligned_new_delete_fundamental_types, |
| 1317 | all_fundamental_types); |
| 1318 | |
| 1319 | // aligned new/delete - pod types |
| 1320 | template <typename T> |
| 1321 | class aligned_new_delete_pod_types : public ::testing::Test {}; |
| 1322 | TYPED_TEST_SUITE_P(aligned_new_delete_pod_types); |
| 1323 | TYPED_TEST_P(aligned_new_delete_pod_types, pod_types) { |
| 1324 | using type = typename TypeParam::type; |
| 1325 | auto with_pfs = TypeParam::with_pfs; |
| 1326 | |
| 1327 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1328 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1329 | type *ptr = with_pfs |
| 1330 | ? ut::aligned_new_withkey<type>( |
| 1331 | ut::make_psi_memory_key(pfs_key), alignment, 2, 5) |
| 1332 | : ut::aligned_new<type>(alignment, 2, 5); |
| 1333 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1334 | EXPECT_EQ(ptr->x, 2); |
| 1335 | EXPECT_EQ(ptr->y, 5); |
| 1336 | ut::aligned_delete(ptr); |
| 1337 | } |
| 1338 | } |
| 1339 | REGISTER_TYPED_TEST_SUITE_P(aligned_new_delete_pod_types, pod_types); |
| 1340 | INSTANTIATE_TYPED_TEST_SUITE_P(My, aligned_new_delete_pod_types, all_pod_types); |
| 1341 | |
| 1342 | // aligned new/delete - non-pod types |
| 1343 | template <typename T> |
| 1344 | class aligned_new_delete_non_pod_types : public ::testing::Test {}; |
| 1345 | TYPED_TEST_SUITE_P(aligned_new_delete_non_pod_types); |
| 1346 | TYPED_TEST_P(aligned_new_delete_non_pod_types, non_pod_types) { |
| 1347 | using type = typename TypeParam::type; |
| 1348 | auto with_pfs = TypeParam::with_pfs; |
| 1349 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1350 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1351 | EXPECT_EQ(type::get_count(), 0); |
| 1352 | type *ptr = |
| 1353 | with_pfs |
| 1354 | ? ut::aligned_new_withkey<type>(ut::make_psi_memory_key(pfs_key), |
| 1355 | alignment, 2, 5, |
| 1356 | std::string("non-pod")) |
| 1357 | : ut::aligned_new<type>(alignment, 2, 5, std::string("non-pod")); |
| 1358 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1359 | EXPECT_EQ(ptr->x, 2); |
| 1360 | EXPECT_EQ(ptr->y, 5); |
| 1361 | EXPECT_EQ(ptr->sum.result, 7); |
| 1362 | EXPECT_EQ(ptr->s, std::string("non-pod")); |
| 1363 | EXPECT_EQ(type::get_count(), 1); |
| 1364 | ut::aligned_delete(ptr); |
| 1365 | EXPECT_EQ(type::get_count(), 0); |
| 1366 | } |
| 1367 | } |
| 1368 | REGISTER_TYPED_TEST_SUITE_P(aligned_new_delete_non_pod_types, non_pod_types); |
| 1369 | INSTANTIATE_TYPED_TEST_SUITE_P(My, aligned_new_delete_non_pod_types, |
| 1370 | all_non_pod_types); |
| 1371 | |
| 1372 | // aligned new/delete - array specialization for fundamental types |
| 1373 | template <typename T> |
| 1374 | class aligned_new_delete_fundamental_types_arr : public ::testing::Test {}; |
| 1375 | TYPED_TEST_SUITE_P(aligned_new_delete_fundamental_types_arr); |
| 1376 | TYPED_TEST_P(aligned_new_delete_fundamental_types_arr, fundamental_types) { |
| 1377 | using type = typename TypeParam::type; |
| 1378 | auto with_pfs = TypeParam::with_pfs; |
| 1379 | constexpr size_t n_elements = 10; |
| 1380 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1381 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1382 | type *ptr = with_pfs ? ut::aligned_new_arr_withkey<type>( |
| 1383 | ut::make_psi_memory_key(pfs_key), alignment, |
| 1384 | std::forward_as_tuple((type)0), |
| 1385 | std::forward_as_tuple((type)1), |
| 1386 | std::forward_as_tuple((type)2), |
| 1387 | std::forward_as_tuple((type)3), |
| 1388 | std::forward_as_tuple((type)4), |
| 1389 | std::forward_as_tuple((type)5), |
| 1390 | std::forward_as_tuple((type)6), |
| 1391 | std::forward_as_tuple((type)7), |
| 1392 | std::forward_as_tuple((type)8), |
| 1393 | std::forward_as_tuple((type)9)) |
| 1394 | : ut::aligned_new_arr<type>( |
| 1395 | alignment, std::forward_as_tuple((type)0), |
| 1396 | std::forward_as_tuple((type)1), |
| 1397 | std::forward_as_tuple((type)2), |
| 1398 | std::forward_as_tuple((type)3), |
| 1399 | std::forward_as_tuple((type)4), |
| 1400 | std::forward_as_tuple((type)5), |
| 1401 | std::forward_as_tuple((type)6), |
| 1402 | std::forward_as_tuple((type)7), |
| 1403 | std::forward_as_tuple((type)8), |
| 1404 | std::forward_as_tuple((type)9)); |
| 1405 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1406 | |
| 1407 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 1408 | EXPECT_EQ(ptr[elem], elem); |
| 1409 | } |
| 1410 | ut::aligned_delete_arr(ptr); |
| 1411 | } |
| 1412 | } |
| 1413 | REGISTER_TYPED_TEST_SUITE_P(aligned_new_delete_fundamental_types_arr, |
| 1414 | fundamental_types); |
| 1415 | INSTANTIATE_TYPED_TEST_SUITE_P(My, aligned_new_delete_fundamental_types_arr, |
| 1416 | all_fundamental_types); |
| 1417 | |
| 1418 | // aligned new/delete - array specialization for pod types |
| 1419 | template <typename T> |
| 1420 | class aligned_new_delete_pod_types_arr : public ::testing::Test {}; |
| 1421 | TYPED_TEST_SUITE_P(aligned_new_delete_pod_types_arr); |
| 1422 | TYPED_TEST_P(aligned_new_delete_pod_types_arr, pod_types) { |
| 1423 | using type = typename TypeParam::type; |
| 1424 | auto with_pfs = TypeParam::with_pfs; |
| 1425 | constexpr size_t n_elements = 5; |
| 1426 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1427 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1428 | type *ptr = |
| 1429 | with_pfs |
| 1430 | ? ut::aligned_new_arr_withkey<type>( |
| 1431 | ut::make_psi_memory_key(pfs_key), alignment, |
| 1432 | std::forward_as_tuple(0, 1), std::forward_as_tuple(2, 3), |
| 1433 | std::forward_as_tuple(4, 5), std::forward_as_tuple(6, 7), |
| 1434 | std::forward_as_tuple(8, 9)) |
| 1435 | : ut::aligned_new_arr<type>( |
| 1436 | alignment, std::forward_as_tuple(0, 1), |
| 1437 | std::forward_as_tuple(2, 3), std::forward_as_tuple(4, 5), |
| 1438 | std::forward_as_tuple(6, 7), std::forward_as_tuple(8, 9)); |
| 1439 | |
| 1440 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1441 | |
| 1442 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 1443 | EXPECT_EQ(ptr[elem].x, 2 * elem); |
| 1444 | EXPECT_EQ(ptr[elem].y, 2 * elem + 1); |
| 1445 | } |
| 1446 | ut::aligned_delete_arr(ptr); |
| 1447 | } |
| 1448 | } |
| 1449 | REGISTER_TYPED_TEST_SUITE_P(aligned_new_delete_pod_types_arr, pod_types); |
| 1450 | INSTANTIATE_TYPED_TEST_SUITE_P(My, aligned_new_delete_pod_types_arr, |
| 1451 | all_pod_types); |
| 1452 | |
| 1453 | // aligned new/delete - array specialization for non-pod types |
| 1454 | template <typename T> |
| 1455 | class aligned_new_delete_non_pod_types_arr : public ::testing::Test {}; |
| 1456 | TYPED_TEST_SUITE_P(aligned_new_delete_non_pod_types_arr); |
| 1457 | TYPED_TEST_P(aligned_new_delete_non_pod_types_arr, non_pod_types) { |
| 1458 | using type = typename TypeParam::type; |
| 1459 | auto with_pfs = TypeParam::with_pfs; |
| 1460 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1461 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1462 | EXPECT_EQ(type::get_count(), 0); |
| 1463 | type *ptr = |
| 1464 | with_pfs ? ut::aligned_new_arr_withkey<type>( |
| 1465 | ut::make_psi_memory_key(pfs_key), alignment, |
| 1466 | std::forward_as_tuple(1, 2, std::string("a")), |
| 1467 | std::forward_as_tuple(3, 4, std::string("b")), |
| 1468 | std::forward_as_tuple(5, 6, std::string("c")), |
| 1469 | std::forward_as_tuple(7, 8, std::string("d")), |
| 1470 | std::forward_as_tuple(9, 10, std::string("e"))) |
| 1471 | : ut::aligned_new_arr<type>( |
| 1472 | alignment, std::forward_as_tuple(1, 2, std::string("a")), |
| 1473 | std::forward_as_tuple(3, 4, std::string("b")), |
| 1474 | std::forward_as_tuple(5, 6, std::string("c")), |
| 1475 | std::forward_as_tuple(7, 8, std::string("d")), |
| 1476 | std::forward_as_tuple(9, 10, std::string("e"))); |
| 1477 | |
| 1478 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1479 | |
| 1480 | EXPECT_EQ(ptr[0].x, 1); |
| 1481 | EXPECT_EQ(ptr[0].y, 2); |
| 1482 | EXPECT_TRUE(ptr[0].s == std::string("a")); |
| 1483 | EXPECT_EQ(ptr[0].sum.result, 3); |
| 1484 | |
| 1485 | EXPECT_EQ(ptr[1].x, 3); |
| 1486 | EXPECT_EQ(ptr[1].y, 4); |
| 1487 | EXPECT_TRUE(ptr[1].s == std::string("b")); |
| 1488 | EXPECT_EQ(ptr[1].sum.result, 7); |
| 1489 | |
| 1490 | EXPECT_EQ(ptr[2].x, 5); |
| 1491 | EXPECT_EQ(ptr[2].y, 6); |
| 1492 | EXPECT_TRUE(ptr[2].s == std::string("c")); |
| 1493 | EXPECT_EQ(ptr[2].sum.result, 11); |
| 1494 | |
| 1495 | EXPECT_EQ(ptr[3].x, 7); |
| 1496 | EXPECT_EQ(ptr[3].y, 8); |
| 1497 | EXPECT_TRUE(ptr[3].s == std::string("d")); |
| 1498 | EXPECT_EQ(ptr[3].sum.result, 15); |
| 1499 | |
| 1500 | EXPECT_EQ(ptr[4].x, 9); |
| 1501 | EXPECT_EQ(ptr[4].y, 10); |
| 1502 | EXPECT_TRUE(ptr[4].s == std::string("e")); |
| 1503 | EXPECT_EQ(ptr[4].sum.result, 19); |
| 1504 | |
| 1505 | EXPECT_EQ(type::get_count(), 5); |
| 1506 | ut::aligned_delete_arr(ptr); |
| 1507 | EXPECT_EQ(type::get_count(), 0); |
| 1508 | } |
| 1509 | } |
| 1510 | REGISTER_TYPED_TEST_SUITE_P(aligned_new_delete_non_pod_types_arr, |
| 1511 | non_pod_types); |
| 1512 | INSTANTIATE_TYPED_TEST_SUITE_P(My, aligned_new_delete_non_pod_types_arr, |
| 1513 | all_non_pod_types); |
| 1514 | |
| 1515 | // aligned new/delete - array specialization for default constructible |
| 1516 | // fundamental types |
| 1517 | template <typename T> |
| 1518 | class aligned_new_delete_default_constructible_fundamental_types_arr |
| 1519 | : public ::testing::Test {}; |
| 1520 | TYPED_TEST_SUITE_P( |
| 1521 | aligned_new_delete_default_constructible_fundamental_types_arr); |
| 1522 | TYPED_TEST_P(aligned_new_delete_default_constructible_fundamental_types_arr, |
| 1523 | fundamental_types) { |
| 1524 | using type = typename TypeParam::type; |
| 1525 | auto with_pfs = TypeParam::with_pfs; |
| 1526 | constexpr size_t n_elements = 5; |
| 1527 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1528 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1529 | type *ptr = |
| 1530 | with_pfs ? ut::aligned_new_arr_withkey<type>( |
| 1531 | ut::make_psi_memory_key(pfs_key), alignment, |
| 1532 | ut::Count{n_elements}) |
| 1533 | : ut::aligned_new_arr<type>(alignment, ut::Count{n_elements}); |
| 1534 | |
| 1535 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1536 | |
| 1537 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 1538 | EXPECT_EQ(ptr[elem], type{}); |
| 1539 | } |
| 1540 | ut::aligned_delete_arr(ptr); |
| 1541 | } |
| 1542 | } |
| 1543 | REGISTER_TYPED_TEST_SUITE_P( |
| 1544 | aligned_new_delete_default_constructible_fundamental_types_arr, |
| 1545 | fundamental_types); |
| 1546 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 1547 | My, aligned_new_delete_default_constructible_fundamental_types_arr, |
| 1548 | all_fundamental_types); |
| 1549 | |
| 1550 | // aligned new/delete - array specialization for default constructible pod types |
| 1551 | template <typename T> |
| 1552 | class aligned_new_delete_default_constructible_pod_types_arr |
| 1553 | : public ::testing::Test {}; |
| 1554 | TYPED_TEST_SUITE_P(aligned_new_delete_default_constructible_pod_types_arr); |
| 1555 | TYPED_TEST_P(aligned_new_delete_default_constructible_pod_types_arr, |
| 1556 | pod_types) { |
| 1557 | using type = typename TypeParam::type; |
| 1558 | auto with_pfs = TypeParam::with_pfs; |
| 1559 | constexpr size_t n_elements = 5; |
| 1560 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1561 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1562 | type *ptr = |
| 1563 | with_pfs ? ut::aligned_new_arr_withkey<type>( |
| 1564 | ut::make_psi_memory_key(pfs_key), alignment, |
| 1565 | ut::Count{n_elements}) |
| 1566 | : ut::aligned_new_arr<type>(alignment, ut::Count{n_elements}); |
| 1567 | |
| 1568 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1569 | |
| 1570 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 1571 | EXPECT_EQ(ptr[elem].x, 0); |
| 1572 | EXPECT_EQ(ptr[elem].y, 1); |
| 1573 | } |
| 1574 | ut::aligned_delete_arr(ptr); |
| 1575 | } |
| 1576 | } |
| 1577 | REGISTER_TYPED_TEST_SUITE_P( |
| 1578 | aligned_new_delete_default_constructible_pod_types_arr, pod_types); |
| 1579 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 1580 | My, aligned_new_delete_default_constructible_pod_types_arr, |
| 1581 | all_default_constructible_pod_types); |
| 1582 | |
| 1583 | // aligned new/delete - array specialization for default constructible non-pod |
| 1584 | // types |
| 1585 | template <typename T> |
| 1586 | class aligned_new_delete_default_constructible_non_pod_types_arr |
| 1587 | : public ::testing::Test {}; |
| 1588 | TYPED_TEST_SUITE_P(aligned_new_delete_default_constructible_non_pod_types_arr); |
| 1589 | TYPED_TEST_P(aligned_new_delete_default_constructible_non_pod_types_arr, |
| 1590 | pod_types) { |
| 1591 | using type = typename TypeParam::type; |
| 1592 | auto with_pfs = TypeParam::with_pfs; |
| 1593 | constexpr size_t n_elements = 5; |
| 1594 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1595 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1596 | type *ptr = |
| 1597 | with_pfs ? ut::aligned_new_arr_withkey<type>( |
| 1598 | ut::make_psi_memory_key(pfs_key), alignment, |
| 1599 | ut::Count{n_elements}) |
| 1600 | : ut::aligned_new_arr<type>(alignment, ut::Count{n_elements}); |
| 1601 | |
| 1602 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1603 | |
| 1604 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 1605 | EXPECT_EQ(ptr[elem].x, 0); |
| 1606 | EXPECT_EQ(ptr[elem].y, 1); |
| 1607 | EXPECT_TRUE(ptr[elem].s == std::string("non-pod-string")); |
| 1608 | } |
| 1609 | ut::aligned_delete_arr(ptr); |
| 1610 | } |
| 1611 | } |
| 1612 | REGISTER_TYPED_TEST_SUITE_P( |
| 1613 | aligned_new_delete_default_constructible_non_pod_types_arr, pod_types); |
| 1614 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 1615 | My, aligned_new_delete_default_constructible_non_pod_types_arr, |
| 1616 | all_default_constructible_non_pod_types); |
| 1617 | |
| 1618 | TEST(aligned_new_delete, unique_ptr_demo) { |
| 1619 | constexpr auto alignment = 4 * 1024; |
| 1620 | struct Aligned_int_deleter { |
| 1621 | void operator()(int *p) { |
| 1622 | std::cout << "Hello from custom deleter!\n"; |
| 1623 | ut::aligned_delete(p); |
| 1624 | } |
| 1625 | }; |
| 1626 | std::unique_ptr<int, Aligned_int_deleter> ptr( |
| 1627 | ut::aligned_new<int>(alignment, 1), Aligned_int_deleter{}); |
| 1628 | } |
| 1629 | |
| 1630 | TEST(aligned_new_delete_arr, unique_ptr_demo) { |
| 1631 | constexpr auto alignment = 4 * 1024; |
| 1632 | struct Aligned_int_arr_deleter { |
| 1633 | void operator()(int *p) { |
| 1634 | std::cout << "Hello from custom deleter!\n"; |
| 1635 | ut::aligned_delete_arr(p); |
| 1636 | } |
| 1637 | }; |
| 1638 | std::unique_ptr<int, Aligned_int_arr_deleter> ptr( |
| 1639 | ut::aligned_new_arr<int>( |
| 1640 | alignment, std::forward_as_tuple(1), std::forward_as_tuple(2), |
| 1641 | std::forward_as_tuple(3), std::forward_as_tuple(4), |
| 1642 | std::forward_as_tuple(5)), |
| 1643 | Aligned_int_arr_deleter{}); |
| 1644 | } |
| 1645 | |
| 1646 | TEST(aligned_new_delete_arr, distance_between_elements_in_arr) { |
| 1647 | using type = Default_constructible_pod; |
| 1648 | constexpr size_t n_elements = 5; |
| 1649 | for (auto alignment = 2 * alignof(std::max_align_t); |
| 1650 | alignment < 1024 * 1024 + 1; alignment *= 2) { |
| 1651 | type *ptr = ut::aligned_new_arr<type>(alignment, ut::Count{n_elements}); |
| 1652 | |
| 1653 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr) % alignment == 0); |
| 1654 | |
| 1655 | for (size_t elem = 1; elem < n_elements; elem++) { |
| 1656 | auto addr_curr = reinterpret_cast<std::uintptr_t>(&ptr[elem]); |
| 1657 | auto addr_prev = reinterpret_cast<std::uintptr_t>(&ptr[elem - 1]); |
| 1658 | auto distance = addr_curr - addr_prev; |
| 1659 | EXPECT_EQ(distance, sizeof(type)); |
| 1660 | } |
| 1661 | ut::aligned_delete_arr(ptr); |
| 1662 | } |
| 1663 | } |
| 1664 | |
| 1665 | TEST( |
| 1666 | ut0new_aligned_new_delete_arr, |
| 1667 | destructors_of_successfully_instantiated_trivially_constructible_elements_are_invoked_when_one_of_the_element_constructors_throws) { |
| 1668 | static int n_constructors = 0; |
| 1669 | static int n_destructors = 0; |
| 1670 | |
| 1671 | struct Type_that_may_throw { |
| 1672 | Type_that_may_throw() { |
| 1673 | n_constructors++; |
| 1674 | if (n_constructors % 4 == 0) { |
| 1675 | throw std::runtime_error("cannot construct"); |
| 1676 | } |
| 1677 | } |
| 1678 | ~Type_that_may_throw() { ++n_destructors; } |
| 1679 | }; |
| 1680 | |
| 1681 | bool exception_thrown_and_caught = false; |
| 1682 | try { |
| 1683 | auto ptr = ut::aligned_new_arr_withkey<Type_that_may_throw>( |
| 1684 | ut::make_psi_memory_key(pfs_key), 2 * alignof(std::max_align_t), |
| 1685 | ut::Count(7)); |
| 1686 | ASSERT_FALSE(ptr); |
| 1687 | } catch (std::runtime_error &) { |
| 1688 | exception_thrown_and_caught = true; |
| 1689 | } |
| 1690 | EXPECT_TRUE(exception_thrown_and_caught); |
| 1691 | EXPECT_EQ(n_constructors, 4); |
| 1692 | EXPECT_EQ(n_destructors, 3); |
| 1693 | } |
| 1694 | |
| 1695 | TEST( |
| 1696 | ut0new_aligned_new_delete_arr, |
| 1697 | destructors_of_successfully_instantiated_non_trivially_constructible_elements_are_invoked_when_one_of_the_element_constructors_throws) { |
| 1698 | static int n_constructors = 0; |
| 1699 | static int n_destructors = 0; |
| 1700 | |
| 1701 | struct Type_that_may_throw { |
| 1702 | Type_that_may_throw(int x, int y) : _x(x), _y(y) { |
| 1703 | n_constructors++; |
| 1704 | if (n_constructors % 4 == 0) { |
| 1705 | throw std::runtime_error("cannot construct"); |
| 1706 | } |
| 1707 | } |
| 1708 | ~Type_that_may_throw() { ++n_destructors; } |
| 1709 | int _x, _y; |
| 1710 | }; |
| 1711 | |
| 1712 | bool exception_thrown_and_caught = false; |
| 1713 | try { |
| 1714 | auto ptr = ut::aligned_new_arr_withkey<Type_that_may_throw>( |
| 1715 | ut::make_psi_memory_key(pfs_key), 2 * alignof(std::max_align_t), |
| 1716 | std::forward_as_tuple(0, 1), std::forward_as_tuple(2, 3), |
| 1717 | std::forward_as_tuple(4, 5), std::forward_as_tuple(6, 7), |
| 1718 | std::forward_as_tuple(8, 9)); |
| 1719 | ASSERT_FALSE(ptr); |
| 1720 | } catch (std::runtime_error &) { |
| 1721 | exception_thrown_and_caught = true; |
| 1722 | } |
| 1723 | EXPECT_TRUE(exception_thrown_and_caught); |
| 1724 | EXPECT_EQ(n_constructors, 4); |
| 1725 | EXPECT_EQ(n_destructors, 3); |
| 1726 | } |
| 1727 | |
| 1728 | TEST( |
| 1729 | ut0new_aligned_new_delete_arr, |
| 1730 | no_destructors_are_invoked_when_first_trivially_constructible_element_constructor_throws) { |
| 1731 | static int n_constructors = 0; |
| 1732 | static int n_destructors = 0; |
| 1733 | |
| 1734 | struct Type_that_always_throws { |
| 1735 | Type_that_always_throws() { |
| 1736 | n_constructors++; |
| 1737 | throw std::runtime_error("cannot construct"); |
| 1738 | } |
| 1739 | ~Type_that_always_throws() { ++n_destructors; } |
| 1740 | }; |
| 1741 | |
| 1742 | bool exception_thrown_and_caught = false; |
| 1743 | try { |
| 1744 | auto ptr = ut::aligned_new_arr_withkey<Type_that_always_throws>( |
| 1745 | ut::make_psi_memory_key(pfs_key), 2 * alignof(std::max_align_t), |
| 1746 | ut::Count(7)); |
| 1747 | ASSERT_FALSE(ptr); |
| 1748 | } catch (std::runtime_error &) { |
| 1749 | exception_thrown_and_caught = true; |
| 1750 | } |
| 1751 | EXPECT_TRUE(exception_thrown_and_caught); |
| 1752 | EXPECT_EQ(n_constructors, 1); |
| 1753 | EXPECT_EQ(n_destructors, 0); |
| 1754 | } |
| 1755 | |
| 1756 | TEST( |
| 1757 | ut0new_aligned_new_delete_arr, |
| 1758 | no_destructors_are_invoked_when_first_non_trivially_constructible_element_constructor_throws) { |
| 1759 | static int n_constructors = 0; |
| 1760 | static int n_destructors = 0; |
| 1761 | |
| 1762 | struct Type_that_always_throws { |
| 1763 | Type_that_always_throws(int x, int y) : _x(x), _y(y) { |
| 1764 | n_constructors++; |
| 1765 | throw std::runtime_error("cannot construct"); |
| 1766 | } |
| 1767 | ~Type_that_always_throws() { ++n_destructors; } |
| 1768 | int _x, _y; |
| 1769 | }; |
| 1770 | |
| 1771 | bool exception_thrown_and_caught = false; |
| 1772 | try { |
| 1773 | auto ptr = ut::aligned_new_arr_withkey<Type_that_always_throws>( |
| 1774 | ut::make_psi_memory_key(pfs_key), 2 * alignof(std::max_align_t), |
| 1775 | std::forward_as_tuple(0, 1), std::forward_as_tuple(2, 3), |
| 1776 | std::forward_as_tuple(4, 5), std::forward_as_tuple(6, 7), |
| 1777 | std::forward_as_tuple(8, 9)); |
| 1778 | ASSERT_FALSE(ptr); |
| 1779 | } catch (std::runtime_error &) { |
| 1780 | exception_thrown_and_caught = true; |
| 1781 | } |
| 1782 | EXPECT_TRUE(exception_thrown_and_caught); |
| 1783 | EXPECT_EQ(n_constructors, 1); |
| 1784 | EXPECT_EQ(n_destructors, 0); |
| 1785 | } |
| 1786 | |
| 1787 | TEST(ut0new_aligned_new_delete, |
| 1788 | no_destructor_is_invoked_when_no_object_is_successfully_constructed) { |
| 1789 | static int n_constructors = 0; |
| 1790 | static int n_destructors = 0; |
| 1791 | |
| 1792 | struct Type_that_always_throws { |
| 1793 | Type_that_always_throws() { |
| 1794 | n_constructors++; |
| 1795 | throw std::runtime_error("cannot construct"); |
| 1796 | } |
| 1797 | ~Type_that_always_throws() { ++n_destructors; } |
| 1798 | }; |
| 1799 | |
| 1800 | bool exception_thrown_and_caught = false; |
| 1801 | try { |
| 1802 | auto ptr = ut::aligned_new_withkey<Type_that_always_throws>( |
| 1803 | ut::make_psi_memory_key(pfs_key), 2 * alignof(std::max_align_t)); |
| 1804 | ASSERT_FALSE(ptr); |
| 1805 | } catch (std::runtime_error &) { |
| 1806 | exception_thrown_and_caught = true; |
| 1807 | } |
| 1808 | EXPECT_TRUE(exception_thrown_and_caught); |
| 1809 | EXPECT_EQ(n_constructors, 1); |
| 1810 | EXPECT_EQ(n_destructors, 0); |
| 1811 | } |
| 1812 | |
| 1813 | TEST(aligned_pointer, access_data_through_implicit_conversion_operator) { |
| 1814 | constexpr auto alignment = 4 * 1024; |
| 1815 | ut::aligned_pointer<int, alignment> ptr; |
| 1816 | ptr.alloc(); |
| 1817 | |
| 1818 | int *data = ptr; |
| 1819 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(data) % alignment == 0); |
| 1820 | EXPECT_EQ(*data, int{}); |
| 1821 | |
| 1822 | ptr.dealloc(); |
| 1823 | } |
| 1824 | |
| 1825 | TEST(aligned_array_pointer, access_data_through_subscript_operator) { |
| 1826 | constexpr auto n_elements = 5; |
| 1827 | constexpr auto alignment = 4 * 1024; |
| 1828 | ut::aligned_array_pointer<Default_constructible_pod, alignment> ptr; |
| 1829 | ptr.alloc(ut::Count{n_elements}); |
| 1830 | |
| 1831 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(&ptr[0]) % alignment == 0); |
| 1832 | for (size_t elem = 0; elem < n_elements; elem++) { |
| 1833 | EXPECT_EQ(ptr[elem].x, 0); |
| 1834 | EXPECT_EQ(ptr[elem].y, 1); |
| 1835 | } |
| 1836 | |
| 1837 | ptr.dealloc(); |
| 1838 | } |
| 1839 | |
| 1840 | TEST(aligned_array_pointer, initialize_an_array_of_non_pod_types) { |
| 1841 | constexpr auto alignment = 4 * 1024; |
| 1842 | ut::aligned_array_pointer<Non_pod_type, alignment> ptr; |
| 1843 | EXPECT_EQ(Non_pod_type::get_count(), 0); |
| 1844 | ptr.alloc(std::forward_as_tuple(1, 2, std::string("a")), |
| 1845 | std::forward_as_tuple(3, 4, std::string("b")), |
| 1846 | std::forward_as_tuple(5, 6, std::string("c")), |
| 1847 | std::forward_as_tuple(7, 8, std::string("d")), |
| 1848 | std::forward_as_tuple(9, 10, std::string("e"))); |
| 1849 | |
| 1850 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(&ptr[0]) % alignment == 0); |
| 1851 | |
| 1852 | EXPECT_EQ(ptr[0].x, 1); |
| 1853 | EXPECT_EQ(ptr[0].y, 2); |
| 1854 | EXPECT_TRUE(ptr[0].s == std::string("a")); |
| 1855 | |
| 1856 | EXPECT_EQ(ptr[1].x, 3); |
| 1857 | EXPECT_EQ(ptr[1].y, 4); |
| 1858 | EXPECT_TRUE(ptr[1].s == std::string("b")); |
| 1859 | |
| 1860 | EXPECT_EQ(ptr[2].x, 5); |
| 1861 | EXPECT_EQ(ptr[2].y, 6); |
| 1862 | EXPECT_TRUE(ptr[2].s == std::string("c")); |
| 1863 | |
| 1864 | EXPECT_EQ(ptr[3].x, 7); |
| 1865 | EXPECT_EQ(ptr[3].y, 8); |
| 1866 | EXPECT_TRUE(ptr[3].s == std::string("d")); |
| 1867 | |
| 1868 | EXPECT_EQ(ptr[4].x, 9); |
| 1869 | EXPECT_EQ(ptr[4].y, 10); |
| 1870 | EXPECT_TRUE(ptr[4].s == std::string("e")); |
| 1871 | |
| 1872 | EXPECT_EQ(Non_pod_type::get_count(), 5); |
| 1873 | ptr.dealloc(); |
| 1874 | EXPECT_EQ(Non_pod_type::get_count(), 0); |
| 1875 | } |
| 1876 | |
| 1877 | TEST(aligned_array_pointer, distance_between_elements_in_arr) { |
| 1878 | constexpr auto n_elements = 5; |
| 1879 | constexpr auto alignment = 4 * 1024; |
| 1880 | ut::aligned_array_pointer<Default_constructible_pod, alignment> ptr; |
| 1881 | ptr.alloc(ut::Count{n_elements}); |
| 1882 | |
| 1883 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(&ptr[0]) % alignment == 0); |
| 1884 | |
| 1885 | for (size_t elem = 1; elem < n_elements; elem++) { |
| 1886 | auto addr_curr = reinterpret_cast<std::uintptr_t>(&ptr[elem]); |
| 1887 | auto addr_prev = reinterpret_cast<std::uintptr_t>(&ptr[elem - 1]); |
| 1888 | auto distance = addr_curr - addr_prev; |
| 1889 | EXPECT_EQ(distance, sizeof(Default_constructible_pod)); |
| 1890 | } |
| 1891 | |
| 1892 | ptr.dealloc(); |
| 1893 | } |
| 1894 | |
| 1895 | template <bool With_pfs, typename T> |
| 1896 | struct select_allocator_variant { |
| 1897 | using type = ut::detail::allocator_base<T>; |
| 1898 | }; |
| 1899 | template <typename T> |
| 1900 | struct select_allocator_variant<false, T> { |
| 1901 | using type = ut::detail::allocator_base_pfs<T>; |
| 1902 | }; |
| 1903 | template <bool With_pfs, typename T> |
| 1904 | using select_allocator_variant_t = |
| 1905 | typename select_allocator_variant<With_pfs, T>::type; |
| 1906 | |
| 1907 | // allocator - fundamental types |
| 1908 | template <typename T> |
| 1909 | class ut0new_allocator_fundamental_types : public ::testing::Test {}; |
| 1910 | TYPED_TEST_SUITE_P(ut0new_allocator_fundamental_types); |
| 1911 | TYPED_TEST_P(ut0new_allocator_fundamental_types, fundamental_types) { |
| 1912 | using T = typename TypeParam::type; |
| 1913 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 1914 | |
| 1915 | auto n_elements = 100; |
| 1916 | ut::allocator<T, allocator_variant> a(pfs_key); |
| 1917 | |
| 1918 | auto ptr = a.allocate(n_elements); |
| 1919 | |
| 1920 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 1921 | |
| 1922 | ptr[0] = std::numeric_limits<T>::max(); |
| 1923 | EXPECT_EQ(ptr[0], std::numeric_limits<T>::max()); |
| 1924 | ptr[n_elements - 1] = std::numeric_limits<T>::min(); |
| 1925 | EXPECT_EQ(ptr[n_elements - 1], std::numeric_limits<T>::min()); |
| 1926 | |
| 1927 | a.deallocate(ptr); |
| 1928 | } |
| 1929 | REGISTER_TYPED_TEST_SUITE_P(ut0new_allocator_fundamental_types, |
| 1930 | fundamental_types); |
| 1931 | INSTANTIATE_TYPED_TEST_SUITE_P(FundamentalTypes, |
| 1932 | ut0new_allocator_fundamental_types, |
| 1933 | all_fundamental_types); |
| 1934 | |
| 1935 | // allocator - pod types |
| 1936 | template <typename T> |
| 1937 | class ut0new_allocator_pod_types : public ::testing::Test {}; |
| 1938 | TYPED_TEST_SUITE_P(ut0new_allocator_pod_types); |
| 1939 | TYPED_TEST_P(ut0new_allocator_pod_types, pod_types) { |
| 1940 | using T = typename TypeParam::type; |
| 1941 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 1942 | |
| 1943 | auto n_elements = 100; |
| 1944 | ut::allocator<T, allocator_variant> a(pfs_key); |
| 1945 | |
| 1946 | auto ptr = a.allocate(n_elements); |
| 1947 | |
| 1948 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 1949 | |
| 1950 | a.deallocate(ptr); |
| 1951 | } |
| 1952 | REGISTER_TYPED_TEST_SUITE_P(ut0new_allocator_pod_types, pod_types); |
| 1953 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, ut0new_allocator_pod_types, |
| 1954 | all_pod_types); |
| 1955 | |
| 1956 | // allocator - non_pod types |
| 1957 | template <typename T> |
| 1958 | class ut0new_allocator_non_pod_types : public ::testing::Test {}; |
| 1959 | TYPED_TEST_SUITE_P(ut0new_allocator_non_pod_types); |
| 1960 | TYPED_TEST_P(ut0new_allocator_non_pod_types, non_pod_types) { |
| 1961 | using T = typename TypeParam::type; |
| 1962 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 1963 | |
| 1964 | auto n_elements = 100; |
| 1965 | ut::allocator<T, allocator_variant> a(pfs_key); |
| 1966 | |
| 1967 | auto ptr = a.allocate(n_elements); |
| 1968 | |
| 1969 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr)); |
| 1970 | |
| 1971 | a.deallocate(ptr); |
| 1972 | } |
| 1973 | REGISTER_TYPED_TEST_SUITE_P(ut0new_allocator_non_pod_types, non_pod_types); |
| 1974 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, ut0new_allocator_non_pod_types, |
| 1975 | all_non_pod_types); |
| 1976 | |
| 1977 | // allocator - std::vector with fundamental types |
| 1978 | template <typename T> |
| 1979 | class ut0new_allocator_std_vector_with_fundamental_types |
| 1980 | : public ::testing::Test {}; |
| 1981 | TYPED_TEST_SUITE_P(ut0new_allocator_std_vector_with_fundamental_types); |
| 1982 | TYPED_TEST_P(ut0new_allocator_std_vector_with_fundamental_types, |
| 1983 | std_vector_with_fundamental_types) { |
| 1984 | using T = typename TypeParam::type; |
| 1985 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 1986 | |
| 1987 | std::vector<T, ut::allocator<T, allocator_variant>> vec; |
| 1988 | vec.push_back(std::numeric_limits<T>::max()); |
| 1989 | vec.push_back(std::numeric_limits<T>::min()); |
| 1990 | vec.push_back(std::numeric_limits<T>::max()); |
| 1991 | vec.push_back(std::numeric_limits<T>::min()); |
| 1992 | |
| 1993 | EXPECT_TRUE(ptr_is_suitably_aligned(&vec[0])); |
| 1994 | |
| 1995 | EXPECT_EQ(vec[0], std::numeric_limits<T>::max()); |
| 1996 | EXPECT_EQ(vec[1], std::numeric_limits<T>::min()); |
| 1997 | EXPECT_EQ(vec[2], std::numeric_limits<T>::max()); |
| 1998 | EXPECT_EQ(vec[3], std::numeric_limits<T>::min()); |
| 1999 | } |
| 2000 | REGISTER_TYPED_TEST_SUITE_P(ut0new_allocator_std_vector_with_fundamental_types, |
| 2001 | std_vector_with_fundamental_types); |
| 2002 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2003 | FundamentalTypes, ut0new_allocator_std_vector_with_fundamental_types, |
| 2004 | all_fundamental_types); |
| 2005 | |
| 2006 | // allocator - std::vector with pod types |
| 2007 | template <typename T> |
| 2008 | class ut0new_allocator_std_vector_with_pod_types : public ::testing::Test {}; |
| 2009 | TYPED_TEST_SUITE_P(ut0new_allocator_std_vector_with_pod_types); |
| 2010 | TYPED_TEST_P(ut0new_allocator_std_vector_with_pod_types, |
| 2011 | std_vector_with_pod_types) { |
| 2012 | using T = typename TypeParam::type; |
| 2013 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 2014 | |
| 2015 | constexpr auto min = std::numeric_limits<int>::min(); |
| 2016 | constexpr auto max = std::numeric_limits<int>::max(); |
| 2017 | std::vector<T, ut::allocator<T, allocator_variant>> vec; |
| 2018 | vec.push_back({min, min}); |
| 2019 | vec.push_back({min, max}); |
| 2020 | vec.push_back({max, min}); |
| 2021 | vec.push_back({max, max}); |
| 2022 | |
| 2023 | EXPECT_TRUE(ptr_is_suitably_aligned(&vec[0])); |
| 2024 | |
| 2025 | EXPECT_EQ(vec[0].x, min); |
| 2026 | EXPECT_EQ(vec[0].y, min); |
| 2027 | EXPECT_EQ(vec[1].x, min); |
| 2028 | EXPECT_EQ(vec[1].y, max); |
| 2029 | EXPECT_EQ(vec[2].x, max); |
| 2030 | EXPECT_EQ(vec[2].y, min); |
| 2031 | EXPECT_EQ(vec[3].x, max); |
| 2032 | EXPECT_EQ(vec[3].y, max); |
| 2033 | } |
| 2034 | REGISTER_TYPED_TEST_SUITE_P(ut0new_allocator_std_vector_with_pod_types, |
| 2035 | std_vector_with_pod_types); |
| 2036 | INSTANTIATE_TYPED_TEST_SUITE_P(PodTypes, |
| 2037 | ut0new_allocator_std_vector_with_pod_types, |
| 2038 | all_pod_types); |
| 2039 | |
| 2040 | // allocator - std::vector with non_pod types |
| 2041 | template <typename T> |
| 2042 | class ut0new_allocator_std_vector_with_non_pod_types : public ::testing::Test { |
| 2043 | }; |
| 2044 | TYPED_TEST_SUITE_P(ut0new_allocator_std_vector_with_non_pod_types); |
| 2045 | TYPED_TEST_P(ut0new_allocator_std_vector_with_non_pod_types, |
| 2046 | std_vector_with_non_pod_types) { |
| 2047 | using T = typename TypeParam::type; |
| 2048 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 2049 | { |
| 2050 | std::vector<T, ut::allocator<T, allocator_variant>> vec; |
| 2051 | EXPECT_EQ(T::get_count(), 0); |
| 2052 | vec.push_back({1, 2, std::string("a")}); |
| 2053 | vec.push_back({3, 4, std::string("b")}); |
| 2054 | vec.push_back({5, 6, std::string("c")}); |
| 2055 | vec.push_back({7, 8, std::string("d")}); |
| 2056 | vec.push_back({9, 10, std::string("e")}); |
| 2057 | |
| 2058 | EXPECT_TRUE(ptr_is_suitably_aligned(&vec[0])); |
| 2059 | |
| 2060 | EXPECT_EQ(vec[0].x, 1); |
| 2061 | EXPECT_EQ(vec[0].y, 2); |
| 2062 | EXPECT_TRUE(vec[0].s == std::string("a")); |
| 2063 | |
| 2064 | EXPECT_EQ(vec[1].x, 3); |
| 2065 | EXPECT_EQ(vec[1].y, 4); |
| 2066 | EXPECT_TRUE(vec[1].s == std::string("b")); |
| 2067 | |
| 2068 | EXPECT_EQ(vec[2].x, 5); |
| 2069 | EXPECT_EQ(vec[2].y, 6); |
| 2070 | EXPECT_TRUE(vec[2].s == std::string("c")); |
| 2071 | |
| 2072 | EXPECT_EQ(vec[3].x, 7); |
| 2073 | EXPECT_EQ(vec[3].y, 8); |
| 2074 | EXPECT_TRUE(vec[3].s == std::string("d")); |
| 2075 | |
| 2076 | EXPECT_EQ(vec[4].x, 9); |
| 2077 | EXPECT_EQ(vec[4].y, 10); |
| 2078 | EXPECT_TRUE(vec[4].s == std::string("e")); |
| 2079 | EXPECT_EQ(T::get_count(), 5); |
| 2080 | } |
| 2081 | EXPECT_EQ(T::get_count(), 0); |
| 2082 | } |
| 2083 | REGISTER_TYPED_TEST_SUITE_P(ut0new_allocator_std_vector_with_non_pod_types, |
| 2084 | std_vector_with_non_pod_types); |
| 2085 | INSTANTIATE_TYPED_TEST_SUITE_P(NonPodTypes, |
| 2086 | ut0new_allocator_std_vector_with_non_pod_types, |
| 2087 | all_non_pod_types); |
| 2088 | |
| 2089 | // allocator - std::vector with default_constructible_pod types |
| 2090 | template <typename T> |
| 2091 | class ut0new_allocator_std_vector_with_default_constructible_pod_types |
| 2092 | : public ::testing::Test {}; |
| 2093 | TYPED_TEST_SUITE_P( |
| 2094 | ut0new_allocator_std_vector_with_default_constructible_pod_types); |
| 2095 | TYPED_TEST_P(ut0new_allocator_std_vector_with_default_constructible_pod_types, |
| 2096 | std_vector_with_default_constructible_pod_types) { |
| 2097 | using T = typename TypeParam::type; |
| 2098 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 2099 | |
| 2100 | std::vector<T, ut::allocator<T, allocator_variant>> vec; |
| 2101 | vec.push_back({}); |
| 2102 | vec.push_back({}); |
| 2103 | vec.push_back({}); |
| 2104 | vec.push_back({}); |
| 2105 | |
| 2106 | EXPECT_TRUE(ptr_is_suitably_aligned(&vec[0])); |
| 2107 | |
| 2108 | EXPECT_EQ(vec[0].x, 0); |
| 2109 | EXPECT_EQ(vec[0].y, 1); |
| 2110 | EXPECT_EQ(vec[1].x, 0); |
| 2111 | EXPECT_EQ(vec[1].y, 1); |
| 2112 | EXPECT_EQ(vec[2].x, 0); |
| 2113 | EXPECT_EQ(vec[2].y, 1); |
| 2114 | EXPECT_EQ(vec[3].x, 0); |
| 2115 | EXPECT_EQ(vec[3].y, 1); |
| 2116 | } |
| 2117 | REGISTER_TYPED_TEST_SUITE_P( |
| 2118 | ut0new_allocator_std_vector_with_default_constructible_pod_types, |
| 2119 | std_vector_with_default_constructible_pod_types); |
| 2120 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2121 | DefaultConstructiblePodTypes, |
| 2122 | ut0new_allocator_std_vector_with_default_constructible_pod_types, |
| 2123 | all_default_constructible_pod_types); |
| 2124 | |
| 2125 | // allocator - std::vector with default_non_pod_constructible types |
| 2126 | template <typename T> |
| 2127 | class ut0new_allocator_std_vector_with_default_non_pod_constructible_types |
| 2128 | : public ::testing::Test {}; |
| 2129 | TYPED_TEST_SUITE_P( |
| 2130 | ut0new_allocator_std_vector_with_default_non_pod_constructible_types); |
| 2131 | TYPED_TEST_P( |
| 2132 | ut0new_allocator_std_vector_with_default_non_pod_constructible_types, |
| 2133 | std_vector_with_default_non_pod_constructible_types) { |
| 2134 | using T = typename TypeParam::type; |
| 2135 | using allocator_variant = select_allocator_variant_t<TypeParam::with_pfs, T>; |
| 2136 | { |
| 2137 | std::vector<T, ut::allocator<T, allocator_variant>> vec; |
| 2138 | |
| 2139 | vec.push_back({}); |
| 2140 | vec.push_back({}); |
| 2141 | vec.push_back({}); |
| 2142 | vec.push_back({}); |
| 2143 | vec.push_back({}); |
| 2144 | |
| 2145 | EXPECT_TRUE(ptr_is_suitably_aligned(&vec[0])); |
| 2146 | |
| 2147 | EXPECT_EQ(vec[0].x, 0); |
| 2148 | EXPECT_EQ(vec[0].y, 1); |
| 2149 | EXPECT_TRUE(vec[0].s == std::string("non-pod-string")); |
| 2150 | |
| 2151 | EXPECT_EQ(vec[1].x, 0); |
| 2152 | EXPECT_EQ(vec[1].y, 1); |
| 2153 | EXPECT_TRUE(vec[1].s == std::string("non-pod-string")); |
| 2154 | |
| 2155 | EXPECT_EQ(vec[2].x, 0); |
| 2156 | EXPECT_EQ(vec[2].y, 1); |
| 2157 | EXPECT_TRUE(vec[2].s == std::string("non-pod-string")); |
| 2158 | |
| 2159 | EXPECT_EQ(vec[3].x, 0); |
| 2160 | EXPECT_EQ(vec[3].y, 1); |
| 2161 | EXPECT_TRUE(vec[3].s == std::string("non-pod-string")); |
| 2162 | } |
| 2163 | } |
| 2164 | REGISTER_TYPED_TEST_SUITE_P( |
| 2165 | ut0new_allocator_std_vector_with_default_non_pod_constructible_types, |
| 2166 | std_vector_with_default_non_pod_constructible_types); |
| 2167 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2168 | NonPodTypes, |
| 2169 | ut0new_allocator_std_vector_with_default_non_pod_constructible_types, |
| 2170 | all_default_constructible_non_pod_types); |
| 2171 | |
| 2172 | TEST(ut0new_allocator, throws_bad_array_new_length_when_max_size_is_exceeded) { |
| 2173 | struct big_t { |
| 2174 | char x[128]; |
| 2175 | }; |
| 2176 | |
| 2177 | ut::allocator<big_t> alloc(mem_key_buf_buf_pool); |
| 2178 | |
| 2179 | const ut::allocator<big_t>::size_type too_many_elements = |
| 2180 | std::numeric_limits<ut::allocator<big_t>::size_type>::max() / |
| 2181 | sizeof(big_t) + |
| 2182 | 1; |
| 2183 | |
| 2184 | big_t *ptr = nullptr; |
| 2185 | bool threw = false; |
| 2186 | try { |
| 2187 | ptr = alloc.allocate(too_many_elements); |
| 2188 | } catch (std::bad_array_new_length &) { |
| 2189 | threw = true; |
| 2190 | } |
| 2191 | EXPECT_TRUE(threw); |
| 2192 | EXPECT_FALSE(ptr); |
| 2193 | } |
| 2194 | |
| 2195 | // make_unique - fundamental types |
| 2196 | template <typename T> |
| 2197 | class ut0new_make_unique_ptr_fundamental_types : public ::testing::Test {}; |
| 2198 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_fundamental_types); |
| 2199 | TYPED_TEST_P(ut0new_make_unique_ptr_fundamental_types, fundamental_types) { |
| 2200 | using type = typename TypeParam::type; |
| 2201 | auto with_pfs = TypeParam::with_pfs; |
| 2202 | auto ptr = with_pfs |
| 2203 | ? ut::make_unique<type>(ut::make_psi_memory_key(pfs_key), 1) |
| 2204 | : ut::make_unique<type>(1); |
| 2205 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2206 | EXPECT_EQ(*ptr, 1); |
| 2207 | } |
| 2208 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_fundamental_types, |
| 2209 | fundamental_types); |
| 2210 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_ptr_fundamental_types, |
| 2211 | all_fundamental_types); |
| 2212 | |
| 2213 | // make_unique - pod types |
| 2214 | template <typename T> |
| 2215 | class ut0new_make_unique_ptr_pod_types : public ::testing::Test {}; |
| 2216 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_pod_types); |
| 2217 | TYPED_TEST_P(ut0new_make_unique_ptr_pod_types, pod_types) { |
| 2218 | using type = typename TypeParam::type; |
| 2219 | auto with_pfs = TypeParam::with_pfs; |
| 2220 | |
| 2221 | auto ptr = with_pfs |
| 2222 | ? ut::make_unique<type>(ut::make_psi_memory_key(pfs_key), 2, 5) |
| 2223 | : ut::make_unique<type>(2, 5); |
| 2224 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2225 | EXPECT_EQ(ptr->x, 2); |
| 2226 | EXPECT_EQ(ptr->y, 5); |
| 2227 | } |
| 2228 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_pod_types, pod_types); |
| 2229 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_ptr_pod_types, |
| 2230 | all_pod_types); |
| 2231 | |
| 2232 | // make_unique - default constructible pod types |
| 2233 | template <typename T> |
| 2234 | class ut0new_make_unique_ptr_default_constructible_pod_types |
| 2235 | : public ::testing::Test {}; |
| 2236 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_default_constructible_pod_types); |
| 2237 | TYPED_TEST_P(ut0new_make_unique_ptr_default_constructible_pod_types, |
| 2238 | default_constructible_pod_types) { |
| 2239 | using type = typename TypeParam::type; |
| 2240 | auto with_pfs = TypeParam::with_pfs; |
| 2241 | |
| 2242 | auto ptr = with_pfs ? ut::make_unique<type>(ut::make_psi_memory_key(pfs_key)) |
| 2243 | : ut::make_unique<type>(); |
| 2244 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2245 | EXPECT_EQ(ptr->x, 0); |
| 2246 | EXPECT_EQ(ptr->y, 1); |
| 2247 | } |
| 2248 | REGISTER_TYPED_TEST_SUITE_P( |
| 2249 | ut0new_make_unique_ptr_default_constructible_pod_types, |
| 2250 | default_constructible_pod_types); |
| 2251 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2252 | My, ut0new_make_unique_ptr_default_constructible_pod_types, |
| 2253 | all_default_constructible_pod_types); |
| 2254 | |
| 2255 | // make_unique - non-pod types |
| 2256 | template <typename T> |
| 2257 | class ut0new_make_unique_ptr_non_pod_types : public ::testing::Test {}; |
| 2258 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_non_pod_types); |
| 2259 | TYPED_TEST_P(ut0new_make_unique_ptr_non_pod_types, non_pod_types) { |
| 2260 | using type = typename TypeParam::type; |
| 2261 | auto with_pfs = TypeParam::with_pfs; |
| 2262 | EXPECT_EQ(type::get_count(), 0); |
| 2263 | { |
| 2264 | auto ptr = with_pfs |
| 2265 | ? ut::make_unique<type>(ut::make_psi_memory_key(pfs_key), 2, |
| 2266 | 5, std::string("non-pod")) |
| 2267 | : ut::make_unique<type>(2, 5, std::string("non-pod")); |
| 2268 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2269 | EXPECT_EQ(ptr->x, 2); |
| 2270 | EXPECT_EQ(ptr->y, 5); |
| 2271 | EXPECT_EQ(ptr->sum.result, 7); |
| 2272 | EXPECT_EQ(ptr->s, std::string("non-pod")); |
| 2273 | EXPECT_EQ(type::get_count(), 1); |
| 2274 | } |
| 2275 | EXPECT_EQ(type::get_count(), 0); |
| 2276 | } |
| 2277 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_non_pod_types, |
| 2278 | non_pod_types); |
| 2279 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_ptr_non_pod_types, |
| 2280 | all_non_pod_types); |
| 2281 | |
| 2282 | // make_unique - default constructible non-pod types |
| 2283 | template <typename T> |
| 2284 | class ut0new_make_unique_ptr_default_constructible_non_pod_types |
| 2285 | : public ::testing::Test {}; |
| 2286 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_default_constructible_non_pod_types); |
| 2287 | TYPED_TEST_P(ut0new_make_unique_ptr_default_constructible_non_pod_types, |
| 2288 | default_constructible_non_pod_types) { |
| 2289 | using type = typename TypeParam::type; |
| 2290 | auto with_pfs = TypeParam::with_pfs; |
| 2291 | { |
| 2292 | auto ptr = with_pfs |
| 2293 | ? ut::make_unique<type>(ut::make_psi_memory_key(pfs_key)) |
| 2294 | : ut::make_unique<type>(); |
| 2295 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2296 | EXPECT_EQ(ptr->x, 0); |
| 2297 | EXPECT_EQ(ptr->y, 1); |
| 2298 | EXPECT_EQ(ptr->s, std::string("non-pod-string")); |
| 2299 | } |
| 2300 | } |
| 2301 | REGISTER_TYPED_TEST_SUITE_P( |
| 2302 | ut0new_make_unique_ptr_default_constructible_non_pod_types, |
| 2303 | default_constructible_non_pod_types); |
| 2304 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2305 | My, ut0new_make_unique_ptr_default_constructible_non_pod_types, |
| 2306 | all_default_constructible_non_pod_types); |
| 2307 | |
| 2308 | // make_unique - array specialization for fundamental types |
| 2309 | template <typename T> |
| 2310 | class ut0new_make_unique_ptr_fundamental_types_arr : public ::testing::Test {}; |
| 2311 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_fundamental_types_arr); |
| 2312 | TYPED_TEST_P(ut0new_make_unique_ptr_fundamental_types_arr, fundamental_types) { |
| 2313 | using type = typename TypeParam::type; |
| 2314 | auto with_pfs = TypeParam::with_pfs; |
| 2315 | auto ptr = with_pfs |
| 2316 | ? ut::make_unique<type[]>(ut::make_psi_memory_key(pfs_key), 3) |
| 2317 | : ut::make_unique<type[]>(3); |
| 2318 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2319 | EXPECT_EQ(ptr[0], type{}); |
| 2320 | EXPECT_EQ(ptr[1], type{}); |
| 2321 | EXPECT_EQ(ptr[2], type{}); |
| 2322 | } |
| 2323 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_fundamental_types_arr, |
| 2324 | fundamental_types); |
| 2325 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_ptr_fundamental_types_arr, |
| 2326 | all_fundamental_types); |
| 2327 | |
| 2328 | // make_unique - array specialization for default_constructible POD types |
| 2329 | template <typename T> |
| 2330 | class ut0new_make_unique_ptr_default_constructible_pod_types_arr |
| 2331 | : public ::testing::Test {}; |
| 2332 | TYPED_TEST_SUITE_P(ut0new_make_unique_ptr_default_constructible_pod_types_arr); |
| 2333 | TYPED_TEST_P(ut0new_make_unique_ptr_default_constructible_pod_types_arr, |
| 2334 | default_constructible_pod_types) { |
| 2335 | using type = typename TypeParam::type; |
| 2336 | auto with_pfs = TypeParam::with_pfs; |
| 2337 | |
| 2338 | auto ptr = with_pfs |
| 2339 | ? ut::make_unique<type[]>(ut::make_psi_memory_key(pfs_key), 3) |
| 2340 | : ut::make_unique<type[]>(3); |
| 2341 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2342 | EXPECT_EQ(ptr[0].x, 0); |
| 2343 | EXPECT_EQ(ptr[0].y, 1); |
| 2344 | EXPECT_EQ(ptr[1].x, 0); |
| 2345 | EXPECT_EQ(ptr[1].y, 1); |
| 2346 | EXPECT_EQ(ptr[2].x, 0); |
| 2347 | EXPECT_EQ(ptr[2].y, 1); |
| 2348 | } |
| 2349 | REGISTER_TYPED_TEST_SUITE_P( |
| 2350 | ut0new_make_unique_ptr_default_constructible_pod_types_arr, |
| 2351 | default_constructible_pod_types); |
| 2352 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2353 | My, ut0new_make_unique_ptr_default_constructible_pod_types_arr, |
| 2354 | all_default_constructible_pod_types); |
| 2355 | |
| 2356 | // make_unique_aligned - fundamental types |
| 2357 | template <typename T> |
| 2358 | class ut0new_make_unique_aligned_fundamental_types : public ::testing::Test {}; |
| 2359 | TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_fundamental_types); |
| 2360 | TYPED_TEST_P(ut0new_make_unique_aligned_fundamental_types, fundamental_types) { |
| 2361 | using type = typename TypeParam::type; |
| 2362 | auto with_pfs = TypeParam::with_pfs; |
| 2363 | auto alignment = 4 * alignof(std::max_align_t); |
| 2364 | auto ptr = with_pfs ? ut::make_unique_aligned<type>( |
| 2365 | ut::make_psi_memory_key(pfs_key), alignment, 1) |
| 2366 | : ut::make_unique_aligned<type>(alignment, 1); |
| 2367 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2368 | EXPECT_EQ(*ptr, 1); |
| 2369 | } |
| 2370 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_fundamental_types, |
| 2371 | fundamental_types); |
| 2372 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_aligned_fundamental_types, |
| 2373 | all_fundamental_types); |
| 2374 | |
| 2375 | // make_unique_aligned - pod types |
| 2376 | template <typename T> |
| 2377 | class ut0new_make_unique_aligned_pod_types : public ::testing::Test {}; |
| 2378 | TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_pod_types); |
| 2379 | TYPED_TEST_P(ut0new_make_unique_aligned_pod_types, pod_types) { |
| 2380 | using type = typename TypeParam::type; |
| 2381 | auto with_pfs = TypeParam::with_pfs; |
| 2382 | auto alignment = 4 * alignof(std::max_align_t); |
| 2383 | auto ptr = with_pfs ? ut::make_unique_aligned<type>( |
| 2384 | ut::make_psi_memory_key(pfs_key), alignment, 2, 5) |
| 2385 | : ut::make_unique_aligned<type>(alignment, 2, 5); |
| 2386 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2387 | EXPECT_EQ(ptr->x, 2); |
| 2388 | EXPECT_EQ(ptr->y, 5); |
| 2389 | } |
| 2390 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_pod_types, pod_types); |
| 2391 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_aligned_pod_types, |
| 2392 | all_pod_types); |
| 2393 | |
| 2394 | // make_unique_aligned - default constructible pod types |
| 2395 | template <typename T> |
| 2396 | class ut0new_make_unique_aligned_default_constructible_pod_types |
| 2397 | : public ::testing::Test {}; |
| 2398 | TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_default_constructible_pod_types); |
| 2399 | TYPED_TEST_P(ut0new_make_unique_aligned_default_constructible_pod_types, |
| 2400 | default_constructible_pod_types) { |
| 2401 | using type = typename TypeParam::type; |
| 2402 | auto with_pfs = TypeParam::with_pfs; |
| 2403 | auto alignment = 4 * alignof(std::max_align_t); |
| 2404 | auto ptr = with_pfs ? ut::make_unique_aligned<type>( |
| 2405 | ut::make_psi_memory_key(pfs_key), alignment) |
| 2406 | : ut::make_unique_aligned<type>(alignment); |
| 2407 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2408 | EXPECT_EQ(ptr->x, 0); |
| 2409 | EXPECT_EQ(ptr->y, 1); |
| 2410 | } |
| 2411 | REGISTER_TYPED_TEST_SUITE_P( |
| 2412 | ut0new_make_unique_aligned_default_constructible_pod_types, |
| 2413 | default_constructible_pod_types); |
| 2414 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2415 | My, ut0new_make_unique_aligned_default_constructible_pod_types, |
| 2416 | all_default_constructible_pod_types); |
| 2417 | |
| 2418 | // make_unique_aligned - non-pod types |
| 2419 | template <typename T> |
| 2420 | class ut0new_make_unique_aligned_non_pod_types : public ::testing::Test {}; |
| 2421 | TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_non_pod_types); |
| 2422 | TYPED_TEST_P(ut0new_make_unique_aligned_non_pod_types, non_pod_types) { |
| 2423 | using type = typename TypeParam::type; |
| 2424 | auto with_pfs = TypeParam::with_pfs; |
| 2425 | auto alignment = 4 * alignof(std::max_align_t); |
| 2426 | EXPECT_EQ(type::get_count(), 0); |
| 2427 | { |
| 2428 | auto ptr = with_pfs ? ut::make_unique_aligned<type>( |
| 2429 | ut::make_psi_memory_key(pfs_key), alignment, 2, 5, |
| 2430 | std::string("non-pod")) |
| 2431 | : ut::make_unique_aligned<type>(alignment, 2, 5, |
| 2432 | std::string("non-pod")); |
| 2433 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2434 | EXPECT_EQ(ptr->x, 2); |
| 2435 | EXPECT_EQ(ptr->y, 5); |
| 2436 | EXPECT_EQ(ptr->sum.result, 7); |
| 2437 | EXPECT_EQ(ptr->s, std::string("non-pod")); |
| 2438 | EXPECT_EQ(type::get_count(), 1); |
| 2439 | } |
| 2440 | EXPECT_EQ(type::get_count(), 0); |
| 2441 | } |
| 2442 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_non_pod_types, |
| 2443 | non_pod_types); |
| 2444 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_unique_aligned_non_pod_types, |
| 2445 | all_non_pod_types); |
| 2446 | |
| 2447 | // make_unique_aligned - default constructible non-pod types |
| 2448 | template <typename T> |
| 2449 | class ut0new_make_unique_aligned_default_constructible_non_pod_types |
| 2450 | : public ::testing::Test {}; |
| 2451 | TYPED_TEST_SUITE_P( |
| 2452 | ut0new_make_unique_aligned_default_constructible_non_pod_types); |
| 2453 | TYPED_TEST_P(ut0new_make_unique_aligned_default_constructible_non_pod_types, |
| 2454 | default_constructible_non_pod_types) { |
| 2455 | using type = typename TypeParam::type; |
| 2456 | auto with_pfs = TypeParam::with_pfs; |
| 2457 | auto alignment = 4 * alignof(std::max_align_t); |
| 2458 | { |
| 2459 | auto ptr = with_pfs ? ut::make_unique_aligned<type>( |
| 2460 | ut::make_psi_memory_key(pfs_key), alignment) |
| 2461 | : ut::make_unique_aligned<type>(alignment); |
| 2462 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2463 | EXPECT_EQ(ptr->x, 0); |
| 2464 | EXPECT_EQ(ptr->y, 1); |
| 2465 | EXPECT_EQ(ptr->s, std::string("non-pod-string")); |
| 2466 | } |
| 2467 | } |
| 2468 | REGISTER_TYPED_TEST_SUITE_P( |
| 2469 | ut0new_make_unique_aligned_default_constructible_non_pod_types, |
| 2470 | default_constructible_non_pod_types); |
| 2471 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2472 | My, ut0new_make_unique_aligned_default_constructible_non_pod_types, |
| 2473 | all_default_constructible_non_pod_types); |
| 2474 | |
| 2475 | // make_unique_aligned - array specialization for fundamental types |
| 2476 | template <typename T> |
| 2477 | class ut0new_make_unique_aligned_fundamental_types_arr |
| 2478 | : public ::testing::Test {}; |
| 2479 | TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_fundamental_types_arr); |
| 2480 | TYPED_TEST_P(ut0new_make_unique_aligned_fundamental_types_arr, |
| 2481 | fundamental_types) { |
| 2482 | using type = typename TypeParam::type; |
| 2483 | auto with_pfs = TypeParam::with_pfs; |
| 2484 | auto alignment = 4 * alignof(std::max_align_t); |
| 2485 | auto ptr = with_pfs ? ut::make_unique_aligned<type[]>( |
| 2486 | ut::make_psi_memory_key(pfs_key), alignment, 3) |
| 2487 | : ut::make_unique_aligned<type[]>(alignment, 3); |
| 2488 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2489 | EXPECT_EQ(ptr[0], type{}); |
| 2490 | EXPECT_EQ(ptr[1], type{}); |
| 2491 | EXPECT_EQ(ptr[2], type{}); |
| 2492 | } |
| 2493 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_unique_aligned_fundamental_types_arr, |
| 2494 | fundamental_types); |
| 2495 | INSTANTIATE_TYPED_TEST_SUITE_P(My, |
| 2496 | ut0new_make_unique_aligned_fundamental_types_arr, |
| 2497 | all_fundamental_types); |
| 2498 | |
| 2499 | // make_unique_aligned - array specialization for default_constructible POD |
| 2500 | // types |
| 2501 | template <typename T> |
| 2502 | class ut0new_make_unique_aligned_default_constructible_pod_types_arr |
| 2503 | : public ::testing::Test {}; |
| 2504 | TYPED_TEST_SUITE_P( |
| 2505 | ut0new_make_unique_aligned_default_constructible_pod_types_arr); |
| 2506 | TYPED_TEST_P(ut0new_make_unique_aligned_default_constructible_pod_types_arr, |
| 2507 | default_constructible_pod_types) { |
| 2508 | using type = typename TypeParam::type; |
| 2509 | auto with_pfs = TypeParam::with_pfs; |
| 2510 | auto alignment = 4 * alignof(std::max_align_t); |
| 2511 | auto ptr = with_pfs ? ut::make_unique_aligned<type[]>( |
| 2512 | ut::make_psi_memory_key(pfs_key), alignment, 3) |
| 2513 | : ut::make_unique_aligned<type[]>(alignment, 3); |
| 2514 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2515 | EXPECT_EQ(ptr[0].x, 0); |
| 2516 | EXPECT_EQ(ptr[0].y, 1); |
| 2517 | EXPECT_EQ(ptr[1].x, 0); |
| 2518 | EXPECT_EQ(ptr[1].y, 1); |
| 2519 | EXPECT_EQ(ptr[2].x, 0); |
| 2520 | EXPECT_EQ(ptr[2].y, 1); |
| 2521 | } |
| 2522 | REGISTER_TYPED_TEST_SUITE_P( |
| 2523 | ut0new_make_unique_aligned_default_constructible_pod_types_arr, |
| 2524 | default_constructible_pod_types); |
| 2525 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2526 | My, ut0new_make_unique_aligned_default_constructible_pod_types_arr, |
| 2527 | all_default_constructible_pod_types); |
| 2528 | |
| 2529 | // make_shared - fundamental types |
| 2530 | template <typename T> |
| 2531 | class ut0new_make_shared_ptr_fundamental_types : public ::testing::Test {}; |
| 2532 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_fundamental_types); |
| 2533 | TYPED_TEST_P(ut0new_make_shared_ptr_fundamental_types, fundamental_types) { |
| 2534 | using type = typename TypeParam::type; |
| 2535 | auto with_pfs = TypeParam::with_pfs; |
| 2536 | auto ptr = with_pfs |
| 2537 | ? ut::make_shared<type>(ut::make_psi_memory_key(pfs_key), 1) |
| 2538 | : ut::make_shared<type>(1); |
| 2539 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2540 | EXPECT_EQ(*ptr, 1); |
| 2541 | } |
| 2542 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_fundamental_types, |
| 2543 | fundamental_types); |
| 2544 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_ptr_fundamental_types, |
| 2545 | all_fundamental_types); |
| 2546 | |
| 2547 | // make_shared - pod types |
| 2548 | template <typename T> |
| 2549 | class ut0new_make_shared_ptr_pod_types : public ::testing::Test {}; |
| 2550 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_pod_types); |
| 2551 | TYPED_TEST_P(ut0new_make_shared_ptr_pod_types, pod_types) { |
| 2552 | using type = typename TypeParam::type; |
| 2553 | auto with_pfs = TypeParam::with_pfs; |
| 2554 | |
| 2555 | auto ptr = with_pfs |
| 2556 | ? ut::make_shared<type>(ut::make_psi_memory_key(pfs_key), 2, 5) |
| 2557 | : ut::make_shared<type>(2, 5); |
| 2558 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2559 | EXPECT_EQ(ptr->x, 2); |
| 2560 | EXPECT_EQ(ptr->y, 5); |
| 2561 | } |
| 2562 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_pod_types, pod_types); |
| 2563 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_ptr_pod_types, |
| 2564 | all_pod_types); |
| 2565 | |
| 2566 | // make_shared - default constructible pod types |
| 2567 | template <typename T> |
| 2568 | class ut0new_make_shared_ptr_default_constructible_pod_types |
| 2569 | : public ::testing::Test {}; |
| 2570 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_default_constructible_pod_types); |
| 2571 | TYPED_TEST_P(ut0new_make_shared_ptr_default_constructible_pod_types, |
| 2572 | default_constructible_pod_types) { |
| 2573 | using type = typename TypeParam::type; |
| 2574 | auto with_pfs = TypeParam::with_pfs; |
| 2575 | |
| 2576 | auto ptr = with_pfs ? ut::make_shared<type>(ut::make_psi_memory_key(pfs_key)) |
| 2577 | : ut::make_shared<type>(); |
| 2578 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2579 | EXPECT_EQ(ptr->x, 0); |
| 2580 | EXPECT_EQ(ptr->y, 1); |
| 2581 | } |
| 2582 | REGISTER_TYPED_TEST_SUITE_P( |
| 2583 | ut0new_make_shared_ptr_default_constructible_pod_types, |
| 2584 | default_constructible_pod_types); |
| 2585 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2586 | My, ut0new_make_shared_ptr_default_constructible_pod_types, |
| 2587 | all_default_constructible_pod_types); |
| 2588 | |
| 2589 | // make_shared - non-pod types |
| 2590 | template <typename T> |
| 2591 | class ut0new_make_shared_ptr_non_pod_types : public ::testing::Test {}; |
| 2592 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_non_pod_types); |
| 2593 | TYPED_TEST_P(ut0new_make_shared_ptr_non_pod_types, non_pod_types) { |
| 2594 | using type = typename TypeParam::type; |
| 2595 | auto with_pfs = TypeParam::with_pfs; |
| 2596 | EXPECT_EQ(type::get_count(), 0); |
| 2597 | { |
| 2598 | auto ptr = with_pfs |
| 2599 | ? ut::make_shared<type>(ut::make_psi_memory_key(pfs_key), 2, |
| 2600 | 5, std::string("non-pod")) |
| 2601 | : ut::make_shared<type>(2, 5, std::string("non-pod")); |
| 2602 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2603 | EXPECT_EQ(ptr->x, 2); |
| 2604 | EXPECT_EQ(ptr->y, 5); |
| 2605 | EXPECT_EQ(ptr->sum.result, 7); |
| 2606 | EXPECT_EQ(ptr->s, std::string("non-pod")); |
| 2607 | EXPECT_EQ(type::get_count(), 1); |
| 2608 | } |
| 2609 | EXPECT_EQ(type::get_count(), 0); |
| 2610 | } |
| 2611 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_non_pod_types, |
| 2612 | non_pod_types); |
| 2613 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_ptr_non_pod_types, |
| 2614 | all_non_pod_types); |
| 2615 | |
| 2616 | // make_shared - default constructible non-pod types |
| 2617 | template <typename T> |
| 2618 | class ut0new_make_shared_ptr_default_constructible_non_pod_types |
| 2619 | : public ::testing::Test {}; |
| 2620 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_default_constructible_non_pod_types); |
| 2621 | TYPED_TEST_P(ut0new_make_shared_ptr_default_constructible_non_pod_types, |
| 2622 | default_constructible_non_pod_types) { |
| 2623 | using type = typename TypeParam::type; |
| 2624 | auto with_pfs = TypeParam::with_pfs; |
| 2625 | { |
| 2626 | auto ptr = with_pfs |
| 2627 | ? ut::make_shared<type>(ut::make_psi_memory_key(pfs_key)) |
| 2628 | : ut::make_shared<type>(); |
| 2629 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2630 | EXPECT_EQ(ptr->x, 0); |
| 2631 | EXPECT_EQ(ptr->y, 1); |
| 2632 | EXPECT_EQ(ptr->s, std::string("non-pod-string")); |
| 2633 | } |
| 2634 | } |
| 2635 | REGISTER_TYPED_TEST_SUITE_P( |
| 2636 | ut0new_make_shared_ptr_default_constructible_non_pod_types, |
| 2637 | default_constructible_non_pod_types); |
| 2638 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2639 | My, ut0new_make_shared_ptr_default_constructible_non_pod_types, |
| 2640 | all_default_constructible_non_pod_types); |
| 2641 | |
| 2642 | // TODO macOS build of clang on PB2 currently exhibits a bug in which |
| 2643 | // std::shared_ptr with array types is not usable. |
| 2644 | // |
| 2645 | // For example, |
| 2646 | // std::shared_ptr<int[]> ptr(new int[3]); |
| 2647 | // ptr[0]; ptr[1]; ptr[2]; |
| 2648 | // |
| 2649 | // will give the following error: |
| 2650 | // type 'std::__1::shared_ptr<int []>' does not provide a subscript operator |
| 2651 | // |
| 2652 | // This can be worked around by reinterpreting it as pointer to int: |
| 2653 | // ((int*)ptr.get())[0]; |
| 2654 | // ((int*)ptr.get())[1]; |
| 2655 | // ((int*)ptr.get())[2]; |
| 2656 | // |
| 2657 | // But this leads to another issue: |
| 2658 | // no matching constructor for initialization of 'std::shared_ptr<int[]>' |
| 2659 | // |
| 2660 | // Interestingly enough, issue is _not_ reproducible on Linux build of clang |
| 2661 | // (Fedora) which implied that there's something wrong with the Apple deployment |
| 2662 | // of clang. And indeed, issue is reproducible only if code is compiled with |
| 2663 | // libc++, an alternative (and default to Apple) implementation of C++ standard |
| 2664 | // library. clang on Linux OTOH is using libstdc++ by default and libstdc++ does |
| 2665 | // not suffer from this problem. So this is a bug in libc++ implementation. |
| 2666 | // |
| 2667 | // For now I am disabling std::shared_ptr-with-arrays subset of unit-tests from |
| 2668 | // running on PB2 macOS jobs so that the build is not broken and so that the |
| 2669 | // other unit-tests keep running. If and when this becomes a real issue (when |
| 2670 | // somebody actually tries to use this approach in production code), a |
| 2671 | // workaround will be needed to be found. It looks like |
| 2672 | // std::shared_ptr<std::array<int, 3>> p(new std::array<int, 3>{}); |
| 2673 | // will do or FWIW ut::make_shared version of it. |
| 2674 | #if !defined(__APPLE__) |
| 2675 | // make_shared - array specialization for fundamental types |
| 2676 | template <typename T> |
| 2677 | class ut0new_make_shared_ptr_fundamental_types_arr : public ::testing::Test {}; |
| 2678 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_fundamental_types_arr); |
| 2679 | TYPED_TEST_P(ut0new_make_shared_ptr_fundamental_types_arr, fundamental_types) { |
| 2680 | using type = typename TypeParam::type; |
| 2681 | auto with_pfs = TypeParam::with_pfs; |
| 2682 | auto ptr = with_pfs |
| 2683 | ? ut::make_shared<type[]>(ut::make_psi_memory_key(pfs_key), 3) |
| 2684 | : ut::make_shared<type[]>(3); |
| 2685 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2686 | EXPECT_EQ(ptr[0], type{}); |
| 2687 | EXPECT_EQ(ptr[1], type{}); |
| 2688 | EXPECT_EQ(ptr[2], type{}); |
| 2689 | } |
| 2690 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_fundamental_types_arr, |
| 2691 | fundamental_types); |
| 2692 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_ptr_fundamental_types_arr, |
| 2693 | all_fundamental_types); |
| 2694 | |
| 2695 | // make_shared - bounded array specialization for fundamental types |
| 2696 | template <typename T> |
| 2697 | class ut0new_make_shared_ptr_fundamental_types_bounded_arr |
| 2698 | : public ::testing::Test {}; |
| 2699 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_fundamental_types_bounded_arr); |
| 2700 | TYPED_TEST_P(ut0new_make_shared_ptr_fundamental_types_bounded_arr, |
| 2701 | fundamental_types) { |
| 2702 | using type = typename TypeParam::type; |
| 2703 | auto with_pfs = TypeParam::with_pfs; |
| 2704 | auto ptr = with_pfs |
| 2705 | ? ut::make_shared<type[3]>(ut::make_psi_memory_key(pfs_key)) |
| 2706 | : ut::make_shared<type[3]>(); |
| 2707 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2708 | EXPECT_EQ(ptr[0], type{}); |
| 2709 | EXPECT_EQ(ptr[1], type{}); |
| 2710 | EXPECT_EQ(ptr[2], type{}); |
| 2711 | } |
| 2712 | REGISTER_TYPED_TEST_SUITE_P( |
| 2713 | ut0new_make_shared_ptr_fundamental_types_bounded_arr, fundamental_types); |
| 2714 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2715 | My, ut0new_make_shared_ptr_fundamental_types_bounded_arr, |
| 2716 | all_fundamental_types); |
| 2717 | |
| 2718 | // make_shared - array specialization for default_constructible POD types |
| 2719 | template <typename T> |
| 2720 | class ut0new_make_shared_ptr_default_constructible_pod_types_arr |
| 2721 | : public ::testing::Test {}; |
| 2722 | TYPED_TEST_SUITE_P(ut0new_make_shared_ptr_default_constructible_pod_types_arr); |
| 2723 | TYPED_TEST_P(ut0new_make_shared_ptr_default_constructible_pod_types_arr, |
| 2724 | default_constructible_pod_types) { |
| 2725 | using type = typename TypeParam::type; |
| 2726 | auto with_pfs = TypeParam::with_pfs; |
| 2727 | auto ptr = with_pfs |
| 2728 | ? ut::make_shared<type[]>(ut::make_psi_memory_key(pfs_key), 3) |
| 2729 | : ut::make_shared<type[]>(3); |
| 2730 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2731 | EXPECT_EQ(ptr[0].x, 0); |
| 2732 | EXPECT_EQ(ptr[0].y, 1); |
| 2733 | EXPECT_EQ(ptr[1].x, 0); |
| 2734 | EXPECT_EQ(ptr[1].y, 1); |
| 2735 | EXPECT_EQ(ptr[2].x, 0); |
| 2736 | EXPECT_EQ(ptr[2].y, 1); |
| 2737 | } |
| 2738 | REGISTER_TYPED_TEST_SUITE_P( |
| 2739 | ut0new_make_shared_ptr_default_constructible_pod_types_arr, |
| 2740 | default_constructible_pod_types); |
| 2741 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2742 | My, ut0new_make_shared_ptr_default_constructible_pod_types_arr, |
| 2743 | all_default_constructible_pod_types); |
| 2744 | |
| 2745 | // make_shared - bounded array specialization for default_constructible POD |
| 2746 | // types |
| 2747 | template <typename T> |
| 2748 | class ut0new_make_shared_ptr_default_constructible_pod_types_bounded_arr |
| 2749 | : public ::testing::Test {}; |
| 2750 | TYPED_TEST_SUITE_P( |
| 2751 | ut0new_make_shared_ptr_default_constructible_pod_types_bounded_arr); |
| 2752 | TYPED_TEST_P(ut0new_make_shared_ptr_default_constructible_pod_types_bounded_arr, |
| 2753 | default_constructible_pod_types) { |
| 2754 | using type = typename TypeParam::type; |
| 2755 | auto with_pfs = TypeParam::with_pfs; |
| 2756 | auto ptr = with_pfs |
| 2757 | ? ut::make_shared<type[3]>(ut::make_psi_memory_key(pfs_key)) |
| 2758 | : ut::make_shared<type[3]>(); |
| 2759 | EXPECT_TRUE(ptr_is_suitably_aligned(ptr.get())); |
| 2760 | EXPECT_EQ(ptr[0].x, 0); |
| 2761 | EXPECT_EQ(ptr[0].y, 1); |
| 2762 | EXPECT_EQ(ptr[1].x, 0); |
| 2763 | EXPECT_EQ(ptr[1].y, 1); |
| 2764 | EXPECT_EQ(ptr[2].x, 0); |
| 2765 | EXPECT_EQ(ptr[2].y, 1); |
| 2766 | } |
| 2767 | REGISTER_TYPED_TEST_SUITE_P( |
| 2768 | ut0new_make_shared_ptr_default_constructible_pod_types_bounded_arr, |
| 2769 | default_constructible_pod_types); |
| 2770 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2771 | My, ut0new_make_shared_ptr_default_constructible_pod_types_bounded_arr, |
| 2772 | all_default_constructible_pod_types); |
| 2773 | #endif |
| 2774 | |
| 2775 | // make_shared_aligned - fundamental types |
| 2776 | template <typename T> |
| 2777 | class ut0new_make_shared_aligned_fundamental_types : public ::testing::Test {}; |
| 2778 | TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_fundamental_types); |
| 2779 | TYPED_TEST_P(ut0new_make_shared_aligned_fundamental_types, fundamental_types) { |
| 2780 | using type = typename TypeParam::type; |
| 2781 | auto with_pfs = TypeParam::with_pfs; |
| 2782 | auto alignment = 4 * alignof(std::max_align_t); |
| 2783 | auto ptr = with_pfs ? ut::make_shared_aligned<type>( |
| 2784 | ut::make_psi_memory_key(pfs_key), alignment, 1) |
| 2785 | : ut::make_shared_aligned<type>(alignment, 1); |
| 2786 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2787 | EXPECT_EQ(*ptr, 1); |
| 2788 | } |
| 2789 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_fundamental_types, |
| 2790 | fundamental_types); |
| 2791 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_aligned_fundamental_types, |
| 2792 | all_fundamental_types); |
| 2793 | |
| 2794 | // make_shared_aligned - pod types |
| 2795 | template <typename T> |
| 2796 | class ut0new_make_shared_aligned_pod_types : public ::testing::Test {}; |
| 2797 | TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_pod_types); |
| 2798 | TYPED_TEST_P(ut0new_make_shared_aligned_pod_types, pod_types) { |
| 2799 | using type = typename TypeParam::type; |
| 2800 | auto with_pfs = TypeParam::with_pfs; |
| 2801 | auto alignment = 4 * alignof(std::max_align_t); |
| 2802 | auto ptr = with_pfs ? ut::make_shared_aligned<type>( |
| 2803 | ut::make_psi_memory_key(pfs_key), alignment, 2, 5) |
| 2804 | : ut::make_shared_aligned<type>(alignment, 2, 5); |
| 2805 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2806 | EXPECT_EQ(ptr->x, 2); |
| 2807 | EXPECT_EQ(ptr->y, 5); |
| 2808 | } |
| 2809 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_pod_types, pod_types); |
| 2810 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_aligned_pod_types, |
| 2811 | all_pod_types); |
| 2812 | |
| 2813 | // make_shared_aligned - default constructible pod types |
| 2814 | template <typename T> |
| 2815 | class ut0new_make_shared_aligned_default_constructible_pod_types |
| 2816 | : public ::testing::Test {}; |
| 2817 | TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_default_constructible_pod_types); |
| 2818 | TYPED_TEST_P(ut0new_make_shared_aligned_default_constructible_pod_types, |
| 2819 | default_constructible_pod_types) { |
| 2820 | using type = typename TypeParam::type; |
| 2821 | auto with_pfs = TypeParam::with_pfs; |
| 2822 | auto alignment = 4 * alignof(std::max_align_t); |
| 2823 | auto ptr = with_pfs ? ut::make_shared_aligned<type>( |
| 2824 | ut::make_psi_memory_key(pfs_key), alignment) |
| 2825 | : ut::make_shared_aligned<type>(alignment); |
| 2826 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2827 | EXPECT_EQ(ptr->x, 0); |
| 2828 | EXPECT_EQ(ptr->y, 1); |
| 2829 | } |
| 2830 | REGISTER_TYPED_TEST_SUITE_P( |
| 2831 | ut0new_make_shared_aligned_default_constructible_pod_types, |
| 2832 | default_constructible_pod_types); |
| 2833 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2834 | My, ut0new_make_shared_aligned_default_constructible_pod_types, |
| 2835 | all_default_constructible_pod_types); |
| 2836 | |
| 2837 | // make_shared_aligned - non-pod types |
| 2838 | template <typename T> |
| 2839 | class ut0new_make_shared_aligned_non_pod_types : public ::testing::Test {}; |
| 2840 | TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_non_pod_types); |
| 2841 | TYPED_TEST_P(ut0new_make_shared_aligned_non_pod_types, non_pod_types) { |
| 2842 | using type = typename TypeParam::type; |
| 2843 | auto with_pfs = TypeParam::with_pfs; |
| 2844 | auto alignment = 4 * alignof(std::max_align_t); |
| 2845 | EXPECT_EQ(type::get_count(), 0); |
| 2846 | { |
| 2847 | auto ptr = with_pfs ? ut::make_shared_aligned<type>( |
| 2848 | ut::make_psi_memory_key(pfs_key), alignment, 2, 5, |
| 2849 | std::string("non-pod")) |
| 2850 | : ut::make_shared_aligned<type>(alignment, 2, 5, |
| 2851 | std::string("non-pod")); |
| 2852 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2853 | EXPECT_EQ(ptr->x, 2); |
| 2854 | EXPECT_EQ(ptr->y, 5); |
| 2855 | EXPECT_EQ(ptr->sum.result, 7); |
| 2856 | EXPECT_EQ(ptr->s, std::string("non-pod")); |
| 2857 | EXPECT_EQ(type::get_count(), 1); |
| 2858 | } |
| 2859 | EXPECT_EQ(type::get_count(), 0); |
| 2860 | } |
| 2861 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_non_pod_types, |
| 2862 | non_pod_types); |
| 2863 | INSTANTIATE_TYPED_TEST_SUITE_P(My, ut0new_make_shared_aligned_non_pod_types, |
| 2864 | all_non_pod_types); |
| 2865 | |
| 2866 | // make_shared_aligned - default constructible non-pod types |
| 2867 | template <typename T> |
| 2868 | class ut0new_make_shared_aligned_default_constructible_non_pod_types |
| 2869 | : public ::testing::Test {}; |
| 2870 | TYPED_TEST_SUITE_P( |
| 2871 | ut0new_make_shared_aligned_default_constructible_non_pod_types); |
| 2872 | TYPED_TEST_P(ut0new_make_shared_aligned_default_constructible_non_pod_types, |
| 2873 | default_constructible_non_pod_types) { |
| 2874 | using type = typename TypeParam::type; |
| 2875 | auto with_pfs = TypeParam::with_pfs; |
| 2876 | auto alignment = 4 * alignof(std::max_align_t); |
| 2877 | |
| 2878 | { |
| 2879 | auto ptr = with_pfs ? ut::make_shared_aligned<type>( |
| 2880 | ut::make_psi_memory_key(pfs_key), alignment) |
| 2881 | : ut::make_shared_aligned<type>(alignment); |
| 2882 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2883 | EXPECT_EQ(ptr->x, 0); |
| 2884 | EXPECT_EQ(ptr->y, 1); |
| 2885 | EXPECT_EQ(ptr->s, std::string("non-pod-string")); |
| 2886 | } |
| 2887 | } |
| 2888 | REGISTER_TYPED_TEST_SUITE_P( |
| 2889 | ut0new_make_shared_aligned_default_constructible_non_pod_types, |
| 2890 | default_constructible_non_pod_types); |
| 2891 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2892 | My, ut0new_make_shared_aligned_default_constructible_non_pod_types, |
| 2893 | all_default_constructible_non_pod_types); |
| 2894 | |
| 2895 | // TODO macOS build of clang on PB2 currently exhibits a bug in which |
| 2896 | // std::shared_ptr with array types is not usable. Please see above for more in |
| 2897 | // depth explanation. |
| 2898 | #if !defined(__APPLE__) |
| 2899 | // make_shared_aligned - array specialization for fundamental types |
| 2900 | template <typename T> |
| 2901 | class ut0new_make_shared_aligned_fundamental_types_arr |
| 2902 | : public ::testing::Test {}; |
| 2903 | TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_fundamental_types_arr); |
| 2904 | TYPED_TEST_P(ut0new_make_shared_aligned_fundamental_types_arr, |
| 2905 | fundamental_types) { |
| 2906 | using type = typename TypeParam::type; |
| 2907 | auto with_pfs = TypeParam::with_pfs; |
| 2908 | auto alignment = 4 * alignof(std::max_align_t); |
| 2909 | auto ptr = with_pfs ? ut::make_shared_aligned<type[]>( |
| 2910 | ut::make_psi_memory_key(pfs_key), alignment, 3) |
| 2911 | : ut::make_shared_aligned<type[]>(alignment, 3); |
| 2912 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2913 | EXPECT_EQ(ptr[0], type{}); |
| 2914 | EXPECT_EQ(ptr[1], type{}); |
| 2915 | EXPECT_EQ(ptr[2], type{}); |
| 2916 | } |
| 2917 | REGISTER_TYPED_TEST_SUITE_P(ut0new_make_shared_aligned_fundamental_types_arr, |
| 2918 | fundamental_types); |
| 2919 | INSTANTIATE_TYPED_TEST_SUITE_P(My, |
| 2920 | ut0new_make_shared_aligned_fundamental_types_arr, |
| 2921 | all_fundamental_types); |
| 2922 | |
| 2923 | // make_shared_aligned - array specialization for default_constructible POD |
| 2924 | // types |
| 2925 | template <typename T> |
| 2926 | class ut0new_make_shared_aligned_default_constructible_pod_types_arr |
| 2927 | : public ::testing::Test {}; |
| 2928 | TYPED_TEST_SUITE_P( |
| 2929 | ut0new_make_shared_aligned_default_constructible_pod_types_arr); |
| 2930 | TYPED_TEST_P(ut0new_make_shared_aligned_default_constructible_pod_types_arr, |
| 2931 | default_constructible_pod_types) { |
| 2932 | using type = typename TypeParam::type; |
| 2933 | auto with_pfs = TypeParam::with_pfs; |
| 2934 | auto alignment = 4 * alignof(std::max_align_t); |
| 2935 | auto ptr = with_pfs ? ut::make_shared_aligned<type[]>( |
| 2936 | ut::make_psi_memory_key(pfs_key), alignment, 3) |
| 2937 | : ut::make_shared_aligned<type[]>(alignment, 3); |
| 2938 | EXPECT_TRUE(reinterpret_cast<std::uintptr_t>(ptr.get()) % alignment == 0); |
| 2939 | EXPECT_EQ(ptr[0].x, 0); |
| 2940 | EXPECT_EQ(ptr[0].y, 1); |
| 2941 | EXPECT_EQ(ptr[1].x, 0); |
| 2942 | EXPECT_EQ(ptr[1].y, 1); |
| 2943 | EXPECT_EQ(ptr[2].x, 0); |
| 2944 | EXPECT_EQ(ptr[2].y, 1); |
| 2945 | } |
| 2946 | REGISTER_TYPED_TEST_SUITE_P( |
| 2947 | ut0new_make_shared_aligned_default_constructible_pod_types_arr, |
| 2948 | default_constructible_pod_types); |
| 2949 | INSTANTIATE_TYPED_TEST_SUITE_P( |
| 2950 | My, ut0new_make_shared_aligned_default_constructible_pod_types_arr, |
| 2951 | all_default_constructible_pod_types); |
| 2952 | #endif |
| 2953 | |
| 2954 | } // namespace innodb_ut0new_unittest |