diff --git a/api/units/data_rate.h b/api/units/data_rate.h index 5c421d4c43..c2202d6a69 100644 --- a/api/units/data_rate.h +++ b/api/units/data_rate.h @@ -133,7 +133,20 @@ class DataRate { return bits_per_sec_ == data_rate_impl::kPlusInfinityVal; } constexpr bool IsFinite() const { return !IsInfinite(); } - + DataRate operator-(const DataRate& other) const { + return DataRate::bps(bps() - other.bps()); + } + DataRate operator+(const DataRate& other) const { + return DataRate::bps(bps() + other.bps()); + } + DataRate& operator-=(const DataRate& other) { + *this = *this - other; + return *this; + } + DataRate& operator+=(const DataRate& other) { + *this = *this + other; + return *this; + } constexpr double operator/(const DataRate& other) const { return bps() / other.bps(); } diff --git a/api/units/data_rate_unittest.cc b/api/units/data_rate_unittest.cc index 9c91fd6247..be020909c8 100644 --- a/api/units/data_rate_unittest.cc +++ b/api/units/data_rate_unittest.cc @@ -100,11 +100,21 @@ TEST(DataRateTest, MathOperations) { const DataRate rate_b = DataRate::bps(kValueB); const int32_t kInt32Value = 123; const double kFloatValue = 123.0; + + EXPECT_EQ((rate_a + rate_b).bps(), kValueA + kValueB); + EXPECT_EQ((rate_a - rate_b).bps(), kValueA - kValueB); + EXPECT_EQ((rate_a * kValueB).bps(), kValueA * kValueB); EXPECT_EQ((rate_a * kInt32Value).bps(), kValueA * kInt32Value); EXPECT_EQ((rate_a * kFloatValue).bps(), kValueA * kFloatValue); EXPECT_EQ(rate_a / rate_b, static_cast(kValueA) / kValueB); + + DataRate mutable_rate = DataRate::bps(kValueA); + mutable_rate += rate_b; + EXPECT_EQ(mutable_rate.bps(), kValueA + kValueB); + mutable_rate -= rate_a; + EXPECT_EQ(mutable_rate.bps(), kValueB); } TEST(UnitConversionTest, DataRateAndDataSizeAndTimeDelta) { diff --git a/api/units/data_size.h b/api/units/data_size.h index ac61a6f5dd..8958b24697 100644 --- a/api/units/data_size.h +++ b/api/units/data_size.h @@ -95,11 +95,11 @@ class DataSize { return DataSize::bytes(bytes() + other.bytes()); } DataSize& operator-=(const DataSize& other) { - bytes_ -= other.bytes(); + *this = *this - other; return *this; } DataSize& operator+=(const DataSize& other) { - bytes_ += other.bytes(); + *this = *this + other; return *this; } constexpr double operator/(const DataSize& other) const {