[1/4] gdb/unittests: add extract_string_maybe_quoted self tests

Message ID 694821ad9a09cabbc3fa3eccda73836fea550773.1707409662.git.aburgess@redhat.com
State New
Headers
Series Rewrite gdb_argv using extract_string_maybe_quoted |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_gdb_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_gdb_check--master-aarch64 success Testing passed

Commit Message

Andrew Burgess Feb. 8, 2024, 4:28 p.m. UTC
  This commit adds some self tests for the extract_string_maybe_quoted
function.  No functionality is changed in this commit.
---
 gdb/Makefile.in                          |  1 +
 gdb/unittests/extract-string-selftests.c | 85 ++++++++++++++++++++++++
 2 files changed, 86 insertions(+)
 create mode 100644 gdb/unittests/extract-string-selftests.c
  

Comments

Tom Tromey Feb. 8, 2024, 6:17 p.m. UTC | #1
>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes:

Andrew> This commit adds some self tests for the extract_string_maybe_quoted
Andrew> function.  No functionality is changed in this commit.

Andrew> +  test_def (const char *input,
Andrew> +	    const char *output,
Andrew> +	    size_t offset)
Andrew> +    : m_input (input),
Andrew> +      m_output (output),
Andrew> +      m_offset (offset)
Andrew> +  { /* Nothing.  */ }

I wonder -- if this constructor was constexpr, would that create the
objects in the rodata section?

I don't really know how that works.  However, making data read-only like
this is one of the advantages of...

Andrew> +test_def tests[] = {
Andrew> +  { "abc def", "abc", 3 },

... making this kind of thing 'static const'.

Tom
  

Patch

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 0e0f19c40c9..92fe06de489 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -457,6 +457,7 @@  SELFTESTS_SRCS = \
 	unittests/copy_bitwise-selftests.c \
 	unittests/enum-flags-selftests.c \
 	unittests/environ-selftests.c \
+	unittests/extract-string-selftests.c \
 	unittests/filtered_iterator-selftests.c \
 	unittests/format_pieces-selftests.c \
 	unittests/frame_info_ptr-selftests.c \
diff --git a/gdb/unittests/extract-string-selftests.c b/gdb/unittests/extract-string-selftests.c
new file mode 100644
index 00000000000..c2eedd75342
--- /dev/null
+++ b/gdb/unittests/extract-string-selftests.c
@@ -0,0 +1,85 @@ 
+/* Self tests for the function extract_string_maybe_quoted.
+
+   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 <http://www.gnu.org/licenses/>.  */
+
+#include "defs.h"
+#include "gdbsupport/selftest.h"
+
+namespace selftests {
+namespace extract_string {
+
+struct test_def
+{
+  test_def (const char *input,
+	    const char *output,
+	    size_t offset)
+    : m_input (input),
+      m_output (output),
+      m_offset (offset)
+  { /* Nothing.  */ }
+
+  void run () const
+  {
+    const char *tmp = m_input;
+    std::string test_out = extract_string_maybe_quoted (&tmp);
+
+    if (run_verbose ())
+      {
+	debug_printf ("Input: %s\n", m_input);
+	debug_printf ("Output [Got]: %s\n", test_out.c_str ());
+	debug_printf ("Output [Exp]: %s\n", m_output);
+	debug_printf ("Rest [Got]: %s\n", tmp);
+	debug_printf ("Rest [Exp]: %s\n", (m_input + m_offset));
+      }
+
+    SELF_CHECK (test_out == m_output);
+    SELF_CHECK (tmp == m_input + m_offset);
+  }
+
+private:
+  const char *m_input;
+  const char *m_output;
+  size_t m_offset;
+};
+
+test_def tests[] = {
+  { "abc def", "abc", 3 },
+  { "'abc' def", "abc", 5 },
+  { "\"abc\" def", "abc", 5 },
+  { "ab\\ cd ef", "ab cd", 6 },
+  { "\"abc\\\"def\" ghi", "abc\"def", 10 },
+  { "\"'abc' 'def'\" ghi", "'abc' 'def'", 13 },
+};
+
+static void
+run_tests ()
+{
+  for (const auto &test : tests)
+    test.run ();
+}
+
+} /* namespace extract_string */
+} /* namespace selftests */
+
+void _initialize_extract_string_selftest ();
+void
+_initialize_extract_string_selftest ()
+{
+  selftests::register_test ("extract-string",
+			    selftests::extract_string::run_tests);
+}