Fix support for booleans in field trial list parser

The FieldTrialList<T> class hold values internally in a std::vector<T>.
One call site assumes a const T& can be retrieved from the vector, but
it turns out that std::vector<bool> has a bit-packed specialization
which means a normal reference can't be generated.
To work around this, change the code to instead use
typename std::vector<T>::const_reference.

Bug: webrtc:13655
Change-Id: I2a515b7064c8760e1a59d1910d59b1bd2c7a6622
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/250543
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Auto-Submit: Erik Språng <sprang@webrtc.org>
Commit-Queue: Erik Språng <sprang@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35902}
This commit is contained in:
Erik Språng 2022-02-03 18:46:48 +01:00 committed by WebRTC LUCI CQ
parent 4a25d780b4
commit da897930c3
2 changed files with 22 additions and 14 deletions

View File

@ -58,7 +58,9 @@ class FieldTrialList : public FieldTrialListBase {
std::vector<T> Get() const { return values_; }
operator std::vector<T>() const { return Get(); }
const T& operator[](size_t index) const { return values_[index]; }
typename std::vector<T>::const_reference operator[](size_t index) const {
return values_[index];
}
const std::vector<T>* operator->() const { return &values_; }
protected:

View File

@ -21,13 +21,15 @@ namespace webrtc {
struct Garment {
int price = 0;
std::string color = "";
bool has_glitter = false;
// Only needed for testing.
Garment() = default;
Garment(int p, std::string c) : price(p), color(c) {}
Garment(int p, std::string c, bool g) : price(p), color(c), has_glitter(g) {}
bool operator==(const Garment& other) const {
return price == other.price && color == other.color;
return price == other.price && color == other.color &&
has_glitter == other.has_glitter;
}
};
@ -60,17 +62,20 @@ TEST(FieldTrialListTest, ParsesListParameter) {
TEST(FieldTrialListTest, ParsesStructList) {
FieldTrialStructList<Garment> my_list(
{FieldTrialStructMember("color", [](Garment* g) { return &g->color; }),
FieldTrialStructMember("price", [](Garment* g) { return &g->price; })},
{{1, "blue"}, {2, "red"}});
FieldTrialStructMember("price", [](Garment* g) { return &g->price; }),
FieldTrialStructMember("has_glitter",
[](Garment* g) { return &g->has_glitter; })},
{{1, "blue", false}, {2, "red", true}});
ParseFieldTrial({&my_list},
"color:mauve|red|gold,"
"price:10|20|30,"
"has_glitter:1|0|1,"
"other_param:asdf");
ASSERT_THAT(my_list.Get(),
ElementsAre(Garment{10, "mauve"}, Garment{20, "red"},
Garment{30, "gold"}));
ElementsAre(Garment{10, "mauve", true}, Garment{20, "red", false},
Garment{30, "gold", true}));
}
// One FieldTrialList has the wrong length, so we use the user-provided default
@ -80,7 +85,7 @@ TEST(FieldTrialListTest, StructListKeepsDefaultWithMismatchingLength) {
{FieldTrialStructMember("wrong_length",
[](Garment* g) { return &g->color; }),
FieldTrialStructMember("price", [](Garment* g) { return &g->price; })},
{{1, "blue"}, {2, "red"}});
{{1, "blue", true}, {2, "red", false}});
ParseFieldTrial({&my_list},
"wrong_length:mauve|magenta|chartreuse|indigo,"
@ -88,7 +93,7 @@ TEST(FieldTrialListTest, StructListKeepsDefaultWithMismatchingLength) {
"price:10|20|30");
ASSERT_THAT(my_list.Get(),
ElementsAre(Garment{1, "blue"}, Garment{2, "red"}));
ElementsAre(Garment{1, "blue", true}, Garment{2, "red", false}));
}
// One list is missing. We set the values we're given, and the others remain
@ -97,12 +102,13 @@ TEST(FieldTrialListTest, StructListUsesDefaultForMissingList) {
FieldTrialStructList<Garment> my_list(
{FieldTrialStructMember("color", [](Garment* g) { return &g->color; }),
FieldTrialStructMember("price", [](Garment* g) { return &g->price; })},
{{1, "blue"}, {2, "red"}});
{{1, "blue", true}, {2, "red", false}});
ParseFieldTrial({&my_list}, "price:10|20|30");
ASSERT_THAT(my_list.Get(),
ElementsAre(Garment{10, ""}, Garment{20, ""}, Garment{30, ""}));
ElementsAre(Garment{10, "", false}, Garment{20, "", false},
Garment{30, "", false}));
}
// The user haven't provided values for any lists, so we use the default list.
@ -110,12 +116,12 @@ TEST(FieldTrialListTest, StructListUsesDefaultListWithoutValues) {
FieldTrialStructList<Garment> my_list(
{FieldTrialStructMember("color", [](Garment* g) { return &g->color; }),
FieldTrialStructMember("price", [](Garment* g) { return &g->price; })},
{{1, "blue"}, {2, "red"}});
{{1, "blue", true}, {2, "red", false}});
ParseFieldTrial({&my_list}, "");
ASSERT_THAT(my_list.Get(),
ElementsAre(Garment{1, "blue"}, Garment{2, "red"}));
ElementsAre(Garment{1, "blue", true}, Garment{2, "red", false}));
}
// Some lists are provided and all are empty, so we return a empty list.
@ -123,7 +129,7 @@ TEST(FieldTrialListTest, StructListHandlesEmptyLists) {
FieldTrialStructList<Garment> my_list(
{FieldTrialStructMember("color", [](Garment* g) { return &g->color; }),
FieldTrialStructMember("price", [](Garment* g) { return &g->price; })},
{{1, "blue"}, {2, "red"}});
{{1, "blue", true}, {2, "red", false}});
ParseFieldTrial({&my_list}, "color,price");