Skip to content

Commit 4cf4fb1

Browse files
committed
8314752: Use google test string comparison macros
Reviewed-by: mdoerr, mbaesken Backport-of: 2d0c61fcaa23d03f56f1ae6dec365e9eea5ff96d
1 parent d66da41 commit 4cf4fb1

8 files changed

+29
-26
lines changed

test/hotspot/gtest/logging/test_logConfiguration.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "unittest.hpp"
3737
#include "utilities/ostream.hpp"
3838

39+
using testing::HasSubstr;
40+
3941
class LogConfigurationTest : public LogTestFixture {
4042
protected:
4143
static char _all_decorators[256];
@@ -71,26 +73,26 @@ TEST_VM_F(LogConfigurationTest, describe) {
7173
const char* description = ss.as_string();
7274

7375
// Verify that stdout and stderr are listed by default
74-
EXPECT_PRED2(string_contains_substring, description, StdoutLog.name());
75-
EXPECT_PRED2(string_contains_substring, description, StderrLog.name());
76+
EXPECT_THAT(description, HasSubstr(StdoutLog.name()));
77+
EXPECT_THAT(description, HasSubstr(StderrLog.name()));
7678

7779
// Verify that each tag, level and decorator is listed
7880
for (size_t i = 0; i < LogTag::Count; i++) {
79-
EXPECT_PRED2(string_contains_substring, description, LogTag::name(static_cast<LogTagType>(i)));
81+
EXPECT_THAT(description, HasSubstr(LogTag::name(static_cast<LogTagType>(i))));
8082
}
8183
for (size_t i = 0; i < LogLevel::Count; i++) {
82-
EXPECT_PRED2(string_contains_substring, description, LogLevel::name(static_cast<LogLevelType>(i)));
84+
EXPECT_THAT(description, HasSubstr(LogLevel::name(static_cast<LogLevelType>(i))));
8385
}
8486
for (size_t i = 0; i < LogDecorators::Count; i++) {
85-
EXPECT_PRED2(string_contains_substring, description, LogDecorators::name(static_cast<LogDecorators::Decorator>(i)));
87+
EXPECT_THAT(description, HasSubstr(LogDecorators::name(static_cast<LogDecorators::Decorator>(i))));
8688
}
8789

8890
// Verify that the default configuration is printed
8991
char expected_buf[256];
9092
int ret = jio_snprintf(expected_buf, sizeof(expected_buf), "=%s", LogLevel::name(LogLevel::Default));
9193
ASSERT_NE(-1, ret);
92-
EXPECT_PRED2(string_contains_substring, description, expected_buf);
93-
EXPECT_PRED2(string_contains_substring, description, "#1: stderr all=off");
94+
EXPECT_THAT(description, HasSubstr(expected_buf));
95+
EXPECT_THAT(description, HasSubstr("#1: stderr all=off"));
9496

9597
// Verify default decorators are listed
9698
LogDecorators default_decorators;
@@ -107,7 +109,7 @@ TEST_VM_F(LogConfigurationTest, describe) {
107109
ASSERT_NE(-1, ret);
108110
}
109111
}
110-
EXPECT_PRED2(string_contains_substring, description, expected_buf);
112+
EXPECT_THAT(description, HasSubstr(expected_buf));
111113

112114
// Add a new output and verify that it gets described after it has been added
113115
const char* what = "all=trace";
@@ -493,8 +495,8 @@ TEST_VM_F(LogConfigurationTest, parse_invalid_tagset) {
493495
bool success = LogConfiguration::parse_log_arguments("stdout", invalid_tagset, NULL, NULL, &ss);
494496
const char* msg = ss.as_string();
495497
EXPECT_TRUE(success) << "Should only cause a warning, not an error";
496-
EXPECT_TRUE(string_contains_substring(msg, "No tag set matches selection:"));
497-
EXPECT_TRUE(string_contains_substring(msg, invalid_tagset));
498+
EXPECT_THAT(msg, HasSubstr("No tag set matches selection:"));
499+
EXPECT_THAT(msg, HasSubstr(invalid_tagset));
498500
}
499501

500502
TEST_VM_F(LogConfigurationTest, output_name_normalization) {
@@ -559,7 +561,7 @@ TEST_VM_F(LogConfigurationTest, suggest_similar_selection) {
559561

560562
const char* suggestion = ss.as_string();
561563
SCOPED_TRACE(suggestion);
562-
EXPECT_TRUE(string_contains_substring(ss.as_string(), "Did you mean any of the following?"));
564+
EXPECT_THAT(suggestion, HasSubstr("Did you mean any of the following?"));
563565
EXPECT_TRUE(string_contains_substring(suggestion, "logging") ||
564566
string_contains_substring(suggestion, "start") ||
565567
string_contains_substring(suggestion, "exit") ||

test/hotspot/gtest/logging/test_logFileOutput.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ TEST_VM(LogFileOutput, invalid_file) {
188188
EXPECT_FALSE(bad_file.initialize("", &ss))
189189
<< "file was initialized when there was an existing directory with the same name";
190190
char* logger_output = ss.as_string();
191-
EXPECT_TRUE(string_contains_substring(logger_output, expected_output_substring))
191+
EXPECT_THAT(logger_output, testing::HasSubstr(expected_output_substring))
192192
<< "missing expected error message, received msg: %s" << logger_output;
193193
delete_empty_directory(path);
194194
}

test/hotspot/gtest/logging/test_logTagSetDescriptions.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ TEST_VM(LogTagSetDescriptions, describe) {
4242
ResourceMark rm;
4343
stringStream stream;
4444
LogConfiguration::describe(&stream);
45-
EXPECT_PRED2(string_contains_substring, stream.as_string(), expected)
45+
EXPECT_THAT(stream.base(), testing::HasSubstr(expected))
4646
<< "missing log tag set descriptions in LogConfiguration::describe";
4747
}
4848
}

test/hotspot/gtest/memory/test_guardedMemory.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ TEST(GuardedMemory, wrap) {
140140
if (HasFatalFailure()) {
141141
return;
142142
}
143-
EXPECT_EQ(0, strcmp(str, str_copy)) << "Not identical copy";
143+
EXPECT_STREQ(str, str_copy) << "Not identical copy";
144144
EXPECT_TRUE(GuardedMemory::free_copy(str_copy)) << "Free copy failed to verify";
145145

146146
void* no_data = NULL;

test/hotspot/gtest/oops/test_instanceKlass.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "oops/instanceKlass.hpp"
2828
#include "unittest.hpp"
2929

30+
using testing::HasSubstr;
31+
3032
// Tests for InstanceKlass::is_class_loader_instance_klass() function
3133
TEST_VM(InstanceKlass, class_loader_class) {
3234
InstanceKlass* klass = vmClasses::ClassLoader_klass();

test/hotspot/gtest/oops/test_markWord.cpp

+2-5
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,17 @@
4141

4242
// The test doesn't work for PRODUCT because it needs WizardMode
4343
#ifndef PRODUCT
44-
static bool test_pattern(stringStream* st, const char* pattern) {
45-
return (strstr(st->as_string(), pattern) != NULL);
46-
}
4744

4845
static void assert_test_pattern(Handle object, const char* pattern) {
4946
stringStream st;
5047
object->print_on(&st);
51-
ASSERT_TRUE(test_pattern(&st, pattern)) << pattern << " not in " << st.as_string();
48+
ASSERT_THAT(st.base(), testing::HasSubstr(pattern));
5249
}
5350

5451
static void assert_not_test_pattern(Handle object, const char* pattern) {
5552
stringStream st;
5653
object->print_on(&st);
57-
ASSERT_FALSE(test_pattern(&st, pattern)) << pattern << " found in " << st.as_string();
54+
ASSERT_THAT(st.base(), testing::Not(testing::HasSubstr(pattern)));
5855
}
5956

6057
class LockerThread : public JavaTestThread {

test/hotspot/gtest/runtime/test_globals.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,19 @@ TEST_VM(FlagGuard, ccstr_flag) {
8181
TEST_VM(FlagAccess, ccstr_flag) {
8282
FLAG_SET_CMDLINE(SharedArchiveConfigFile, "");
8383
ASSERT_EQ(FLAG_IS_CMDLINE(SharedArchiveConfigFile), true);
84-
ASSERT_EQ(strcmp(SharedArchiveConfigFile, ""), 0);
84+
EXPECT_STREQ(SharedArchiveConfigFile, "");
8585

8686
FLAG_SET_ERGO(SharedArchiveConfigFile, "foobar");
8787
ASSERT_EQ(FLAG_IS_ERGO(SharedArchiveConfigFile), true);
88-
ASSERT_EQ(strcmp(SharedArchiveConfigFile, "foobar") , 0);
88+
EXPECT_STREQ(SharedArchiveConfigFile, "foobar");
8989

9090
FLAG_SET_ERGO(SharedArchiveConfigFile, nullptr);
9191
ASSERT_EQ(FLAG_IS_ERGO(SharedArchiveConfigFile), true);
9292
ASSERT_EQ(SharedArchiveConfigFile, nullptr);
9393

9494
FLAG_SET_ERGO(SharedArchiveConfigFile, "xyz");
9595
ASSERT_EQ(FLAG_IS_ERGO(SharedArchiveConfigFile), true);
96-
ASSERT_EQ(strcmp(SharedArchiveConfigFile, "xyz"), 0);
96+
EXPECT_STREQ(SharedArchiveConfigFile, "xyz");
9797
}
9898

9999
template <typename T, int type_enum>

test/hotspot/gtest/runtime/test_os.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "unittest.hpp"
3535
#include "runtime/frame.inline.hpp"
3636

37+
using testing::HasSubstr;
38+
3739
static size_t small_page_size() {
3840
return os::vm_page_size();
3941
}
@@ -167,7 +169,7 @@ static void do_test_print_hex_dump(address addr, size_t len, int unitsize, const
167169
os::print_hex_dump(&ss, addr, addr + len, unitsize);
168170
// tty->print_cr("expected: %s", expected);
169171
// tty->print_cr("result: %s", buf);
170-
ASSERT_NE(strstr(buf, expected), (char*)NULL);
172+
EXPECT_THAT(buf, HasSubstr(expected));
171173
}
172174

173175
TEST_VM(os, test_print_hex_dump) {
@@ -769,7 +771,7 @@ TEST_VM(os, pagesizes_test_print) {
769771
char buffer[256];
770772
stringStream ss(buffer, sizeof(buffer));
771773
pss.print_on(&ss);
772-
ASSERT_EQ(strcmp(expected, buffer), 0);
774+
EXPECT_STREQ(expected, buffer);
773775
}
774776

775777
TEST_VM(os, dll_address_to_function_and_library_name) {
@@ -778,9 +780,9 @@ TEST_VM(os, dll_address_to_function_and_library_name) {
778780
stringStream st(output, sizeof(output));
779781

780782
#define EXPECT_CONTAINS(haystack, needle) \
781-
EXPECT_NE(::strstr(haystack, needle), (char*)NULL)
783+
EXPECT_THAT(haystack, HasSubstr(needle));
782784
#define EXPECT_DOES_NOT_CONTAIN(haystack, needle) \
783-
EXPECT_EQ(::strstr(haystack, needle), (char*)NULL)
785+
EXPECT_THAT(haystack, Not(HasSubstr(needle)));
784786
// #define LOG(...) tty->print_cr(__VA_ARGS__); // enable if needed
785787
#define LOG(...)
786788

0 commit comments

Comments
 (0)