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