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/>. */
21 #include "common/selftest.h"
22 #include "common/array-view.h"
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
);
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
>
50 return std::is_convertible
<From
, To
>::value
;
53 /* Check for implicit conversion to immutable and mutable views. */
59 using gdb::array_view
;
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>
77 && !is_convertible
<float, array_view
<const T
>> ()
78 && !is_convertible
<float, array_view
<T
>> ());
81 static_assert (check_convertible (), "");
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. */
94 using gdb::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
>> ()
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
;
131 /* Note there's no such thing as std::vector<const T>. */
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
;
148 /* Note: a non-const T view can't refer to a const T array. */
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)
162 template<typename View
, typename Container
>
164 check_container_view (const View
&view
, const Container
&c
)
168 if (view
.size () != c
.size ())
170 if (view
.data () != c
.data ())
172 for (size_t i
= 0; i
< c
.size (); i
++)
174 if (&view
[i
] != &c
[i
])
182 /* Check that VIEW views E (an object of the type of a view element)
185 template<typename View
, typename Elem
>
187 check_elem_view (const View
&view
, const Elem
&e
)
191 if (view
.size () != 1)
193 if (view
.data () != &e
)
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
> ())>
209 check_op_subscript (const View
&view
)
214 /* This overload is taken iff 'view<T>()[0] = T()' is not a valid
218 check_op_subscript (...)
223 /* Check construction with pointer + size. This is a template in
224 order to test both gdb_byte and const gdb_byte. */
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). */
260 /* Check the array_view<T>(PTR, SIZE) ctor, when T is a pointer. */
263 check_ptr_size_ctor2 ()
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. */
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. */
319 check_ptr_ptr_mixed_cv ()
321 gdb_byte array
[] = {0x11, 0x22, 0x33, 0x44};
322 const gdb_byte
*cp
= 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. */
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
)
343 SELF_CHECK (sum
== 1 + 2 + 3 + 4);
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
;
388 SELF_CHECK (check_container_view (view
, vec
));
389 view
= std::move (vec
);
390 SELF_CHECK (check_container_view (view
, vec
));
393 SELF_CHECK (check_container_view (view
, array
));
394 view
= std::move (array
);
395 SELF_CHECK (check_container_view (view
, array
));
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
;
433 view3
= std::move (view1
);
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]);
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. */
454 SELF_CHECK (vec2
[0] == 0x33);
456 /* OTOH, check that assigning through op[] on a view of const T
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
476 using Vec
= std::vector
<gdb_byte
>;
479 gdb::array_view
<Vec
> view_array
= vecs
;
480 SELF_CHECK (view_array
.size () == 3);
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
]);
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 */
527 _initialize_array_view_selftests ()
529 selftests::register_test ("array_view",
530 selftests::array_view_tests::run_tests
);