Adds % unit to float field trial parser.

This can improve readability in some situations and prevents the need
to define a parameter as some_value_in_percent.

Bug: webrtc:9510
Change-Id: I0959d2b6c463f1bc1cea8e66f0bd5b56380b8c03
Reviewed-on: https://webrtc-review.googlesource.com/97302
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Sebastian Jansson <srte@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#24538}
This commit is contained in:
Sebastian Jansson
2018-09-03 11:31:26 +02:00
committed by Commit Bot
parent 5dd6167908
commit ec76466da2
2 changed files with 13 additions and 1 deletions

View File

@ -82,7 +82,10 @@ absl::optional<bool> ParseTypedParameter<bool>(std::string str) {
template <> template <>
absl::optional<double> ParseTypedParameter<double>(std::string str) { absl::optional<double> ParseTypedParameter<double>(std::string str) {
double value; double value;
if (sscanf(str.c_str(), "%lf", &value) == 1) { char unit[2]{0, 0};
if (sscanf(str.c_str(), "%lf%1s", &value, unit) >= 1) {
if (unit[0] == '%')
return value / 100;
return value; return value;
} else { } else {
return absl::nullopt; return absl::nullopt;

View File

@ -76,6 +76,15 @@ TEST(FieldTrialParserTest, CanHandleMixedInput) {
EXPECT_EQ(exp.ping.Get(), true); EXPECT_EQ(exp.ping.Get(), true);
EXPECT_EQ(exp.hash.Get(), ""); EXPECT_EQ(exp.hash.Get(), "");
} }
TEST(FieldTrialParserTest, ParsesDoubleParameter) {
FieldTrialParameter<double> double_param("f", 0.0);
ParseFieldTrial({&double_param}, "f:45%");
EXPECT_EQ(double_param.Get(), 0.45);
ParseFieldTrial({&double_param}, "f:34 %");
EXPECT_EQ(double_param.Get(), 0.34);
ParseFieldTrial({&double_param}, "f:0.67");
EXPECT_EQ(double_param.Get(), 0.67);
}
TEST(FieldTrialParserTest, IgnoresNewKey) { TEST(FieldTrialParserTest, IgnoresNewKey) {
DummyExperiment exp("Disabled,r:-11,foo"); DummyExperiment exp("Disabled,r:-11,foo");
EXPECT_FALSE(exp.enabled.Get()); EXPECT_FALSE(exp.enabled.Get());