diff options
author | Tom de Vries <tdevries@suse.de> | 2024-10-19 08:10:38 +0200 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2024-10-19 08:10:38 +0200 |
commit | e686338105177046975bcff8fe6199536e80caf9 (patch) | |
tree | d45b762e839f333832bcc6916b3139ba63af3cfd /gdb/unittests/array-view-selftests.c | |
parent | 6c8052f6b22fbb6ae1edc759454626d465ee317c (diff) | |
download | gdb-e686338105177046975bcff8fe6199536e80caf9.zip gdb-e686338105177046975bcff8fe6199536e80caf9.tar.gz gdb-e686338105177046975bcff8fe6199536e80caf9.tar.bz2 |
[gdbsupport] Add gdb::array_view::{iterator,const_iterator}
While trying to substitute some std::vector type A in the code with a
gdb::array_view:
...
- using A = std::vector<T>
+ using A = gdb::array_view<T>
....
I ran into the problem that the code was using A::iterator while
gdb::array_view doesn't define such a type.
Fix this by:
- adding types gdb::array_view::iterator and gdb::array_view::const_iterator,
- using them in gdb::array_view::(c)begin and gdb::array_view::(c)end, as is
usual, and
- using them explicitly in a unit test.
Tested on aarch64-linux.
Approved-By: Tom Tromey <tom@tromey.com>
Diffstat (limited to 'gdb/unittests/array-view-selftests.c')
-rw-r--r-- | gdb/unittests/array-view-selftests.c | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/gdb/unittests/array-view-selftests.c b/gdb/unittests/array-view-selftests.c index eb3ae30..c07b572 100644 --- a/gdb/unittests/array-view-selftests.c +++ b/gdb/unittests/array-view-selftests.c @@ -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. */ |