From: Tom de Vries Date: Tue, 9 Jun 2026 12:37:16 +0000 (+0200) Subject: [gdb] Add unit test for next_iterator X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8aa13e4e29c4891adbef0dee4e5da26349cbc2c9;p=thirdparty%2Fbinutils-gdb.git [gdb] Add unit test for next_iterator I noticed that next_iterator doesn't have a selftest. Add this. --- 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 . */ + +#include "gdbsupport/selftest.h" +#include "gdbsupport/next-iterator.h" +#include + +namespace selftests { +namespace next_iterator { + +struct list; +using list_iterator = ::next_iterator; +using list_range = next_range; + +/* 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 &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 expected = {3, 2, 1}; + std::vector 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); +}