/* Self tests for array_view for GDB, the GNU debugger.
- Copyright (C) 2017 Free Software Foundation, Inc.
+ Copyright (C) 2017-2020 Free Software Foundation, Inc.
This file is part of GDB.
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
-#include "selftest.h"
-#include "common/array-view.h"
+#include "gdbsupport/selftest.h"
+#include "gdbsupport/array-view.h"
#include <array>
namespace selftests {
/* Check the array_view<T>(PTR, SIZE) ctor, when T is a pointer. */
-void
+static void
check_ptr_size_ctor2 ()
{
struct A {};
/* op[] */
{
- std::vector<gdb_byte> vec = {0x11, 0x22};
- gdb::array_view<gdb_byte> view = vec;
- gdb::array_view<const gdb_byte> cview = vec;
+ std::vector<gdb_byte> vec2 = {0x11, 0x22};
+ gdb::array_view<gdb_byte> view = vec2;
+ gdb::array_view<const gdb_byte> cview = vec2;
/* Check that op[] on a non-const view of non-const T returns a
mutable reference. */
view[0] = 0x33;
- SELF_CHECK (vec[0] == 0x33);
+ SELF_CHECK (vec2[0] == 0x33);
/* OTOH, check that assigning through op[] on a view of const T
wouldn't compile. */
gdb::array_view<Vec> view_elem = elem;
SELF_CHECK (view_elem.size () == 1);
}
+
+ /* gdb::make_array_view, int length. */
+ {
+ gdb_byte data[] = {0x55, 0x66, 0x77, 0x88};
+ int len = sizeof (data) / sizeof (data[0]);
+ auto view = gdb::make_array_view (data, len);
+
+ SELF_CHECK (view.data () == data);
+ SELF_CHECK (view.size () == len);
+
+ for (size_t i = 0; i < len; i++)
+ SELF_CHECK (view[i] == data[i]);
+ }
+
+ /* Test slicing. */
+ {
+ gdb_byte data[] = {0x55, 0x66, 0x77, 0x88, 0x99};
+ gdb::array_view<gdb_byte> view = data;
+
+ {
+ auto slc = view.slice (1, 3);
+ SELF_CHECK (slc.data () == data + 1);
+ SELF_CHECK (slc.size () == 3);
+ SELF_CHECK (slc[0] == data[1]);
+ SELF_CHECK (slc[0] == view[1]);
+ }
+
+ {
+ auto slc = view.slice (2);
+ SELF_CHECK (slc.data () == data + 2);
+ SELF_CHECK (slc.size () == 3);
+ SELF_CHECK (slc[0] == view[2]);
+ SELF_CHECK (slc[0] == data[2]);
+ }
+ }
}
} /* namespace array_view_tests */
} /* namespace selftests */
+void _initialize_array_view_selftests ();
void
_initialize_array_view_selftests ()
{
- selftests::register_test (selftests::array_view_tests::run_tests);
+ selftests::register_test ("array_view",
+ selftests::array_view_tests::run_tests);
}