Add register_test variant with std::function arg

Message ID 20210913154000.GA3078@delia.home
State New
Headers show
Series
  • Add register_test variant with std::function arg
Related show

Commit Message

Tom de Vries via Gdb-patches Sept. 13, 2021, 3:40 p.m.
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(+)

Patch

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.