blob: 69103bd0462aa6096ad4448da487e04e972c393f [file] [log] [blame]
Sebastian Jansson55251c32019-08-08 11:14:51 +02001/*
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;
19 bool ping = 0;
Sebastian Jansson55251c32019-08-08 11:14:51 +020020 absl::optional<TimeDelta> duration;
21 absl::optional<TimeDelta> latency = TimeDelta::ms(100);
Sebastian Jansson0ee80082019-08-14 13:16:26 +020022 std::unique_ptr<StructParametersParser> Parser();
Sebastian Jansson55251c32019-08-08 11:14:51 +020023};
24
Sebastian Jansson0ee80082019-08-14 13:16:26 +020025std::unique_ptr<StructParametersParser> DummyConfig::Parser() {
Sebastian Jansson55251c32019-08-08 11:14:51 +020026 // The empty comments ensures that each pair is on a separate line.
Sebastian Jansson0ee80082019-08-14 13:16:26 +020027 return StructParametersParser::Create("e", &enabled, //
28 "f", &factor, //
29 "r", &retries, //
30 "p", &ping, //
31 "d", &duration, //
32 "l", &latency);
Sebastian Jansson55251c32019-08-08 11:14:51 +020033}
34} // namespace
35
36TEST(StructParametersParserTest, ParsesValidParameters) {
Sebastian Jansson0ee80082019-08-14 13:16:26 +020037 DummyConfig exp;
38 exp.Parser()->Parse("e:1,f:-1.7,r:2,p:1,d:8,l:,");
Sebastian Jansson55251c32019-08-08 11:14:51 +020039 EXPECT_TRUE(exp.enabled);
40 EXPECT_EQ(exp.factor, -1.7);
41 EXPECT_EQ(exp.retries, 2);
42 EXPECT_EQ(exp.ping, true);
43 EXPECT_EQ(exp.duration.value().ms(), 8);
44 EXPECT_FALSE(exp.latency);
45}
46
47TEST(StructParametersParserTest, UsesDefaults) {
Sebastian Jansson0ee80082019-08-14 13:16:26 +020048 DummyConfig exp;
49 exp.Parser()->Parse("");
Sebastian Jansson55251c32019-08-08 11:14:51 +020050 EXPECT_FALSE(exp.enabled);
51 EXPECT_EQ(exp.factor, 0.5);
52 EXPECT_EQ(exp.retries, 5);
53 EXPECT_EQ(exp.ping, false);
Sebastian Jansson55251c32019-08-08 11:14:51 +020054}
55
56TEST(StructParametersParserTest, EncodeAll) {
57 DummyConfig exp;
Sebastian Jansson0ee80082019-08-14 13:16:26 +020058 auto encoded = exp.Parser()->Encode();
Sebastian Jansson55251c32019-08-08 11:14:51 +020059 // All parameters are encoded.
Sebastian Jansson0ee80082019-08-14 13:16:26 +020060 EXPECT_EQ(encoded, "e:false,f:0.5,r:5,p:false,d:,l:100 ms");
Sebastian Jansson55251c32019-08-08 11:14:51 +020061}
62
63} // namespace webrtc