[binutils-gdb] [gdb] Add unit test for next_iterator

Tom de Vries via Gdb-cvs <[email protected]> Tue, 9 Jun 2026 12:37:21 +0000 (GMT)
Newsgroups gmane.comp.gdb.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=8aa13e4e29c4891adbef0dee4e5da26349cbc2c9

commit 8aa13e4e29c4891adbef0dee4e5da26349cbc2c9
Author: Tom de Vries <[email protected]>
Date:   Tue Jun 9 14:37:16 2026 +0200

    [gdb] Add unit test for next_iterator
    
    I noticed that next_iterator doesn't have a selftest.  Add this.

Diff:
---
 gdb/Makefile.in                         |   1 +
 gdb/unittests/next-iterator-selftests.c | 106 ++++++++++++++++++++++++++++++++
 2 files changed, 107 insertions(+)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index b2b7ef3d606..43e05326fc3 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -472,6 +472,7 @@ SELFTESTS_SRCS = \
 	unittests/lookup_name_info-selftests.c \
 	unittests/memory-map-selftests.c \
 	unittests/memrange-selftests.c \
+	unittests/next-iterator-selftests.c \
 	unittests/offset-type-selftests.c \
 	unittests/observable-selftests.c \
 	unittests/packed-selftests.c \
diff --git a/gdb/unittests/next-iterator-selftests.c b/gdb/unittests/next-iterator-selftests.c
new file mode 100644
index 00000000000..a4abfb4fde4
--- /dev/null
+++ b/gdb/unittests/next-iterator-selftests.c
@@ -0,0 +1,106 @@
+/* Self tests for the next_iterator class.
+
+   Copyright (C) 2026 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include "gdbsupport/selftest.h"
+#include "gdbsupport/next-iterator.h"
+#include <vector>
+
+namespace selftests {
+namespace next_iterator {
+
+struct list;
+using list_iterator = ::next_iterator<struct list>;
+using list_range = next_range<struct list>;
+
+/* The next_iterator allows usage with an incomplete type, provided the type
+   is defined later in the same file.  While not essential to its functioning,
+   we're currently using this property in the sources, so check/demonstrate
+   it.  */
+
+static void
+test_next_iterator_incomplete_type (struct list *l,
+				    std::vector<int> &expected)
+{
+  list_iterator begin (l);
+  list_iterator end;
+
+  unsigned cnt = 0;
+  list_iterator i;
+  for (i = begin; i != end; ++i)
+    cnt++;
+  SELF_CHECK (cnt == expected.size ());
+}
+
+struct list
+{
+  int i;
+  struct list *next;
+};
+
+static void
+test_next_iterator ()
+{
+  struct list a = { 1, nullptr };
+  struct list b = { 2, &a };
+  struct list c = { 3, &b };
+
+  /* Constructor with parameter.  */
+  list_iterator begin (&c);
+
+  /* Constructor without parameter.  */
+  list_iterator end;
+
+  /* Operator*.  */
+  SELF_CHECK (*begin == &c);
+  SELF_CHECK (*end == nullptr);
+
+  /* Operator==, operator!=.  */
+  SELF_CHECK (begin == begin);
+  SELF_CHECK (end == end);
+  SELF_CHECK (begin != end);
+
+  /* Operator++.  */
+  list_iterator i (&c);
+  ++i;
+  SELF_CHECK (*i == &b);
+
+  /* Loop using iterators.  */
+  std::vector<int> expected = {3, 2, 1};
+  std::vector<int> v;
+  for (i = begin; i != end; ++i)
+    v.push_back ((*i)->i);
+  SELF_CHECK (v == expected);
+
+  /* Loop using range.  */
+  v.clear ();
+  for (auto l : list_range (begin))
+    v.push_back (l->i);
+  SELF_CHECK (v == expected);
+
+  test_next_iterator_incomplete_type (&c, expected);
+}
+
+} /* namespace next_iterator */
+} /* namespace selftests */
+
+INIT_GDB_FILE (next_iterator_selftests)
+{
+  selftests::register_test ("next-iterator",
+			    selftests::next_iterator::test_next_iterator);
+}