]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Always use std::function for self-tests
authorTom Tromey <tromey@adacore.com>
Thu, 23 Sep 2021 19:09:48 +0000 (13:09 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 19 Oct 2021 18:58:50 +0000 (12:58 -0600)
Now that there is a register_test variant that accepts std::function,
it seems to me that the 'selftest' struct and accompanying code is
obsolete -- simply always using std::function is simpler.  This patch
implements this idea.

gdbsupport/selftest.cc
gdbsupport/selftest.h

index 589ef1e6797659401836d044ce3186f1e9a758ad..d65063820ac69100687560513eedc068dc6ec93e 100644 (file)
@@ -29,43 +29,18 @@ namespace selftests
    the order of tests stable and easily looking up whether a test name
    exists.  */
 
-static std::map<std::string, std::unique_ptr<selftest>> tests;
+static std::map<std::string, std::function<void(void)>> tests;
 
 /* See selftest.h.  */
 
 void
-register_test (const std::string &name, selftest *test)
+register_test (const std::string &name,
+              std::function<void(void)> function)
 {
   /* Check that no test with this name already exist.  */
   gdb_assert (tests.find (name) == tests.end ());
 
-  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,
-              std::function<void(void)> function)
-{
-  register_test (name, new lambda_selftest (function));
+  tests[name] = function;
 }
 
 /* See selftest.h.  */
@@ -91,7 +66,7 @@ run_tests (gdb::array_view<const char *const> filters, bool verbose)
   for (const auto &pair : tests)
     {
       const std::string &name = pair.first;
-      const std::unique_ptr<selftest> &test = pair.second;
+      const auto &test = pair.second;
       bool run = false;
 
       if (filters.empty ())
@@ -112,7 +87,7 @@ run_tests (gdb::array_view<const char *const> filters, bool verbose)
        {
          debug_printf (_("Running selftest %s.\n"), name.c_str ());
          ++ran;
-         (*test) ();
+         test ();
        }
       catch (const gdb_exception_error &ex)
        {
index d76fc4b37d368c7516ae1417df551c8102c55a31..2326ebad495eb932161fc580e1056d666fb06b9e 100644 (file)
 /* A test is just a function that does some checks and throws an
    exception if something has gone wrong.  */
 
-typedef void self_test_function (void);
-
 namespace selftests
 {
 
-/* Interface for the various kinds of selftests.  */
-
-struct selftest
-{
-  virtual ~selftest () = default;
-  virtual void operator() () const = 0;
-};
-
 /* True if selftest should run verbosely.  */
 
 extern bool run_verbose ();
 
 /* Register a new self-test.  */
 
-extern void register_test (const std::string &name, selftest *test);
-
-/* Register a new self-test.  */
-
 extern void register_test (const std::string &name,
                           std::function<void(void)> function);