public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH][gdb] Add register_test variant with std::function arg
@ 2021-09-13 15:40 Tom de Vries
  2021-09-20 15:58 ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom de Vries @ 2021-09-13 15:40 UTC (permalink / raw)
  To: gdb-patches

Hi,

Add register_test variant with std::function arg, such that we can do:
...
  register_test (test_name, [=] () { SELF_CHECK (...); });
...

Any comments?

Thanks,
- Tom

[gdb] Add register_test variant with std::function arg

---
 gdbsupport/selftest.cc | 25 +++++++++++++++++++++++++
 gdbsupport/selftest.h  |  3 +++
 2 files changed, 28 insertions(+)

diff --git a/gdbsupport/selftest.cc b/gdbsupport/selftest.cc
index d0511abe654..6d92844880f 100644
--- a/gdbsupport/selftest.cc
+++ b/gdbsupport/selftest.cc
@@ -21,6 +21,7 @@
 #include "common-debug.h"
 #include "selftest.h"
 #include <map>
+#include <functional>
 
 namespace selftests
 {
@@ -57,6 +58,30 @@ register_test (const std::string &name, selftest *test)
   tests[name] = std::unique_ptr<selftest> (test);
 }
 
+struct lambda_selftest : public selftest
+{
+  lambda_selftest (std::function<void(void)> function_)
+  {
+    function  = function_;
+  }
+
+  void operator() () const override
+  {
+    function ();
+  }
+
+  std::function<void(void)> function;
+};
+
+/* See selftest.h.  */
+
+void
+register_test (const std::string &name,
+	       std::function<void(void)> function)
+{
+  register_test (name, new lambda_selftest (function));
+}
+
 /* See selftest.h.  */
 
 void
diff --git a/gdbsupport/selftest.h b/gdbsupport/selftest.h
index 13441b05a21..d2fa07228e8 100644
--- a/gdbsupport/selftest.h
+++ b/gdbsupport/selftest.h
@@ -46,6 +46,9 @@ extern void register_test (const std::string &name, selftest *test);
 extern void register_test (const std::string &name,
 			   self_test_function *function);
 
+extern void register_test (const std::string &name,
+			   std::function<void(void)> function);
+
 /* Run all the self tests.  This print a message describing the number
    of test and the number of failures.
 

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

* Re: [PATCH][gdb] Add register_test variant with std::function arg
  2021-09-13 15:40 [PATCH][gdb] Add register_test variant with std::function arg Tom de Vries
@ 2021-09-20 15:58 ` Simon Marchi
  2021-09-20 16:57   ` Tom de Vries
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2021-09-20 15:58 UTC (permalink / raw)
  To: Tom de Vries, gdb-patches

On 2021-09-13 11:40 a.m., Tom de Vries via Gdb-patches wrote:
> Hi,
> 
> Add register_test variant with std::function arg, such that we can do:
> ...
>   register_test (test_name, [=] () { SELF_CHECK (...); });
> ...
> 
> Any comments?
> 
> Thanks,
> - Tom
> 
> [gdb] Add register_test variant with std::function arg
> 
> ---
>  gdbsupport/selftest.cc | 25 +++++++++++++++++++++++++
>  gdbsupport/selftest.h  |  3 +++
>  2 files changed, 28 insertions(+)
> 
> diff --git a/gdbsupport/selftest.cc b/gdbsupport/selftest.cc
> index d0511abe654..6d92844880f 100644
> --- a/gdbsupport/selftest.cc
> +++ b/gdbsupport/selftest.cc
> @@ -21,6 +21,7 @@
>  #include "common-debug.h"
>  #include "selftest.h"
>  #include <map>
> +#include <functional>
>  
>  namespace selftests
>  {
> @@ -57,6 +58,30 @@ register_test (const std::string &name, selftest *test)
>    tests[name] = std::unique_ptr<selftest> (test);
>  }
>  
> +struct lambda_selftest : public selftest
> +{
> +  lambda_selftest (std::function<void(void)> function_)
> +  {
> +    function  = function_;
> +  }
> +
> +  void operator() () const override
> +  {
> +    function ();
> +  }
> +
> +  std::function<void(void)> function;
> +};
> +
> +/* See selftest.h.  */
> +
> +void
> +register_test (const std::string &name,
> +	       std::function<void(void)> function)
> +{
> +  register_test (name, new lambda_selftest (function));
> +}
> +
>  /* See selftest.h.  */

I presume this is for your other patch ([RFC][gdb/testsuite] Register
test for each arch separately in register_test_foreach_arch).  Since
this is just for tests and we don't care about performance (at least, we
care more about simplicity), I think it would be fine to just make the
existing register_test take an std::function.

Simon

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

* Re: [PATCH][gdb] Add register_test variant with std::function arg
  2021-09-20 15:58 ` Simon Marchi
@ 2021-09-20 16:57   ` Tom de Vries
  0 siblings, 0 replies; 3+ messages in thread
