libinput/src/util-multivalue.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

200 lines
4.1 KiB
C
Raw Normal View History

/*
* Copyright © 2024 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include "util-strings.h"
struct multivalue {
char type;
union {
char s[256];
char c;
double d;
bool b;
uint32_t u;
int32_t i;
} value;
char name[64];
};
static inline void
multivalue_extract(const struct multivalue *v, void *ptr)
{
test: implement support for parametrizing tests litest supports ranged tests but they are not enough, doubly so with tests where we want to parametrize across multiple options. This patch adds support for just that, in clunky C style. The typical invocation for a test is by giving the test parameter a name, a number of values and then the values themselves: struct litest_parameters *params = litest_parameters_new("axis", 's', 2, "ABS_X", "ABS_Y", "enabled", 'b', '2', true, false, "number", 'u', '2', 10, 11, NULL); litest_add_parametrized(sometest, LITEST_ANY, LITEST_ANY, params); litest_parameters_unref(params); Currently supported are u (uint32), i (int32), d (double), b (bool), c (char) and s (string). In the test itself, the `test_env->params` variable is available and retrieval of the parameters works like this: const char *axis; uint32_t number; bool enabled; litest_test_param_fetch(test_env->params, "axis", &axis, "enabled", &enabled, "number", &number, NULL); Note that since this is an effectively internal test-suite only functionality we don't do type-checking here, it's assumed that if you write the code to pass parameters into a test you remember the type of said params when you write the test code. Because we don't have hashmaps or anything useful other than lists the implementation is a bit clunky: we copy the parameter into the test during litest_add_*, permutate it for our test list which gives us yet another linked list C struct, and finally copy the actual value into the test and test environment as it's executed. Not pretty, but it works. A few tests are switched as simple demonstration. The name of the test has the parameters with their names and values appended now, e.g.: "pointer:pointer_scroll_wheel_hires_send_only_lores:ms-surface-cover:axis:ABS_X" "pointer:pointer_motion_relative_min_decel:mouse-roccat:direction:NW" Filtering by parameters can be done via globs of their string representation: libinput-test-suite --filter-params="axis:ABS_*,enabled:true,number:10*" Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1109>
2024-12-22 00:06:19 +10:00
/* ignore false positives from gcc:
* ../src/util-multivalue.h:52:33: warning: array subscript double[0] is
* partly outside array bounds of int32_t[1] {aka int[1]} [-Warray-bounds=]
* 52 | case 'd': *(double*)ptr = v->value.d; break;
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Warray-bounds"
switch (v->type) {
case 'b':
*(bool *)ptr = v->value.b;
break;
case 'c':
*(char *)ptr = v->value.c;
break;
case 'u':
*(uint32_t *)ptr = v->value.u;
break;
case 'i':
*(int32_t *)ptr = v->value.i;
break;
case 'd':
*(double *)ptr = v->value.d;
break;
case 's':
*(const char **)ptr = v->value.s;
break;
default:
abort();
}
test: implement support for parametrizing tests litest supports ranged tests but they are not enough, doubly so with tests where we want to parametrize across multiple options. This patch adds support for just that, in clunky C style. The typical invocation for a test is by giving the test parameter a name, a number of values and then the values themselves: struct litest_parameters *params = litest_parameters_new("axis", 's', 2, "ABS_X", "ABS_Y", "enabled", 'b', '2', true, false, "number", 'u', '2', 10, 11, NULL); litest_add_parametrized(sometest, LITEST_ANY, LITEST_ANY, params); litest_parameters_unref(params); Currently supported are u (uint32), i (int32), d (double), b (bool), c (char) and s (string). In the test itself, the `test_env->params` variable is available and retrieval of the parameters works like this: const char *axis; uint32_t number; bool enabled; litest_test_param_fetch(test_env->params, "axis", &axis, "enabled", &enabled, "number", &number, NULL); Note that since this is an effectively internal test-suite only functionality we don't do type-checking here, it's assumed that if you write the code to pass parameters into a test you remember the type of said params when you write the test code. Because we don't have hashmaps or anything useful other than lists the implementation is a bit clunky: we copy the parameter into the test during litest_add_*, permutate it for our test list which gives us yet another linked list C struct, and finally copy the actual value into the test and test environment as it's executed. Not pretty, but it works. A few tests are switched as simple demonstration. The name of the test has the parameters with their names and values appended now, e.g.: "pointer:pointer_scroll_wheel_hires_send_only_lores:ms-surface-cover:axis:ABS_X" "pointer:pointer_motion_relative_min_decel:mouse-roccat:direction:NW" Filtering by parameters can be done via globs of their string representation: libinput-test-suite --filter-params="axis:ABS_*,enabled:true,number:10*" Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1109>
2024-12-22 00:06:19 +10:00
#pragma GCC diagnostic pop
}
static inline void
multivalue_extract_typed(const struct multivalue *v, char type, void *ptr)
{
assert(type == v->type);
multivalue_extract(v, ptr);
}
static inline struct multivalue
multivalue_copy(const struct multivalue *v)
{
struct multivalue copy = *v;
return copy;
}
static inline struct multivalue
multivalue_new_string(const char *str)
{
struct multivalue v = { .type = 's' };
assert(strlen(str) < sizeof(v.value.s));
snprintf(v.value.s, sizeof(v.value.s), "%s", str);
return v;
}
static inline struct multivalue
multivalue_new_char(char c)
{
struct multivalue v = {
.type = 'c',
.value.c = c,
};
return v;
}
static inline struct multivalue
multivalue_new_double(double d)
{
struct multivalue v = {
.type = 'd',
.value.d = d,
};
return v;
}
static inline struct multivalue
multivalue_new_u32(uint32_t u)
{
struct multivalue v = {
.type = 'u',
.value.u = u,
};
return v;
}
static inline struct multivalue
multivalue_new_i32(int32_t i)
{
struct multivalue v = {
.type = 'i',
.value.i = i,
};
return v;
}
static inline struct multivalue
multivalue_new_bool(bool b)
{
struct multivalue v = {
.type = 'b',
.value.b = b,
};
return v;
}
static inline struct multivalue
multivalue_new_named_i32(int32_t value, const char *name)
{
struct multivalue v = multivalue_new_i32(value);
assert(strlen(name) < sizeof(v.name));
snprintf(v.name, sizeof(v.name), "%s", name);
return v;
}
static inline char *
multivalue_as_str(const struct multivalue *v)
{
char *str;
if (v->name[0])
return safe_strdup(v->name);
switch (v->type) {
case 'd':
xasprintf(&str, "%f", v->value.d);
break;
case 'u':
xasprintf(&str, "%u", v->value.u);
break;
case 'i':
xasprintf(&str, "%d", v->value.i);
break;
case 'b':
xasprintf(&str, "%s", truefalse(v->value.b));
break;
case 'c':
xasprintf(&str, "%c", v->value.c);
break;
case 's':
str = safe_strdup(v->value.s);
break;
default:
abort();
}
return str;
}