From c45a683f8f9195bf7cc53bc645a20092c7155629 Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Tue, 21 Sep 2021 00:41:26 +0200 Subject: [PATCH] [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 static void -test_intrusive_list () +test_intrusive_list_1 () { intrusive_list_test tests; @@ -804,8 +804,8 @@ test_node_is_linked () static void test_intrusive_list () { - test_intrusive_list (); - test_intrusive_list (); + test_intrusive_list_1 (); + test_intrusive_list_1 (); 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 +#include namespace selftests { @@ -30,22 +31,6 @@ namespace selftests static std::map> 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 (test); } +/* A selftest that calls the test function without arguments. */ + +struct lambda_selftest : public selftest +{ + lambda_selftest (std::function function_) + { + function = function_; + } + + void operator() () const override + { + function (); + } + + std::function function; +}; + /* See selftest.h. */ void -register_test (const std::string &name, self_test_function *function) +register_test (const std::string &name, + std::function 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 function); /* Run all the self tests. This print a message describing the number of test and the number of failures. -- 2.30.2