/** * Copyright (c) 2026 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #include "dusktest.h" #include "animation/keyframe.h" static void test_keyframeGetValueInterpolatesLinear(void **state) { keyframe_t keyframes[] = { { .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR }, { .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR } }; assert_float_equal(keyframeGetValue(keyframes, 2, 0.5f), 5.0f, 0.0001f); } static void test_keyframeGetValueClampsAtBoundaries(void **state) { keyframe_t keyframes[] = { { .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR }, { .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR } }; // Exactly on, and beyond, the final keyframe: holds its value rather // than dividing by zero (start == end at that boundary). assert_float_equal(keyframeGetValue(keyframes, 2, 1.0f), 10.0f, 0.0001f); assert_float_equal(keyframeGetValue(keyframes, 2, 5.0f), 10.0f, 0.0001f); // Exactly on the first keyframe: same boundary case at the start. assert_float_equal(keyframeGetValue(keyframes, 2, 0.0f), 0.0f, 0.0001f); } static void test_keyframeGetValueRespectsEasing(void **state) { keyframe_t keyframes[] = { { .time = 0.0f, .value = 0.0f, .easing = EASING_IN_QUAD }, { .time = 1.0f, .value = 10.0f, .easing = EASING_IN_QUAD } }; // easingInQuad(0.5) = 0.25 -> lerp(0, 10, 0.25) = 2.5, not the linear // midpoint (5.0). assert_float_equal(keyframeGetValue(keyframes, 2, 0.5f), 2.5f, 0.0001f); } static void test_keyframeGetValueWithThreeKeyframes(void **state) { keyframe_t keyframes[] = { { .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR }, { .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }, { .time = 2.0f, .value = 0.0f, .easing = EASING_LINEAR } }; assert_float_equal(keyframeGetValue(keyframes, 3, 1.0f), 10.0f, 0.0001f); assert_float_equal(keyframeGetValue(keyframes, 3, 1.5f), 5.0f, 0.0001f); } int main(int argc, char **argv) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_keyframeGetValueInterpolatesLinear), cmocka_unit_test(test_keyframeGetValueClampsAtBoundaries), cmocka_unit_test(test_keyframeGetValueRespectsEasing), cmocka_unit_test(test_keyframeGetValueWithThreeKeyframes), }; return cmocka_run_group_tests(tests, NULL, NULL); }