test: make litest_parameters fetching more type-safe

Require the type to be added in the litest_test_params_fetch() so we can
easily detect a mismatch. And add some type-safe getters that are much
easier to use for all the tests that only have a single parameter to
fetch anyway.

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1139>
This commit is contained in:
Peter Hutterer 2025-02-12 12:03:41 +10:00 committed by Marge Bot
parent 9ae15c6a45
commit 5ed75e7e9f
10 changed files with 109 additions and 95 deletions

View file

@ -662,9 +662,12 @@ _litest_test_param_fetch(const struct litest_test_parameters *params, ...)
while ((name = va_arg(args, const char *))) {
bool found = false;
char type = (char)va_arg(args, int);
void **ptr = va_arg(args, void *);
list_for_each(p, &params->test_params, link) {
if (streq(p->name, name)) {
if (p->value.type != type)
litest_abort_msg("Paramter type mismatch: parameter '%s' is of type %c", p->name, p->value.type);
found = true;
multivalue_extract(&p->value, ptr);
break;

View file

@ -70,6 +70,54 @@ litest_test_parameters_unref(struct litest_test_parameters *params);
void
_litest_test_param_fetch(const struct litest_test_parameters *params, ...);
static inline const char *
litest_test_param_get_string(const struct litest_test_parameters *params, const char *name)
{
const char *p;
litest_test_param_fetch(params, name, 's', &p);
return p;
}
static inline bool
litest_test_param_get_bool(const struct litest_test_parameters *params, const char *name)
{
bool p;
litest_test_param_fetch(params, name, 'b', &p);
return p;
}
static inline int32_t
litest_test_param_get_i32(const struct litest_test_parameters *params, const char *name)
{
int32_t p;
litest_test_param_fetch(params, name, 'i', &p);
return p;
}
static inline uint32_t
litest_test_param_get_u32(const struct litest_test_parameters *params, const char *name)
{
uint32_t p;
litest_test_param_fetch(params, name, 'u', &p);
return p;
}
static inline char
litest_test_param_get_char(const struct litest_test_parameters *params, const char *name)
{
char p;
litest_test_param_fetch(params, name, 'c', &p);
return p;
}
static inline double
litest_test_param_get_double(const struct litest_test_parameters *params, const double *name)
{
double p;
litest_test_param_fetch(params, name, 'd', &p);
return p;
}
/**
* This struct is passed into every test.
*/

View file

@ -173,8 +173,7 @@ START_TEST(log_axisrange_warning)
struct libinput *li = dev->libinput;
const struct input_absinfo *abs;
const char *axisname;
litest_test_param_fetch(test_env->params, "axis", &axisname);
const char *axisname = litest_test_param_get_string(test_env->params, "axis");
int axis = libevdev_event_code_from_code_name(axisname);
litest_assert_int_ne(axis, -1);

View file

@ -185,8 +185,7 @@ START_TEST(pointer_motion_relative_min_decel)
int dx, dy;
double len;
const char *direction;
litest_test_param_fetch(test_env->params, "direction", &direction, NULL);
const char *direction = litest_test_param_get_string(test_env->params, "direction");
if (streq(direction, "N")) {
dx = 0; dy = 1;
@ -275,8 +274,7 @@ START_TEST(pointer_absolute_initial_state)
struct libinput_event *ev1, *ev2;
struct libinput_event_pointer *p1, *p2;
const char *axisname;
litest_test_param_fetch(test_env->params, "axis", &axisname);
const char *axisname = litest_test_param_get_string(test_env->params, "axis");
int axis = libevdev_event_code_from_code_name(axisname);
litest_assert_int_ne(axis, -1);
@ -780,8 +778,7 @@ START_TEST(pointer_scroll_wheel_hires_send_only_lores)
unsigned int lores_code, hires_code;
int direction;
const char *axisname;
litest_test_param_fetch(test_env->params, "axis", &axisname, NULL);
const char *axisname = litest_test_param_get_string(test_env->params, "axis");
if (streq(axisname, "vertical")) {
lores_code = REL_WHEEL;

View file

@ -121,8 +121,7 @@ START_TEST(switch_toggle)
struct libinput *li = dev->libinput;
struct libinput_event *event;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch sw = switch_param_lookup(str);
litest_drain_events(li);
@ -159,8 +158,7 @@ START_TEST(switch_toggle_double)
struct libinput *li = dev->libinput;
struct libinput_event *event;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch sw = switch_param_lookup(str);
if (libinput_device_switch_has_switch(dev->libinput_device, sw) <= 0)
@ -208,8 +206,7 @@ START_TEST(switch_down_on_init)
struct libinput *li;
struct libinput_event *event;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch sw = switch_param_lookup(str);
if (libinput_device_switch_has_switch(dev->libinput_device, sw) <= 0)
@ -299,8 +296,7 @@ START_TEST(switch_disable_touchpad)
struct litest_device *touchpad;
struct libinput *li = sw->libinput;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -341,8 +337,7 @@ START_TEST(switch_disable_touchpad_during_touch)
struct litest_device *sw = litest_current_device();
struct litest_device *touchpad;
struct libinput *li = sw->libinput;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -376,8 +371,7 @@ START_TEST(switch_disable_touchpad_edge_scroll)
struct litest_device *touchpad;
struct libinput *li = sw->libinput;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -419,8 +413,7 @@ START_TEST(switch_disable_touchpad_edge_scroll_interrupt)
struct libinput *li = sw->libinput;
struct libinput_event *event;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -462,8 +455,7 @@ START_TEST(switch_disable_touchpad_already_open)
struct litest_device *touchpad;
struct libinput *li = sw->libinput;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -500,8 +492,7 @@ START_TEST(switch_dont_resume_disabled_touchpad)
struct litest_device *touchpad;
struct libinput *li = sw->libinput;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -544,8 +535,7 @@ START_TEST(switch_dont_resume_disabled_touchpad_external_mouse)
struct litest_device *touchpad, *mouse;
struct libinput *li = sw->libinput;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
if (libinput_device_switch_has_switch(sw->libinput_device, which) <= 0)
@ -679,8 +669,7 @@ START_TEST(switch_suspend_with_keyboard)
struct litest_device *keyboard;
struct litest_device *sw;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
li = litest_create_context();
@ -723,8 +712,7 @@ START_TEST(switch_suspend_with_touchpad)
struct libinput *li;
struct litest_device *touchpad, *sw;
const char *str;
litest_test_param_fetch(test_env->params, "switch", &str);
const char *str = litest_test_param_get_string(test_env->params, "switch");
enum libinput_switch which = switch_param_lookup(str);
li = litest_create_context();

View file

@ -670,8 +670,7 @@ START_TEST(tip_up_motion_one_axis)
double start_x = 20,
start_y = 20;
const char *axisname;
litest_test_param_fetch(test_env->params, "axis", &axisname);
const char *axisname = litest_test_param_get_string(test_env->params, "axis");
int axis = libevdev_event_code_from_code_name(axisname);
litest_assert_int_ne(axis, -1);
@ -4026,11 +4025,9 @@ START_TEST(tablet_area_set_rectangle)
double x, y;
double *scaled, *unscaled;
const char *param_axis;
const char *param_direction;
litest_test_param_fetch(test_env->params,
"axis", &param_axis,
"direction", &param_direction);
const char *param_axis = litest_test_param_get_string(test_env->params, "axis");
const char *param_direction = litest_test_param_get_string(test_env->params, "direction");
bool use_vertical = streq(param_axis, "vertical");
int direction = streq(param_direction, "down") ? 1 : -1;
@ -5327,8 +5324,7 @@ START_TEST(tilt_fixed_points)
int axis_value;
double expected;
const char *testcase;
litest_test_param_fetch(test_env->params, "tilt", &testcase);
const char *testcase = litest_test_param_get_string(test_env->params, "tilt");
/* On devices with a range of [-N, M], make sure we calculate the hw zero position
* as zero and that the respective min/max resolve to our (hardcoded) min/max degree
@ -6431,10 +6427,10 @@ START_TEST(tablet_rotation_left_handed)
goto out;
litest_test_param_fetch(test_env->params,
"tablet_from", &tablet_from,
"touch_from", &touch_from,
"tablet_to", &tablet_to,
"touch_to", &touch_to);
"tablet_from", 'b', &tablet_from,
"touch_from", 'b', &touch_from,
"tablet_to", 'b', &tablet_to,
"touch_to", 'b', &touch_to);
enabled_from = tablet_from || touch_from;
enabled_to = tablet_to || touch_to;
@ -6482,10 +6478,10 @@ START_TEST(tablet_rotation_left_handed_configuration)
goto out;
litest_test_param_fetch(test_env->params,
"tablet_from", &tablet_from,
"touch_from", &touch_from,
"tablet_to", &tablet_to,
"touch_to", &touch_to);
"tablet_from", 'b', &tablet_from,
"touch_from", 'b', &touch_from,
"tablet_to", 'b', &tablet_to,
"touch_to", 'b', &touch_to);
tablet_dev = tablet->libinput_device;
touch_dev = finger->libinput_device;
@ -6537,10 +6533,10 @@ START_TEST(tablet_rotation_left_handed_while_in_prox)
goto out;
litest_test_param_fetch(test_env->params,
"tablet_from", &tablet_from,
"touch_from", &touch_from,
"tablet_to", &tablet_to,
"touch_to", &touch_to);
"tablet_from", 'b', &tablet_from,
"touch_from", 'b', &touch_from,
"tablet_to", 'b', &tablet_to,
"touch_to", 'b', &touch_to);
enabled_from = tablet_from || touch_from;
enabled_to = tablet_to || touch_to;
@ -6639,10 +6635,10 @@ START_TEST(tablet_rotation_left_handed_while_touch_down)
goto out;
litest_test_param_fetch(test_env->params,
"tablet_from", &tablet_from,
"touch_from", &touch_from,
"tablet_to", &tablet_to,
"touch_to", &touch_to);
"tablet_from", 'b', &tablet_from,
"touch_from", 'b', &touch_from,
"tablet_to", 'b', &tablet_to,
"touch_to", 'b', &touch_to);
enabled_from = tablet_from || touch_from;
enabled_to = tablet_to || touch_to;
@ -6700,10 +6696,10 @@ START_TEST(tablet_rotation_left_handed_add_touchpad)
return LITEST_NOT_APPLICABLE;
litest_test_param_fetch(test_env->params,
"tablet_from", &tablet_from,
"touch_from", &touch_from,
"tablet_to", &tablet_to,
"touch_to", &touch_to);
"tablet_from", 'b', &tablet_from,
"touch_from", 'b', &touch_from,
"tablet_to", 'b', &tablet_to,
"touch_to", 'b', &touch_to);
enabled_from = tablet_from || touch_from;
enabled_to = tablet_to || touch_to;
@ -6892,14 +6888,13 @@ START_TEST(huion_static_btn_tool_pen_disable_quirk_on_prox_out)
{
struct litest_device *dev = litest_current_device();
struct libinput *li = dev->libinput;
bool with_timeout;
int i;
/* test is run twice, once where the real BTN_TOOL_PEN is triggered
* during proximity out, one where the real BTN_TOOL_PEN is
* triggered after we already triggered the quirk timeout
*/
litest_test_param_fetch(test_env->params, "btn_tool_pen_timeout", &with_timeout);
bool with_timeout = litest_test_param_get_bool(test_env->params, "btn_tool_pen_timeout");
litest_drain_events(li);

View file

@ -816,8 +816,7 @@ START_TEST(touch_initial_state)
struct libinput_event_touch *t1, *t2;
struct libinput_device *device1, *device2;
const char *axisname;
litest_test_param_fetch(test_env->params, "axis", &axisname);
const char *axisname = litest_test_param_get_string(test_env->params, "axis");
int axis = libevdev_event_code_from_code_name(axisname);
litest_assert_int_ne(axis, -1);

View file

@ -271,8 +271,7 @@ START_TEST(touchpad_2fg_clickfinger)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_clickfinger_button_map map = map_param_lookup(mapstr);
litest_enable_clickfinger(dev);
@ -315,8 +314,7 @@ START_TEST(touchpad_3fg_clickfinger)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_clickfinger_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) < 3)
@ -366,8 +364,7 @@ START_TEST(touchpad_3fg_clickfinger_btntool)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_clickfinger_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) >= 3 ||
@ -523,8 +520,7 @@ START_TEST(touchpad_2fg_clickfinger_distance)
bool small_touchpad = false;
unsigned int expected_button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_clickfinger_button_map map = map_param_lookup(mapstr);
if (libinput_device_get_size(dev->libinput_device, &w, &h) == 0 &&
@ -592,8 +588,7 @@ START_TEST(touchpad_3fg_clickfinger_distance)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_clickfinger_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) < 3)
@ -642,8 +637,7 @@ START_TEST(touchpad_3fg_clickfinger_distance_btntool)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_clickfinger_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) > 2)

