Add test for multiple function handling

This commit is contained in:
Kovid Goyal
2024-07-17 21:23:29 +05:30
parent 9c75ea795d
commit 21e19a90f4
2 changed files with 12 additions and 7 deletions

View File

@@ -179,7 +179,7 @@ apply_easing_curve(const Animation *a, double val, monotonic_t duration) {
if (!a->count) return val; if (!a->count) return val;
size_t idx = MIN((size_t)(val * a->count), a->count - 1); size_t idx = MIN((size_t)(val * a->count), a->count - 1);
animation_function *f = a->functions + idx; animation_function *f = a->functions + idx;
double interval_size = 1. / a->count, interval_start = val - idx * interval_size; double interval_size = 1. / a->count, interval_start = idx * interval_size;
val = (val - interval_start) / interval_size; val = (val - interval_start) / interval_size;
double ans = f->curve(&f->params, val, duration); double ans = f->curve(&f->params, val, duration);
return f->y_at_start + unit_value(ans) * f->y_size; return f->y_at_start + unit_value(ans) * f->y_size;
@@ -282,12 +282,15 @@ test_cursor_blink_easing_function(PyObject *self UNUSED, PyObject *args) {
PyErr_SetString(PyExc_RuntimeError, "must set a cursor blink animation on the global options object first"); PyErr_SetString(PyExc_RuntimeError, "must set a cursor blink animation on the global options object first");
return NULL; return NULL;
} }
double t, duration_s = 0.5; double t, duration_s = 0.5; int only_single = 1;
if (!PyArg_ParseTuple(args, "d|d", &t, &duration_s)) return NULL; if (!PyArg_ParseTuple(args, "d|pd", &t, &only_single, &duration_s)) return NULL;
monotonic_t duration = s_double_to_monotonic_t(duration_s); monotonic_t duration = s_double_to_monotonic_t(duration_s);
if (only_single) {
animation_function f = a->functions[0]; animation_function f = a->functions[0];
return PyFloat_FromDouble(f.curve(f.params, t, duration)); return PyFloat_FromDouble(f.curve(f.params, t, duration));
} }
return PyFloat_FromDouble(apply_easing_curve(a, t, duration));
}
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
METHODB(test_cursor_blink_easing_function, METH_VARARGS), METHODB(test_cursor_blink_easing_function, METH_VARARGS),

View File

@@ -157,14 +157,16 @@ class TestConfParsing(BaseTest):
cb('linear(0, 0.25 25% 75%, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.25, 0.75, 1.0), linear_y=(0, 0.25, 0.25, 1.0))) cb('linear(0, 0.25 25% 75%, 1)', first=EasingFunction('linear', linear_x=(0.0, 0.25, 0.75, 1.0), linear_y=(0, 0.25, 0.25, 1.0)))
# test that easing functions give expected values # test that easing functions give expected values
def ef(spec, tests, duration=0.5, accuracy=0): def ef(spec, tests, only_single=True, duration=0.5, accuracy=0):
cfv = p('cursor_blink_interval ' + spec).cursor_blink_interval cfv = p('cursor_blink_interval ' + spec).cursor_blink_interval
self.set_options({'cursor_blink_interval': cfv}) self.set_options({'cursor_blink_interval': cfv})
for t, expected in tests.items(): for t, expected in tests.items():
actual = test_cursor_blink_easing_function(t, duration) actual = test_cursor_blink_easing_function(t, only_single, duration)
if abs(actual - expected) > accuracy: if abs(actual - expected) > accuracy:
self.ae(expected, actual, f'Failed for {spec=} with {t=}: {expected} != {actual}') self.ae(expected, actual, f'Failed for {spec=} with {t=}: {expected} != {actual}')
ef('linear', {0:1, 0.25: 0.5, 0.5: 0, 0.75: 0.5, 1: 1}, only_single=False)
ef('linear(0, 0.25 25% 75%, 1)', {0: 0, 0.25: 0.25, 0.3: 0.25, 0.75: 0.25, 1:1}) ef('linear(0, 0.25 25% 75%, 1)', {0: 0, 0.25: 0.25, 0.3: 0.25, 0.75: 0.25, 1:1})
linear_vals = {0: 0, 1: 1, 0.1234: 0.1234, 0.6453: 0.6453} linear_vals = {0: 0, 1: 1, 0.1234: 0.1234, 0.6453: 0.6453}
for spec in ('linear', 'linear(0, 1)', 'cubic-bezier(0, 0, 1, 1)', 'cubic-bezier(0.2, 0.2, 0.7, 0.7)'): for spec in ('linear', 'linear(0, 1)', 'cubic-bezier(0, 0, 1, 1)', 'cubic-bezier(0.2, 0.2, 0.7, 0.7)'):