34 #error You must not include this file twice.
36 #define GOOGLETEST_H__
50 #include <sys/types.h>
59 #if __cplusplus < 201103L && !defined(_MSC_VER)
60 #define GOOGLE_GLOG_THROW_BAD_ALLOC throw (std::bad_alloc)
62 #define GOOGLE_GLOG_THROW_BAD_ALLOC
73 _END_GOOGLE_NAMESPACE_
79 vector<string> temp_directories_list;
82 if (temp_directories_list.empty()) {
83 fprintf(stderr,
"No temporary directory found\n");
88 return temp_directories_list.front();
91 #if defined(GLOG_OS_WINDOWS) && defined(_MSC_VER) && !defined(TEST_SRC_DIR)
95 #elif !defined(TEST_SRC_DIR)
96 # warning TEST_SRC_DIR should be defined in config.h
104 "Source-dir root, needed to find glog_unittest_flagfile");
107 DEFINE_int32(benchmark_iters, 100000000,
"Number of iterations per benchmark");
109 DEFINE_int32(benchmark_iters, 100000,
"Number of iterations per benchmark");
112 #ifdef HAVE_LIB_GTEST
116 # undef ASSERT_DEBUG_DEATH
128 #define EXPECT_NEAR(val1, val2, abs_error) \
130 if (abs(val1 - val2) > abs_error) { \
131 fprintf(stderr, "Check failed: %s within %s of %s\n", #val1, #abs_error, \
133 exit(EXIT_FAILURE); \
137 #define EXPECT_TRUE(cond) \
140 fprintf(stderr, "Check failed: %s\n", #cond); \
141 exit(EXIT_FAILURE); \
145 #define EXPECT_FALSE(cond) EXPECT_TRUE(!(cond))
147 #define EXPECT_OP(op, val1, val2) \
149 if (!((val1) op (val2))) { \
150 fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2); \
151 exit(EXIT_FAILURE); \
155 #define EXPECT_EQ(val1, val2) EXPECT_OP(==, val1, val2)
156 #define EXPECT_NE(val1, val2) EXPECT_OP(!=, val1, val2)
157 #define EXPECT_GT(val1, val2) EXPECT_OP(>, val1, val2)
158 #define EXPECT_LT(val1, val2) EXPECT_OP(<, val1, val2)
160 #define EXPECT_NAN(arg) \
163 fprintf(stderr, "Check failed: isnan(%s)\n", #arg); \
164 exit(EXIT_FAILURE); \
168 #define EXPECT_INF(arg) \
171 fprintf(stderr, "Check failed: isinf(%s)\n", #arg); \
172 exit(EXIT_FAILURE); \
176 #define EXPECT_DOUBLE_EQ(val1, val2) \
178 if (((val1) < (val2) - 0.001 || (val1) > (val2) + 0.001)) { \
179 fprintf(stderr, "Check failed: %s == %s\n", #val1, #val2); \
180 exit(EXIT_FAILURE); \
184 #define EXPECT_STREQ(val1, val2) \
186 if (strcmp((val1), (val2)) != 0) { \
187 fprintf(stderr, "Check failed: streq(%s, %s)\n", #val1, #val2); \
188 exit(EXIT_FAILURE); \
195 struct Test_##a##_##b { \
196 Test_##a##_##b() { g_testlist.push_back(&Run); } \
197 static void Run() { FlagSaver fs; RunTest(); } \
198 static void RunTest(); \
200 static Test_##a##_##b g_test_##a##_##b; \
201 void Test_##a##_##b::RunTest()
205 vector<
void (*)()>::const_iterator
it;
209 fprintf(stderr,
"Passed %d tests\n\nPASS\n",
214 _END_GOOGLE_NAMESPACE_
216 #endif // ! HAVE_LIB_GTEST
227 #ifdef GLOG_OS_WINDOWS
229 #define ASSERT_DEATH(fn, msg)
231 #define ASSERT_DEATH(fn, msg) \
233 g_called_abort = false; \
235 void (*original_logging_fail_func)() = g_logging_fail_func; \
236 g_logging_fail_func = &CalledAbort; \
237 if (!setjmp(g_jmp_buf)) fn; \
239 g_logging_fail_func = original_logging_fail_func; \
240 if (!g_called_abort) { \
241 fprintf(stderr, "Function didn't die (%s): %s\n", msg, #fn); \
242 exit(EXIT_FAILURE); \
248 #define ASSERT_DEBUG_DEATH(fn, msg)
250 #define ASSERT_DEBUG_DEATH(fn, msg) ASSERT_DEATH(fn, msg)
255 #define BENCHMARK(n) static BenchmarkRegisterer __benchmark_ ## n (#n, &n);
267 if (!FLAGS_run_benchmark) {
271 int iter_cnt = FLAGS_benchmark_iters;
272 puts(
"Benchmark\tTime(ns)\tIterations");
273 for (
map<
string,
void (*)(
int)>::const_iterator iter =
g_benchlist.begin();
276 clock_t
start = clock();
277 iter->second(iter_cnt);
278 double elapsed_ns = (
static_cast<double>(clock()) -
start) /
279 CLOCKS_PER_SEC * 1000 * 1000 * 1000;
280 #if defined(__GNUC__) && !defined(__clang__)
281 #pragma GCC diagnostic push
282 #pragma GCC diagnostic ignored "-Wformat="
284 printf(
"%s\t%8.2lf\t%10d\n",
285 iter->first.c_str(), elapsed_ns / iter_cnt, iter_cnt);
286 #if defined(__GNUC__) && !defined(__clang__)
287 #pragma GCC diagnostic pop
321 O_CREAT | O_TRUNC | O_WRONLY,
328 CHECK(close(cap_fd) != -1);
352 CHECK((fd == STDOUT_FILENO) || (fd == STDERR_FILENO));
364 fseek(file, 0, SEEK_END);
365 return static_cast<size_t>(ftell(file));
370 char *
const buffer =
new char[file_size];
372 size_t bytes_last_read = 0;
373 size_t bytes_read = 0;
375 fseek(file, 0, SEEK_SET);
380 bytes_last_read = fread(
buffer+bytes_read, 1, file_size-bytes_read, file);
381 bytes_read += bytes_last_read;
382 }
while (bytes_last_read > 0 && bytes_read < file_size);
392 CHECK(fd == STDOUT_FILENO || fd == STDERR_FILENO);
395 <<
": did you forget CaptureTestStdout() or CaptureTestStderr()?";
401 FILE *
const file = fopen(cap->
filename().c_str(),
"r");
422 if (!strchr(
"IWEF",
s[0]))
return false;
423 for (
size_t i = 1;
i <= 8; ++
i) {
424 if (!isdigit(
s[
i]) &&
s[
i] !=
"YEARDATE"[
i-1])
return false;
435 string before, logcode_date, time, thread_lineinfo;
436 std::size_t begin_of_logging_prefix = 0;
438 ++begin_of_logging_prefix) {
446 }
else if (begin_of_logging_prefix > 0) {
447 before = line.substr(0, begin_of_logging_prefix - 1);
449 std::istringstream iss(line.substr(begin_of_logging_prefix));
452 iss >> thread_lineinfo;
453 CHECK(!thread_lineinfo.empty());
454 if (thread_lineinfo[thread_lineinfo.size() - 1] !=
']') {
460 thread_lineinfo =
"THREADID " + tmp;
462 size_t index = thread_lineinfo.find(
':');
464 thread_lineinfo = thread_lineinfo.substr(0,
index+1) +
"LINE]";
466 std::getline(iss, rest);
467 return (before + logcode_date[0] +
"YEARDATE TIME__ " + thread_lineinfo +
472 const string& oldsub,
473 const string& newsub) {
474 size_t pos =
str->find(oldsub);
475 if (pos != string::npos) {
476 str->replace(pos, oldsub.size(), newsub);
480 static inline string Munge(
const string& filename) {
481 FILE* fp = fopen(filename.c_str(),
"rb");
482 CHECK(fp !=
NULL) << filename <<
": couldn't open";
485 while (fgets(
buf, 4095, fp)) {
487 const size_t str_size = 256;
488 char null_str[str_size];
489 char ptr_str[str_size];
490 snprintf(null_str, str_size,
"%p",
static_cast<void*
>(
NULL));
501 result += line +
"\n";
507 static inline void WriteToFile(
const string& body,
const string& file) {
508 FILE* fp = fopen(file.c_str(),
"wb");
509 fwrite(body.data(), 1, body.size(), fp);
516 CHECK(cap) <<
": did you forget CaptureTestStdout()?";
518 CHECK(cap) <<
": did you forget CaptureTestStderr()?";
525 const string golden =
Munge(golden_filename);
526 if (captured != golden) {
528 "Test with golden file failed. We'll try to show the diff:\n");
529 string munged_golden = golden_filename +
".munged";
531 string munged_captured = cap->
filename() +
".munged";
533 #ifdef GLOG_OS_WINDOWS
534 string diffcmd(
"fc " + munged_golden +
" " + munged_captured);
536 string diffcmd(
"diff -u " + munged_golden +
" " + munged_captured);
538 if (system(diffcmd.c_str()) != 0) {
539 fprintf(stderr,
"diff command was failed.\n");
541 unlink(munged_golden.c_str());
542 unlink(munged_captured.c_str());
545 LOG(
INFO) <<
"Diff was successful";
558 #ifndef HAVE_LIB_GFLAGS
583 #if defined(GLOG_OS_WINDOWS) && !defined(GLOG_OS_CYGWIN)
585 handle_ = CreateThread(
NULL,
587 &Thread::InvokeThreadW,
591 CHECK(handle_) <<
"CreateThread";
594 WaitForSingleObject(handle_, INFINITE);
596 #elif defined(HAVE_PTHREAD)
604 # error No thread implementation.
608 virtual void Run() = 0;
612 (
static_cast<Thread*
>(
self))->Run();
616 #if defined(GLOG_OS_WINDOWS) && !defined(GLOG_OS_CYGWIN)
617 static DWORD __stdcall InvokeThreadW(LPVOID
self) {
629 #ifndef GLOG_OS_WINDOWS
630 # if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 199309L
631 const struct timespec
req = {0, t * 1000 * 1000};
645 _END_GOOGLE_NAMESPACE_
655 return ::operator
new(
size);
658 void operator delete(
void*
p)
throw() {
662 void operator delete(
void*
p, size_t)
throw() {
663 ::operator
delete(
p);
666 void operator delete[](
void*
p)
throw() {
667 ::operator
delete(
p);
670 void operator delete[](
void*
p, size_t)
throw() {
671 ::operator
delete(
p);