ANIM
This commit is contained in:
@@ -99,12 +99,112 @@ static void test_animationUpdateLoopsWithMultipleChannels(void **state) {
|
||||
assert_float_equal(anim.time, 1.0f, 0.0001f);
|
||||
}
|
||||
|
||||
static uint32_t TEST_EVENT_FIRE_COUNT;
|
||||
static animation_t *TEST_EVENT_LAST_ANIM;
|
||||
static void *TEST_EVENT_LAST_USER;
|
||||
|
||||
static void test_resetEventCounters(void) {
|
||||
TEST_EVENT_FIRE_COUNT = 0;
|
||||
TEST_EVENT_LAST_ANIM = NULL;
|
||||
TEST_EVENT_LAST_USER = NULL;
|
||||
}
|
||||
|
||||
static void test_onAnimationEvent(animation_t *anim, void *user) {
|
||||
TEST_EVENT_FIRE_COUNT++;
|
||||
TEST_EVENT_LAST_ANIM = anim;
|
||||
TEST_EVENT_LAST_USER = user;
|
||||
}
|
||||
|
||||
static void test_animationEventFiresOnceWhenCrossed(void **state) {
|
||||
test_resetEventCounters();
|
||||
|
||||
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;
|
||||
|
||||
int32_t userValue = 7;
|
||||
animationSetEvent(&anim, 0.5f, test_onAnimationEvent, &userValue);
|
||||
|
||||
TIME.delta = 0.3f;
|
||||
animationUpdate(&anim);
|
||||
assert_int_equal(TEST_EVENT_FIRE_COUNT, 0);
|
||||
|
||||
// 0.3 -> 0.6 crosses the 0.5 event time.
|
||||
animationUpdate(&anim);
|
||||
assert_int_equal(TEST_EVENT_FIRE_COUNT, 1);
|
||||
assert_ptr_equal(TEST_EVENT_LAST_ANIM, &anim);
|
||||
assert_ptr_equal(TEST_EVENT_LAST_USER, &userValue);
|
||||
|
||||
// Doesn't refire on later ticks past the event time.
|
||||
animationUpdate(&anim);
|
||||
assert_int_equal(TEST_EVENT_FIRE_COUNT, 1);
|
||||
}
|
||||
|
||||
static void test_animationEventFiresEachLoopAcrossWrap(void **state) {
|
||||
test_resetEventCounters();
|
||||
|
||||
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.loop = true;
|
||||
anim.playing = true;
|
||||
animationSetEvent(&anim, 0.9f, test_onAnimationEvent, NULL);
|
||||
|
||||
TIME.delta = 0.95f;
|
||||
animationUpdate(&anim);
|
||||
assert_int_equal(TEST_EVENT_FIRE_COUNT, 1);
|
||||
assert_float_equal(anim.time, 0.95f, 0.0001f);
|
||||
|
||||
// 0.95 + 0.95 = 1.9 -> wraps to 0.9 against a 1.0s duration, crossing
|
||||
// the event time again on the way around.
|
||||
animationUpdate(&anim);
|
||||
assert_int_equal(TEST_EVENT_FIRE_COUNT, 2);
|
||||
assert_float_equal(anim.time, 0.9f, 0.0001f);
|
||||
}
|
||||
|
||||
static void test_animationEventClearedByNullCallback(void **state) {
|
||||
test_resetEventCounters();
|
||||
|
||||
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;
|
||||
animationSetEvent(&anim, 0.5f, test_onAnimationEvent, NULL);
|
||||
animationSetEvent(&anim, 0.5f, NULL, NULL);
|
||||
|
||||
TIME.delta = 1.0f;
|
||||
animationUpdate(&anim);
|
||||
assert_int_equal(TEST_EVENT_FIRE_COUNT, 0);
|
||||
}
|
||||
|
||||
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),
|
||||
cmocka_unit_test(test_animationEventFiresOnceWhenCrossed),
|
||||
cmocka_unit_test(test_animationEventFiresEachLoopAcrossWrap),
|
||||
cmocka_unit_test(test_animationEventClearedByNullCallback),
|
||||
};
|
||||
|
||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||
|
||||
Reference in New Issue
Block a user