Debian Codegraph @

libgomp/testsuite/libgomp.c++/allocator-1.C

raw
1 // { dg-do run }
2
3 #include <omp.h>
4 #include <memory>
5 #include <limits>
6
7 template<typename T, template<typename> class Alloc>
8 void test (T const initial_value = T())
9 {
10 using Allocator = Alloc<T>;
11 Allocator a;
12 using Traits = std::allocator_traits<Allocator>;
13 static_assert (__is_same(typename Traits::allocator_type, Allocator ));
14 static_assert (__is_same(typename Traits::value_type, T ));
15 static_assert (__is_same(typename Traits::pointer, T* ));
16 static_assert (__is_same(typename Traits::const_pointer, T const* ));
17 static_assert (__is_same(typename Traits::void_pointer, void* ));
18 static_assert (__is_same(typename Traits::const_void_pointer, void const* ));
19 static_assert (__is_same(typename Traits::difference_type, __PTRDIFF_TYPE__));
20 static_assert (__is_same(typename Traits::size_type, __SIZE_TYPE__ ));
21 static_assert (Traits::propagate_on_container_copy_assignment::value == false);
22 static_assert (Traits::propagate_on_container_move_assignment::value == false);
23 static_assert (Traits::propagate_on_container_swap::value == false);
24 static_assert (Traits::is_always_equal::value == true);
25
26 static constexpr __SIZE_TYPE__ correct_max_size
27 = std::numeric_limits<__SIZE_TYPE__>::max () / sizeof (T);
28 if (Traits::max_size (a) != correct_max_size)
29 __builtin_abort ();
30
31 static constexpr __SIZE_TYPE__ alloc_count = 1;
32 T *p = Traits::allocate (a, alloc_count);
33 if (p == nullptr)
34 __builtin_abort ();
35 Traits::construct (a, p, initial_value);
36 if (*p != initial_value)
37 __builtin_abort ();
38 Traits::destroy (a, p);
39 Traits::deallocate (a, p, alloc_count);
40 /* Not interesting but might as well test it. */
41 static_cast<void>(Traits::select_on_container_copy_construction (a));
42
43 if (!(a == Allocator()))
44 __builtin_abort ();
45 if (a != Allocator())
46 __builtin_abort ();
47 if (!(a == Alloc<void>()))
48 __builtin_abort ();
49 if (a != Alloc<void>())
50 __builtin_abort ();
51 }
52
53 #define CHECK_INEQUALITY(other_alloc_templ, type) \
54 do { \
55 /* Skip tests for itself, those are equal. Intantiate each */ \
56 /* one with void so we can easily tell if they are the same. */ \
57 if (!__is_same (AllocTempl<void>, other_alloc_templ<void>)) \
58 { \
59 other_alloc_templ<type> other; \
60 if (a == other) \
61 __builtin_abort (); \
62 if (!(a != other)) \
63 __builtin_abort (); \
64 } \
65 } while (false)
66
67 template<typename T, template<typename> class AllocTempl>
68 void test_inequality ()
69 {
70 using Allocator = AllocTempl<T>;
71 Allocator a;
72 CHECK_INEQUALITY (omp::allocator::null_allocator, void);
73 CHECK_INEQUALITY (omp::allocator::default_mem, void);
74 CHECK_INEQUALITY (omp::allocator::large_cap_mem, void);
75 CHECK_INEQUALITY (omp::allocator::const_mem, void);
76 CHECK_INEQUALITY (omp::allocator::high_bw_mem, void);
77 CHECK_INEQUALITY (omp::allocator::low_lat_mem, void);
78 CHECK_INEQUALITY (omp::allocator::cgroup_mem, void);
79 CHECK_INEQUALITY (omp::allocator::pteam_mem, void);
80 CHECK_INEQUALITY (omp::allocator::thread_mem, void);
81 #ifdef __gnu_linux__
82 /* Pinning not implemented on other targets. */
83 CHECK_INEQUALITY (ompx::allocator::gnu_pinned_mem, void);
84 #endif
85 /* And again with the same type passed to the allocator. */
86 CHECK_INEQUALITY (omp::allocator::null_allocator, T);
87 CHECK_INEQUALITY (omp::allocator::default_mem, T);
88 CHECK_INEQUALITY (omp::allocator::large_cap_mem, T);
89 CHECK_INEQUALITY (omp::allocator::const_mem, T);
90 CHECK_INEQUALITY (omp::allocator::high_bw_mem, T);
91 CHECK_INEQUALITY (omp::allocator::low_lat_mem, T);
92 CHECK_INEQUALITY (omp::allocator::cgroup_mem, T);
93 CHECK_INEQUALITY (omp::allocator::pteam_mem, T);
94 CHECK_INEQUALITY (omp::allocator::thread_mem, T);
95 #ifdef __gnu_linux__
96 CHECK_INEQUALITY (ompx::allocator::gnu_pinned_mem, T);
97 #endif
98 }
99
100 #undef CHECK_INEQUALITY
101
102 struct S
103 {
104 int _v0;
105 bool _v1;
106 float _v2;
107
108 bool operator== (S const& other) const noexcept {
109 return _v0 == other._v0
110 && _v1 == other._v1
111 && _v2 == other._v2;
112 }
113 bool operator!= (S const& other) const noexcept {
114 return !this->operator==(other);
115 }
116 };
117
118 int main ()
119 {
120 test<int, omp::allocator::null_allocator>(42);
121 test<int, omp::allocator::default_mem>(42);
122 test<int, omp::allocator::large_cap_mem>(42);
123 test<int, omp::allocator::const_mem>(42);
124 test<int, omp::allocator::high_bw_mem>(42);
125 test<int, omp::allocator::low_lat_mem>(42);
126 test<int, omp::allocator::cgroup_mem>(42);
127 test<int, omp::allocator::pteam_mem>(42);
128 test<int, omp::allocator::thread_mem>(42);
129 #ifdef __gnu_linux__
130 test<int, ompx::allocator::gnu_pinned_mem>(42);
131 #endif
132
133 test<long long, omp::allocator::null_allocator>(42);
134 test<long long, omp::allocator::default_mem>(42);
135 test<long long, omp::allocator::large_cap_mem>(42);
136 test<long long, omp::allocator::const_mem>(42);
137 test<long long, omp::allocator::high_bw_mem>(42);
138 test<long long, omp::allocator::low_lat_mem>(42);
139 test<long long, omp::allocator::cgroup_mem>(42);
140 test<long long, omp::allocator::pteam_mem>(42);
141 test<long long, omp::allocator::thread_mem>(42);
142 #ifdef __gnu_linux__
143 test<long long, ompx::allocator::gnu_pinned_mem>(42);
144 #endif
145
146 test<S, omp::allocator::null_allocator>( S{42, true, 128.f});
147 test<S, omp::allocator::default_mem>( S{42, true, 128.f});
148 test<S, omp::allocator::large_cap_mem>( S{42, true, 128.f});
149 test<S, omp::allocator::const_mem>( S{42, true, 128.f});
150 test<S, omp::allocator::high_bw_mem>( S{42, true, 128.f});
151 test<S, omp::allocator::low_lat_mem>( S{42, true, 128.f});
152 test<S, omp::allocator::cgroup_mem>( S{42, true, 128.f});
153 test<S, omp::allocator::pteam_mem>( S{42, true, 128.f});
154 test<S, omp::allocator::thread_mem>( S{42, true, 128.f});
155 #ifdef __gnu_linux__
156 test<S, ompx::allocator::gnu_pinned_mem>(S{42, true, 128.f});
157 #endif
158
159 test_inequality<int, omp::allocator::null_allocator>();
160 test_inequality<int, omp::allocator::default_mem>();
161 test_inequality<int, omp::allocator::large_cap_mem>();
162 test_inequality<int, omp::allocator::const_mem>();
163 test_inequality<int, omp::allocator::high_bw_mem>();
164 test_inequality<int, omp::allocator::low_lat_mem>();
165 test_inequality<int, omp::allocator::cgroup_mem>();
166 test_inequality<int, omp::allocator::pteam_mem>();
167 test_inequality<int, omp::allocator::thread_mem>();
168 #ifdef __gnu_linux__
169 test_inequality<int, ompx::allocator::gnu_pinned_mem>();
170 #endif
171 }