blob: 71b117f9ddae1c597029be2853e313077346b6e4 [file] [log] [blame]
Sebastian Jansson55251c32019-08-08 09:14:511/*
2 * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10#include "rtc_base/experiments/struct_parameters_parser.h"
11#include "rtc_base/gunit.h"
12
13namespace webrtc {
14namespace {
15struct DummyConfig {
16 bool enabled = false;
17 double factor = 0.5;
18 int retries = 5;
Bjorn Terelius9f00f0e2019-08-30 07:39:3119 unsigned size = 3;
Sebastian Jansson55251c32019-08-08 09:14:5120 bool ping = 0;
Sebastian Jansson55251c32019-08-08 09:14:5121 absl::optional<TimeDelta> duration;
22 absl::optional<TimeDelta> latency = TimeDelta::ms(100);
Sebastian Jansson0ee80082019-08-14 11:16:2623 std::unique_ptr<StructParametersParser> Parser();
Sebastian Jansson55251c32019-08-08 09:14:5124};
25
Sebastian Jansson0ee80082019-08-14 11:16:2626std::unique_ptr<StructParametersParser> DummyConfig::Parser() {
Sebastian Jansson55251c32019-08-08 09:14:5127 // The empty comments ensures that each pair is on a separate line.
Sebastian Jansson0ee80082019-08-14 11:16:2628 return StructParametersParser::Create("e", &enabled, //
29 "f", &factor, //
30 "r", &retries, //
Bjorn Terelius9f00f0e2019-08-30 07:39:3131 "s", &size, //
Sebastian Jansson0ee80082019-08-14 11:16:2632 "p", &ping, //
33 "d", &duration, //
34 "l", &latency);
Sebastian Jansson55251c32019-08-08 09:14:5135}
36} // namespace
37
38TEST(StructParametersParserTest, ParsesValidParameters) {
Sebastian Jansson0ee80082019-08-14 11:16:2639 DummyConfig exp;
Bjorn Terelius9f00f0e2019-08-30 07:39:3140 exp.Parser()->Parse("e:1,f:-1.7,r:2,s:7,p:1,d:8,l:,");
Sebastian Jansson55251c32019-08-08 09:14:5141 EXPECT_TRUE(exp.enabled);
42 EXPECT_EQ(exp.factor, -1.7);
43 EXPECT_EQ(exp.retries, 2);
Bjorn Terelius9f00f0e2019-08-30 07:39:3144 EXPECT_EQ(exp.size, 7u);
Sebastian Jansson55251c32019-08-08 09:14:5145 EXPECT_EQ(exp.ping, true);
46 EXPECT_EQ(exp.duration.value().ms(), 8);
47 EXPECT_FALSE(exp.latency);
48}
49
50TEST(StructParametersParserTest, UsesDefaults) {
Sebastian Jansson0ee80082019-08-14 11:16:2651 DummyConfig exp;
52 exp.Parser()->Parse("");
Sebastian Jansson55251c32019-08-08 09:14:5153 EXPECT_FALSE(exp.enabled);
54 EXPECT_EQ(exp.factor, 0.5);
55 EXPECT_EQ(exp.retries, 5);
Bjorn Terelius9f00f0e2019-08-30 07:39:3156 EXPECT_EQ(exp.size, 3u);
Sebastian Jansson55251c32019-08-08 09:14:5157 EXPECT_EQ(exp.ping, false);
Sebastian Jansson55251c32019-08-08 09:14:5158}
59
60TEST(StructParametersParserTest, EncodeAll) {
61 DummyConfig exp;
Sebastian Jansson0ee80082019-08-14 11:16:2662 auto encoded = exp.Parser()->Encode();
Sebastian Jansson55251c32019-08-08 09:14:5163 // All parameters are encoded.
Bjorn Terelius9f00f0e2019-08-30 07:39:3164 EXPECT_EQ(encoded, "e:false,f:0.5,r:5,s:3,p:false,d:,l:100 ms");
Sebastian Jansson55251c32019-08-08 09:14:5165}
66
67} // namespace webrtc