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