Skip to content

Commit

Permalink
Address some more -Wsign-compare warnings.
Browse files Browse the repository at this point in the history
Fixes #499.

Change-Id: I5405db7dff30ba84387c89a6064902f948b1ee3c
Reviewed-on: https://code-review.googlesource.com/c/re2/+/63370
Reviewed-by: Alex Chernyakhovsky <[email protected]>
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Jun 20, 2024
1 parent 67570de commit 85dd7ad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 56 deletions.
38 changes: 19 additions & 19 deletions re2/testing/filtered_re2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ TEST(FilteredRE2Test, EmptyTest) {
FilterTestVars v;

v.f.Compile(&v.atoms);
EXPECT_EQ(0, v.atoms.size());
EXPECT_EQ(size_t{0}, v.atoms.size());

// Compile has no effect at all when called before Add: it will not
// record that it has been called and it will not clear the vector.
// The second point does not matter here, but the first point means
// that an error will be logged during the call to AllMatches.
v.f.AllMatches("foo", v.atom_indices, &v.matches);
EXPECT_EQ(0, v.matches.size());
EXPECT_EQ(size_t{0}, v.matches.size());
}

TEST(FilteredRE2Test, SmallOrTest) {
Expand All @@ -49,10 +49,10 @@ TEST(FilteredRE2Test, SmallOrTest) {
v.f.Add("(foo|bar)", v.opts, &id);

v.f.Compile(&v.atoms);
EXPECT_EQ(0, v.atoms.size());
EXPECT_EQ(size_t{0}, v.atoms.size());

v.f.AllMatches("lemurs bar", v.atom_indices, &v.matches);
EXPECT_EQ(1, v.matches.size());
EXPECT_EQ(size_t{1}, v.matches.size());
EXPECT_EQ(id, v.matches[0]);
}

Expand All @@ -63,12 +63,12 @@ TEST(FilteredRE2Test, SmallLatinTest) {
v.opts.set_encoding(RE2::Options::EncodingLatin1);
v.f.Add("\xde\xadQ\xbe\xef", v.opts, &id);
v.f.Compile(&v.atoms);
EXPECT_EQ(1, v.atoms.size());
EXPECT_EQ(size_t{1}, v.atoms.size());
EXPECT_EQ(v.atoms[0], "\xde\xadq\xbe\xef");

v.atom_indices.push_back(0);
v.f.AllMatches("foo\xde\xadQ\xbe\xeflemur", v.atom_indices, &v.matches);
EXPECT_EQ(1, v.matches.size());
EXPECT_EQ(size_t{1}, v.matches.size());
EXPECT_EQ(id, v.matches[0]);
}

Expand Down Expand Up @@ -256,7 +256,7 @@ TEST(FilteredRE2Test, MatchTests) {
FindAtomIndices(v.atoms, atoms, &atom_ids);
std::vector<int> matching_regexps;
v.f.AllMatches(text, atom_ids, &matching_regexps);
EXPECT_EQ(1, matching_regexps.size());
EXPECT_EQ(size_t{1}, matching_regexps.size());

text = "abc12312yyyzzz";
atoms.clear();
Expand All @@ -265,7 +265,7 @@ TEST(FilteredRE2Test, MatchTests) {
atoms.push_back("yyyzzz");
FindAtomIndices(v.atoms, atoms, &atom_ids);
v.f.AllMatches(text, atom_ids, &matching_regexps);
EXPECT_EQ(1, matching_regexps.size());
EXPECT_EQ(size_t{1}, matching_regexps.size());

text = "abcd12yyy32yyyzzz";
atoms.clear();
Expand All @@ -278,7 +278,7 @@ TEST(FilteredRE2Test, MatchTests) {
for (size_t i = 0; i < atom_ids.size(); i++)
ABSL_LOG(INFO) << "i: " << i << " : " << atom_ids[i];
v.f.AllMatches(text, atom_ids, &matching_regexps);
EXPECT_EQ(2, matching_regexps.size());
EXPECT_EQ(size_t{2}, matching_regexps.size());
}

TEST(FilteredRE2Test, EmptyStringInStringSetBug) {
Expand All @@ -301,43 +301,43 @@ TEST(FilteredRE2Test, MoveSemantics) {
v1.f.Add("foo\\d+", v1.opts, &id);
EXPECT_EQ(0, id);
v1.f.Compile(&v1.atoms);
EXPECT_EQ(1, v1.atoms.size());
EXPECT_EQ(size_t{1}, v1.atoms.size());
EXPECT_EQ("foo", v1.atoms[0]);
v1.f.AllMatches("abc foo1 xyz", {0}, &v1.matches);
EXPECT_EQ(1, v1.matches.size());
EXPECT_EQ(size_t{1}, v1.matches.size());
EXPECT_EQ(0, v1.matches[0]);
v1.f.AllMatches("abc bar2 xyz", {0}, &v1.matches);
EXPECT_EQ(0, v1.matches.size());
EXPECT_EQ(size_t{0}, v1.matches.size());

// The moved-to object should do what the moved-from object did.
FilterTestVars v2;
v2.f = std::move(v1.f);
v2.f.AllMatches("abc foo1 xyz", {0}, &v2.matches);
EXPECT_EQ(1, v2.matches.size());
EXPECT_EQ(size_t{1}, v2.matches.size());
EXPECT_EQ(0, v2.matches[0]);
v2.f.AllMatches("abc bar2 xyz", {0}, &v2.matches);
EXPECT_EQ(0, v2.matches.size());
EXPECT_EQ(size_t{0}, v2.matches.size());

// The moved-from object should have been reset and be reusable.
v1.f.Add("bar\\d+", v1.opts, &id);
EXPECT_EQ(0, id);
v1.f.Compile(&v1.atoms);
EXPECT_EQ(1, v1.atoms.size());
EXPECT_EQ(size_t{1}, v1.atoms.size());
EXPECT_EQ("bar", v1.atoms[0]);
v1.f.AllMatches("abc foo1 xyz", {0}, &v1.matches);
EXPECT_EQ(0, v1.matches.size());
EXPECT_EQ(size_t{0}, v1.matches.size());
v1.f.AllMatches("abc bar2 xyz", {0}, &v1.matches);
EXPECT_EQ(1, v1.matches.size());
EXPECT_EQ(size_t{1}, v1.matches.size());
EXPECT_EQ(0, v1.matches[0]);

// Verify that "overwriting" works and also doesn't leak memory.
// (The latter will need a leak detector such as LeakSanitizer.)
v1.f = std::move(v2.f);
v1.f.AllMatches("abc foo1 xyz", {0}, &v1.matches);
EXPECT_EQ(1, v1.matches.size());
EXPECT_EQ(size_t{1}, v1.matches.size());
EXPECT_EQ(0, v1.matches[0]);
v1.f.AllMatches("abc bar2 xyz", {0}, &v1.matches);
EXPECT_EQ(0, v1.matches.size());
EXPECT_EQ(size_t{0}, v1.matches.size());
}

} // namespace re2
25 changes: 16 additions & 9 deletions re2/testing/re2_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -558,14 +558,14 @@ TEST(Capture, NamedGroups) {
RE2 re("(hello world)");
ASSERT_EQ(re.NumberOfCapturingGroups(), 1);
const std::map<std::string, int>& m = re.NamedCapturingGroups();
ASSERT_EQ(m.size(), 0);
ASSERT_EQ(m.size(), size_t{0});
}

{
RE2 re("(?P<A>expr(?P<B>expr)(?P<C>expr))((expr)(?P<D>expr))");
ASSERT_EQ(re.NumberOfCapturingGroups(), 6);
const std::map<std::string, int>& m = re.NamedCapturingGroups();
ASSERT_EQ(m.size(), 4);
ASSERT_EQ(m.size(), size_t{4});
ASSERT_EQ(m.find("A")->second, 1);
ASSERT_EQ(m.find("B")->second, 2);
ASSERT_EQ(m.find("C")->second, 3);
Expand Down Expand Up @@ -687,7 +687,7 @@ TEST(RE2, FullMatchStringViewArg) {
absl::string_view sp;
// string_view-arg
ASSERT_TRUE(RE2::FullMatch("ruby:1234", "(\\w+):(\\d+)", &sp, &i));
ASSERT_EQ(sp.size(), 4);
ASSERT_EQ(sp.size(), size_t{4});
ASSERT_TRUE(memcmp(sp.data(), "ruby", 4) == 0);
ASSERT_EQ(i, 1234);
}
Expand Down Expand Up @@ -796,6 +796,11 @@ TEST(RE2, FullMatchTypeTests) {
ASSERT_TRUE(RE2::FullMatch("Hello", "(H)ello", &c));
ASSERT_EQ(c, 'H');
}
{
signed char c;
ASSERT_TRUE(RE2::FullMatch("Hello", "(H)ello", &c));
ASSERT_EQ(c, static_cast<signed char>('H'));
}
{
unsigned char c;
ASSERT_TRUE(RE2::FullMatch("Hello", "(H)ello", &c));
Expand Down Expand Up @@ -841,7 +846,7 @@ TEST(RE2, FullMatchTypeTests) {
{
uint32_t v;
static const uint32_t max = UINT32_C(0xffffffff);
ASSERT_TRUE(RE2::FullMatch("100", "(\\d+)", &v)); ASSERT_EQ(v, 100);
ASSERT_TRUE(RE2::FullMatch("100", "(\\d+)", &v)); ASSERT_EQ(v, uint32_t{100});
ASSERT_TRUE(RE2::FullMatch("4294967295", "(\\d+)", &v)); ASSERT_EQ(v, max);
ASSERT_FALSE(RE2::FullMatch("4294967296", "(\\d+)", &v));
ASSERT_FALSE(RE2::FullMatch("-1", "(\\d+)", &v));
Expand Down Expand Up @@ -879,7 +884,7 @@ TEST(RE2, FullMatchTypeTests) {
static const uint64_t max = UINT64_C(0xffffffffffffffff);
std::string str;

ASSERT_TRUE(RE2::FullMatch("100", "(-?\\d+)", &v)); ASSERT_EQ(v, 100);
ASSERT_TRUE(RE2::FullMatch("100", "(-?\\d+)", &v)); ASSERT_EQ(v, uint64_t{100});
ASSERT_TRUE(RE2::FullMatch("-100", "(-?\\d+)", &v2)); ASSERT_EQ(v2, -100);

str = std::to_string(max);
Expand All @@ -897,11 +902,11 @@ TEST(RE2, FloatingPointFullMatchTypes) {
float v;
ASSERT_TRUE(RE2::FullMatch("100", "(.*)", &v)); ASSERT_EQ(v, 100);
ASSERT_TRUE(RE2::FullMatch("-100.", "(.*)", &v)); ASSERT_EQ(v, -100);
ASSERT_TRUE(RE2::FullMatch("1e23", "(.*)", &v)); ASSERT_EQ(v, float(1e23));
ASSERT_TRUE(RE2::FullMatch("1e23", "(.*)", &v)); ASSERT_EQ(v, float{1e23});
ASSERT_TRUE(RE2::FullMatch(" 100", "(.*)", &v)); ASSERT_EQ(v, 100);

ASSERT_TRUE(RE2::FullMatch(zeros + "1e23", "(.*)", &v));
ASSERT_EQ(v, float(1e23));
ASSERT_EQ(v, float{1e23});

// 6700000000081920.1 is an edge case.
// 6700000000081920 is exactly halfway between
Expand Down Expand Up @@ -930,9 +935,11 @@ TEST(RE2, FloatingPointFullMatchTypes) {
double v;
ASSERT_TRUE(RE2::FullMatch("100", "(.*)", &v)); ASSERT_EQ(v, 100);
ASSERT_TRUE(RE2::FullMatch("-100.", "(.*)", &v)); ASSERT_EQ(v, -100);
ASSERT_TRUE(RE2::FullMatch("1e23", "(.*)", &v)); ASSERT_EQ(v, 1e23);
ASSERT_TRUE(RE2::FullMatch("1e23", "(.*)", &v)); ASSERT_EQ(v, double{1e23});
ASSERT_TRUE(RE2::FullMatch(" 100", "(.*)", &v)); ASSERT_EQ(v, 100);

ASSERT_TRUE(RE2::FullMatch(zeros + "1e23", "(.*)", &v));
ASSERT_EQ(v, double(1e23));
ASSERT_EQ(v, double{1e23});

ASSERT_TRUE(RE2::FullMatch("0.1", "(.*)", &v));
ASSERT_EQ(v, 0.1) << absl::StrFormat("%.17g != %.17g", v, 0.1);
Expand Down
6 changes: 3 additions & 3 deletions re2/testing/regexp_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ TEST(Regexp, NamedCaptures) {
EXPECT_EQ(4, x->NumCaptures());
const std::map<std::string, int>* have = x->NamedCaptures();
EXPECT_TRUE(have != NULL);
EXPECT_EQ(2, have->size()); // there are only two named groups in
// the regexp: 'g1' and 'g2'.
// there are only two named groups in the regexp: 'g1' and 'g2'.
EXPECT_EQ(size_t{2}, have->size());
std::map<std::string, int> want;
want["g1"] = 1;
want["g2"] = 3;
Expand All @@ -73,7 +73,7 @@ TEST(Regexp, CaptureNames) {
EXPECT_EQ(4, x->NumCaptures());
const std::map<int, std::string>* have = x->CaptureNames();
EXPECT_TRUE(have != NULL);
EXPECT_EQ(3, have->size());
EXPECT_EQ(size_t{3}, have->size());
std::map<int, std::string> want;
want[1] = "g1";
want[3] = "g2";
Expand Down
48 changes: 24 additions & 24 deletions re2/testing/set_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ TEST(Set, Unanchored) {

std::vector<int> v;
ASSERT_EQ(s.Match("foobar", &v), true);
ASSERT_EQ(v.size(), 2);
ASSERT_EQ(v.size(), size_t{2});
ASSERT_EQ(v[0], 0);
ASSERT_EQ(v[1], 1);

ASSERT_EQ(s.Match("fooba", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);

ASSERT_EQ(s.Match("oobar", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 1);
}

Expand All @@ -56,21 +56,21 @@ TEST(Set, UnanchoredFactored) {

std::vector<int> v;
ASSERT_EQ(s.Match("foobar", &v), true);
ASSERT_EQ(v.size(), 2);
ASSERT_EQ(v.size(), size_t{2});
ASSERT_EQ(v[0], 0);
ASSERT_EQ(v[1], 1);

ASSERT_EQ(s.Match("obarfoobaroo", &v), true);
ASSERT_EQ(v.size(), 2);
ASSERT_EQ(v.size(), size_t{2});
ASSERT_EQ(v[0], 0);
ASSERT_EQ(v[1], 1);

ASSERT_EQ(s.Match("fooba", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);

ASSERT_EQ(s.Match("oobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});
}

TEST(Set, UnanchoredDollar) {
Expand All @@ -84,11 +84,11 @@ TEST(Set, UnanchoredDollar) {

std::vector<int> v;
ASSERT_EQ(s.Match("foo", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);

ASSERT_EQ(s.Match("foobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});
}

TEST(Set, UnanchoredWordBoundary) {
Expand All @@ -103,14 +103,14 @@ TEST(Set, UnanchoredWordBoundary) {

std::vector<int> v;
ASSERT_EQ(s.Match("foo", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);

ASSERT_EQ(s.Match("foobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("foo bar", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);
}

Expand All @@ -130,20 +130,20 @@ TEST(Set, Anchored) {

std::vector<int> v;
ASSERT_EQ(s.Match("foobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("fooba", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("oobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("foo", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);

ASSERT_EQ(s.Match("bar", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 1);
}

Expand All @@ -157,10 +157,10 @@ TEST(Set, EmptyUnanchored) {

std::vector<int> v;
ASSERT_EQ(s.Match("", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("foobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});
}

TEST(Set, EmptyAnchored) {
Expand All @@ -173,10 +173,10 @@ TEST(Set, EmptyAnchored) {

std::vector<int> v;
ASSERT_EQ(s.Match("", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("foobar", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});
}

TEST(Set, Prefix) {
Expand All @@ -191,14 +191,14 @@ TEST(Set, Prefix) {

std::vector<int> v;
ASSERT_EQ(s.Match("/prefix", &v), false);
ASSERT_EQ(v.size(), 0);
ASSERT_EQ(v.size(), size_t{0});

ASSERT_EQ(s.Match("/prefix/", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);

ASSERT_EQ(s.Match("/prefix/42", &v), true);
ASSERT_EQ(v.size(), 1);
ASSERT_EQ(v.size(), size_t{1});
ASSERT_EQ(v[0], 0);
}

Expand Down
2 changes: 1 addition & 1 deletion re2/testing/string_generator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void RunTest(int len, const std::string& alphabet, bool donull) {
EXPECT_TRUE(g.HasNext());
absl::string_view sp = g.Next();
EXPECT_EQ(sp.data(), static_cast<const char*>(NULL));
EXPECT_EQ(sp.size(), 0);
EXPECT_EQ(sp.size(), size_t{0});
}

while (g.HasNext()) {
Expand Down

0 comments on commit 85dd7ad

Please sign in to comment.