[PATCH 30/57][Arm][GAS] Add support for MVE instructions: vqmovnt, vqmovnb, vqmovunt...
[binutils-gdb.git] / gdb / unittests / array-view-selftests.c
blobd0d1128cd2a0452c2272c42d219c5fa635d6ab33
1 /* Self tests for array_view for GDB, the GNU debugger.
3 Copyright (C) 2017-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "common/selftest.h"
22 #include "common/array-view.h"
23 #include <array>
25 namespace selftests {
26 namespace array_view_tests {
28 /* Triviality checks. */
29 #define CHECK_TRAIT(TRAIT) \
30 static_assert (std::TRAIT<gdb::array_view<gdb_byte>>::value, "")
32 #if HAVE_IS_TRIVIALLY_COPYABLE
34 CHECK_TRAIT (is_trivially_copyable);
35 CHECK_TRAIT (is_trivially_move_assignable);
36 CHECK_TRAIT (is_trivially_move_constructible);
37 CHECK_TRAIT (is_trivially_destructible);
39 #endif
41 #undef CHECK_TRAIT
43 /* Wrapper around std::is_convertible to make the code using it a bit
44 shorter. (With C++14 we'd use a variable template instead.) */
46 template<typename From, typename To>
47 static constexpr bool
48 is_convertible ()
50 return std::is_convertible<From, To>::value;
53 /* Check for implicit conversion to immutable and mutable views. */
55 static constexpr bool
56 check_convertible ()
58 using T = gdb_byte;
59 using gdb::array_view;
61 return (true
62 /* immutable array_view */
63 && is_convertible<const T (&) [1], array_view<const T>> ()
64 && is_convertible<T (&) [1], array_view<const T>> ()
65 && is_convertible<const T, array_view<const T>> ()
66 && is_convertible<T, array_view<const T>> ()
68 /* mutable array_view */
69 && is_convertible<T (&) [1], array_view<T>> ()
70 && !is_convertible<const T (&) [1], array_view<T>> ()
71 && is_convertible<T, array_view<T>> ()
72 && !is_convertible<const T, array_view<T>> ()
74 /* While float is implicitly convertible to gdb_byte, we
75 don't want implicit float->array_view<gdb_byte>
76 conversion. */
77 && !is_convertible<float, array_view<const T>> ()
78 && !is_convertible<float, array_view<T>> ());
81 static_assert (check_convertible (), "");
83 namespace no_slicing
85 struct A { int i; };
86 struct B : A { int j; };
87 struct C : A { int l; };
89 /* Check that there's no array->view conversion for arrays of derived
90 types or subclasses. */
91 static constexpr bool
92 check ()
94 using gdb::array_view;
96 return (true
98 /* array->view */
100 && is_convertible <A (&)[1], array_view<A>> ()
101 && !is_convertible <B (&)[1], array_view<A>> ()
102 && !is_convertible <C (&)[1], array_view<A>> ()
104 && !is_convertible <A (&)[1], array_view<B>> ()
105 && is_convertible <B (&)[1], array_view<B>> ()
106 && !is_convertible <C (&)[1], array_view<B>> ()
108 /* elem->view */
110 && is_convertible <A, array_view<A>> ()
111 && !is_convertible <B, array_view<A>> ()
112 && !is_convertible <C, array_view<A>> ()
114 && !is_convertible <A, array_view<B>> ()
115 && is_convertible <B, array_view<B>> ()
116 && !is_convertible <C, array_view<B>> ());
119 } /* namespace no_slicing */
121 static_assert (no_slicing::check (), "");
123 /* Check that array_view implicitly converts from std::vector. */
125 static constexpr bool
126 check_convertible_from_std_vector ()
128 using gdb::array_view;
129 using T = gdb_byte;
131 /* Note there's no such thing as std::vector<const T>. */
133 return (true
134 && is_convertible <std::vector<T>, array_view<T>> ()
135 && is_convertible <std::vector<T>, array_view<const T>> ());
138 static_assert (check_convertible_from_std_vector (), "");
140 /* Check that array_view implicitly converts from std::array. */
142 static constexpr bool
143 check_convertible_from_std_array ()
145 using gdb::array_view;
146 using T = gdb_byte;
148 /* Note: a non-const T view can't refer to a const T array. */
150 return (true
151 && is_convertible <std::array<T, 1>, array_view<T>> ()
152 && is_convertible <std::array<T, 1>, array_view<const T>> ()
153 && !is_convertible <std::array<const T, 1>, array_view<T>> ()
154 && is_convertible <std::array<const T, 1>, array_view<const T>> ());
157 static_assert (check_convertible_from_std_array (), "");
159 /* Check that VIEW views C (a container like std::vector/std::array)
160 correctly. */
162 template<typename View, typename Container>
163 static bool
164 check_container_view (const View &view, const Container &c)
166 if (view.empty ())
167 return false;
168 if (view.size () != c.size ())
169 return false;
170 if (view.data () != c.data ())
171 return false;
172 for (size_t i = 0; i < c.size (); i++)
174 if (&view[i] != &c[i])
175 return false;
176 if (view[i] != c[i])
177 return false;
179 return true;
182 /* Check that VIEW views E (an object of the type of a view element)
183 correctly. */
185 template<typename View, typename Elem>
186 static bool
187 check_elem_view (const View &view, const Elem &e)
189 if (view.empty ())
190 return false;
191 if (view.size () != 1)
192 return false;
193 if (view.data () != &e)
194 return false;
195 if (&view[0] != &e)
196 return false;
197 if (view[0] != e)
198 return false;
199 return true;
202 /* Check for operator[]. The first overload is taken iff
203 'view<T>()[0] = T()' is a valid expression. */
205 template<typename View,
206 typename = decltype (std::declval<View> ()[0]
207 = std::declval<typename View::value_type> ())>
208 static bool
209 check_op_subscript (const View &view)
211 return true;
214 /* This overload is taken iff 'view<T>()[0] = T()' is not a valid
215 expression. */
217 static bool
218 check_op_subscript (...)
220 return false;
223 /* Check construction with pointer + size. This is a template in
224 order to test both gdb_byte and const gdb_byte. */
226 template<typename T>
227 static void
228 check_ptr_size_ctor ()
230 T data[] = {0x11, 0x22, 0x33, 0x44};
232 gdb::array_view<T> view (data + 1, 2);
234 SELF_CHECK (!view.empty ());
235 SELF_CHECK (view.size () == 2);
236 SELF_CHECK (view.data () == &data[1]);
237 SELF_CHECK (view[0] == data[1]);
238 SELF_CHECK (view[1] == data[2]);
240 gdb::array_view<const T> cview (data + 1, 2);
241 SELF_CHECK (!cview.empty ());
242 SELF_CHECK (cview.size () == 2);
243 SELF_CHECK (cview.data () == &data[1]);
244 SELF_CHECK (cview[0] == data[1]);
245 SELF_CHECK (cview[1] == data[2]);
248 /* Asserts std::is_constructible. */
250 template<typename T, typename... Args>
251 static constexpr bool
252 require_not_constructible ()
254 static_assert (!std::is_constructible<T, Args...>::value, "");
256 /* constexpr functions can't return void in C++11 (N3444). */
257 return true;
260 /* Check the array_view<T>(PTR, SIZE) ctor, when T is a pointer. */
262 void
263 check_ptr_size_ctor2 ()
265 struct A {};
266 A an_a;
268 A *array[] = { &an_a };
269 const A * const carray[] = { &an_a };
271 gdb::array_view<A *> v1 = {array, ARRAY_SIZE (array)};
272 gdb::array_view<A *> v2 = {array, (char) ARRAY_SIZE (array)};
273 gdb::array_view<A * const> v3 = {array, ARRAY_SIZE (array)};
274 gdb::array_view<const A * const> cv1 = {carray, ARRAY_SIZE (carray)};
276 require_not_constructible<gdb::array_view<A *>, decltype (carray), size_t> ();
278 SELF_CHECK (v1[0] == array[0]);
279 SELF_CHECK (v2[0] == array[0]);
280 SELF_CHECK (v3[0] == array[0]);
282 SELF_CHECK (!v1.empty ());
283 SELF_CHECK (v1.size () == 1);
284 SELF_CHECK (v1.data () == &array[0]);
286 SELF_CHECK (cv1[0] == carray[0]);
288 SELF_CHECK (!cv1.empty ());
289 SELF_CHECK (cv1.size () == 1);
290 SELF_CHECK (cv1.data () == &carray[0]);
293 /* Check construction with a pair of pointers. This is a template in
294 order to test both gdb_byte and const gdb_byte. */
296 template<typename T>
297 static void
298 check_ptr_ptr_ctor ()
300 T data[] = {0x11, 0x22, 0x33, 0x44};
302 gdb::array_view<T> view (data + 1, data + 3);
304 SELF_CHECK (!view.empty ());
305 SELF_CHECK (view.size () == 2);
306 SELF_CHECK (view.data () == &data[1]);
307 SELF_CHECK (view[0] == data[1]);
308 SELF_CHECK (view[1] == data[2]);
310 gdb_byte array[] = {0x11, 0x22, 0x33, 0x44};
311 const gdb_byte *p1 = array;
312 gdb_byte *p2 = array + ARRAY_SIZE (array);
313 gdb::array_view<const gdb_byte> view2 (p1, p2);
316 /* Check construction with a pair of pointers of mixed constness. */
318 static void
319 check_ptr_ptr_mixed_cv ()
321 gdb_byte array[] = {0x11, 0x22, 0x33, 0x44};
322 const gdb_byte *cp = array;
323 gdb_byte *p = array;
324 gdb::array_view<const gdb_byte> view1 (cp, p);
325 gdb::array_view<const gdb_byte> view2 (p, cp);
326 SELF_CHECK (view1.empty ());
327 SELF_CHECK (view2.empty ());
330 /* Check range-for support (i.e., begin()/end()). This is a template
331 in order to test both gdb_byte and const gdb_byte. */
333 template<typename T>
334 static void
335 check_range_for ()
337 T data[] = {1, 2, 3, 4};
338 gdb::array_view<T> view (data);
340 typename std::decay<T>::type sum = 0;
341 for (auto &elem : view)
342 sum += elem;
343 SELF_CHECK (sum == 1 + 2 + 3 + 4);
346 /* Entry point. */
348 static void
349 run_tests ()
351 /* Empty views. */
353 constexpr gdb::array_view<gdb_byte> view1;
354 constexpr gdb::array_view<const gdb_byte> view2;
356 static_assert (view1.empty (), "");
357 static_assert (view1.data () == nullptr, "");
358 static_assert (view1.size () == 0, "");
359 static_assert (view2.empty (), "");
360 static_assert (view2.size () == 0, "");
361 static_assert (view2.data () == nullptr, "");
364 std::vector<gdb_byte> vec = {0x11, 0x22, 0x33, 0x44 };
365 std::array<gdb_byte, 4> array = {{0x11, 0x22, 0x33, 0x44}};
367 /* Various tests of views over std::vector. */
369 gdb::array_view<gdb_byte> view = vec;
370 SELF_CHECK (check_container_view (view, vec));
371 gdb::array_view<const gdb_byte> cview = vec;
372 SELF_CHECK (check_container_view (cview, vec));
375 /* Likewise, over std::array. */
377 gdb::array_view<gdb_byte> view = array;
378 SELF_CHECK (check_container_view (view, array));
379 gdb::array_view<gdb_byte> cview = array;
380 SELF_CHECK (check_container_view (cview, array));
383 /* op=(std::vector/std::array/elem) */
385 gdb::array_view<gdb_byte> view;
387 view = vec;
388 SELF_CHECK (check_container_view (view, vec));
389 view = std::move (vec);
390 SELF_CHECK (check_container_view (view, vec));
392 view = array;
393 SELF_CHECK (check_container_view (view, array));
394 view = std::move (array);
395 SELF_CHECK (check_container_view (view, array));
397 gdb_byte elem = 0;
398 view = elem;
399 SELF_CHECK (check_elem_view (view, elem));
400 view = std::move (elem);
401 SELF_CHECK (check_elem_view (view, elem));
404 /* Test copy/move ctor and mutable->immutable conversion. */
406 gdb_byte data[] = {0x11, 0x22, 0x33, 0x44};
407 gdb::array_view<gdb_byte> view1 = data;
408 gdb::array_view<gdb_byte> view2 = view1;
409 gdb::array_view<gdb_byte> view3 = std::move (view1);
410 gdb::array_view<const gdb_byte> cview1 = data;
411 gdb::array_view<const gdb_byte> cview2 = cview1;
412 gdb::array_view<const gdb_byte> cview3 = std::move (cview1);
413 SELF_CHECK (view1[0] == data[0]);
414 SELF_CHECK (view2[0] == data[0]);
415 SELF_CHECK (view3[0] == data[0]);
416 SELF_CHECK (cview1[0] == data[0]);
417 SELF_CHECK (cview2[0] == data[0]);
418 SELF_CHECK (cview3[0] == data[0]);
421 /* Same, but op=(view). */
423 gdb_byte data[] = {0x55, 0x66, 0x77, 0x88};
424 gdb::array_view<gdb_byte> view1;
425 gdb::array_view<gdb_byte> view2;
426 gdb::array_view<gdb_byte> view3;
427 gdb::array_view<const gdb_byte> cview1;
428 gdb::array_view<const gdb_byte> cview2;
429 gdb::array_view<const gdb_byte> cview3;
431 view1 = data;
432 view2 = view1;
433 view3 = std::move (view1);
434 cview1 = data;
435 cview2 = cview1;
436 cview3 = std::move (cview1);
437 SELF_CHECK (view1[0] == data[0]);
438 SELF_CHECK (view2[0] == data[0]);
439 SELF_CHECK (view3[0] == data[0]);
440 SELF_CHECK (cview1[0] == data[0]);
441 SELF_CHECK (cview2[0] == data[0]);
442 SELF_CHECK (cview3[0] == data[0]);
445 /* op[] */
447 std::vector<gdb_byte> vec2 = {0x11, 0x22};
448 gdb::array_view<gdb_byte> view = vec2;
449 gdb::array_view<const gdb_byte> cview = vec2;
451 /* Check that op[] on a non-const view of non-const T returns a
452 mutable reference. */
453 view[0] = 0x33;
454 SELF_CHECK (vec2[0] == 0x33);
456 /* OTOH, check that assigning through op[] on a view of const T
457 wouldn't compile. */
458 SELF_CHECK (!check_op_subscript (cview));
459 /* For completeness. */
460 SELF_CHECK (check_op_subscript (view));
463 check_ptr_size_ctor<const gdb_byte> ();
464 check_ptr_size_ctor<gdb_byte> ();
465 check_ptr_size_ctor2 ();
466 check_ptr_ptr_ctor<const gdb_byte> ();
467 check_ptr_ptr_ctor<gdb_byte> ();
468 check_ptr_ptr_mixed_cv ();
470 check_range_for<gdb_byte> ();
471 check_range_for<const gdb_byte> ();
473 /* Check that the right ctor overloads are taken when the element is
474 a container. */
476 using Vec = std::vector<gdb_byte>;
477 Vec vecs[3];
479 gdb::array_view<Vec> view_array = vecs;
480 SELF_CHECK (view_array.size () == 3);
482 Vec elem;
483 gdb::array_view<Vec> view_elem = elem;
484 SELF_CHECK (view_elem.size () == 1);
487 /* gdb::make_array_view, int length. */
489 gdb_byte data[] = {0x55, 0x66, 0x77, 0x88};
490 int len = sizeof (data) / sizeof (data[0]);
491 auto view = gdb::make_array_view (data, len);
493 SELF_CHECK (view.data () == data);
494 SELF_CHECK (view.size () == len);
496 for (size_t i = 0; i < len; i++)
497 SELF_CHECK (view[i] == data[i]);
500 /* Test slicing. */
502 gdb_byte data[] = {0x55, 0x66, 0x77, 0x88, 0x99};
503 gdb::array_view<gdb_byte> view = data;
506 auto slc = view.slice (1, 3);
507 SELF_CHECK (slc.data () == data + 1);
508 SELF_CHECK (slc.size () == 3);
509 SELF_CHECK (slc[0] == data[1]);
510 SELF_CHECK (slc[0] == view[1]);
514 auto slc = view.slice (2);
515 SELF_CHECK (slc.data () == data + 2);
516 SELF_CHECK (slc.size () == 3);
517 SELF_CHECK (slc[0] == view[2]);
518 SELF_CHECK (slc[0] == data[2]);
523 } /* namespace array_view_tests */
524 } /* namespace selftests */
526 void
527 _initialize_array_view_selftests ()
529 selftests::register_test ("array_view",
530 selftests::array_view_tests::run_tests);