Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Jan Vrany <jan.vrany@labware.com>
To: gdb-patches@sourceware.org
Cc: Jan Vrany <jan.vrany@labware.com>,
	Simon Marchi <simon.marchi@efficios.com>
Subject: [pushed] gdb: add unittests for blockvector lookup functions
Date: Fri, 28 Nov 2025 13:49:49 +0000	[thread overview]
Message-ID: <20251128134950.1763596-3-jan.vrany@labware.com> (raw)
In-Reply-To: <20251128134950.1763596-1-jan.vrany@labware.com>

This commit adds new unittest - blockvector-lookup - that tests
blockvector::lookup () and blockvector::contains ().

Approved-By: Simon Marchi <simon.marchi@efficios.com>
---
 gdb/Makefile.in       |   1 +
 gdb/block-selftests.c | 128 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+)
 create mode 100644 gdb/block-selftests.c

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 538d10a824d..ed29f1703cd 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -451,6 +451,7 @@ SUBDIR_PYTHON_LDFLAGS =
 SUBDIR_PYTHON_CFLAGS =
 
 SELFTESTS_SRCS = \
+	block-selftests.c \
 	disasm-selftests.c \
 	gdbarch-selftests.c \
 	selftest-arch.c \
diff --git a/gdb/block-selftests.c b/gdb/block-selftests.c
new file mode 100644
index 00000000000..f5883f18660
--- /dev/null
+++ b/gdb/block-selftests.c
@@ -0,0 +1,128 @@
+/* Self tests for blockvectors
+
+   Copyright (C) 2025 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 "block.h"
+#include "addrmap.h"
+
+
+namespace selftests {
+
+/* Create and add new block to given blockvector.  */
+static struct block *
+make_block (struct blockvector &bv, struct obstack &ob, CORE_ADDR start,
+	    CORE_ADDR end, struct block *superblock = nullptr)
+{
+  auto b = new (&ob) struct block;
+  b->set_start (start);
+  b->set_end (end);
+  b->set_superblock (superblock);
+  b->set_multidict (mdict_create_linear_expandable (language_unknown));
+
+  bv.append_block (b);
+
+  return b;
+}
+
+/* A helper to create and set address map given a blockvector.  */
+static void
+make_map (struct blockvector &bv, struct obstack &ob)
+{
+  struct addrmap_mutable map;
+
+  for (int i = bv.num_blocks () - 1; i > STATIC_BLOCK; i--)
+    {
+      auto b = bv.block (i);
+      map.set_empty (b->start (), b->end () - 1, b);
+    }
+
+  bv.set_map (new (&ob) addrmap_fixed (&ob, &map));
+}
+
+/* Create and return blockvector with following blocks:
+
+	B0    0x1000 - 0x4000   (global block)
+	B1    0x1000 - 0x4000   (static block)
+	 B2   0x1000 - 0x2000
+				(hole)
+	 B3   0x3000 - 0x4000
+
+  If USE_MAP is true, then also set blockvector's address map.
+*/
+static blockvector_up
+make_blockvector (struct obstack &ob, bool use_map)
+{
+  auto bv = std::make_unique<struct blockvector> (0);
+
+  auto global_block = make_block (*bv.get (), ob, 0x1000, 0x4000);
+  auto static_block = make_block (*bv.get (), ob, 0x1000, 0x4000,
+				  global_block);
+  make_block (*bv.get (), ob, 0x1000, 0x2000, static_block);
+  make_block (*bv.get (), ob, 0x3000, 0x4000, static_block);
+
+  if (use_map)
+    make_map (*bv.get (), ob);
+
+  return bv;
+}
+
+static void
+test_blockvector_lookup_contains ()
+{
+  for (bool with_map : { false, true })
+    {
+      /* Test blockvector without an address map.  */
+      auto_obstack ob;
+      blockvector_up bv = make_blockvector (ob, with_map);
+
+      /* Test address outside global block's range.  */
+      SELF_CHECK (bv->lookup (0x0500) == nullptr);
+      SELF_CHECK (bv->contains (0x0500) == false);
+
+      /* Test address falling into a block.  */
+      SELF_CHECK (bv->lookup (0x1500) == bv->block (2));
+      SELF_CHECK (bv->contains (0x1500) == true);
+
+      /* Test address falling into a "hole".  If BV has an address map,
+	 lookup () returns nullptr. If not, lookup () return static block.
+	 contains() returns false in both cases.  */
+      if (with_map)
+	SELF_CHECK (bv->lookup (0x2500) == nullptr);
+      else
+	SELF_CHECK (bv->lookup (0x2500) == bv->block (STATIC_BLOCK));
+      SELF_CHECK (bv->contains (0x2500) == false);
+
+      /* Test address falling into a block above the "hole".  */
+      SELF_CHECK (bv->lookup (0x3500) == bv->block (3));
+      SELF_CHECK (bv->contains (0x3500) == true);
+
+      /* Test address outside global block's range.  */
+      SELF_CHECK (bv->lookup (0x4000) == nullptr);
+      SELF_CHECK (bv->contains (0x4000) == false);
+    }
+}
+
+} /* namespace selftests */
+
+
+INIT_GDB_FILE (block_selftest)
+{
+  selftests::register_test ("blockvector-lookup-contains",
+			    selftests::test_blockvector_lookup_contains);
+}
-- 
2.51.0


  parent reply	other threads:[~2025-11-28 13:58 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-28 13:49 [pushed] gdb: update blockvector::lookup Jan Vrany
2025-11-28 13:49 ` [pushed] gdb: change blockvector::contains() to handle blockvectors with "holes" Jan Vrany
2025-12-02 17:41   ` Tom Tromey
2025-12-02 21:58     ` Jan Vraný
2025-12-03 19:36       ` Tom Tromey
2025-12-03 21:31         ` Jan Vraný
2025-12-04 11:42           ` Tom de Vries
2025-12-04 15:03             ` Jan Vraný
2025-11-28 13:49 ` Jan Vrany [this message]
2025-11-28 13:49 ` [pushed] gdb: update is_addr_in_objfile to support "dynamic" objfiles Jan Vrany

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=20251128134950.1763596-3-jan.vrany@labware.com \
    --to=jan.vrany@labware.com \
    --cc=gdb-patches@sourceware.org \
    --cc=simon.marchi@efficios.com \
    /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