binutils-gdb/gdb/unittests/function-view-selftests.c
Simon Marchi 1526853e34 Add selftests run filtering
With the growing number of selftests, I think it would be useful to be
able to run only a subset of the tests.  This patch associates a name to
each registered selftest.  It then allows doing something like:

  (gdb) maintenance selftest aarch64
  Running self-tests.
  Running selftest aarch64-analyze-prologue.
  Running selftest aarch64-process-record.
  Ran 2 unit tests, 0 failed

or with gdbserver:

  ./gdbserver --selftest=aarch64

In both cases, only the tests that contain "aarch64" in their name are
ran.  To help validate that the tests you want to run were actually ran,
it also prints a message with the test name before running each test.

Right now, all the arch-dependent tests are registered as a single test
of the selftests.  To be able to filter those too, I made them
"first-class citizen" selftests.  The selftest type is an interface,
with different implementations for "simple selftests" and "arch
selftests".  The run_tests function simply iterates on that an invokes
operator() on each test.

I changed the tests data structure from a vector to a map, because

  - it allows iterating in a stable (alphabetical) order
  - it allows to easily verify if a test with a given name has been
    registered, to avoid duplicates

There's also a new command "maintenance info selftests" that lists the
registered selftests.

gdb/ChangeLog:

	* common/selftest.h (selftest): New struct/interface.
	(register_test): Add name parameter, add new overload.
	(run_tests): Add filter parameter.
	(for_each_selftest_ftype): New typedef.
	(for_each_selftest): New declaration.
	* common/selftest.c (tests): Change type to
	map<string, unique_ptr<selftest>>.
	(simple_selftest): New struct.
	(register_test): New function.
	(register_test): Add name parameter and use it.
	(run_tests): Add filter parameter and use it.  Add prints.
	Adjust to vector -> map change.
	* aarch64-tdep.c (_initialize_aarch64_tdep): Add names when
	registering selftests.
	* arm-tdep.c (_initialize_arm_tdep): Likewise.
	* disasm-selftests.c (_initialize_disasm_selftests): Likewise.
	* dwarf2-frame.c (_initialize_dwarf2_frame): Likewise.
	* dwarf2loc.c (_initialize_dwarf2loc): Likewise.
	* findvar.c (_initialize_findvar): Likewise.
	* gdbarch-selftests.c (_initialize_gdbarch_selftests): Likewise.
	* maint.c (maintenance_selftest): Update call to run_tests.
	(maintenance_info_selftests): New function.
	(_initialize_maint_cmds): Register "maintenance info selftests"
	command.  Update "maintenance selftest" doc.
	* regcache.c (_initialize_regcache): Add names when registering
	selftests.
	* rust-exp.y (_initialize_rust_exp): Likewise.
	* selftest-arch.c (gdbarch_selftest): New struct.
	(gdbarch_tests): Remove.
	(register_test_foreach_arch): Add name parameter.  Call
	register_test.
	(tests_with_arch): Remove, move most content to
	gdbarch_selftest::operator().
	(_initialize_selftests_foreach_arch): Remove.
	* selftest-arch.h (register_test_foreach_arch): Add name
	parameter.
	(run_tests_with_arch): New declaration.
	* utils-selftests.c (_initialize_utils_selftests): Add names
	when registering selftests.
	* utils.c (_initialize_utils): Likewise.
	* unittests/array-view-selftests.c
	(_initialize_array_view_selftests): Likewise.
	* unittests/environ-selftests.c (_initialize_environ_selftests):
	Likewise.
	* unittests/function-view-selftests.c
	(_initialize_function_view_selftests): Likewise.
	* unittests/offset-type-selftests.c
	(_initialize_offset_type_selftests): Likewise.
	* unittests/optional-selftests.c
	(_initialize_optional_selftests): Likewise.
	* unittests/scoped_restore-selftests.c
	(_initialize_scoped_restore_selftests): Likewise.
	* NEWS: Document "maintenance selftest" and "maint info
	selftests".

gdb/gdbserver/ChangeLog:

	* server.c (captured_main): Accept argument for --selftest.
	Update run_tests call.
	* linux-x86-tdesc-selftest.c (initialize_low_tdesc): Add names
	when registering selftests.

gdb/doc/ChangeLog:

	* gdb.texinfo (Maintenance Commands): Document filter parameter
	of "maint selftest".  Document "maint info selftests" command.
2017-09-16 14:06:03 +02:00

180 lines
5.4 KiB
C

