aboutsummaryrefslogtreecommitdiff
path: root/gdb/unittests/array-view-selftests.c
diff options
context:
space:
mode:
Diffstat (limited to 'gdb/unittests/array-view-selftests.c')
-rw-r--r--gdb/unittests/array-view-selftests.c55
1 files changed, 51 insertions, 4 deletions
diff --git a/gdb/unittests/array-view-selftests.c b/gdb/unittests/array-view-selftests.c
index eb3ae30..914800a 100644
--- a/gdb/unittests/array-view-selftests.c
+++ b/gdb/unittests/array-view-selftests.c
@@ -1,6 +1,6 @@
/* Self tests for array_view for GDB, the GNU debugger.
- Copyright (C) 2017-2024 Free Software Foundation, Inc.
+ Copyright (C) 2017-2025 Free Software Foundation, Inc.
This file is part of GDB.
@@ -364,6 +364,39 @@ check_range_for ()
SELF_CHECK (sum == 1 + 2 + 3 + 4);
}
+template<typename T>
+static void
+check_iterator ()
+{
+ T data[] = {1, 2, 3, 4};
+ gdb::array_view<T> view (data);
+
+ typename std::decay<T>::type sum = 0;
+ for (typename gdb::array_view<T>::iterator it = view.begin ();
+ it != view.end (); it++)
+ {
+ *it *= 2;
+ sum += *it;
+ }
+
+ SELF_CHECK (sum == 2 + 4 + 6 + 8);
+}
+
+template<typename T>
+static void
+check_const_iterator ()
+{
+ T data[] = {1, 2, 3, 4};
+ gdb::array_view<T> view (data);
+
+ typename std::decay<T>::type sum = 0;
+ for (typename gdb::array_view<T>::const_iterator it = view.cbegin ();
+ it != view.cend (); it++)
+ sum += *it;
+
+ SELF_CHECK (sum == 1 + 2 + 3 + 4);
+}
+
/* Entry point. */
static void
@@ -490,6 +523,9 @@ run_tests ()
check_range_for<gdb_byte> ();
check_range_for<const gdb_byte> ();
+ check_iterator<gdb_byte> ();
+ check_const_iterator<gdb_byte> ();
+ check_const_iterator<const gdb_byte> ();
/* Check that the right ctor overloads are taken when the element is
a container. */
@@ -518,6 +554,19 @@ run_tests ()
SELF_CHECK (view[i] == data[i]);
}
+ /* gdb::make_array_view with an array. */
+ {
+ const gdb_byte data[] = {0x55, 0x66, 0x77, 0x88};
+ const auto len = sizeof (data) / sizeof (data[0]);
+ const auto view = gdb::make_array_view (data);
+
+ SELF_CHECK (view.data () == data);
+ SELF_CHECK (view.size () == len);
+
+ for (std::size_t i = 0; i < len; ++i)
+ SELF_CHECK (view[i] == data[i]);
+ }
+
/* Test slicing. */
{
gdb_byte data[] = {0x55, 0x66, 0x77, 0x88, 0x99};
@@ -646,9 +695,7 @@ run_copy_tests ()
} /* namespace array_view_tests */
} /* namespace selftests */
-void _initialize_array_view_selftests ();
-void
-_initialize_array_view_selftests ()
+INIT_GDB_FILE (array_view_selftests)
{
selftests::register_test ("array_view",
selftests::array_view_tests::run_tests);