Adds arithmetic support to DataRate.

Bug: webrtc:9709
Change-Id: Id3fde2b7bbc63c6372cadc76b92c4568665b17f9
Reviewed-on: https://webrtc-review.googlesource.com/97702
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24562}
This commit is contained in:
Sebastian Jansson
2018-09-04 18:34:45 +02:00
committed by Commit Bot
parent 96816753d9
commit af21eab531
3 changed files with 26 additions and 3 deletions

View File

@ -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<double>() / other.bps<double>();
}

View File

@ -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<double>(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) {

View File

@ -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 {