Skip to content

Commit 0f6bdcb

Browse files
committed
remove dependency on internal gtest functions
1 parent 3648ca1 commit 0f6bdcb

File tree

2 files changed

+63
-44
lines changed

2 files changed

+63
-44
lines changed

include/gtest_mpi/gtest_mpi.hpp

+29-17
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@
6969
#include <gtest/gtest.h>
7070
#include <mpi.h>
7171
#include <unistd.h>
72+
73+
#include <cstdlib>
7274
#include <set>
7375
#include <string>
76+
7477
#include "gtest_mpi_internal.hpp"
7578

7679
namespace gtest_mpi {
@@ -149,7 +152,6 @@ class PrettyMPIUnitTestResultPrinter : public ::testing::TestEventListener {
149152
void PrettyMPIUnitTestResultPrinter::OnTestIterationStart(const ::testing::UnitTest& unit_test,
150153
int iteration) {
151154
using namespace ::testing;
152-
using namespace ::testing::internal;
153155
if (rank_ != 0) return;
154156

155157
if (GTEST_FLAG(repeat) != 1)
@@ -159,14 +161,14 @@ void PrettyMPIUnitTestResultPrinter::OnTestIterationStart(const ::testing::UnitT
159161

160162
// Prints the filter if it's not *. This reminds the user that some
161163
// tests may be skipped.
162-
if (!String::CStringEquals(filter, kUniversalFilter)) {
164+
if (!CStringEquals(filter, kUniversalFilter)) {
163165
ColoredPrintf(COLOR_YELLOW, "Note: %s filter = %s\n", GTEST_NAME_, filter);
164166
}
165167

166168
if (ShouldShard(kTestTotalShards, kTestShardIndex, false)) {
167169
const Int32 shard_index = Int32FromEnvOrDie(kTestShardIndex, -1);
168170
ColoredPrintf(COLOR_YELLOW, "Note: This is test shard %d of %s.\n",
169-
static_cast<int>(shard_index) + 1, internal::posix::GetEnv(kTestTotalShards));
171+
static_cast<int>(shard_index) + 1, std::getenv("GTEST_SHARD_STATUS_FILE"));
170172
}
171173

172174
if (GTEST_FLAG(shuffle)) {
@@ -180,6 +182,22 @@ void PrettyMPIUnitTestResultPrinter::OnTestIterationStart(const ::testing::UnitT
180182
fflush(stdout);
181183
}
182184

185+
// Taken / modified from Googletest
186+
// Formats a source file path and a line number as they would appear
187+
// in an error message from the compiler used to compile this code.
188+
GTEST_API_ ::std::string FormatFileLocation(const char* file, int line) {
189+
const std::string file_name(file == nullptr ? "unkown file" : file);
190+
191+
if (line < 0) {
192+
return file_name + ":";
193+
}
194+
#ifdef _MSC_VER
195+
return file_name + "(" + std::to_string(line) + "):";
196+
#else
197+
return file_name + ":" + std::to_string(line) + ":";
198+
#endif // _MSC_VER
199+
}
200+
183201
// Taken / modified from Googletest
184202
void PrettyMPIUnitTestResultPrinter::OnEnvironmentsSetUpStart(
185203
const ::testing::UnitTest& /*unit_test*/) {
@@ -192,7 +210,6 @@ void PrettyMPIUnitTestResultPrinter::OnEnvironmentsSetUpStart(
192210
// Taken / modified from Googletest
193211
void PrettyMPIUnitTestResultPrinter::OnTestCaseStart(const ::testing::TestCase& test_case) {
194212
using namespace ::testing;
195-
using namespace ::testing::internal;
196213
if (rank_ != 0) return;
197214
const std::string counts = FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
198215
ColoredPrintf(COLOR_GREEN, "[----------] ");
@@ -218,7 +235,6 @@ void PrettyMPIUnitTestResultPrinter::OnTestStart(const ::testing::TestInfo& test
218235
// Taken / modified from Googletest
219236
void PrettyMPIUnitTestResultPrinter::OnTestPartResult(const ::testing::TestPartResult& result) {
220237
using namespace ::testing;
221-
using namespace ::testing::internal;
222238
// If the test part succeeded, we don't need to do anything.
223239
if (result.type() == TestPartResult::kSuccess) return;
224240
failed_results_.Add(result);
@@ -228,13 +244,11 @@ void PrettyMPIUnitTestResultPrinter::OnTestPartResult(const ::testing::TestPartR
228244
void PrintFailedTestResultCollection(const TestPartResultCollection& collection, int rank) {
229245
for (std::size_t i = 0; i < collection.Size(); ++i) {
230246
std::string m =
231-
(::testing::Message() << "Rank " << rank << ": "
232-
<< ::testing::internal::FormatFileLocation(
233-
collection.file_names.get_str(i), collection.line_numbers[i])
234-
<< " "
235-
<< TestPartResultTypeToString(
236-
::testing::TestPartResult::Type(collection.types[i]))
237-
<< collection.messages.get_str(i))
247+
(::testing::Message()
248+
<< "Rank " << rank << ": "
249+
<< FormatFileLocation(collection.file_names.get_str(i), collection.line_numbers[i]) << " "
250+
<< TestPartResultTypeToString(::testing::TestPartResult::Type(collection.types[i]))
251+
<< collection.messages.get_str(i))
238252
.GetString();
239253
printf("%s\n", m.c_str());
240254
fflush(stdout);
@@ -244,7 +258,6 @@ void PrintFailedTestResultCollection(const TestPartResultCollection& collection,
244258
// Taken / modified from Googletest
245259
void PrettyMPIUnitTestResultPrinter::OnTestEnd(const ::testing::TestInfo& test_info) {
246260
using namespace ::testing;
247-
using namespace ::testing::internal;
248261

249262
// check if any ranks failed
250263
int failed_locally = failed_results_.Size() > 0;
@@ -308,7 +321,7 @@ void PrettyMPIUnitTestResultPrinter::OnTestEnd(const ::testing::TestInfo& test_i
308321
if (failed_globally) PrintFullTestCommentIfPresent(test_info);
309322

310323
if (GTEST_FLAG(print_time)) {
311-
printf(" (%s ms)\n", internal::StreamableToString(test_info.result()->elapsed_time()).c_str());
324+
printf(" (%s ms)\n", std::to_string(test_info.result()->elapsed_time()).c_str());
312325
} else {
313326
printf("\n");
314327
}
@@ -323,7 +336,7 @@ void PrettyMPIUnitTestResultPrinter::OnTestCaseEnd(const ::testing::TestCase& te
323336
const std::string counts = FormatCountableNoun(test_case.test_to_run_count(), "test", "tests");
324337
ColoredPrintf(COLOR_GREEN, "[----------] ");
325338
printf("%s from %s (%s ms total)\n\n", counts.c_str(), test_case.name(),
326-
internal::StreamableToString(test_case.elapsed_time()).c_str());
339+
std::to_string(test_case.elapsed_time()).c_str());
327340
fflush(stdout);
328341
}
329342

@@ -368,7 +381,7 @@ void PrettyMPIUnitTestResultPrinter::OnTestIterationEnd(const ::testing::UnitTes
368381
printf("%s from %s ran on %d ranks.", FormatTestCount(unit_test.test_to_run_count()).c_str(),
369382
FormatTestCaseCount(unit_test.test_case_to_run_count()).c_str(), comm_size_);
370383
if (GTEST_FLAG(print_time)) {
371-
printf(" (%s ms total)", internal::StreamableToString(unit_test.elapsed_time()).c_str());
384+
printf(" (%s ms total)", std::to_string(unit_test.elapsed_time()).c_str());
372385
}
373386
printf("\n");
374387
ColoredPrintf(COLOR_GREEN, "[ PASSED ] ");
@@ -412,4 +425,3 @@ void PrettyMPIUnitTestResultPrinter::OnEnvironmentsTearDownStart(
412425
} // namespace gtest_mpi
413426

414427
#endif
415-

include/gtest_mpi/gtest_mpi_internal.hpp

+34-27
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,17 @@
6969
#include <gtest/gtest.h>
7070
#include <mpi.h>
7171
#include <unistd.h>
72+
73+
#include <algorithm>
7274
#include <cstdarg>
75+
#include <cstdint>
76+
#include <cstring>
7377
#include <string>
7478

7579
namespace gtest_mpi {
76-
namespace { // no external linkage
80+
namespace { // no external linkage
7781

82+
using Int32 = std::int32_t;
7883
// Taken / modified from Googletest
7984
static const char kDisableTestFilter[] = "DISABLED_*:*/DISABLED_*";
8085
static const char kDeathTestCaseFilter[] = "*DeathTest:*DeathTest/*";
@@ -107,36 +112,40 @@ static void PrintFullTestCommentIfPresent(const ::testing::TestInfo& test_info)
107112
}
108113
}
109114

115+
bool CStringEquals(const char* l, const char* r) { return std::strcmp(l, r) == 0; }
116+
117+
bool CaseInsensitiveCStringEquals(const char* l, const char* r) {
118+
bool equal = true;
119+
120+
for (; equal && *l && *r; ++l, ++r) {
121+
equal &= std::tolower(*l) == std::tolower(*r);
122+
}
123+
124+
equal &= *l == *r;
125+
return equal;
126+
}
127+
110128
// Taken / modified from Googletest
111129
bool ShouldUseColor(bool stdout_is_tty) {
112130
using namespace ::testing;
113-
using namespace ::testing::internal;
114131
const char* const gtest_color = GTEST_FLAG(color).c_str();
115132

116-
if (String::CaseInsensitiveCStringEquals(gtest_color, "auto")) {
117-
#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
118-
// On Windows the TERM variable is usually not set, but the
119-
// console there does support colors.
120-
return stdout_is_tty;
121-
#else
133+
if (CaseInsensitiveCStringEquals(gtest_color, "auto")) {
122134
// On non-Windows platforms, we rely on the TERM variable.
123135
const char* const term = getenv("TERM");
124136
const bool term_supports_color =
125-
String::CStringEquals(term, "xterm") || String::CStringEquals(term, "xterm-color") ||
126-
String::CStringEquals(term, "xterm-256color") || String::CStringEquals(term, "screen") ||
127-
String::CStringEquals(term, "screen-256color") || String::CStringEquals(term, "tmux") ||
128-
String::CStringEquals(term, "tmux-256color") ||
129-
String::CStringEquals(term, "rxvt-unicode") ||
130-
String::CStringEquals(term, "rxvt-unicode-256color") ||
131-
String::CStringEquals(term, "linux") || String::CStringEquals(term, "cygwin");
137+
CStringEquals(term, "xterm") || CStringEquals(term, "xterm-color") ||
138+
CStringEquals(term, "xterm-256color") || CStringEquals(term, "screen") ||
139+
CStringEquals(term, "screen-256color") || CStringEquals(term, "tmux") ||
140+
CStringEquals(term, "tmux-256color") || CStringEquals(term, "rxvt-unicode") ||
141+
CStringEquals(term, "rxvt-unicode-256color") || CStringEquals(term, "linux") ||
142+
CStringEquals(term, "cygwin");
132143
return stdout_is_tty && term_supports_color;
133-
#endif // GTEST_OS_WINDOWS
134144
}
135145

136-
return String::CaseInsensitiveCStringEquals(gtest_color, "yes") ||
137-
String::CaseInsensitiveCStringEquals(gtest_color, "true") ||
138-
String::CaseInsensitiveCStringEquals(gtest_color, "t") ||
139-
String::CStringEquals(gtest_color, "1");
146+
return CaseInsensitiveCStringEquals(gtest_color, "yes") ||
147+
CaseInsensitiveCStringEquals(gtest_color, "true") ||
148+
CaseInsensitiveCStringEquals(gtest_color, "t") || CStringEquals(gtest_color, "1");
140149
// We take "yes", "true", "t", and "1" as meaning "yes". If the
141150
// value is neither one of these nor "auto", we treat it as "no" to
142151
// be conservative.
@@ -172,13 +181,12 @@ static void ColoredPrintf(GTestColor color, const char* fmt, ...) {
172181

173182
printf("\033[0;3%sm", GetAnsiColorCode(color));
174183
vprintf(fmt, args);
175-
printf("\033[m"); // Resets the terminal to default.
184+
printf("\033[m"); // Resets the terminal to default.
176185
va_end(args);
177186
}
178187

179188
// Taken / modified from Googletest
180-
::testing::internal::Int32 Int32FromEnvOrDie(const char* var,
181-
::testing::internal::Int32 default_val) {
189+
Int32 Int32FromEnvOrDie(const char* var, Int32 default_val) {
182190
using namespace ::testing;
183191
using namespace ::testing::internal;
184192
const char* str_val = getenv(var);
@@ -197,7 +205,7 @@ ::testing::internal::Int32 Int32FromEnvOrDie(const char* var,
197205
static std::string FormatCountableNoun(int count, const char* singular_form,
198206
const char* plural_form) {
199207
using namespace ::testing;
200-
return internal::StreamableToString(count) + " " + (count == 1 ? singular_form : plural_form);
208+
return std::to_string(count) + " " + (count == 1 ? singular_form : plural_form);
201209
}
202210

203211
// Taken / modified from Googletest
@@ -392,7 +400,6 @@ struct TestPartResultCollection {
392400
StringCollection file_names;
393401
};
394402

395-
} // anonymous namespace
396-
} // namespace gtest_mpi
403+
} // anonymous namespace
404+
} // namespace gtest_mpi
397405
#endif
398-

0 commit comments

Comments
 (0)