Animation: Expand unit tests for `BKE_fcurve_active_keyframe_index()`

Expand unit test for `BKE_fcurve_active_keyframe_index()` to test edge
cases better.

This also introduces a new test macro `EXPECT_BLI_ASSERT()`, which can be
used to test that an assertion fails successfully.

No functional changes to actual Blender code.
This commit is contained in:
Sybren A. Stüvel 2020-11-10 15:33:14 +01:00
parent 75a2db5d97
commit feb71f1d71
4 changed files with 45 additions and 10 deletions

View File

@ -5,11 +5,6 @@
#include "MEM_guardedalloc.h"
/* We expect to abort on integer overflow, to prevent possible exploits. */
#ifdef _WIN32
# define ABORT_PREDICATE ::testing::ExitedWithCode(3)
#else
# define ABORT_PREDICATE ::testing::KilledBySignal(SIGABRT)
#endif
#if defined(__GNUC__) && !defined(__clang__)
/* Disable since it's the purpose of this test. */

View File

@ -855,7 +855,7 @@ void BKE_fcurve_active_keyframe_set(FCurve *fcu, const BezTriple *active_bezt)
}
/* The active keyframe should always be selected. */
BLI_assert(BEZT_ISSEL_ANY(active_bezt));
BLI_assert(BEZT_ISSEL_ANY(active_bezt) || !"active keyframe must be selected");
fcu->active_keyframe_index = (int)offset;
}

View File

@ -22,6 +22,7 @@
#include "BKE_fcurve.h"
#include "ED_keyframing.h"
#include "ED_types.h" /* For SELECT. */
#include "DNA_anim_types.h"
@ -295,17 +296,38 @@ TEST(fcurve_active_keyframe, ActiveKeyframe)
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
/* Check a "normal" action. */
fcu->bezt[2].f2 |= SELECT;
BKE_fcurve_active_keyframe_set(fcu, &fcu->bezt[2]);
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), 2);
/* Check out of bounds. */
BKE_fcurve_active_keyframe_set(fcu, fcu->bezt - 20);
/* Check setting an unselected keyframe as active. */
fcu->bezt[2].f1 = fcu->bezt[2].f2 = fcu->bezt[2].f3 = 0;
EXPECT_BLI_ASSERT(BKE_fcurve_active_keyframe_set(fcu, &fcu->bezt[2]),
"active keyframe must be selected");
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
/* Check out of bounds again. */
BKE_fcurve_active_keyframe_set(fcu, fcu->bezt + 4);
/* Check out of bounds (lower). */
BKE_fcurve_active_keyframe_set(fcu, fcu->bezt - 20);
EXPECT_EQ(fcu->active_keyframe_index, FCURVE_ACTIVE_KEYFRAME_NONE)
<< "Setting out-of-bounds value via the API should result in valid active_keyframe_index";
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
fcu->active_keyframe_index = -20;
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE)
<< "Even with active_keyframe_index out of bounds, getting it via the API should produce a "
"valid value";
/* Check out of bounds (higher). */
BKE_fcurve_active_keyframe_set(fcu, fcu->bezt + 4);
EXPECT_EQ(fcu->active_keyframe_index, FCURVE_ACTIVE_KEYFRAME_NONE)
<< "Setting out-of-bounds value via the API should result in valid active_keyframe_index";
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE);
fcu->active_keyframe_index = fcu->totvert;
EXPECT_EQ(BKE_fcurve_active_keyframe_index(fcu), FCURVE_ACTIVE_KEYFRAME_NONE)
<< "Even with active_keyframe_index out of bounds, getting it via the API should produce a "
"valid value";
BKE_fcurve_free(fcu);
}

View File

@ -137,4 +137,22 @@ inline void EXPECT_EQ_ARRAY_ND(const T *expected, const T *actual, const size_t
}
}
#ifdef _WIN32
# define ABORT_PREDICATE ::testing::ExitedWithCode(3)
#else
# define ABORT_PREDICATE ::testing::KilledBySignal(SIGABRT)
#endif
/* Test macro for when BLI_assert() is expected to fail.
* Note that the EXPECT_BLI_ASSERT macro is a no-op, unless used in a debug build with
* WITH_ASSERT_ABORT=ON. */
#if defined(WITH_ASSERT_ABORT) && !defined(NDEBUG)
/* EXPECT_EXIT() is used as that's the only exit-expecting function in GTest that allows us to
* check for SIGABRT. */
# define EXPECT_BLI_ASSERT(function_call, expect_message) \
EXPECT_EXIT(function_call, ABORT_PREDICATE, expect_message)
#else
# define EXPECT_BLI_ASSERT(function_call, expect_message) function_call
#endif
#endif // __BLENDER_TESTING_H__