Files
dusk/test/animation/test_animation.c
T

112 lines
3.4 KiB
C

/**
* Copyright (c) 2026 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "dusktest.h"
#include "time/time.h"
#include "animation/animation.h"
static void test_animationInitDefaults(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { keyframes };
uint16_t trackCounts[] = { 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 1);
assert_float_equal(anim.time, 0.0f, 0.0001f);
assert_float_equal(anim.speed, 1.0f, 0.0001f);
assert_false(anim.loop);
assert_false(anim.playing);
assert_float_equal(animationGetValue(&anim, 0), 0.0f, 0.0001f);
}
static void test_animationUpdateNoopWhenNotPlaying(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { keyframes };
uint16_t trackCounts[] = { 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 1);
TIME.delta = 1.0f;
animationUpdate(&anim);
assert_float_equal(anim.time, 0.0f, 0.0001f);
}
static void test_animationUpdateStopsAtEndWhenNotLooping(void **state) {
keyframe_t keyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { keyframes };
uint16_t trackCounts[] = { 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 1);
anim.playing = true;
TIME.delta = 0.5f;
animationUpdate(&anim);
assert_true(anim.playing);
assert_float_equal(animationGetValue(&anim, 0), 5.0f, 0.0001f);
animationUpdate(&anim);
assert_false(anim.playing);
assert_float_equal(animationGetValue(&anim, 0), 10.0f, 0.0001f);
// Stays clamped/held once stopped, even if updated again.
animationUpdate(&anim);
assert_float_equal(anim.time, 1.0f, 0.0001f);
}
static void test_animationUpdateLoopsWithMultipleChannels(void **state) {
keyframe_t xKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 1.0f, .value = 10.0f, .easing = EASING_LINEAR }
};
keyframe_t yKeyframes[] = {
{ .time = 0.0f, .value = 0.0f, .easing = EASING_LINEAR },
{ .time = 2.0f, .value = 100.0f, .easing = EASING_LINEAR }
};
keyframe_t *tracks[] = { xKeyframes, yKeyframes };
uint16_t trackCounts[] = { 2, 2 };
animation_t anim;
animationInit(&anim, tracks, trackCounts, 2);
anim.loop = true;
anim.playing = true;
// Duration is the longer (Y) channel's 2s -- looping wraps against
// that, not X's shorter 1s.
TIME.delta = 1.5f;
animationUpdate(&anim);
assert_true(anim.playing);
assert_float_equal(anim.time, 1.5f, 0.0001f);
animationUpdate(&anim);
// 1.5 + 1.5 = 3.0 -> wraps to 1.0 against a 2.0s duration.
assert_true(anim.playing);
assert_float_equal(anim.time, 1.0f, 0.0001f);
}
int main(int argc, char **argv) {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_animationInitDefaults),
cmocka_unit_test(test_animationUpdateNoopWhenNotPlaying),
cmocka_unit_test(test_animationUpdateStopsAtEndWhenNotLooping),
cmocka_unit_test(test_animationUpdateLoopsWithMultipleChannels),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}