Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 04/10] [gdb] Add unit test for next_iterator
Date: Fri,  1 May 2026 14:44:58 +0200	[thread overview]
Message-ID: <20260501124504.2233495-5-tdevries@suse.de> (raw)
In-Reply-To: <20260501124504.2233495-1-tdevries@suse.de>

I noticed that next_iterator doesn't have a selftest.  Add this.
---
 gdb/Makefile.in                         |   1 +
 gdb/unittests/next-iterator-selftests.c | 105 ++++++++++++++++++++++++
 2 files changed, 106 insertions(+)
 create mode 100644 gdb/unittests/next-iterator-selftests.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index e38ba95eebd..8b1a40dad7e 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -476,6 +476,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..8934e5bbaa0
--- /dev/null
+++ b/gdb/unittests/next-iterator-selftests.c
@@ -0,0 +1,105 @@
+/* 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)
+{
+  list_iterator begin (l);
+  list_iterator end;
+
+  unsigned cnt = 0;
+  list_iterator i;
+  for (i = begin; i != end; ++i)
+    cnt++;
+  SELF_CHECK (cnt == 3);
+}
+
+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);
+}
+
+} /* namespace next_iterator */
+} /* namespace selftests */
+
+INIT_GDB_FILE (next_iterator_selftests)
+{
+  selftests::register_test ("next-iterator",
+			    selftests::next_iterator::test_next_iterator);
+}
-- 
2.51.0


  parent reply	other threads:[~2026-05-01 12:46 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-01 12:44 [PATCH 00/10] [gdb] Add superblocks range loops Tom de Vries
2026-05-01 12:44 ` [PATCH 01/10] [gdb] Use block::containing_function Tom de Vries
2026-05-01 12:44 ` [PATCH 02/10] [gdb] Factor out block::containing_function_block Tom de Vries
2026-05-01 12:44 ` [PATCH 03/10] [gdb] Use block::containing_function_block Tom de Vries
2026-05-01 12:44 ` Tom de Vries [this message]
2026-05-01 12:44 ` [PATCH 05/10] [gdbsupport] Use using instead of typedef in next_iterator Tom de Vries
2026-05-01 12:45 ` [PATCH 06/10] [gdbsupport] Factor out base_next_iterator Tom de Vries
2026-05-08 19:17   ` Tom Tromey
2026-05-01 12:45 ` [PATCH 07/10] [gdb] Add block::block_and_superblocks Tom de Vries
2026-05-01 12:45 ` [PATCH 08/10] [gdb] Use block::block_and_superblocks Tom de Vries
2026-05-08 19:41   ` Tom Tromey
2026-05-01 12:45 ` [PATCH 09/10] [gdb] Add block::block_and_superblocks_in_fn Tom de Vries
2026-05-01 12:45 ` [PATCH 10/10] [gdb] Use block::block_and_superblocks_in_fn Tom de Vries
2026-06-09 12:40 ` [PATCH 00/10] [gdb] Add superblocks range loops Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260501124504.2233495-5-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox