From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id 1rmiBzpr52mCQDAAWB0awg (envelope-from ) for ; Tue, 21 Apr 2026 08:19:06 -0400 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tIcW5vOP; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=4DomN2ya; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tIcW5vOP; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=4DomN2ya; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 0D4DD1E067; Tue, 21 Apr 2026 08:19:06 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_MSPIKE_H2,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 5C17C1E067 for ; Tue, 21 Apr 2026 08:19:04 -0400 (EDT) Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id CC1F34B9DB78 for ; Tue, 21 Apr 2026 12:19:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CC1F34B9DB78 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tIcW5vOP; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=4DomN2ya; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=tIcW5vOP; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=4DomN2ya Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2a07:de40:b251:101:10:150:64:1]) by sourceware.org (Postfix) with ESMTPS id E501A4BA9006 for ; Tue, 21 Apr 2026 12:18:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E501A4BA9006 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E501A4BA9006 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a07:de40:b251:101:10:150:64:1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776773914; cv=none; b=HWO6l/x1rU4aQJJLdbB5GSSXzcFS/AYqhxid2BjQTtNwtAOXpB2Jvd2PkRLJn32bwS9OVIoI9inuK+CfLnBchSsXvdrtxMb1hmGal4zACjlOaF9n2l62YMoqVHWvKNJYa2SC4Be6kDP4SJoFozC4L4jGrZimD+7+AKoSFIQ1hTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1776773914; c=relaxed/simple; bh=FwzMuqDQAZqbcL2GDchfe20i0gu7j+YcCzRP4wTa5AQ=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=kk5ER8sJSPli4uV5f4Gn3Jm4vjYNJbO7Jr9E9VRTrg9uqUbvW5R59iuFTM/MhljiosZ3rYfQzLe3ni7JDXyEwKRQjdpk/xSrbyzF7k0oRyAMkN+M0ON53aPvilVetkeUAHkmWIeK8dqRdcrVLhkeNg6sTEvx/qKrXfmxXZBleV4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E501A4BA9006 Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 1E8C66A896 for ; Tue, 21 Apr 2026 12:18:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1776773912; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Fu4FycThs4rA5RJPbzjdBGEothl2NSStc1uBtaEBxPk=; b=tIcW5vOP+9FJ0rLMOcM4Ta7vObazMn05qaRONqb74px+LFIUTOT0i29K6PuDWp77BfzEBA p/kpNM8abuuXBbG12wue7l0P6rxhyXt0ZDlvlGTVh0/tkXaQZ96LTeJoAKROW2u0BKMKGQ 2JN1zKLIIA0+tH2Ju7Cq0smIgMmpV3o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1776773912; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Fu4FycThs4rA5RJPbzjdBGEothl2NSStc1uBtaEBxPk=; b=4DomN2yav+w+ivjXXKrqj51lm0WoiA0sh0b1cNgTS/hcHdYgbL2A8vFSuCW+IjlDwsm4ny N4AkxhwC8/SnW7CA== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1776773912; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Fu4FycThs4rA5RJPbzjdBGEothl2NSStc1uBtaEBxPk=; b=tIcW5vOP+9FJ0rLMOcM4Ta7vObazMn05qaRONqb74px+LFIUTOT0i29K6PuDWp77BfzEBA p/kpNM8abuuXBbG12wue7l0P6rxhyXt0ZDlvlGTVh0/tkXaQZ96LTeJoAKROW2u0BKMKGQ 2JN1zKLIIA0+tH2Ju7Cq0smIgMmpV3o= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1776773912; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Fu4FycThs4rA5RJPbzjdBGEothl2NSStc1uBtaEBxPk=; b=4DomN2yav+w+ivjXXKrqj51lm0WoiA0sh0b1cNgTS/hcHdYgbL2A8vFSuCW+IjlDwsm4ny N4AkxhwC8/SnW7CA== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 0988B593AF for ; Tue, 21 Apr 2026 12:18:32 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id o+QhARhr52l9SAAAD6G6ig (envelope-from ) for ; Tue, 21 Apr 2026 12:18:32 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH] [gdb] Add unit test for iterator_range Date: Tue, 21 Apr 2026 14:18:31 +0200 Message-ID: <20260421121831.241318-1-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[100.00%]; MID_CONTAINS_FROM(1.00)[]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-0.999]; MIME_GOOD(-0.10)[text/plain]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:mid]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[] X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org I noticed iterator_range doesn't have a selftest. I found int_array_iterator in filtered_iterator-selftests.c, and decided to use that as iterator. Factor out int-array-iterator.h out of filtered_iterator-selftests.c, and use it in new unit test iterator-range-selftests.c. --- gdb/Makefile.in | 1 + gdb/unittests/filtered_iterator-selftests.c | 75 +-------------- gdb/unittests/int-array-iterator.h | 101 ++++++++++++++++++++ gdb/unittests/iterator-range-selftests.c | 100 +++++++++++++++++++ 4 files changed, 203 insertions(+), 74 deletions(-) create mode 100644 gdb/unittests/int-array-iterator.h create mode 100644 gdb/unittests/iterator-range-selftests.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 1acf99e3346..e38ba95eebd 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -472,6 +472,7 @@ SELFTESTS_SRCS = \ unittests/gdb_tilde_expand-selftests.c \ unittests/gmp-utils-selftests.c \ unittests/intrusive_list-selftests.c \ + unittests/iterator-range-selftests.c \ unittests/lookup_name_info-selftests.c \ unittests/memory-map-selftests.c \ unittests/memrange-selftests.c \ diff --git a/gdb/unittests/filtered_iterator-selftests.c b/gdb/unittests/filtered_iterator-selftests.c index 17408b2798b..f40c2a1c4ab 100644 --- a/gdb/unittests/filtered_iterator-selftests.c +++ b/gdb/unittests/filtered_iterator-selftests.c @@ -19,83 +19,10 @@ #include "gdbsupport/selftest.h" #include "gdbsupport/filtered-iterator.h" - -#include +#include "int-array-iterator.h" namespace selftests { -/* An iterator class that iterates on integer arrays. */ - -struct int_array_iterator -{ - using value_type = int; - using reference = int &; - using pointer = int *; - using iterator_category = std::forward_iterator_tag; - using difference_type = int; - - /* Create an iterator that points at the first element of an integer - array at ARRAY of size SIZE. */ - int_array_iterator (int *array, size_t size) - : m_array (array), m_size (size) - {} - - /* Create a past-the-end iterator. */ - int_array_iterator () - : m_array (nullptr), m_size (0) - {} - - bool operator== (const int_array_iterator &other) const - { - /* If both are past-the-end, they are equal. */ - if (m_array == nullptr && other.m_array == nullptr) - return true; - - /* If just one of them is past-the-end, they are not equal. */ - if (m_array == nullptr || other.m_array == nullptr) - return false; - - /* If they are both not past-the-end, make sure they iterate on the - same array (we shouldn't compare iterators that iterate on different - things). */ - SELF_CHECK (m_array == other.m_array); - - /* They are equal if they have the same current index. */ - return m_cur_idx == other.m_cur_idx; - } - - bool operator!= (const int_array_iterator &other) const - { - return !(*this == other); - } - - void operator++ () - { - /* Make sure nothing tries to increment a past the end iterator. */ - SELF_CHECK (m_cur_idx < m_size); - - m_cur_idx++; - - /* Mark the iterator as "past-the-end" if we have reached the end. */ - if (m_cur_idx == m_size) - m_array = nullptr; - } - - int operator* () const - { - /* Make sure nothing tries to dereference a past the end iterator. */ - SELF_CHECK (m_cur_idx < m_size); - - return m_array[m_cur_idx]; - } - -private: - /* A nullptr value in M_ARRAY indicates a past-the-end iterator. */ - int *m_array; - size_t m_size; - size_t m_cur_idx = 0; -}; - /* Filter to only keep the even numbers. */ struct even_numbers_only diff --git a/gdb/unittests/int-array-iterator.h b/gdb/unittests/int-array-iterator.h new file mode 100644 index 00000000000..03b0e038501 --- /dev/null +++ b/gdb/unittests/int-array-iterator.h @@ -0,0 +1,101 @@ +/* An iterator class that iterates on integer arrays. + + Copyright (C) 2019-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 . */ + +#ifndef GDB_UNITTESTS_INT_ARRAY_ITERATOR_H +#define GDB_UNITTESTS_INT_ARRAY_ITERATOR_H + +#include "gdbsupport/selftest.h" + +#include + +namespace selftests { + +struct int_array_iterator +{ + using value_type = int; + using reference = int &; + using pointer = int *; + using iterator_category = std::forward_iterator_tag; + using difference_type = int; + + /* Create an iterator that points at the first element of an integer + array at ARRAY of size SIZE. */ + int_array_iterator (int *array, size_t size) + : m_array (array), m_size (size) + {} + + /* Create a past-the-end iterator. */ + int_array_iterator () + : m_array (nullptr), m_size (0) + {} + + bool operator== (const int_array_iterator &other) const + { + /* If both are past-the-end, they are equal. */ + if (m_array == nullptr && other.m_array == nullptr) + return true; + + /* If just one of them is past-the-end, they are not equal. */ + if (m_array == nullptr || other.m_array == nullptr) + return false; + + /* If they are both not past-the-end, make sure they iterate on the + same array (we shouldn't compare iterators that iterate on different + things). */ + SELF_CHECK (m_array == other.m_array); + + /* They are equal if they have the same current index. */ + return m_cur_idx == other.m_cur_idx; + } + + bool operator!= (const int_array_iterator &other) const + { + return !(*this == other); + } + + void operator++ () + { + /* Make sure nothing tries to increment a past the end iterator. */ + SELF_CHECK (m_cur_idx < m_size); + + m_cur_idx++; + + /* Mark the iterator as "past-the-end" if we have reached the end. */ + if (m_cur_idx == m_size) + m_array = nullptr; + } + + int operator* () const + { + /* Make sure nothing tries to dereference a past the end iterator. */ + SELF_CHECK (m_cur_idx < m_size); + + return m_array[m_cur_idx]; + } + +private: + /* A nullptr value in M_ARRAY indicates a past-the-end iterator. */ + int *m_array; + size_t m_size; + size_t m_cur_idx = 0; +}; + +} /* namespace selftests */ + +#endif /* GDB_UNITTESTS_INT_ARRAY_ITERATOR_H */ diff --git a/gdb/unittests/iterator-range-selftests.c b/gdb/unittests/iterator-range-selftests.c new file mode 100644 index 00000000000..a05531d95e0 --- /dev/null +++ b/gdb/unittests/iterator-range-selftests.c @@ -0,0 +1,100 @@ +/* Self tests for the iterator_range 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/iterator-range.h" +#include "int-array-iterator.h" + +namespace selftests { + +using int_array_iterator_range = iterator_range; + +static void +test_iterator_range_1 (int_array_iterator_range &r, int array[], int size, + int_array_iterator &begin, int_array_iterator &end) +{ + SELF_CHECK (r.begin () == begin); + SELF_CHECK (r.end () == end); + SELF_CHECK (r.size () == size); + SELF_CHECK (r.empty () == (size == 0)); + + int j = 0; + for (auto i : r) + { + SELF_CHECK (j < size); + SELF_CHECK (i == array[j]); + j++; + } + SELF_CHECK (j == size); +} + +static void +test_iterator_range () +{ + int array[] = { 4, 4, 5, 6, 7, 8, 9 }; + int array_size = ARRAY_SIZE (array); + + int_array_iterator begin (array, array_size); + int_array_iterator end; + + { + /* Constructor using begin and end. */ + auto r = int_array_iterator_range (begin, end); + test_iterator_range_1 (r, array, array_size, begin, end); + } + + { + /* Constructor using begin, assuming end can be default-constructed. */ + auto r2 = int_array_iterator_range (begin); + test_iterator_range_1 (r2, array, array_size, begin, end); + } + + { + /* Empty range. */ + auto r3 = int_array_iterator_range (end, end); + test_iterator_range_1 (r3, nullptr, 0, end, end); + } + + { + auto r4 = int_array_iterator_range (begin, end); + + /* Copy constructor. */ + auto r5 (r4); + test_iterator_range_1 (r5, array, array_size, begin, end); + + /* Move constructor. */ + auto r6 (std::move (r4)); + test_iterator_range_1 (r6, array, array_size, begin, end); + } + + { + const auto r7 = int_array_iterator_range (begin, end); + + /* Const copy constructor. */ + auto r8 (r7); + test_iterator_range_1 (r8, array, array_size, begin, end); + } +} + +} /* namespace selftests */ + +INIT_GDB_FILE (iterator_range_selftests) +{ + selftests::register_test ("iterator_range", selftests::test_iterator_range); +} base-commit: c365a263f5dcf95982464cf6d53db00cc6f04c1c -- 2.51.0