test-runner: Add ability to perform basic divisions and multiplications
We may need to compare monitor size to fractions and while it can be done using inverse divisions, they were not supported as we didn't parse floating point values. So, just add support for both multiplications and divisions, so that we can easily do stuff like `MONITOR_HEIGHT*3/4`. Additions would be easy to support too if we don't care about operator priorities, but that's out of scope for now. Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3701>
This commit is contained in:
parent
5e6aee6367
commit
8dc6115277
1 changed files with 52 additions and 8 deletions
|
@ -429,22 +429,66 @@ test_case_check_xserver_stacking (TestCase *test,
|
|||
}
|
||||
|
||||
static int
|
||||
maybe_divide (const char *str,
|
||||
int value)
|
||||
maybe_multiply (const char *str,
|
||||
int value,
|
||||
const char **out_str)
|
||||
{
|
||||
if (strstr (str, "/") == str)
|
||||
*out_str = str;
|
||||
|
||||
if (str[0] == '*')
|
||||
{
|
||||
int divisor;
|
||||
double multiplier;
|
||||
|
||||
str += 1;
|
||||
divisor = atoi (str);
|
||||
multiplier = g_strtod (str, (char **) out_str);
|
||||
|
||||
value /= divisor;
|
||||
value = round (multiplier * value);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static int
|
||||
maybe_divide (const char *str,
|
||||
int value,
|
||||
const char **out_str)
|
||||
{
|
||||
*out_str = str;
|
||||
|
||||
if (str[0] == '/')
|
||||
{
|
||||
double divider;
|
||||
|
||||
str += 1;
|
||||
divider = g_strtod (str, (char **) out_str);
|
||||
|
||||
value = round (value / divider);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static int
|
||||
maybe_do_math (const char *str,
|
||||
int value,
|
||||
const char **out_str)
|
||||
{
|
||||
switch (str[0])
|
||||
{
|
||||
case '*':
|
||||
value = maybe_multiply (str, value, &str);
|
||||
break;
|
||||
case '/':
|
||||
value = maybe_divide (str, value, &str);
|
||||
break;
|
||||
default:
|
||||
*out_str = str;
|
||||
return value;
|
||||
}
|
||||
|
||||
return maybe_do_math (str, value, out_str);
|
||||
}
|
||||
|
||||
static int
|
||||
parse_window_size (MetaWindow *window,
|
||||
const char *size_str)
|
||||
|
@ -462,13 +506,13 @@ parse_window_size (MetaWindow *window,
|
|||
{
|
||||
value = logical_monitor_layout.width;
|
||||
size_str += strlen ("MONITOR_WIDTH");
|
||||
value = maybe_divide (size_str, value);
|
||||
value = maybe_do_math (size_str, value, &size_str);
|
||||
}
|
||||
else if (strstr (size_str, "MONITOR_HEIGHT") == size_str)
|
||||
{
|
||||
value = logical_monitor_layout.height;
|
||||
size_str += strlen ("MONITOR_HEIGHT");
|
||||
value = maybe_divide (size_str, value);
|
||||
value = maybe_do_math (size_str, value, &size_str);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue