From ec23174dc4ac3ffbb26a782eb5a008cd68315fbc Mon Sep 17 00:00:00 2001 From: Jan Vrany Date: Thu, 19 Feb 2026 13:04:58 +0000 Subject: [PATCH] gdb: add unit test for blockvector::lookup of non-contiguous blocks This commit add a new unit test that tests block lookup when blocks are non-contiguous. --- gdb/block-selftests.c | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/gdb/block-selftests.c b/gdb/block-selftests.c index 77b8d67ded8..ca4289fbb6f 100644 --- a/gdb/block-selftests.c +++ b/gdb/block-selftests.c @@ -20,6 +20,7 @@ #include "gdbsupport/selftest.h" #include "block.h" #include "addrmap.h" +#include "obstack.h" namespace selftests { @@ -81,6 +82,76 @@ test_blockvector_lookup_contains () SELF_CHECK (bv->contains (0x4000) == false); } +/* Create and return struct blockranges* from 2 ranges. */ +static struct blockranges * +make_blockranges_2 (struct obstack &ob, CORE_ADDR start0, + CORE_ADDR end0, CORE_ADDR start1, + CORE_ADDR end1) +{ + struct blockranges *ranges = (struct blockranges *) obstack_alloc + (&ob, sizeof (struct blockranges) + (2 - 1) * sizeof (struct blockrange)); + + ranges->nranges = 2; + ranges->range[0].set_start (start0); + ranges->range[0].set_end (end0); + ranges->range[1].set_start (start1); + ranges->range[1].set_end (end1); + + return ranges; +} + +static void +test_blockvector_lookup_non_continuguous () +{ + /* Create blockvector with following blocks: + + B0 0x1000 - 0x8000 (global block) + B1 0x1000 - 0x8000 (static block) + B2 0x1000 - 0x2000 (B2's range 1) + B3 0x2000 - 0x3000 (B3's range 1) + (hole 1) + B4 0x5000 - 0x5500 + (hole 2) + (B2) 0x6000 - 0x7000 (B2's range 2) + (B3) 0x7000 - 0x8000 (B3's range 2) + + Blocks B2 and B3 are non-continguous (consist of two + disjoint ranges) and interleaved. + */ + auto_obstack ob; + blockvector_up bv = std::make_unique (0); + + auto global_block = make_block (*bv.get (), ob, 0x1000, 0x8000); + auto static_block = make_block (*bv.get (), ob, 0x1000, 0x8000, + global_block); + auto b2 = make_block (*bv.get (), ob, 0x1000, 0x7000, static_block); + b2->set_ranges (make_blockranges_2 (ob, 0x1000, 0x2000, 0x6000, 0x7000)); + auto b3 = make_block (*bv.get (), ob, 0x2000, 0x8000, static_block); + b3->set_ranges (make_blockranges_2 (ob, 0x2000, 0x3000, 0x7000, 0x8000)); + auto b4 = make_block (*bv.get (), ob, 0x5000, 0x5500, static_block); + + /* Test address falling into range 1 of B2. */ + SELF_CHECK (bv->lookup (0x1500) == b2); + + /* Test address falling into range 2 of B2. */ + SELF_CHECK (bv->lookup (0x6000) == b2); + + /* Test address falling into range 1 of B3. */ + SELF_CHECK (bv->lookup (0x2500) == b3); + + /* Test address falling into range 2 of B3. */ + SELF_CHECK (bv->lookup (0x7999) == b3); + + /* Test address falling into B4. */ + SELF_CHECK (bv->lookup (0x5250) == b4); + + /* Test address falling into hole 1. */ + SELF_CHECK (bv->lookup (0x4000) == static_block); + + /* Test address falling into hole 2. */ + SELF_CHECK (bv->lookup (0x5750) == static_block); +} + } /* namespace selftests */ @@ -88,4 +159,7 @@ INIT_GDB_FILE (block_selftest) { selftests::register_test ("blockvector-lookup-contains", selftests::test_blockvector_lookup_contains); + selftests::register_test + ("blockvector-lookup-non-contiguous", + selftests::test_blockvector_lookup_non_continuguous); }