Make Unit types factories from float numbers be constexpr

std::isnan is not constexpr until c++23 and thus prevents constexpr construction from float numbers of unit types like DataRate
That check is not needed because is redundant with later check that value is convertable to int64_t

Bug: None
Change-Id: Ie8a06287492eb2122edf23fed63f1cd1bcd9df3d
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/355220
Auto-Submit: Danil Chapovalov <danilchap@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42520}
This commit is contained in:
Danil Chapovalov 2024-06-20 15:51:47 +02:00 committed by WebRTC LUCI CQ
parent e226676a16
commit 3a45801d34
2 changed files with 17 additions and 1 deletions

View File

@ -110,7 +110,6 @@ class UnitBase {
} else if (value == -std::numeric_limits<T>::infinity()) { } else if (value == -std::numeric_limits<T>::infinity()) {
return MinusInfinity(); return MinusInfinity();
} else { } else {
RTC_DCHECK(!std::isnan(value));
return FromValue(rtc::dchecked_cast<int64_t>(value)); return FromValue(rtc::dchecked_cast<int64_t>(value));
} }
} }

View File

@ -54,11 +54,19 @@ TEST(UnitBaseTest, ConstExpr) {
constexpr TestUnit kTestUnitZero = TestUnit::Zero(); constexpr TestUnit kTestUnitZero = TestUnit::Zero();
constexpr TestUnit kTestUnitPlusInf = TestUnit::PlusInfinity(); constexpr TestUnit kTestUnitPlusInf = TestUnit::PlusInfinity();
constexpr TestUnit kTestUnitMinusInf = TestUnit::MinusInfinity(); constexpr TestUnit kTestUnitMinusInf = TestUnit::MinusInfinity();
static_assert(kTestUnitZero.IsZero(), ""); static_assert(kTestUnitZero.IsZero(), "");
static_assert(kTestUnitPlusInf.IsPlusInfinity(), ""); static_assert(kTestUnitPlusInf.IsPlusInfinity(), "");
static_assert(kTestUnitMinusInf.IsMinusInfinity(), ""); static_assert(kTestUnitMinusInf.IsMinusInfinity(), "");
static_assert(kTestUnitPlusInf.ToKiloOr(-1) == -1, ""); static_assert(kTestUnitPlusInf.ToKiloOr(-1) == -1, "");
// Check FromValue is constexpr for floats.
static_assert(TestUnit::FromValue(0.0).IsZero());
static_assert(TestUnit::FromValue(INFINITY).IsPlusInfinity());
static_assert(TestUnit::FromValue(-INFINITY).IsMinusInfinity());
static_assert(TestUnit::FromValue(250.0) == TestUnit::FromValue(250));
static_assert(TestUnit::FromValue(-250.0) == TestUnit::FromValue(-250));
static_assert(kTestUnitPlusInf > kTestUnitZero, ""); static_assert(kTestUnitPlusInf > kTestUnitZero, "");
constexpr TestUnit kTestUnitKilo = TestUnit::FromKilo(kValue); constexpr TestUnit kTestUnitKilo = TestUnit::FromKilo(kValue);
@ -69,6 +77,7 @@ TEST(UnitBaseTest, ConstExpr) {
static_assert(TestUnitAddKilo(kTestUnitValue, 2).ToValue() == kValue + 2000, static_assert(TestUnitAddKilo(kTestUnitValue, 2).ToValue() == kValue + 2000,
""); "");
static_assert(TestUnit::FromValue(500) / 2 == TestUnit::FromValue(250)); static_assert(TestUnit::FromValue(500) / 2 == TestUnit::FromValue(250));
static_assert(TestUnit::FromValue(500.0) / 2 == TestUnit::FromValue(250.0));
} }
TEST(UnitBaseTest, GetBackSameValues) { TEST(UnitBaseTest, GetBackSameValues) {
@ -223,6 +232,14 @@ TEST(UnitBaseTest, MathOperations) {
EXPECT_EQ(TestUnit::FromValue(-789) / 10, TestUnit::FromValue(-78)); EXPECT_EQ(TestUnit::FromValue(-789) / 10, TestUnit::FromValue(-78));
} }
#if GTEST_HAS_DEATH_TEST && RTC_DCHECK_IS_ON && !defined(WEBRTC_ANDROID)
TEST(UnitBaseTest, CrashesWhenCreatedFromNan) {
EXPECT_DEATH(TestUnit::FromValue(NAN), "");
EXPECT_DEATH(TestUnit::FromValue(0.0 / 0.0), "");
EXPECT_DEATH(TestUnit::FromValue(INFINITY - INFINITY), "");
}
#endif
TEST(UnitBaseTest, InfinityOperations) { TEST(UnitBaseTest, InfinityOperations) {
const int64_t kValue = 267; const int64_t kValue = 267;
const TestUnit finite = TestUnit::FromKilo(kValue); const TestUnit finite = TestUnit::FromKilo(kValue);