From 85dd7ad833a73095ecf3e3baea608ba051bbe2c7 Mon Sep 17 00:00:00 2001 From: Paul Wankadia Date: Thu, 20 Jun 2024 14:11:28 +0000 Subject: [PATCH] Address some more `-Wsign-compare` warnings. Fixes #499. Change-Id: I5405db7dff30ba84387c89a6064902f948b1ee3c Reviewed-on: https://code-review.googlesource.com/c/re2/+/63370 Reviewed-by: Alex Chernyakhovsky Reviewed-by: Paul Wankadia --- re2/testing/filtered_re2_test.cc | 38 +++++++++++----------- re2/testing/re2_test.cc | 25 +++++++++------ re2/testing/regexp_test.cc | 6 ++-- re2/testing/set_test.cc | 48 ++++++++++++++-------------- re2/testing/string_generator_test.cc | 2 +- 5 files changed, 63 insertions(+), 56 deletions(-) diff --git a/re2/testing/filtered_re2_test.cc b/re2/testing/filtered_re2_test.cc index b951d7c7f..6da76b228 100644 --- a/re2/testing/filtered_re2_test.cc +++ b/re2/testing/filtered_re2_test.cc @@ -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) { @@ -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]); } @@ -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]); } @@ -256,7 +256,7 @@ TEST(FilteredRE2Test, MatchTests) { FindAtomIndices(v.atoms, atoms, &atom_ids); std::vector 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(); @@ -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(); @@ -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) { @@ -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 diff --git a/re2/testing/re2_test.cc b/re2/testing/re2_test.cc index d46666bea..04c040e9e 100644 --- a/re2/testing/re2_test.cc +++ b/re2/testing/re2_test.cc @@ -558,14 +558,14 @@ TEST(Capture, NamedGroups) { RE2 re("(hello world)"); ASSERT_EQ(re.NumberOfCapturingGroups(), 1); const std::map& m = re.NamedCapturingGroups(); - ASSERT_EQ(m.size(), 0); + ASSERT_EQ(m.size(), size_t{0}); } { RE2 re("(?Pexpr(?Pexpr)(?Pexpr))((expr)(?Pexpr))"); ASSERT_EQ(re.NumberOfCapturingGroups(), 6); const std::map& 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); @@ -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); } @@ -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('H')); + } { unsigned char c; ASSERT_TRUE(RE2::FullMatch("Hello", "(H)ello", &c)); @@ -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)); @@ -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); @@ -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 @@ -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); diff --git a/re2/testing/regexp_test.cc b/re2/testing/regexp_test.cc index e971bd0b3..edbbe0839 100644 --- a/re2/testing/regexp_test.cc +++ b/re2/testing/regexp_test.cc @@ -54,8 +54,8 @@ TEST(Regexp, NamedCaptures) { EXPECT_EQ(4, x->NumCaptures()); const std::map* 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 want; want["g1"] = 1; want["g2"] = 3; @@ -73,7 +73,7 @@ TEST(Regexp, CaptureNames) { EXPECT_EQ(4, x->NumCaptures()); const std::map* have = x->CaptureNames(); EXPECT_TRUE(have != NULL); - EXPECT_EQ(3, have->size()); + EXPECT_EQ(size_t{3}, have->size()); std::map want; want[1] = "g1"; want[3] = "g2"; diff --git a/re2/testing/set_test.cc b/re2/testing/set_test.cc index 5962295e2..b4aaf92a1 100644 --- a/re2/testing/set_test.cc +++ b/re2/testing/set_test.cc @@ -28,16 +28,16 @@ TEST(Set, Unanchored) { std::vector 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); } @@ -56,21 +56,21 @@ TEST(Set, UnanchoredFactored) { std::vector 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) { @@ -84,11 +84,11 @@ TEST(Set, UnanchoredDollar) { std::vector 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) { @@ -103,14 +103,14 @@ TEST(Set, UnanchoredWordBoundary) { std::vector 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); } @@ -130,20 +130,20 @@ TEST(Set, Anchored) { std::vector 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); } @@ -157,10 +157,10 @@ TEST(Set, EmptyUnanchored) { std::vector 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) { @@ -173,10 +173,10 @@ TEST(Set, EmptyAnchored) { std::vector 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) { @@ -191,14 +191,14 @@ TEST(Set, Prefix) { std::vector 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); } diff --git a/re2/testing/string_generator_test.cc b/re2/testing/string_generator_test.cc index 62868d507..909843425 100644 --- a/re2/testing/string_generator_test.cc +++ b/re2/testing/string_generator_test.cc @@ -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(NULL)); - EXPECT_EQ(sp.size(), 0); + EXPECT_EQ(sp.size(), size_t{0}); } while (g.HasNext()) {