From: Tom de Vries @ 2021-09-20 16:57 UTC (permalink / raw)
  To: Simon Marchi, gdb-patches

[-- Attachment #1: Type: text/plain, Size: 601 bytes --]

On 9/20/21 5:58 PM, Simon Marchi wrote:
> On 2021-09-13 11:40 a.m., Tom de Vries via Gdb-patches wrote:
> I presume this is for your other patch ([RFC][gdb/testsuite] Register
> test for each arch separately in register_test_foreach_arch).

Yes.

> Since
> this is just for tests and we don't care about performance (at least, we
> care more about simplicity), I think it would be fine to just make the
> existing register_test take an std::function.

Ack, done.

I'll commit like this, unless there are further comments (f.i., to not
do the simple_selftest -> lambda_selftest rename).

Thanks,
- Tom

[-- Attachment #2: 0001-gdb-Change-register_test-to-use-std-function-arg.patch --]
[-- Type: text/x-patch, Size: 3234 bytes --]

[gdb] Change register_test to use std::function arg

Change register_test to use std::function arg, such that we can do:
...
  register_test (test_name, [=] () { SELF_CHECK (...); });
...

Tested on x86_64-linux.

---
 gdb/unittests/intrusive_list-selftests.c |  6 ++---
 gdbsupport/selftest.cc                   | 39 +++++++++++++++++---------------
 gdbsupport/selftest.h                    |  2 +-
 3 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/gdb/unittests/intrusive_list-selftests.c b/gdb/unittests/intrusive_list-selftests.c
index 8b2b2d1391e..1b38ac39edf 100644
--- a/gdb/unittests/intrusive_list-selftests.c
+++ b/gdb/unittests/intrusive_list-selftests.c
@@ -754,7 +754,7 @@ struct intrusive_list_test
 
 template <typename ListType>
 static void
-test_intrusive_list ()
+test_intrusive_list_1 ()
 {
   intrusive_list_test<ListType> tests;
 
@@ -804,8 +804,8 @@ test_node_is_linked ()
 static void
 test_intrusive_list ()
 {
-  test_intrusive_list<item_with_base_list> ();
-  test_intrusive_list<item_with_member_list> ();
+  test_intrusive_list_1<item_with_base_list> ();
+  test_intrusive_list_1<item_with_member_list> ();
   test_node_is_linked ();
 }
 
diff --git a/gdbsupport/selftest.cc b/gdbsupport/selftest.cc
index d0511abe654..55f530ad98e 100644
--- a/gdbsupport/selftest.cc
+++ b/gdbsupport/selftest.cc
@@ -21,6 +21,7 @@
 #include "common-debug.h"
 #include "selftest.h"
 #include <map>
+#include <functional>
 
 namespace selftests
 {
@@ -30,22 +31,6 @@ namespace selftests
 
 static std::map<std::string, std::unique_ptr<selftest>> tests;
 
-/* A selftest that calls the test function without arguments.  */
-
-struct simple_selftest : public selftest
-{
-  simple_selftest (self_test_function *function_)
-  : function (function_)
-  {}
-
-  void operator() () const override
-  {
-    function ();
-  }
-
-  self_test_function *function;
-};
-
 /* See selftest.h.  */
 
 void
@@ -57,12 +42,30 @@ register_test (const std::string &name, selftest *test)
   tests[name] = std::unique_ptr<selftest> (test);
 }
 
+/* A selftest that calls the test function without arguments.  */
+
+struct lambda_selftest : public selftest
+{
+  lambda_selftest (std::function<void(void)> function_)
+  {
+    function  = function_;
+  }
+
+  void operator() () const override
+  {
+    function ();
+  }
+
+  std::function<void(void)> function;
+};
+
 /* See selftest.h.  */
 
 void
-register_test (const std::string &name, self_test_function *function)
+register_test (const std::string &name,
+	       std::function<void(void)> function)
 {
-  register_test (name, new simple_selftest (function));
+  register_test (name, new lambda_selftest (function));
 }
 
 /* See selftest.h.  */
diff --git a/gdbsupport/selftest.h b/gdbsupport/selftest.h
index 13441b05a21..b75e01e197f 100644
--- a/gdbsupport/selftest.h
+++ b/gdbsupport/selftest.h
@@ -44,7 +44,7 @@ extern void register_test (const std::string &name, selftest *test);
 /* Register a new self-test.  */
 
 extern void register_test (const std::string &name,
-			   self_test_function *function);
+			   std::function<void(void)> function);
 
 /* Run all the self tests.  This print a message describing the number
    of test and the number of failures.

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

end of thread, other threads:[~2021-09-20 16:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-13 15:40 [PATCH][gdb] Add register_test variant with std::function arg Tom de Vries
2021-09-20 15:58 ` Simon Marchi
2021-09-20 16:57   ` Tom de Vries

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).