Debian Codegraph @

libgomp/testsuite/libgomp.c++/target-flex-100.C

raw
1 /* Container adaptors in target region.
2 Does not test comparison operators other than equality to allow these tests
3 to be generalized to arbitrary input data. */
4
5 #include <algorithm>
6 #include <cstdio>
7 #include <deque>
8 #include <queue>
9 #include <stack>
10 #include <vector>
11
12 #include "target-flex-common.h"
13
14 template<typename T, std::size_t Size>
15 bool test_stack(T (&arr)[Size])
16 {
17 bool ok;
18 #pragma omp target map(from: ok) map(to: arr[:Size])
19 {
20 bool inner_ok = true;
21 const std::size_t half_size = Size / 2;
22 const T first_element = arr[0];
23 const T middle_element = arr[half_size - 1];
24 const T last_element = arr[Size - 1];
25 typedef std::stack<T, std::vector<T> > stack_type;
26 stack_type stack;
27 VERIFY (stack.empty());
28 VERIFY (stack.size() == 0);
29 {
30 /* Do half with push. */
31 std::size_t idx = 0;
32 for (; idx < half_size; ++idx)
33 {
34 stack.push(arr[idx]);
35 VERIFY (stack.top() == arr[idx]);
36 }
37 VERIFY (stack.size() == half_size);
38 VERIFY (static_cast<const stack_type&>(stack).size() == half_size);
39 for (; idx < Size; ++idx)
40 {
41 #if __cplusplus >= 201103L
42 /* Do the rest with emplace if C++11 or higher. */
43 stack.emplace(arr[idx]);
44 #else
45 /* Otherwise just use push again. */
46 stack.push(arr[idx]);
47 #endif
48 VERIFY (stack.top() == arr[idx]);
49 }
50 VERIFY (stack.size() == Size);
51 VERIFY (static_cast<const stack_type&>(stack).size() == Size);
52
53 const stack_type stack_orig = stack_type(std::vector<T>(arr, arr + Size));
54 VERIFY (stack == stack_orig);
55 /* References are contained in their own scope so we don't accidently
56 add tests referencing them after they have been invalidated. */
57 {
58 const T& const_top = static_cast<const stack_type&>(stack).top();
59 VERIFY (const_top == last_element);
60 T& mutable_top = stack.top();
61 mutable_top = first_element;
62 VERIFY (const_top == first_element);
63 }
64 /* Will only compare inequal if the first and last elements are different. */
65 VERIFY (first_element != last_element || stack != stack_orig);
66 for (std::size_t count = Size - half_size; count != 0; --count)
67 stack.pop();
68 VERIFY (stack.top() == middle_element);
69 const stack_type stack_half_orig = stack_type(std::vector<T>(arr, arr + half_size));
70 VERIFY (stack == stack_half_orig);
71 }
72 end:
73 ok = inner_ok;
74 }
75 return ok;
76 }
77
78 template<typename T, std::size_t Size>
79 bool test_queue(T (&arr)[Size])
80 {
81 bool ok;
82 #pragma omp target map(from: ok) map(to: arr[:Size])
83 {
84 bool inner_ok = true;
85 const std::size_t half_size = Size / 2;
86 const T first_element = arr[0];
87 const T last_element = arr[Size - 1];
88 typedef std::queue<T, std::deque<T> > queue_type;
89 queue_type queue;
90 VERIFY (queue.empty());
91 VERIFY (queue.size() == 0);
92 {
93 /* Do half with push. */
94 std::size_t idx = 0;
95 for (; idx < half_size; ++idx)
96 {
97 queue.push(arr[idx]);
98 VERIFY (queue.back() == arr[idx]);
99 VERIFY (queue.front() == first_element);
100 }
101 VERIFY (queue.size() == half_size);
102 VERIFY (static_cast<const queue_type&>(queue).size() == half_size);
103 for (; idx < Size; ++idx)
104 {
105 #if __cplusplus >= 201103L
106 /* Do the rest with emplace if C++11 or higher. */
107 queue.emplace(arr[idx]);
108 #else
109 /* Otherwise just use push again. */
110 queue.push(arr[idx]);
111 #endif
112 VERIFY (queue.back() == arr[idx]);
113 }
114 VERIFY (queue.size() == Size);
115 VERIFY (static_cast<const queue_type&>(queue).size() == Size);
116
117 const queue_type queue_orig = queue_type(std::deque<T>(arr, arr + Size));
118 VERIFY (queue == queue_orig);
119
120 /* References are contained in their own scope so we don't accidently
121 add tests referencing them after they have been invalidated. */
122 {
123 const T& const_front = static_cast<const queue_type&>(queue).front();
124 VERIFY (const_front == first_element);
125 T& mutable_front = queue.front();
126
127 const T& const_back = static_cast<const queue_type&>(queue).back();
128 VERIFY (const_back == last_element);
129 T& mutable_back = queue.back();
130 {
131 using std::swap;
132 swap(mutable_front, mutable_back);
133 }
134 VERIFY (const_front == last_element);
135 VERIFY (const_back == first_element);
136 /* Will only compare inequal if the first and last elements are different. */
137 VERIFY (first_element != last_element || queue != queue_orig);
138 /* Return the last element to normal for the next comparison. */
139 mutable_back = last_element;
140 }
141
142 const T middle_element = arr[half_size];
143 for (std::size_t count = Size - half_size; count != 0; --count)
144 queue.pop();
145 VERIFY (queue.front() == middle_element);
146 const queue_type queue_upper_half = queue_type(std::deque<T>(arr + half_size, arr + Size));
147 VERIFY (queue == queue_upper_half);
148 }
149 end:
150 ok = inner_ok;
151 }
152 return ok;
153 }
154
155 template<typename T, std::size_t Size>
156 bool test_priority_queue(T (&arr)[Size], const T min_value, const T max_value)
157 {
158 bool ok;
159 #pragma omp target map(from: ok) map(to: arr[:Size])
160 {
161 bool inner_ok = true;
162 typedef std::priority_queue<T, std::vector<T> > priority_queue_type;
163 {
164 priority_queue_type pqueue;
165 VERIFY (pqueue.empty());
166 VERIFY (pqueue.size() == 0);
167 }
168 {
169 priority_queue_type pqueue(arr, arr + Size);
170 VERIFY (!pqueue.empty());
171 VERIFY (pqueue.size() == Size);
172 VERIFY (static_cast<const priority_queue_type&>(pqueue).size() == Size);
173
174 const T old_max = pqueue.top();
175
176 #if __cplusplus >= 201103L
177 pqueue.emplace(max_value);
178 #else
179 pqueue.push(max_value);
180 #endif
181 VERIFY (pqueue.top() == max_value);
182 pqueue.pop();
183 VERIFY (pqueue.top() == old_max);
184 pqueue.push(min_value);
185 VERIFY (pqueue.top() == old_max);
186 pqueue.push(max_value);
187 VERIFY (pqueue.top() == max_value);
188 pqueue.pop();
189 VERIFY (pqueue.top() == old_max);
190 VERIFY (pqueue.size() == Size + 1);
191
192 for (std::size_t count = Size; count != 0; --count)
193 pqueue.pop();
194 VERIFY (pqueue.size() == 1);
195 VERIFY (pqueue.top() == min_value);
196 }
197 end:
198 ok = inner_ok;
199 }
200 return ok;
201 }
202
203 int main()
204 {
205 int arr[10] = {0,1,2,3,4,5,6,7,8,9};
206
207 return test_stack(arr)
208 && test_queue(arr)
209 && test_priority_queue(arr, 0, 1000) ? 0 : 1;
210 }