/* Self tests for function_view for GDB, the GNU debugger.
Copyright (C) 2017 Free Software Foundation, Inc.
This file is part of GDB.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
#include "selftest.h"
#include "common/function-view.h"
namespace selftests {
namespace function_view {
static int
plus_one_fn_int (int val)
{
return ++val;
}
static short
plus_one_fn_short (short val)
{
return ++val;
}
static int
call_callback_int (int val, gdb::function_view <int (int)> callback)
{
return callback (val);
}
static void
call_callback_void (int val, gdb::function_view <void (int)> callback)
{
callback (val);
}
struct plus_one_int_func_obj
{
int operator () (int val)
{
++call_count;
return ++val;
}
/* Number of times called. */
int call_count = 0;
};
static void
run_tests ()
{
/* A simple lambda. */
auto plus_one_lambda = [] (int val) { return ++val; };
/* A function_view that references the lambda. */
gdb::function_view<int (int)> plus_one_func_view (plus_one_lambda);
/* Check calling the lambda directly. */
SELF_CHECK (plus_one_lambda (0) == 1);
SELF_CHECK (plus_one_lambda (1) == 2);
/* Check calling lambda via the view. */
SELF_CHECK (plus_one_func_view (2) == 3);
SELF_CHECK (plus_one_func_view (3) == 4);
/* Check calling a function that takes a function_view as argument,
by value. Pass a lambda, making sure a function_view is properly
constructed implicitly. */
SELF_CHECK (call_callback_int (1, [] (int val)
{
return val + 2;
}) == 3);
/* Same, passing a named/lvalue lambda. */
SELF_CHECK (call_callback_int (1, plus_one_lambda) == 2);
/* Same, passing a named/lvalue function_view (should copy). */
SELF_CHECK (call_callback_int (1, plus_one_func_view) == 2);
/* Check constructing a function view over a function-object
callable, and calling it. */
plus_one_int_func_obj func_obj;
SELF_CHECK (func_obj (0) == 1);
SELF_CHECK (call_callback_int (1, func_obj) == 2);
/* Check that the callable was referenced, not copied. */
SELF_CHECK (func_obj.call_count == 2);
/* Check constructing a function_view over a free-function callable,
and calling it. */
SELF_CHECK (call_callback_int (1, plus_one_fn_int) == 2);
/* Check calling a function with a
compatible-but-not-exactly-the-same prototype. */
SELF_CHECK (call_callback_int (1, [] (short val) -> short
{
return val + 2;
}) == 3);
/* Same, but passing a function pointer. */
SELF_CHECK (call_callback_int (1, plus_one_fn_short) == 2);
/* Like std::function, a function_view that expects a void return
can reference callables with non-void return type. The result is
simply discarded. Check a lambda, function object and a function
pointer. */
call_callback_void (1, [] (int val) -> int
{
return val + 2;
});
call_callback_void (1, func_obj);
call_callback_void (1, plus_one_fn_int);
/* Check that the main ctor doesn't hijack the copy ctor. */
auto plus_one_func_view2 (plus_one_func_view);
auto plus_one_func_view3 (plus_one_func_view2);
static_assert (std::is_same<decltype (plus_one_func_view),
decltype (plus_one_func_view2)>::value, "");
static_assert (std::is_same<decltype (plus_one_func_view),
decltype (plus_one_func_view3)>::value, "");
SELF_CHECK (plus_one_func_view3 (1) == 2);
/* Likewise, but propagate a NULL callable. If this calls the main
function_view ctor instead of the copy ctor by mistake, then
null_func_2 ends up non-NULL (because it'd instead reference
null_func_1 as just another callable). */
constexpr gdb::function_view<int (int)> null_func_view_1 = nullptr;
constexpr auto null_func_view_2 (null_func_view_1);
/* While at it, check whether the function_view is bound using
various forms, op==, op!= and op bool. */
/* op== */
static_assert (null_func_view_2 == nullptr, "");
static_assert (nullptr == null_func_view_2, "");
static_assert (null_func_view_2 == NULL, "");
static_assert (NULL == null_func_view_2, "");
/* op!= */
static_assert (!(null_func_view_2 != nullptr), "");
static_assert (!(nullptr != null_func_view_2), "");
static_assert (!(null_func_view_2 != NULL), "");
static_assert (!(NULL != null_func_view_2), "");
/* op bool */
static_assert (!null_func_view_2, "");
/* Check the nullptr_t ctor. */
constexpr gdb::function_view<int (int)> check_ctor_nullptr (nullptr);
static_assert (!check_ctor_nullptr, "");
/* Check the nullptr_t op= */
gdb::function_view<int (int)> check_op_eq_null (plus_one_fn_int);
SELF_CHECK (check_op_eq_null);
check_op_eq_null = nullptr;
SELF_CHECK (!check_op_eq_null);
}
} /* namespace function_view */
} /* namespace selftests */
void
_initialize_function_view_selftests ()
{
selftests::register_test ("function_view",
selftests::function_view::run_tests);
}