Adopt absl::string_view in field trial test helpers

Bug: webrtc:13579
Change-Id: Ie16b2f1cf5288cf795ea6d40f4b3a37f76f00f76
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/258422
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36505}
This commit is contained in:
Danil Chapovalov 2022-04-08 16:01:50 +02:00 committed by WebRTC LUCI CQ
parent 1397c4bfd9
commit 385b6c5460
7 changed files with 37 additions and 29 deletions

View file

@ -222,6 +222,7 @@ rtc_library("field_trial") {
"field_trial.h",
]
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
deps = [
"../rtc_base:checks",
"../system_wrappers:field_trial",

View file

@ -10,13 +10,14 @@
#include "test/explicit_key_value_config.h"
#include "absl/strings/string_view.h"
#include "api/field_trials_view.h"
#include "rtc_base/checks.h"
namespace webrtc {
namespace test {
ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) {
ExplicitKeyValueConfig::ExplicitKeyValueConfig(absl::string_view s) {
std::string::size_type field_start = 0;
while (field_start < s.size()) {
std::string::size_type separator_pos = s.find('/', field_start);
@ -24,7 +25,7 @@ ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) {
<< "Missing separator '/' after field trial key.";
RTC_CHECK_GT(separator_pos, field_start)
<< "Field trial key cannot be empty.";
std::string key = s.substr(field_start, separator_pos - field_start);
std::string key(s.substr(field_start, separator_pos - field_start));
field_start = separator_pos + 1;
RTC_CHECK_LT(field_start, s.size())
@ -34,7 +35,7 @@ ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) {
<< "Missing terminating '/' in field trial string.";
RTC_CHECK_GT(separator_pos, field_start)
<< "Field trial value cannot be empty.";
std::string value = s.substr(field_start, separator_pos - field_start);
std::string value(s.substr(field_start, separator_pos - field_start));
field_start = separator_pos + 1;
key_value_map_[key] = value;
@ -46,7 +47,7 @@ ExplicitKeyValueConfig::ExplicitKeyValueConfig(const std::string& s) {
}
std::string ExplicitKeyValueConfig::Lookup(absl::string_view key) const {
auto it = key_value_map_.find(std::string(key));
auto it = key_value_map_.find(key);
if (it != key_value_map_.end())
return it->second;
return "";

View file

@ -11,6 +11,7 @@
#ifndef TEST_EXPLICIT_KEY_VALUE_CONFIG_H_
#define TEST_EXPLICIT_KEY_VALUE_CONFIG_H_
#include <functional>
#include <map>
#include <string>
@ -22,11 +23,13 @@ namespace test {
class ExplicitKeyValueConfig : public FieldTrialsView {
public:
explicit ExplicitKeyValueConfig(const std::string& s);
explicit ExplicitKeyValueConfig(absl::string_view s);
std::string Lookup(absl::string_view key) const override;
private:
std::map<std::string, std::string> key_value_map_;
// Unlike std::less<std::string>, std::less<> is transparent and allows
// heterogeneous lookup directly with absl::string_view.
std::map<std::string, std::string, std::less<>> key_value_map_;
};
} // namespace test

View file

@ -10,23 +10,21 @@
#include "test/field_trial.h"
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <string>
#include "absl/strings/string_view.h"
#include "rtc_base/checks.h"
#include "system_wrappers/include/field_trial.h"
namespace webrtc {
namespace test {
ScopedFieldTrials::ScopedFieldTrials(const std::string& config)
: previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) {
RTC_CHECK(webrtc::field_trial::FieldTrialsStringIsValid(config.c_str()))
<< "Invalid field trials string: " << config;
current_field_trials_ = config;
ScopedFieldTrials::ScopedFieldTrials(absl::string_view config)
: current_field_trials_(config),
previous_field_trials_(webrtc::field_trial::GetFieldTrialString()) {
RTC_CHECK(webrtc::field_trial::FieldTrialsStringIsValid(
current_field_trials_.c_str()))
<< "Invalid field trials string: " << current_field_trials_;
webrtc::field_trial::InitFieldTrialsFromString(current_field_trials_.c_str());
}

View file

@ -11,9 +11,10 @@
#ifndef TEST_FIELD_TRIAL_H_
#define TEST_FIELD_TRIAL_H_
#include <map>
#include <string>
#include "absl/strings/string_view.h"
namespace webrtc {
namespace test {
@ -21,7 +22,7 @@ namespace test {
// After this class goes out of scope previous field trials will be restored.
class ScopedFieldTrials {
public:
explicit ScopedFieldTrials(const std::string& config);
explicit ScopedFieldTrials(absl::string_view config);
ScopedFieldTrials(const ScopedFieldTrials&) = delete;
ScopedFieldTrials& operator=(const ScopedFieldTrials&) = delete;
~ScopedFieldTrials();

View file

@ -18,8 +18,9 @@
namespace {
// This part is copied from system_wrappers/field_trial.cc.
void InsertIntoMap(std::map<std::string, std::string>& key_value_map,
const std::string& s) {
void InsertIntoMap(
std::map<std::string, std::string, std::less<>>& key_value_map,
absl::string_view s) {
std::string::size_type field_start = 0;
while (field_start < s.size()) {
std::string::size_type separator_pos = s.find('/', field_start);
@ -27,7 +28,7 @@ void InsertIntoMap(std::map<std::string, std::string>& key_value_map,
<< "Missing separator '/' after field trial key.";
RTC_CHECK_GT(separator_pos, field_start)
<< "Field trial key cannot be empty.";
std::string key = s.substr(field_start, separator_pos - field_start);
std::string key(s.substr(field_start, separator_pos - field_start));
field_start = separator_pos + 1;
RTC_CHECK_LT(field_start, s.size())
@ -37,7 +38,7 @@ void InsertIntoMap(std::map<std::string, std::string>& key_value_map,
<< "Missing terminating '/' in field trial string.";
RTC_CHECK_GT(separator_pos, field_start)
<< "Field trial value cannot be empty.";
std::string value = s.substr(field_start, separator_pos - field_start);
std::string value(s.substr(field_start, separator_pos - field_start));
field_start = separator_pos + 1;
key_value_map[key] = value;
@ -56,15 +57,15 @@ namespace test {
ScopedKeyValueConfig::ScopedKeyValueConfig()
: ScopedKeyValueConfig(nullptr, "") {}
ScopedKeyValueConfig::ScopedKeyValueConfig(const std::string& s)
ScopedKeyValueConfig::ScopedKeyValueConfig(absl::string_view s)
: ScopedKeyValueConfig(nullptr, s) {}
ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig& parent,
const std::string& s)
absl::string_view s)
: ScopedKeyValueConfig(&parent, s) {}
ScopedKeyValueConfig::ScopedKeyValueConfig(ScopedKeyValueConfig* parent,
const std::string& s)
absl::string_view s)
: parent_(parent), leaf_(nullptr) {
InsertIntoMap(key_value_map_, s);
@ -105,7 +106,7 @@ std::string ScopedKeyValueConfig::Lookup(absl::string_view key) const {
}
std::string ScopedKeyValueConfig::LookupRecurse(absl::string_view key) const {
auto it = key_value_map_.find(std::string(key));
auto it = key_value_map_.find(key);
if (it != key_value_map_.end())
return it->second;

View file

@ -11,6 +11,7 @@
#ifndef TEST_SCOPED_KEY_VALUE_CONFIG_H_
#define TEST_SCOPED_KEY_VALUE_CONFIG_H_
#include <functional>
#include <map>
#include <memory>
#include <string>
@ -26,13 +27,13 @@ class ScopedKeyValueConfig : public FieldTrialsView {
public:
virtual ~ScopedKeyValueConfig();
ScopedKeyValueConfig();
explicit ScopedKeyValueConfig(const std::string& s);
ScopedKeyValueConfig(ScopedKeyValueConfig& parent, const std::string& s);
explicit ScopedKeyValueConfig(absl::string_view s);
ScopedKeyValueConfig(ScopedKeyValueConfig& parent, absl::string_view s);
std::string Lookup(absl::string_view key) const override;
private:
ScopedKeyValueConfig(ScopedKeyValueConfig* parent, const std::string& s);
ScopedKeyValueConfig(ScopedKeyValueConfig* parent, absl::string_view s);
ScopedKeyValueConfig* GetRoot(ScopedKeyValueConfig* n);
std::string LookupRecurse(absl::string_view key) const;
@ -42,7 +43,9 @@ class ScopedKeyValueConfig : public FieldTrialsView {
// Only set on root (e.g with parent_ == nullptr).
const ScopedKeyValueConfig* leaf_;
std::map<std::string, std::string> key_value_map_;
// Unlike std::less<std::string>, std::less<> is transparent and allows
// heterogeneous lookup directly with absl::string_view.
std::map<std::string, std::string, std::less<>> key_value_map_;
std::unique_ptr<ScopedFieldTrials> scoped_field_trials_;
};