[LoongArch] Supports FP_TO_SINT operation for fp16 (#118303)
[llvm-project.git] / libcxx / test / std / utilities / memory / allocator.traits / allocator.traits.members / destroy.pass.cpp
blob047b7c1836dfd4a476ecbd12e031a6a269a63e6c
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // <memory>
11 // template <class Alloc>
12 // struct allocator_traits
13 // {
14 // template <class Ptr>
15 // static constexpr void destroy(allocator_type& a, Ptr p);
16 // ...
17 // };
19 #include <memory>
20 #include <cassert>
21 #include <cstddef>
23 #include "test_macros.h"
24 #include "incomplete_type_helper.h"
26 template <class T>
27 struct NoDestroy
29 typedef T value_type;
31 TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n)
33 return std::allocator<T>().allocate(n);
36 TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n)
38 return std::allocator<T>().deallocate(p, n);
42 template <class T>
43 struct CountDestroy
45 TEST_CONSTEXPR explicit CountDestroy(int* counter)
46 : counter_(counter)
47 { }
49 typedef T value_type;
51 TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n)
53 return std::allocator<T>().allocate(n);
56 TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n)
58 return std::allocator<T>().deallocate(p, n);
61 template <class U>
62 TEST_CONSTEXPR_CXX20 void destroy(U* p)
64 ++*counter_;
65 p->~U();
68 int* counter_;
71 struct CountDestructor
73 TEST_CONSTEXPR explicit CountDestructor(int* counter)
74 : counter_(counter)
75 { }
77 TEST_CONSTEXPR_CXX20 ~CountDestructor() { ++*counter_; }
79 int* counter_;
82 TEST_CONSTEXPR_CXX20 bool test()
85 typedef NoDestroy<CountDestructor> Alloc;
86 int destructors = 0;
87 Alloc alloc;
88 CountDestructor* pool = std::allocator_traits<Alloc>::allocate(alloc, 1);
90 std::allocator_traits<Alloc>::construct(alloc, pool, &destructors);
91 assert(destructors == 0);
93 std::allocator_traits<Alloc>::destroy(alloc, pool);
94 assert(destructors == 1);
96 std::allocator_traits<Alloc>::deallocate(alloc, pool, 1);
99 typedef IncompleteHolder* T;
100 typedef NoDestroy<T> Alloc;
101 Alloc alloc;
102 T* pool = std::allocator_traits<Alloc>::allocate(alloc, 1);
103 std::allocator_traits<Alloc>::construct(alloc, pool, nullptr);
104 std::allocator_traits<Alloc>::destroy(alloc, pool);
105 std::allocator_traits<Alloc>::deallocate(alloc, pool, 1);
108 typedef CountDestroy<CountDestructor> Alloc;
109 int destroys_called = 0;
110 int destructors_called = 0;
111 Alloc alloc(&destroys_called);
113 CountDestructor* pool = std::allocator_traits<Alloc>::allocate(alloc, 1);
114 std::allocator_traits<Alloc>::construct(alloc, pool, &destructors_called);
115 assert(destroys_called == 0);
116 assert(destructors_called == 0);
118 std::allocator_traits<Alloc>::destroy(alloc, pool);
119 assert(destroys_called == 1);
120 assert(destructors_called == 1);
122 std::allocator_traits<Alloc>::deallocate(alloc, pool, 1);
124 return true;
127 int main(int, char**)
129 test();
130 #if TEST_STD_VER > 17
131 static_assert(test());
132 #endif
133 return 0;