Functions: add sign from boolean function with unit tests

This commit is contained in:
Matthias Grob
2022-03-09 20:20:13 +01:00
parent e03585ca06
commit 74c0ae6e55
2 changed files with 20 additions and 0 deletions
+11
View File
@@ -54,6 +54,17 @@ int signNoZero(T val)
return (T(0) <= val) - (val < T(0));
}
/**
* Sign function based on a boolean
*
* @param[in] positive Truth value to take the sign from
* @return 1 if positive is true, -1 if positive is false
*/
inline int signFromBool(bool positive)
{
return positive ? 1 : -1;
}
template<typename T>
T sq(T val)
{
+9
View File
@@ -47,6 +47,15 @@ TEST(FunctionsTest, signNoZero)
EXPECT_FLOAT_EQ(signNoZero(123.456f), 1.f);
}
TEST(FunctionsTest, signFromBool)
{
EXPECT_EQ(signFromBool(true), 1);
EXPECT_EQ(signFromBool(false), -1);
EXPECT_EQ(signFromBool(100), 1);
EXPECT_EQ(signFromBool(-100), 1);
EXPECT_EQ(signFromBool(0), -1);
}
TEST(FunctionsTest, expo)
{
// input value limits