1 /*
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 
13 namespace webrtc {
14 namespace {
15 struct DummyConfig {
16   bool enabled = false;
17   double factor = 0.5;
18   int retries = 5;
19   unsigned size = 3;
20   bool ping = 0;
21   absl::optional<TimeDelta> duration;
22   absl::optional<TimeDelta> latency = TimeDelta::Millis(100);
23   std::unique_ptr<StructParametersParser> Parser();
24 };
25 
Parser()26 std::unique_ptr<StructParametersParser> DummyConfig::Parser() {
27   // The empty comments ensures that each pair is on a separate line.
28   return StructParametersParser::Create("e", &enabled,   //
29                                         "f", &factor,    //
30                                         "r", &retries,   //
31                                         "s", &size,      //
32                                         "p", &ping,      //
33                                         "d", &duration,  //
34                                         "l", &latency);
35 }
36 }  // namespace
37 
TEST(StructParametersParserTest,ParsesValidParameters)38 TEST(StructParametersParserTest, ParsesValidParameters) {
39   DummyConfig exp;
40   exp.Parser()->Parse("e:1,f:-1.7,r:2,s:7,p:1,d:8,l:,");
41   EXPECT_TRUE(exp.enabled);
42   EXPECT_EQ(exp.factor, -1.7);
43   EXPECT_EQ(exp.retries, 2);
44   EXPECT_EQ(exp.size, 7u);
45   EXPECT_EQ(exp.ping, true);
46   EXPECT_EQ(exp.duration.value().ms(), 8);
47   EXPECT_FALSE(exp.latency);
48 }
49 
TEST(StructParametersParserTest,UsesDefaults)50 TEST(StructParametersParserTest, UsesDefaults) {
51   DummyConfig exp;
52   exp.Parser()->Parse("");
53   EXPECT_FALSE(exp.enabled);
54   EXPECT_EQ(exp.factor, 0.5);
55   EXPECT_EQ(exp.retries, 5);
56   EXPECT_EQ(exp.size, 3u);
57   EXPECT_EQ(exp.ping, false);
58 }
59 
TEST(StructParametersParserTest,EncodeAll)60 TEST(StructParametersParserTest, EncodeAll) {
61   DummyConfig exp;
62   auto encoded = exp.Parser()->Encode();
63   // All parameters are encoded.
64   EXPECT_EQ(encoded, "e:false,f:0.5,r:5,s:3,p:false,d:,l:100 ms");
65 }
66 
67 }  // namespace webrtc
68