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", "field_trial.h",
] ]
absl_deps = [ "//third_party/abseil-cpp/absl/strings" ]
deps = [ deps = [
"../rtc_base:checks", "../rtc_base:checks",
"../system_wrappers:field_trial", "../system_wrappers:field_trial",

View File

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

View File

@ -11,6 +11,7 @@
#ifndef TEST_EXPLICIT_KEY_VALUE_CONFIG_H_ #ifndef TEST_EXPLICIT_KEY_VALUE_CONFIG_H_
#define TEST_EXPLICIT_KEY_VALUE_CONFIG_H_ #define TEST_EXPLICIT_KEY_VALUE_CONFIG_H_
#include <functional>
#include <map> #include <map>
#include <string> #include <string>
@ -22,11 +23,13 @@ namespace test {
class ExplicitKeyValueConfig : public FieldTrialsView { class ExplicitKeyValueConfig : public FieldTrialsView {
public: public:
explicit ExplicitKeyValueConfig(const std::string& s); explicit ExplicitKeyValueConfig(absl::string_view s);
std::string Lookup(absl::string_view key) const override; std::string Lookup(absl::string_view key) const override;
private: 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 } // namespace test

View File

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

View File

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

View File

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

View File

@ -11,6 +11,7 @@
#ifndef TEST_SCOPED_KEY_VALUE_CONFIG_H_ #ifndef TEST_SCOPED_KEY_VALUE_CONFIG_H_
#define TEST_SCOPED_KEY_VALUE_CONFIG_H_ #define TEST_SCOPED_KEY_VALUE_CONFIG_H_
#include <functional>
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
@ -26,13 +27,13 @@ class ScopedKeyValueConfig : public FieldTrialsView {
public: public:
virtual ~ScopedKeyValueConfig(); virtual ~ScopedKeyValueConfig();
ScopedKeyValueConfig(); ScopedKeyValueConfig();
explicit ScopedKeyValueConfig(const std::string& s); explicit ScopedKeyValueConfig(absl::string_view s);
ScopedKeyValueConfig(ScopedKeyValueConfig& parent, const std::string& s); ScopedKeyValueConfig(ScopedKeyValueConfig& parent, absl::string_view s);
std::string Lookup(absl::string_view key) const override; std::string Lookup(absl::string_view key) const override;
private: private:
ScopedKeyValueConfig(ScopedKeyValueConfig* parent, const std::string& s); ScopedKeyValueConfig(ScopedKeyValueConfig* parent, absl::string_view s);
ScopedKeyValueConfig* GetRoot(ScopedKeyValueConfig* n); ScopedKeyValueConfig* GetRoot(ScopedKeyValueConfig* n);
std::string LookupRecurse(absl::string_view key) const; 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). // Only set on root (e.g with parent_ == nullptr).
const ScopedKeyValueConfig* leaf_; 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_; std::unique_ptr<ScopedFieldTrials> scoped_field_trials_;
}; };