Give unique names to s390 assembler opcode tests.
[binutils-gdb.git] / gdb / unittests / array-view-selftests.c
blobc07b5720ef0abe5bbe5c01c503d407394b135665
1 /* Self tests for array_view for GDB, the GNU debugger.
3 Copyright (C) 2017-2024 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 "gdbsupport/selftest.h"
21 #include "gdbsupport/array-view.h"
22 #include <array>
23 #include <vector>
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 CHECK_TRAIT (is_trivially_copyable);
33 CHECK_TRAIT (is_trivially_move_assignable);
34 CHECK_TRAIT (is_trivially_move_constructible);
35 CHECK_TRAIT (is_trivially_destructible);
37 #undef CHECK_TRAIT
39 /* Wrapper around std::is_convertible to make the code using it a bit
40 shorter. (With C++14 we'd use a variable template instead.) */
42 template<typename From, typename To>
43 static constexpr bool
44 is_convertible ()
46 return std::is_convertible<From, To>::value;
49 /* Check for implicit conversion to immutable and mutable views. */
51 static constexpr bool
52 check_convertible ()
54 using T = gdb_byte;
55 using gdb::array_view;
57 return (true
58 /* immutable array_view */
59 && is_convertible<const T (&) [1], array_view<const T>> ()
60 && is_convertible<T (&) [1], array_view<const T>> ()
61 && is_convertible<const T, array_view<const T>> ()
62 && is_convertible<T, array_view<const T>> ()
64 /* mutable array_view */
65 && is_convertible<T (&) [1], array_view<T>> ()
66 && !is_convertible<const T (&) [1], array_view<T>> ()
67 && is_convertible<T, array_view<T>> ()
68 && !is_convertible<const T, array_view<T>> ()
70 /* While float is implicitly convertible to gdb_byte, we
71 don't want implicit float->array_view<gdb_byte>
72 conversion. */
73 && !is_convertible<float, array_view<const T>> ()
74 && !is_convertible<float, array_view<T>> ());
77 static_assert (check_convertible (), "");
79 namespace no_slicing
81 struct A { int i; };
82 struct B : A { int j; };
83 struct C : A { int l; };
85 /* Check that there's no array->view conversion for arrays of derived types or
86 subclasses. */
87 static constexpr bool
88 check ()
90 using gdb::array_view;
92 return (true
94 /* array->view */
96 && is_convertible <A (&)[1], array_view<A>> ()
97 && !is_convertible <B (&)[1], array_view<A>> ()
98 && !is_convertible <C (&)[1], array_view<A>> ()
100 && !is_convertible <A (&)[1], array_view<B>> ()
101 && is_convertible <B (&)[1], array_view<B>> ()
102 && !is_convertible <C (&)[1], array_view<B>> ()
104 /* elem->view */
106 && is_convertible <A, array_view<A>> ()
107 && !is_convertible <B, array_view<A>> ()
108 && !is_convertible <C, array_view<A>> ()
110 && !is_convertible <A, array_view<B>> ()
111 && is_convertible <B, array_view<B>> ()
112 && !is_convertible <C, array_view<B>> ());
115 /* Check that there's no container->view conversion for containers of derived
116 types or subclasses. */
118 template<template<typename ...> class Container>
119 static constexpr bool
120 check_ctor_from_container ()
122 using gdb::array_view;
124 return ( is_convertible <Container<A>, array_view<A>> ()
125 && !is_convertible <Container<B>, array_view<A>> ()
126 && !is_convertible <Container<C>, array_view<A>> ()
128 && !is_convertible <Container<A>, array_view<B>> ()
129 && is_convertible <Container<B>, array_view<B>> ()
130 && !is_convertible <Container<C>, array_view<B>> ());
133 } /* namespace no_slicing */
135 /* std::array with only one template argument, so we can pass it to
136 check_ctor_from_container. */
137 template<typename T> using StdArray1 = std::array<T, 1>;
139 static_assert (no_slicing::check (), "");
140 static_assert (no_slicing::check_ctor_from_container<std::vector> (), "");
141 static_assert (no_slicing::check_ctor_from_container<StdArray1> (), "");
142 static_assert (no_slicing::check_ctor_from_container<gdb::array_view> (), "");
144 /* Check that array_view implicitly converts from std::vector. */
146 static constexpr bool
147 check_convertible_from_std_vector ()
149 using gdb::array_view;
150 using T = gdb_byte;
152 /* Note there's no such thing as std::vector<const T>. */
154 return (true
155 && is_convertible <std::vector<T>, array_view<T>> ()
156 && is_convertible <std::vector<T>, array_view<const T>> ());
159 static_assert (check_convertible_from_std_vector (), "");
161 /* Check that array_view implicitly converts from std::array. */
163 static constexpr bool
164 check_convertible_from_std_array ()
166 using gdb::array_view;
167 using T = gdb_byte;
169 /* Note: a non-const T view can't refer to a const T array. */
171 return (true
172 && is_convertible <std::array<T, 1>, array_view<T>> ()
173 && is_convertible <std::array<T, 1>, array_view<const T>> ()
174 && !is_convertible <std::array<const T, 1>, array_view<T>> ()
175 && is_convertible <std::array<const T, 1>, array_view<const T>> ());
178 static_assert (check_convertible_from_std_array (), "");
180 /* Check that VIEW views C (a container like std::vector/std::array)
181 correctly. */
183 template<typename View, typename Container>
184 static bool
185 check_container_view (const View &view, const Container &c)
187 if (view.empty ())
188 return false;
189 if (view.size () != c.size ())
190 return false;
191 if (view.data () != c.data ())
192 return false;
193 for (size_t i = 0; i < c.size (); i++)
195 if (&view[i] != &c[i])
196 return false;
197 if (view[i] != c[i])
198 return false;
200 return true;
203 /* Check that VIEW views E (an object of the type of a view element)
204 correctly. */
206 template<typename View, typename Elem>
207 static bool
208 check_elem_view (const View &view, const Elem &e)
210 if (view.empty ())
211 return false;
212 if (view.size () != 1)
213 return false;
214 if (view.data () != &e)
215 return false;
216 if (&view[0] != &e)
217 return false;
218 if (view[0] != e)
219 return false;
220 return true;
223 /* Check for operator[]. The first overload is taken iff
224 'view<T>()[0] = T()' is a valid expression. */
226 template<typename View,
227 typename = decltype (std::declval<View> ()[0]
228 = std::declval<typename View::value_type> ())>
229 static bool
230 check_op_subscript (const View &view)
232 return true;
235 /* This overload is taken iff 'view<T>()[0] = T()' is not a valid
236 expression. */
238 static bool
239 check_op_subscript (...)
241 return false;
244 /* Check construction with pointer + size. This is a template in
245 order to test both gdb_byte and const gdb_byte. */
247 template<typename T>
248 static void
249 check_ptr_size_ctor ()
251 T data[] = {0x11, 0x22, 0x33, 0x44};
253 gdb::array_view<T> view (data + 1, 2);
255 SELF_CHECK (!view.empty ());
256 SELF_CHECK (view.size () == 2);
257 SELF_CHECK (view.data () == &data[1]);
258 SELF_CHECK (view[0] == data[1]);
259 SELF_CHECK (view[1] == data[2]);
261 gdb::array_view<const T> cview (data + 1, 2);
262 SELF_CHECK (!cview.empty ());
263 SELF_CHECK (cview.size () == 2);
264 SELF_CHECK (cview.data () == &data[1]);
265 SELF_CHECK (cview[0] == data[1]);
266 SELF_CHECK (cview[1] == data[2]);
269 /* Asserts std::is_constructible. */
271 template<typename T, typename... Args>
272 static constexpr bool
273 require_not_constructible ()
275 static_assert (!std::is_constructible<T, Args...>::value, "");
277 /* constexpr functions can't return void in C++11 (N3444). */
278 return true;
281 /* Check the array_view<T>(PTR, SIZE) ctor, when T is a pointer. */
283 static void
284 check_ptr_size_ctor2 ()
286 struct A {};
287 A an_a;
289 A *array[] = { &an_a };
290 const A * const carray[] = { &an_a };
292 gdb::array_view<A *> v1 = {array, ARRAY_SIZE (array)};
293 gdb::array_view<A *> v2 = {array, (char) ARRAY_SIZE (array)};
294 gdb::array_view<A * const> v3 = {array, ARRAY_SIZE (array)};
295 gdb::array_view<const A * const> cv1 = {carray, ARRAY_SIZE (carray)};
297 require_not_constructible<gdb::array_view<A *>, decltype (carray), size_t> ();
299 SELF_CHECK (v1[0] == array[0]);
300 SELF_CHECK (v2[0] == array[0]);
301 SELF_CHECK (v3[0] == array[0]);
303 SELF_CHECK (!v1.empty ());
304 SELF_CHECK (v1.size () == 1);
305 SELF_CHECK (v1.data () == &array[0]);
307 SELF_CHECK (cv1[0] == carray[0]);
309 SELF_CHECK (!cv1.empty ());
310 SELF_CHECK (cv1.size () == 1);
311 SELF_CHECK (cv1.data () == &carray[0]);
314 /* Check construction with a pair of pointers. This is a template in
315 order to test both gdb_byte and const gdb_byte. */
317 template<typename T>
318 static void
319 check_ptr_ptr_ctor ()
321 T data[] = {0x11, 0x22, 0x33, 0x44};
323 gdb::array_view<T> view (data + 1, data + 3);
325 SELF_CHECK (!view.empty ());
326 SELF_CHECK (view.size () == 2);
327 SELF_CHECK (view.data () == &data[1]);
328 SELF_CHECK (view[0] == data[1]);
329 SELF_CHECK (view[1] == data[2]);
331 gdb_byte array[] = {0x11, 0x22, 0x33, 0x44};
332 const gdb_byte *p1 = array;
333 gdb_byte *p2 = array + ARRAY_SIZE (array);
334 gdb::array_view<const gdb_byte> view2 (p1, p2);
337 /* Check construction with a pair of pointers of mixed constness. */
339 static void
340 check_ptr_ptr_mixed_cv ()
342 gdb_byte array[] = {0x11, 0x22, 0x33, 0x44};
343 const gdb_byte *cp = array;
344 gdb_byte *p = array;
345 gdb::array_view<const gdb_byte> view1 (cp, p);
346 gdb::array_view<const gdb_byte> view2 (p, cp);
347 SELF_CHECK (view1.empty ());
348 SELF_CHECK (view2.empty ());
351 /* Check range-for support (i.e., begin()/end()). This is a template
352 in order to test both gdb_byte and const gdb_byte. */
354 template<typename T>
355 static void
356 check_range_for ()
358 T data[] = {1, 2, 3, 4};
359 gdb::array_view<T> view (data);
361 typename std::decay<T>::type sum = 0;
362 for (auto &elem : view)
363 sum += elem;
364 SELF_CHECK (sum == 1 + 2 + 3 + 4);
367 template<typename T>
368 static void
369 check_iterator ()
371 T data[] = {1, 2, 3, 4};
372 gdb::array_view<T> view (data);
374 typename std::decay<T>::type sum = 0;
375 for (typename gdb::array_view<T>::iterator it = view.begin ();
376 it != view.end (); it++)
378 *it *= 2;
379 sum += *it;
382 SELF_CHECK (sum == 2 + 4 + 6 + 8);
385 template<typename T>
386 static void
387 check_const_iterator ()
389 T data[] = {1, 2, 3, 4};
390 gdb::array_view<T> view (data);
392 typename std::decay<T>::type sum = 0;
393 for (typename gdb::array_view<T>::const_iterator it = view.cbegin ();
394 it != view.cend (); it++)
395 sum += *it;
397 SELF_CHECK (sum == 1 + 2 + 3 + 4);
400 /* Entry point. */
402 static void
403 run_tests ()
405 /* Empty views. */
407 constexpr gdb::array_view<gdb_byte> view1;
408 constexpr gdb::array_view<const gdb_byte> view2;
410 static_assert (view1.empty (), "");
411 static_assert (view1.data () == nullptr, "");
412 static_assert (view1.size () == 0, "");
413 static_assert (view2.empty (), "");
414 static_assert (view2.size () == 0, "");
415 static_assert (view2.data () == nullptr, "");
418 std::vector<gdb_byte> vec = {0x11, 0x22, 0x33, 0x44 };
419 std::array<gdb_byte, 4> array = {{0x11, 0x22, 0x33, 0x44}};
421 /* Various tests of views over std::vector. */
423 gdb::array_view<gdb_byte> view = vec;
424 SELF_CHECK (check_container_view (view, vec));
425 gdb::array_view<const gdb_byte> cview = vec;
426 SELF_CHECK (check_container_view (cview, vec));
429 /* Likewise, over std::array. */
431 gdb::array_view<gdb_byte> view = array;
432 SELF_CHECK (check_container_view (view, array));
433 gdb::array_view<gdb_byte> cview = array;
434 SELF_CHECK (check_container_view (cview, array));
437 /* op=(std::vector/std::array/elem) */
439 gdb::array_view<gdb_byte> view;
441 view = vec;
442 SELF_CHECK (check_container_view (view, vec));
443 view = std::move (vec);
444 SELF_CHECK (check_container_view (view, vec));
446 view = array;
447 SELF_CHECK (check_container_view (view, array));
448 view = std::move (array);
449 SELF_CHECK (check_container_view (view, array));
451 gdb_byte elem = 0;
452 view = elem;
453 SELF_CHECK (check_elem_view (view, elem));
454 view = std::move (elem);
455 SELF_CHECK (check_elem_view (view, elem));
458 /* Test copy/move ctor and mutable->immutable conversion. */
460 gdb_byte data[] = {0x11, 0x22, 0x33, 0x44};
461 gdb::array_view<gdb_byte> view1 = data;
462 gdb::array_view<gdb_byte> view2 = view1;
463 gdb::array_view<gdb_byte> view3 = std::move (view1);
464 gdb::array_view<const gdb_byte> cview1 = data;
465 gdb::array_view<const gdb_byte> cview2 = cview1;
466 gdb::array_view<const gdb_byte> cview3 = std::move (cview1);
467 SELF_CHECK (view1[0] == data[0]);
468 SELF_CHECK (view2[0] == data[0]);
469 SELF_CHECK (view3[0] == data[0]);
470 SELF_CHECK (cview1[0] == data[0]);
471 SELF_CHECK (cview2[0] == data[0]);
472 SELF_CHECK (cview3[0] == data[0]);
475 /* Same, but op=(view). */
477 gdb_byte data[] = {0x55, 0x66, 0x77, 0x88};
478 gdb::array_view<gdb_byte> view1;
479 gdb::array_view<gdb_byte> view2;
480 gdb::array_view<gdb_byte> view3;
481 gdb::array_view<const gdb_byte> cview1;
482 gdb::array_view<const gdb_byte> cview2;
483 gdb::array_view<const gdb_byte> cview3;
485 view1 = data;
486 view2 = view1;
487 view3 = std::move (view1);
488 cview1 = data;
489 cview2 = cview1;
490 cview3 = std::move (cview1);
491 SELF_CHECK (view1[0] == data[0]);
492 SELF_CHECK (view2[0] == data[0]);
493 SELF_CHECK (view3[0] == data[0]);
494 SELF_CHECK (cview1[0] == data[0]);
495 SELF_CHECK (cview2[0] == data[0]);
496 SELF_CHECK (cview3[0] == data[0]);
499 /* op[] */
501 std::vector<gdb_byte> vec2 = {0x11, 0x22};
502 gdb::array_view<gdb_byte> view = vec2;
503 gdb::array_view<const gdb_byte> cview = vec2;
505 /* Check that op[] on a non-const view of non-const T returns a
506 mutable reference. */
507 view[0] = 0x33;
508 SELF_CHECK (vec2[0] == 0x33);
510 /* OTOH, check that assigning through op[] on a view of const T
511 wouldn't compile. */
512 SELF_CHECK (!check_op_subscript (cview));
513 /* For completeness. */
514 SELF_CHECK (check_op_subscript (view));
517 check_ptr_size_ctor<const gdb_byte> ();
518 check_ptr_size_ctor<gdb_byte> ();
519 check_ptr_size_ctor2 ();
520 check_ptr_ptr_ctor<const gdb_byte> ();
521 check_ptr_ptr_ctor<gdb_byte> ();
522 check_ptr_ptr_mixed_cv ();
524 check_range_for<gdb_byte> ();
525 check_range_for<const gdb_byte> ();
526 check_iterator<gdb_byte> ();
527 check_const_iterator<gdb_byte> ();
528 check_const_iterator<const gdb_byte> ();
530 /* Check that the right ctor overloads are taken when the element is
531 a container. */
533 using Vec = std::vector<gdb_byte>;
534 Vec vecs[3];
536 gdb::array_view<Vec> view_array = vecs;
537 SELF_CHECK (view_array.size () == 3);
539 Vec elem;
540 gdb::array_view<Vec> view_elem = elem;
541 SELF_CHECK (view_elem.size () == 1);
544 /* gdb::make_array_view, int length. */
546 gdb_byte data[] = {0x55, 0x66, 0x77, 0x88};
547 int len = sizeof (data) / sizeof (data[0]);
548 auto view = gdb::make_array_view (data, len);
550 SELF_CHECK (view.data () == data);
551 SELF_CHECK (view.size () == len);
553 for (size_t i = 0; i < len; i++)
554 SELF_CHECK (view[i] == data[i]);
557 /* Test slicing. */
559 gdb_byte data[] = {0x55, 0x66, 0x77, 0x88, 0x99};
560 gdb::array_view<gdb_byte> view = data;
563 auto slc = view.slice (1, 3);
564 SELF_CHECK (slc.data () == data + 1);
565 SELF_CHECK (slc.size () == 3);
566 SELF_CHECK (slc[0] == data[1]);
567 SELF_CHECK (slc[0] == view[1]);
571 auto slc = view.slice (2);
572 SELF_CHECK (slc.data () == data + 2);
573 SELF_CHECK (slc.size () == 3);
574 SELF_CHECK (slc[0] == view[2]);
575 SELF_CHECK (slc[0] == data[2]);
580 template <typename T>
581 void
582 run_copy_test ()
584 /* Test non-overlapping copy. */
586 const std::vector<T> src_v = {1, 2, 3, 4};
587 std::vector<T> dest_v (4, -1);
589 SELF_CHECK (dest_v != src_v);
590 copy (gdb::array_view<const T> (src_v), gdb::array_view<T> (dest_v));
591 SELF_CHECK (dest_v == src_v);
594 /* Test overlapping copy, where the source is before the destination. */
596 std::vector<T> vec = {1, 2, 3, 4, 5, 6, 7, 8};
597 gdb::array_view<T> v = vec;
599 copy (v.slice (1, 4),
600 v.slice (2, 4));
602 std::vector<T> expected = {1, 2, 2, 3, 4, 5, 7, 8};
603 SELF_CHECK (vec == expected);
606 /* Test overlapping copy, where the source is after the destination. */
608 std::vector<T> vec = {1, 2, 3, 4, 5, 6, 7, 8};
609 gdb::array_view<T> v = vec;
611 copy (v.slice (2, 4),
612 v.slice (1, 4));
614 std::vector<T> expected = {1, 3, 4, 5, 6, 6, 7, 8};
615 SELF_CHECK (vec == expected);
618 /* Test overlapping copy, where the source is the same as the destination. */
620 std::vector<T> vec = {1, 2, 3, 4, 5, 6, 7, 8};
621 gdb::array_view<T> v = vec;
623 copy (v.slice (2, 4),
624 v.slice (2, 4));
626 std::vector<T> expected = {1, 2, 3, 4, 5, 6, 7, 8};
627 SELF_CHECK (vec == expected);
631 /* Class with a non-trivial copy assignment operator, used to test the
632 array_view copy function. */
633 struct foo
635 /* Can be implicitly constructed from an int, such that we can use the same
636 templated test function to test against array_view<int> and
637 array_view<foo>. */
638 foo (int n)
639 : n (n)
642 /* Needed to avoid -Wdeprecated-copy-with-user-provided-copy error with
643 Clang. */
644 foo (const foo &other) = default;
646 void operator= (const foo &other)
648 this->n = other.n;
649 this->n_assign_op_called++;
652 bool operator==(const foo &other) const
654 return this->n == other.n;
657 int n;
659 /* Number of times the assignment operator has been called. */
660 static int n_assign_op_called;
663 int foo::n_assign_op_called = 0;
665 /* Test the array_view copy free function. */
667 static void
668 run_copy_tests ()
670 /* Test with a trivial type. */
671 run_copy_test<int> ();
673 /* Test with a non-trivial type. */
674 foo::n_assign_op_called = 0;
675 run_copy_test<foo> ();
677 /* Make sure that for the non-trivial type foo, the assignment operator was
678 called an amount of times that makes sense. */
679 SELF_CHECK (foo::n_assign_op_called == 12);
682 } /* namespace array_view_tests */
683 } /* namespace selftests */
685 void _initialize_array_view_selftests ();
686 void
687 _initialize_array_view_selftests ()
689 selftests::register_test ("array_view",
690 selftests::array_view_tests::run_tests);
691 selftests::register_test ("array_view-copy",
692 selftests::array_view_tests::run_copy_tests);