]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb] Add unit test for next_iterator
authorTom de Vries <tdevries@suse.de>
Tue, 9 Jun 2026 12:37:16 +0000 (14:37 +0200)
committerTom de Vries <tdevries@suse.de>
Tue, 9 Jun 2026 12:37:16 +0000 (14:37 +0200)
I noticed that next_iterator doesn't have a selftest.  Add this.

gdb/Makefile.in
gdb/unittests/next-iterator-selftests.c [new file with mode: 0644]

index b2b7ef3d6065a4c37b8496e247a7d45ef722ae59..43e05326fc3dc475802435b901a0e62b83ac759e 100644 (file)
@@ -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 (file)
index 0000000..a4abfb4
--- /dev/null
@@ -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);
+}