blob: 1e88f8cfc1a378ebaf15389ca21b8b09872b59e0 [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;
20 std::string hash = "a80";
21 absl::optional<TimeDelta> duration;
22 absl::optional<TimeDelta> latency = TimeDelta::ms(100);
23 static StructParametersParser<DummyConfig>* Parser();
24};
25
26StructParametersParser<DummyConfig>* DummyConfig::Parser() {
27 using C = DummyConfig;
28 // The empty comments ensures that each pair is on a separate line.
29 static auto parser = CreateStructParametersParser(
30 "e", [](C* c) { return &c->enabled; }, //
31 "f", [](C* c) { return &c->factor; }, //
32 "r", [](C* c) { return &c->retries; }, //
33 "p", [](C* c) { return &c->ping; }, //
34 "h", [](C* c) { return &c->hash; }, //
35 "d", [](C* c) { return &c->duration; }, //
36 "l", [](C* c) { return &c->latency; }); //
37 return parser.get();
38}
39} // namespace
40
41TEST(StructParametersParserTest, ParsesValidParameters) {
42 DummyConfig exp =
43 DummyConfig::Parser()->Parse("e:1,f:-1.7,r:2,p:1,h:x7c,d:8,l:,");
44 EXPECT_TRUE(exp.enabled);
45 EXPECT_EQ(exp.factor, -1.7);
46 EXPECT_EQ(exp.retries, 2);
47 EXPECT_EQ(exp.ping, true);
48 EXPECT_EQ(exp.duration.value().ms(), 8);
49 EXPECT_FALSE(exp.latency);
50}
51
52TEST(StructParametersParserTest, UsesDefaults) {
53 DummyConfig exp = DummyConfig::Parser()->Parse("");
54 EXPECT_FALSE(exp.enabled);
55 EXPECT_EQ(exp.factor, 0.5);
56 EXPECT_EQ(exp.retries, 5);
57 EXPECT_EQ(exp.ping, false);
58 EXPECT_EQ(exp.hash, "a80");
59}
60
61TEST(StructParametersParserTest, EmptyDefaults) {
62 DummyConfig exp;
63 auto encoded = DummyConfig::Parser()->EncodeChanged(exp);
64 // Unchanged parameters are not encoded.
65 EXPECT_EQ(encoded, "");
66}
67
68TEST(StructParametersParserTest, EncodeAll) {
69 DummyConfig exp;
70 auto encoded = DummyConfig::Parser()->EncodeAll(exp);
71 // All parameters are encoded.
72 EXPECT_EQ(encoded, "d:,e:false,f:0.5,h:a80,l:100 ms,p:false,r:5");
73}
74
75TEST(StructParametersParserTest, EncodeChanged) {
76 DummyConfig exp;
77 exp.ping = true;
78 exp.retries = 4;
79 auto encoded = DummyConfig::Parser()->EncodeChanged(exp);
80 // We expect the changed parameters to be encoded in alphabetical order.
81 EXPECT_EQ(encoded, "p:true,r:4");
82}
83
84} // namespace webrtc