View file

@ -2296,8 +2296,7 @@ START_TEST(touchpad_2fg_tap)
struct libinput_event_pointer *ptrev;
uint64_t ptime, rtime;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_tap_button_map map = map_param_lookup(mapstr);
litest_enable_tap(dev->libinput_device);
@ -2354,8 +2353,7 @@ START_TEST(touchpad_2fg_tap_inverted)
struct libinput_event_pointer *ptrev;
uint64_t ptime, rtime;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_tap_button_map map = map_param_lookup(mapstr);
litest_enable_tap(dev->libinput_device);
@ -2886,8 +2884,7 @@ START_TEST(touchpad_3fg_tap)
unsigned int button = 0;
int i;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_tap_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) < 3)
@ -3161,8 +3158,7 @@ START_TEST(touchpad_3fg_tap_btntool)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_tap_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) >= 3 ||
@ -3215,8 +3211,7 @@ START_TEST(touchpad_3fg_tap_btntool_inverted)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_tap_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) > 3 ||
@ -3269,8 +3264,7 @@ START_TEST(touchpad_3fg_tap_btntool_pointerjump)
struct libinput *li = dev->libinput;
unsigned int button = 0;
const char *mapstr;
litest_test_param_fetch(test_env->params, "map", &mapstr);
const char *mapstr = litest_test_param_get_string(test_env->params, "map");
enum libinput_config_tap_button_map map = map_param_lookup(mapstr);
if (litest_slot_count(dev) > 3 ||

View file

@ -3618,8 +3618,7 @@ START_TEST(touchpad_initial_state)
struct libinput *libinput1, *libinput2;
int x = 40, y = 60;
const char *axisname;
litest_test_param_fetch(test_env->params, "axis", &axisname);
const char *axisname = litest_test_param_get_string(test_env->params, "axis");
int axis = libevdev_event_code_from_code_name(axisname);
litest_assert_int_ne(axis, -1);
@ -6943,8 +6942,7 @@ START_TEST(touchpad_suspend_abba)
struct litest_device *lid, *tabletmode, *extmouse;
struct libinput *li = tp->libinput;
const char *mode;
litest_test_param_fetch(test_env->params, "mode", &mode);
const char *mode = litest_test_param_get_string(test_env->params, "mode");
enum suspend first = mode_param_lookup(mode);
if (first == SUSPEND_EXT_MOUSE && litest_touchpad_is_external(tp))
@ -7085,8 +7083,7 @@ START_TEST(touchpad_suspend_abab)
struct litest_device *lid, *tabletmode, *extmouse;
struct libinput *li = tp->libinput;
const char *mode;
litest_test_param_fetch(test_env->params, "mode", &mode);
const char *mode = litest_test_param_get_string(test_env->params, "mode");
enum suspend first = mode_param_lookup(mode);
if (first == SUSPEND_EXT_MOUSE && litest_touchpad_is_external(tp))