From af21eab53135f932ae21e5d754c25e5627a67f21 Mon Sep 17 00:00:00 2001 From: Sebastian Jansson Date: Tue, 4 Sep 2018 18:34:45 +0200 Subject: [PATCH] Adds arithmetic support to DataRate. Bug: webrtc:9709 Change-Id: Id3fde2b7bbc63c6372cadc76b92c4568665b17f9 Reviewed-on: https://webrtc-review.googlesource.com/97702 Reviewed-by: Karl Wiberg Commit-Queue: Sebastian Jansson Cr-Commit-Position: refs/heads/master@{#24562} --- api/units/data_rate.h | 15 ++++++++++++++- api/units/data_rate_unittest.cc | 10 ++++++++++ api/units/data_size.h | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) 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 {