From patchwork Thu Feb 8 16:28:42 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 85469 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 93B1B385841D for ; Thu, 8 Feb 2024 16:29:48 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 991963858283 for ; Thu, 8 Feb 2024 16:28:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 991963858283 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 991963858283 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707409737; cv=none; b=SXQ58NWpMxeQ3w4Co9SINqLU1i3zrhQ/A6v/v2ta2z831m010i3haEnalN2yrf/AC054kcGa/QjXMWNEWJUUG5E+NC5KycP5wp0DiW6luaUn6BvcqGYB/MauZpR5xr60J7dGIs6YsZT+U7px4J1c0u9mNyfMP65rZbVsP0hP4KI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707409737; c=relaxed/simple; bh=leJVwhwHf0QkrpCEkh4x+rBSD8zGONw9imS4+conGCs=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=kzbvoYZkIbu1bN//rfqEZk4bUl0/xU2cvBqVyZN4LkYzlnxdKRvCYjdMrHN4IGWkiRKnw4JDZRCkRsFWSu2GME+FhGOu9T2Id7nNsfKhqoyjZkMiqhu+rWMwxcGDGBBFZJrLV+IT/nkDwNxrspzy5ayYHfqfkK9EVSD98U+tt8o= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707409735; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3dxKYTmzgWDhHhRE6Hm8TaOEsXB7xQLHg3z41fZrAgY=; b=Xy2ceGUUoB4UxqjxLKeodIHv/Lv6YJOspzXKJ/5AE0teC0kCP75XWtcVL4Nuadq5jOX16N 7nGSdd935+SxVGUm11VJf+St85XSenFju0MS9Tm/nYjMEyTW6Uy3fBl3z0sfivnFCBja0B P683DhwmswlbEzyXwCe2JO9tEoYbegA= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-161-xqSB_w5INkybPKi1WaR2bQ-1; Thu, 08 Feb 2024 11:28:52 -0500 X-MC-Unique: xqSB_w5INkybPKi1WaR2bQ-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-33b1ad7dfe1so1059452f8f.2 for ; Thu, 08 Feb 2024 08:28:52 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707409731; x=1708014531; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3dxKYTmzgWDhHhRE6Hm8TaOEsXB7xQLHg3z41fZrAgY=; b=CDnRxfMfi3KdFuq+mBKMNRJKvbmddhHLArur+HZPPxZCJ8RX0hLFqWUcY3RxScOvSR yEuU+Okp4woYeH2YWmRO6ip8QiWC8K0VNnciWOZ3ZCsJcnIK/DpljrWaUDsynvPcp77T YKQhjl7WaZHcigCmekIgtFPP3vDJnDqIZKRhestjpvSPqsZjkYn4MjPT8nYsQHWNGMBr 3jNRNBkZ+Ho3fyj9k2uYfR9OqDECk1u47R3LzLfuVAzMH26KSaMv27+acamCX3ZSL0hl AU4uHPvyhGgZYq65ABH1f3tXM/oEd1g4fZTciIv5TfFTyVJY6S2FXll+CRkCU2rxmda/ JIIQ== X-Gm-Message-State: AOJu0Yzsr4AMbrOW3tkU+ZOqfz3XJ9MyFhAZKt6g6RPzAQ4MgDmsyT+I 9GWhmTtvzchGDkxP7w0JSoi1gyIheEg/Yv+sUTJ+QnF0XuiCKOqIlUAhf+zpn7wTa5pHx3L49j3 zzUosYL/wJi885lhX7+KeQuey8Lkh48P1B+GkqYaM72X0yHbhe5eWwadensumUu8NzmATB+DlkL HB04LqAwZqMUCQVrZLI7JMRVPwFgfVHPpgrEMrNDVySHY= X-Received: by 2002:adf:e5cb:0:b0:33b:2464:9d8f with SMTP id a11-20020adfe5cb000000b0033b24649d8fmr5921115wrn.58.1707409731021; Thu, 08 Feb 2024 08:28:51 -0800 (PST) X-Google-Smtp-Source: AGHT+IGTys/zK8QLNgSaoNWw1DKlPbCHDumYvvPEHoiD5zDMAJPOMCktNJOHWvhP7YVhM2WfVDvtmw== X-Received: by 2002:adf:e5cb:0:b0:33b:2464:9d8f with SMTP id a11-20020adfe5cb000000b0033b24649d8fmr5921108wrn.58.1707409730680; Thu, 08 Feb 2024 08:28:50 -0800 (PST) Received: from localhost (185.223.159.143.dyn.plus.net. [143.159.223.185]) by smtp.gmail.com with ESMTPSA id j8-20020a5d6e48000000b0033929310ae4sm3924449wrz.73.2024.02.08.08.28.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Feb 2024 08:28:49 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/4] gdb/unittests: additional tests for gdb_argv class Date: Thu, 8 Feb 2024 16:28:42 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org 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+patchwork=sourceware.org@sourceware.org We already have some gdb_argv tests, but these only test the gdb_argv::as_array_view member function. This commit creates a new unittests/gdb_argv-selftests.c file and adds a wider range of gdb_argv tests, this includes covering the existing as_array_view tests, so the existing tests have been deleted from gdb/utils.c. I've also added an additional test to gdb.python/py-cmd.exp which tests calling gdb.string_to_argv with an empty string. The gdb.string_to_argv function is a wrapper around gdb_argv, and I thought the empty string case was worth covering. --- gdb/Makefile.in | 1 + gdb/testsuite/gdb.python/py-cmd.exp | 4 + gdb/unittests/gdb_argv-selftests.c | 131 ++++++++++++++++++++++++++++ gdb/utils.c | 26 ------ 4 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 gdb/unittests/gdb_argv-selftests.c diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 92fe06de489..18dec570389 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -462,6 +462,7 @@ SELFTESTS_SRCS = \ unittests/format_pieces-selftests.c \ unittests/frame_info_ptr-selftests.c \ unittests/function-view-selftests.c \ + unittests/gdb_argv-selftests.c \ unittests/gdb_tilde_expand-selftests.c \ unittests/gmp-utils-selftests.c \ unittests/intrusive_list-selftests.c \ diff --git a/gdb/testsuite/gdb.python/py-cmd.exp b/gdb/testsuite/gdb.python/py-cmd.exp index 82cb4cb557a..01af475dded 100644 --- a/gdb/testsuite/gdb.python/py-cmd.exp +++ b/gdb/testsuite/gdb.python/py-cmd.exp @@ -142,6 +142,10 @@ gdb_test "python print (gdb.string_to_argv ('1\\ 2 3'))" \ {\['1 2', '3'\]} \ "string_to_argv ('1\\ 2 3')" +gdb_test "python print (gdb.string_to_argv (''))" \ + {\[\]} \ + "string_to_argv ('')" + # Test user-defined python commands. gdb_test_multiline "input simple user-defined command" \ "python" "" \ diff --git a/gdb/unittests/gdb_argv-selftests.c b/gdb/unittests/gdb_argv-selftests.c new file mode 100644 index 00000000000..b6a87254c93 --- /dev/null +++ b/gdb/unittests/gdb_argv-selftests.c @@ -0,0 +1,131 @@ +/* Self tests for the gdb_argv class from gdbsupport. + + Copyright (C) 2024 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 "defs.h" +#include "gdbsupport/selftest.h" +#include "gdbsupport/buildargv.h" + +namespace selftests { +namespace gdb_argv_tests { + +static extract_string_ctrl shell_extract_string_ctrl + (nullptr, "", "\"$`\\", "\n", "", "\n"); + +struct test_def +{ + /* INPUT is the string to pass to the gdb_argv constructor. OUTPUT is + the vector of arguments that gdb_argv should split INPUT into. */ + + test_def (const char *input, + std::vector output) + : m_input (input), + m_output (output) + { /* Nothing. */ } + + /* Run this test and check the results. Throws an exception if the test + fails. */ + + void run () const + { + gdb_argv argv (m_input); + int count = argv.count (); + + if (run_verbose ()) + { + debug_printf ("Input: %s\n", m_input); + debug_printf ("Output [Count]: %d\n", count); + } + + SELF_CHECK (count == m_output.size ()); + + gdb::array_view view = argv.as_array_view (); + SELF_CHECK (view.size () == count); + + const char *const *data = argv.get (); + for (int i = 0; i < count; ++i) + { + const char *a = argv[i]; + SELF_CHECK (view[i] == a); + SELF_CHECK (strcmp (a, m_output[i]) == 0); + SELF_CHECK (a == *data); + ++data; + } + + SELF_CHECK (*data == nullptr); + } + +private: + /* What to pass to gdb_argv constructor. */ + + const char *m_input; + + /* The expected contents of gdb_argv after construction. */ + + std::vector m_output; +}; + +/* The array of all tests. */ + +test_def tests[] = { + { "abc def", {"abc", "def"} }, + { "one two 3", {"one", "two", "3"} }, + { "one two\\ three", {"one", "two three"} }, + { "one\\ two three", {"one two", "three"} }, + { "'one two' three", {"one two", "three"} }, + { "one \"two three\"", {"one", "two three"} }, + { "'\"' \"''\"", {"\"", "''"} }, +}; + +/* Test the creation of an empty gdb_argv object. */ + +static void +empty_argv_tests () +{ + { + gdb_argv argv; + + SELF_CHECK (argv.get () == nullptr); + SELF_CHECK (argv.count () == 0); + + gdb::array_view view = argv.as_array_view (); + + SELF_CHECK (view.data () == nullptr); + SELF_CHECK (view.size () == 0); + } +} + +static void +run_tests () +{ + empty_argv_tests (); + + for (const auto &test : tests) + test.run (); +} + +} /* namespace gdb_argv_tests */ +} /* namespace selftests */ + +void _initialize_gdb_argv_selftest (); +void +_initialize_gdb_argv_selftest () +{ + selftests::register_test ("gdb_argv", + selftests::gdb_argv_tests::run_tests); +} diff --git a/gdb/utils.c b/gdb/utils.c index 702c3f15826..05fbbdc2b63 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3294,31 +3294,6 @@ gdb_realpath_tests () gdb_realpath_check_trailer ("", ""); } -/* Test the gdb_argv::as_array_view method. */ - -static void -gdb_argv_as_array_view_test () -{ - { - gdb_argv argv; - - gdb::array_view view = argv.as_array_view (); - - SELF_CHECK (view.data () == nullptr); - SELF_CHECK (view.size () == 0); - } - { - gdb_argv argv ("une bonne 50"); - - gdb::array_view view = argv.as_array_view (); - - SELF_CHECK (view.size () == 3); - SELF_CHECK (strcmp (view[0], "une") == 0); - SELF_CHECK (strcmp (view[1], "bonne") == 0); - SELF_CHECK (strcmp (view[2], "50") == 0); - } -} - #endif /* GDB_SELF_TEST */ /* Simple, portable version of dirname that does not modify its @@ -3794,7 +3769,6 @@ When set, debugging messages will be marked with seconds and microseconds."), #if GDB_SELF_TEST selftests::register_test ("gdb_realpath", gdb_realpath_tests); - selftests::register_test ("gdb_argv_array_view", gdb_argv_as_array_view_test); selftests::register_test ("strncmp_iw_with_mode", strncmp_iw_with_mode_tests); selftests::register_test ("pager", test_pager);