public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Merge/shared string_printf and string_vprintf unit tests
@ 2017-10-16 22:58 Pedro Alves
  2017-10-18 22:30 ` John Baldwin
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2017-10-16 22:58 UTC (permalink / raw)
  To: gdb-patches

Merge the string_printf and string_vprintf tests, running them all
against both functions.

gdb/ChangeLog:
2017-10-16  Pedro Alves  <palves@redhat.com>

	* unittests/common-utils-selftests.c (format_func): New typedef.
	(string_printf_tests, string_vprintf_tests): Tests factored out
	and merged to ...
	(test_format_func): ... this new function.
	(string_printf_tests, string_vprintf_tests): Reimplement on top of
	test_format_func.
---
 gdb/unittests/common-utils-selftests.c | 43 ++++++++++++++++++++++------------
 1 file changed, 28 insertions(+), 15 deletions(-)

diff --git a/gdb/unittests/common-utils-selftests.c b/gdb/unittests/common-utils-selftests.c
index 0f26b21..cf65513 100644
--- a/gdb/unittests/common-utils-selftests.c
+++ b/gdb/unittests/common-utils-selftests.c
@@ -22,23 +22,41 @@
 
 namespace selftests {
 
+/* Type of both 'string_printf' and the 'format' function below.  Used
+   to run the same tests against both string_printf and
+   string_vprintf.  */
+typedef std::string (format_func) (const char *fmt, ...);
+
 static void
-string_printf_tests ()
+test_format_func (format_func *func)
 {
-  SELF_CHECK (string_printf ("%s", "") == "");
-  SELF_CHECK (string_printf ("%d comes before 2", 1) == "1 comes before 2");
-  SELF_CHECK (string_printf ("hello %s", "world") == "hello world");
+  /* Basic smoke tests.  */
+  SELF_CHECK (func ("%s", "") == "");
+  SELF_CHECK (func ("%s", "test") == "test");
+  SELF_CHECK (func ("%d", 23) == "23");
+  SELF_CHECK (func ("%s %d %s", "test", 23, "done") == "test 23 done");
+  SELF_CHECK (func ("nothing") == "nothing");
+  SELF_CHECK (func ("%d comes before 2", 1) == "1 comes before 2");
+  SELF_CHECK (func ("hello %s", "world") == "hello world");
 
+  /* Check that we don't mishandle very large strings.  (An earlier
+     non-public implementation of string_printf mishandled this).  */
 #define X10 "0123456789"
 #define X100 X10 X10 X10 X10 X10 X10 X10 X10 X10 X10
 #define X1000 X100 X100 X100 X100 X100 X100 X100 X100 X100 X100
 #define X10000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000 X1000
 #define X100000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000 X10000
-  SELF_CHECK (string_printf ("%s", X10) == X10);
-  SELF_CHECK (string_printf ("%s", X100) == X100);
-  SELF_CHECK (string_printf ("%s", X1000) == X1000);
-  SELF_CHECK (string_printf ("%s", X10000) == X10000);
-  SELF_CHECK (string_printf ("%s", X100000) == X100000);
+  SELF_CHECK (func ("%s", X10) == X10);
+  SELF_CHECK (func ("%s", X100) == X100);
+  SELF_CHECK (func ("%s", X1000) == X1000);
+  SELF_CHECK (func ("%s", X10000) == X10000);
+  SELF_CHECK (func ("%s", X100000) == X100000);
+}
+
+static void
+string_printf_tests ()
+{
+  test_format_func (string_printf);
 }
 
 static std::string
@@ -55,12 +73,7 @@ format (const char *fmt, ...)
 static void
 string_vprintf_tests ()
 {
-  /* Basic smoke tests.  */
-  SELF_CHECK (format ("%s", "test") == "test");
-  SELF_CHECK (format ("%d", 23) == "23");
-  SELF_CHECK (format ("%s %d %s", "test", 23, "done")
-	      == "test 23 done");
-  SELF_CHECK (format ("nothing") == "nothing");
+  test_format_func (format);
 }
 
 } /* namespace selftests */
-- 
2.5.5

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Merge/shared string_printf and string_vprintf unit tests
  2017-10-16 22:58 [PATCH] Merge/shared string_printf and string_vprintf unit tests Pedro Alves
@ 2017-10-18 22:30 ` John Baldwin
  2017-10-30 11:47   ` Pedro Alves
  0 siblings, 1 reply; 3+ messages in thread
From: John Baldwin @ 2017-10-18 22:30 UTC (permalink / raw)
  To: gdb-patches; +Cc: Pedro Alves

On Monday, October 16, 2017 11:57:57 PM Pedro Alves wrote:
> Merge the string_printf and string_vprintf tests, running them all
> against both functions.
> 
> gdb/ChangeLog:
> 2017-10-16  Pedro Alves  <palves@redhat.com>
> 
> 	* unittests/common-utils-selftests.c (format_func): New typedef.
> 	(string_printf_tests, string_vprintf_tests): Tests factored out
> 	and merged to ...
> 	(test_format_func): ... this new function.
> 	(string_printf_tests, string_vprintf_tests): Reimplement on top of
> 	test_format_func.

Looks good to me.

-- 
John Baldwin

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Merge/shared string_printf and string_vprintf unit tests
  2017-10-18 22:30 ` John Baldwin
@ 2017-10-30 11:47   ` Pedro Alves
  0 siblings, 0 replies; 3+ messages in thread
From: Pedro Alves @ 2017-10-30 11:47 UTC (permalink / raw)
  To: John Baldwin, gdb-patches

On 10/18/2017 06:40 PM, John Baldwin wrote:
> On Monday, October 16, 2017 11:57:57 PM Pedro Alves wrote:
>> Merge the string_printf and string_vprintf tests, running them all
>> against both functions.
>>
>> gdb/ChangeLog:
>> 2017-10-16  Pedro Alves  <palves@redhat.com>
>>
>> 	* unittests/common-utils-selftests.c (format_func): New typedef.
>> 	(string_printf_tests, string_vprintf_tests): Tests factored out
>> 	and merged to ...
>> 	(test_format_func): ... this new function.
>> 	(string_printf_tests, string_vprintf_tests): Reimplement on top of
>> 	test_format_func.
> 
> Looks good to me.
> 

Thanks much for the review John.  I've pushed this in now, with a
tweak to add ATTRIBUTE_PRINTF where appropriate:

 typedef std::string (format_func) (const char *fmt, ...)
   ATTRIBUTE_PRINTF (1, 2);

 static void ATTRIBUTE_PRINTF (2, 3)
 string_vappendf_wrapper (std::string &str, const char *fmt, ...)

... after noticing Simon's recent similar change to this file
that meanwhile went in.

Thanks,
Pedro Alves

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-10-30 11:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-16 22:58 [PATCH] Merge/shared string_printf and string_vprintf unit tests Pedro Alves
2017-10-18 22:30 ` John Baldwin
2017-10-30 11:47   ` Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).