mirror of
https://github.com/mollyim/webrtc.git
synced 2025-05-12 21:30:45 +01:00
Optional: Use nullopt and implicit construction in /rtc_base
Changes places where we explicitly construct an Optional to instead use nullopt or the requisite value type only. This CL was uploaded by git cl split. Bug: None Change-Id: I2c1e50090e48c892fda74f1678231a7624cac9fa Reviewed-on: https://webrtc-review.googlesource.com/23575 Commit-Queue: Oskar Sundbom <ossu@webrtc.org> Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Cr-Commit-Position: refs/heads/master@{#20884}
This commit is contained in:
parent
80adac053c
commit
9c78058f50
5 changed files with 42 additions and 44 deletions
|
@ -53,7 +53,7 @@ rtc::Optional<uint32_t> HistogramPercentileCounter::GetPercentile(
|
|||
RTC_CHECK_LE(fraction, 1.0);
|
||||
RTC_CHECK_GE(fraction, 0.0);
|
||||
if (total_elements_ == 0)
|
||||
return rtc::Optional<uint32_t>();
|
||||
return rtc::nullopt;
|
||||
size_t elements_to_skip = static_cast<size_t>(
|
||||
std::max(0.0f, std::ceil(total_elements_ * fraction) - 1));
|
||||
if (elements_to_skip >= total_elements_)
|
||||
|
@ -61,19 +61,19 @@ rtc::Optional<uint32_t> HistogramPercentileCounter::GetPercentile(
|
|||
if (elements_to_skip < total_elements_low_) {
|
||||
for (uint32_t value = 0; value < long_tail_boundary_; ++value) {
|
||||
if (elements_to_skip < histogram_low_[value])
|
||||
return rtc::Optional<uint32_t>(value);
|
||||
return value;
|
||||
elements_to_skip -= histogram_low_[value];
|
||||
}
|
||||
} else {
|
||||
elements_to_skip -= total_elements_low_;
|
||||
for (const auto& it : histogram_high_) {
|
||||
if (elements_to_skip < it.second)
|
||||
return rtc::Optional<uint32_t>(it.first);
|
||||
return it.first;
|
||||
elements_to_skip -= it.second;
|
||||
}
|
||||
}
|
||||
RTC_NOTREACHED();
|
||||
return rtc::Optional<uint32_t>();
|
||||
return rtc::nullopt;
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
|
|
@ -14,15 +14,15 @@
|
|||
TEST(MovingMaxCounter, ReportsMaximumInTheWindow) {
|
||||
rtc::MovingMaxCounter<int> counter(100);
|
||||
counter.Add(1, 1);
|
||||
EXPECT_EQ(counter.Max(1), rtc::Optional<int>(1));
|
||||
EXPECT_EQ(counter.Max(1), 1);
|
||||
counter.Add(2, 30);
|
||||
EXPECT_EQ(counter.Max(30), rtc::Optional<int>(2));
|
||||
EXPECT_EQ(counter.Max(30), 2);
|
||||
counter.Add(100, 60);
|
||||
EXPECT_EQ(counter.Max(60), rtc::Optional<int>(100));
|
||||
EXPECT_EQ(counter.Max(60), 100);
|
||||
counter.Add(4, 70);
|
||||
EXPECT_EQ(counter.Max(70), rtc::Optional<int>(100));
|
||||
EXPECT_EQ(counter.Max(70), 100);
|
||||
counter.Add(5, 90);
|
||||
EXPECT_EQ(counter.Max(90), rtc::Optional<int>(100));
|
||||
EXPECT_EQ(counter.Max(90), 100);
|
||||
}
|
||||
|
||||
TEST(MovingMaxCounter, IgnoresOldElements) {
|
||||
|
@ -32,9 +32,9 @@ TEST(MovingMaxCounter, IgnoresOldElements) {
|
|||
counter.Add(100, 60);
|
||||
counter.Add(4, 70);
|
||||
counter.Add(5, 90);
|
||||
EXPECT_EQ(counter.Max(160), rtc::Optional<int>(100));
|
||||
EXPECT_EQ(counter.Max(160), 100);
|
||||
// 100 is now out of the window. Next maximum is 5.
|
||||
EXPECT_EQ(counter.Max(161), rtc::Optional<int>(5));
|
||||
EXPECT_EQ(counter.Max(161), 5);
|
||||
}
|
||||
|
||||
TEST(MovingMaxCounter, HandlesEmptyWindow) {
|
||||
|
@ -47,11 +47,11 @@ TEST(MovingMaxCounter, HandlesEmptyWindow) {
|
|||
TEST(MovingMaxCounter, HandlesSamplesWithEqualTimestamps) {
|
||||
rtc::MovingMaxCounter<int> counter(100);
|
||||
counter.Add(2, 30);
|
||||
EXPECT_EQ(counter.Max(30), rtc::Optional<int>(2));
|
||||
EXPECT_EQ(counter.Max(30), 2);
|
||||
counter.Add(5, 30);
|
||||
EXPECT_EQ(counter.Max(30), rtc::Optional<int>(5));
|
||||
EXPECT_EQ(counter.Max(30), 5);
|
||||
counter.Add(4, 30);
|
||||
EXPECT_EQ(counter.Max(30), rtc::Optional<int>(5));
|
||||
EXPECT_EQ(counter.Max(30), 5);
|
||||
counter.Add(1, 90);
|
||||
EXPECT_EQ(counter.Max(150), rtc::Optional<int>(1));
|
||||
EXPECT_EQ(counter.Max(150), 1);
|
||||
}
|
||||
|
|
|
@ -23,10 +23,10 @@ rtc::Optional<signed_type> ParseSigned(const char* str, int base) {
|
|||
errno = 0;
|
||||
const signed_type value = std::strtoll(str, &end, base);
|
||||
if (end && *end == '\0' && errno == 0) {
|
||||
return rtc::Optional<signed_type>(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return rtc::Optional<signed_type>();
|
||||
return rtc::nullopt;
|
||||
}
|
||||
|
||||
rtc::Optional<unsigned_type> ParseUnsigned(const char* str, int base) {
|
||||
|
@ -40,10 +40,10 @@ rtc::Optional<unsigned_type> ParseUnsigned(const char* str, int base) {
|
|||
errno = 0;
|
||||
const unsigned_type value = std::strtoull(str, &end, base);
|
||||
if (end && *end == '\0' && errno == 0 && (value == 0 || !is_negative)) {
|
||||
return rtc::Optional<unsigned_type>(value);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return rtc::Optional<unsigned_type>();
|
||||
return rtc::nullopt;
|
||||
}
|
||||
|
||||
} // namespace string_to_number_internal
|
||||
|
|
|
@ -65,9 +65,9 @@ StringToNumber(const char* str, int base = 10) {
|
|||
string_to_number_internal::ParseSigned(str, base);
|
||||
if (value && *value >= std::numeric_limits<T>::lowest() &&
|
||||
*value <= std::numeric_limits<T>::max()) {
|
||||
return rtc::Optional<T>(static_cast<T>(*value));
|
||||
return static_cast<T>(*value);
|
||||
}
|
||||
return rtc::Optional<T>();
|
||||
return rtc::nullopt;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
|
@ -83,9 +83,9 @@ StringToNumber(const char* str, int base = 10) {
|
|||
rtc::Optional<unsigned_type> value =
|
||||
string_to_number_internal::ParseUnsigned(str, base);
|
||||
if (value && *value <= std::numeric_limits<T>::max()) {
|
||||
return rtc::Optional<T>(static_cast<T>(*value));
|
||||
return static_cast<T>(*value);
|
||||
}
|
||||
return rtc::Optional<T>();
|
||||
return rtc::nullopt;
|
||||
}
|
||||
|
||||
// The std::string overloads only exists if there is a matching const char*
|
||||
|
|
|
@ -67,10 +67,10 @@ TYPED_TEST_P(BasicNumberTest, TestInvalidNumbers) {
|
|||
(min_value == 0) ? "-2" : (std::to_string(min_value) + "1");
|
||||
// Make the large value approximately ten times larger than the maximum.
|
||||
const std::string too_large_string = std::to_string(max_value) + "1";
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_low_string.c_str()));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(too_large_string.c_str()));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_low_string));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_low_string.c_str()));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_large_string));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(too_large_string.c_str()));
|
||||
}
|
||||
|
||||
TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) {
|
||||
|
@ -78,20 +78,18 @@ TYPED_TEST_P(BasicNumberTest, TestInvalidInputs) {
|
|||
const char kInvalidCharArray[] = "Invalid string containing 47";
|
||||
const char kPlusMinusCharArray[] = "+-100";
|
||||
const char kNumberFollowedByCruft[] = "640x480";
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kInvalidCharArray));
|
||||
EXPECT_EQ(rtc::Optional<T>(),
|
||||
StringToNumber<T>(std::string(kInvalidCharArray)));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kPlusMinusCharArray));
|
||||
EXPECT_EQ(rtc::Optional<T>(),
|
||||
StringToNumber<T>(std::string(kPlusMinusCharArray)));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(kNumberFollowedByCruft));
|
||||
EXPECT_EQ(rtc::Optional<T>(),
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(kInvalidCharArray));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(std::string(kInvalidCharArray)));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(kPlusMinusCharArray));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(std::string(kPlusMinusCharArray)));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(kNumberFollowedByCruft));
|
||||
EXPECT_EQ(rtc::nullopt,
|
||||
StringToNumber<T>(std::string(kNumberFollowedByCruft)));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" 5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" - 5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>("- 5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>(" -5"));
|
||||
EXPECT_EQ(rtc::Optional<T>(), StringToNumber<T>("5 "));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(" 5"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(" - 5"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>("- 5"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>(" -5"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<T>("5 "));
|
||||
}
|
||||
|
||||
REGISTER_TYPED_TEST_CASE_P(BasicNumberTest,
|
||||
|
@ -106,10 +104,10 @@ INSTANTIATE_TYPED_TEST_CASE_P(StringToNumberTest_Integers,
|
|||
IntegerTypes);
|
||||
|
||||
TEST(StringToNumberTest, TestSpecificValues) {
|
||||
EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("256"));
|
||||
EXPECT_EQ(rtc::Optional<uint8_t>(), StringToNumber<uint8_t>("-256"));
|
||||
EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("256"));
|
||||
EXPECT_EQ(rtc::Optional<int8_t>(), StringToNumber<int8_t>("-256"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<uint8_t>("256"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<uint8_t>("-256"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<int8_t>("256"));
|
||||
EXPECT_EQ(rtc::nullopt, StringToNumber<int8_t>("-256"));
|
||||
}
|
||||
|
||||
} // namespace rtc
|
||||
|
|
Loading…
Reference in a new issue