googletest.h 18.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright (c) 2009, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Author: Shinichiro Hamaji
//   (based on googletest: http://code.google.com/p/googletest/)

's avatar
committed
33 34 35 36 37
#ifdef GOOGLETEST_H__
#error You must not include this file twice.
#endif
#define GOOGLETEST_H__

's avatar
committed
38 39
#include "utilities.h"

's avatar
committed
40 41 42 43 44 45 46 47 48
#include <ctype.h>
#include <setjmp.h>
#include <time.h>

#include <map>
#include <sstream>
#include <string>
#include <vector>

49 50 51
#include <stdio.h>
#include <stdlib.h>

's avatar
committed
52 53 54
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
55 56 57
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
's avatar
committed
58 59 60 61 62 63 64

#include "base/commandlineflags.h"

using std::map;
using std::string;
using std::vector;

65 66 67 68 69 70 71 72 73
_START_GOOGLE_NAMESPACE_

extern GOOGLE_GLOG_DLL_DECL void (*g_logging_fail_func)();

_END_GOOGLE_NAMESPACE_

#undef GOOGLE_GLOG_DLL_DECL
#define GOOGLE_GLOG_DLL_DECL

74
static inline string GetTempDir() {
75 76 77 78 79 80 81 82 83
#ifndef OS_WINDOWS
  return "/tmp";
#else
  char tmp[MAX_PATH];
  GetTempPathA(MAX_PATH, tmp);
  return tmp;
#endif
}

84
#if defined(OS_WINDOWS) && defined(_MSC_VER) && !defined(TEST_SRC_DIR)
85 86 87
// The test will run in glog/vsproject/<project name>
// (e.g., glog/vsproject/logging_unittest).
static const char TEST_SRC_DIR[] = "../..";
88 89
#elif !defined(TEST_SRC_DIR)
# warning TEST_SRC_DIR should be defined in config.h
90 91 92 93 94
static const char TEST_SRC_DIR[] = ".";
#endif

DEFINE_string(test_tmpdir, GetTempDir(), "Dir we use for temp files");
DEFINE_string(test_srcdir, TEST_SRC_DIR,
's avatar
committed
95
              "Source-dir root, needed to find glog_unittest_flagfile");
96
DEFINE_bool(run_benchmark, false, "If true, run benchmarks");
97
#ifdef NDEBUG
's avatar
committed
98
DEFINE_int32(benchmark_iters, 100000000, "Number of iterations per benchmark");
99
#else
's avatar
committed
100
DEFINE_int32(benchmark_iters, 100000, "Number of iterations per benchmark");
101
#endif
's avatar
committed
102

's avatar
committed
103 104 105 106 107 108 109 110
#ifdef HAVE_LIB_GTEST
# include <gtest/gtest.h>
// Use our ASSERT_DEATH implementation.
# undef ASSERT_DEATH
# undef ASSERT_DEBUG_DEATH
using testing::InitGoogleTest;
#else

's avatar
committed
111 112
_START_GOOGLE_NAMESPACE_

113
void InitGoogleTest(int*, char**) {}
's avatar
committed
114

's avatar
committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
// The following is some bare-bones testing infrastructure

#define EXPECT_TRUE(cond)                               \
  do {                                                  \
    if (!(cond)) {                                      \
      fprintf(stderr, "Check failed: %s\n", #cond);     \
      exit(1);                                          \
    }                                                   \
  } while (0)

#define EXPECT_FALSE(cond)  EXPECT_TRUE(!(cond))

#define EXPECT_OP(op, val1, val2)                                       \
  do {                                                                  \
    if (!((val1) op (val2))) {                                          \
      fprintf(stderr, "Check failed: %s %s %s\n", #val1, #op, #val2);   \
      exit(1);                                                          \
    }                                                                   \
  } while (0)

#define EXPECT_EQ(val1, val2)  EXPECT_OP(==, val1, val2)
#define EXPECT_NE(val1, val2)  EXPECT_OP(!=, val1, val2)
#define EXPECT_GT(val1, val2)  EXPECT_OP(>, val1, val2)
#define EXPECT_LT(val1, val2)  EXPECT_OP(<, val1, val2)

#define EXPECT_NAN(arg)                                         \
  do {                                                          \
    if (!isnan(arg)) {                                          \
      fprintf(stderr, "Check failed: isnan(%s)\n", #arg);       \
      exit(1);                                                  \
    }                                                           \
  } while (0)

#define EXPECT_INF(arg)                                         \
  do {                                                          \
    if (!isinf(arg)) {                                          \
      fprintf(stderr, "Check failed: isinf(%s)\n", #arg);       \
      exit(1);                                                  \
    }                                                           \
  } while (0)

#define EXPECT_DOUBLE_EQ(val1, val2)                                    \
  do {                                                                  \
    if (((val1) < (val2) - 0.001 || (val1) > (val2) + 0.001)) {         \
      fprintf(stderr, "Check failed: %s == %s\n", #val1, #val2);        \
      exit(1);                                                          \
    }                                                                   \
  } while (0)

#define EXPECT_STREQ(val1, val2)                                        \
  do {                                                                  \
    if (strcmp((val1), (val2)) != 0) {                                  \
      fprintf(stderr, "Check failed: streq(%s, %s)\n", #val1, #val2);   \
      exit(1);                                                          \
    }                                                                   \
  } while (0)

's avatar
committed
172 173 174 175 176 177 178 179 180 181 182 183
vector<void (*)()> g_testlist;  // the tests to run

#define TEST(a, b)                                      \
  struct Test_##a##_##b {                               \
    Test_##a##_##b() { g_testlist.push_back(&Run); }    \
    static void Run() { FlagSaver fs; RunTest(); }      \
    static void RunTest();                              \
  };                                                    \
  static Test_##a##_##b g_test_##a##_##b;               \
  void Test_##a##_##b::RunTest()


184
static inline int RUN_ALL_TESTS() {
's avatar
committed
185 186 187 188 189 190 191 192 193 194
  vector<void (*)()>::const_iterator it;
  for (it = g_testlist.begin(); it != g_testlist.end(); ++it) {
    (*it)();
  }
  fprintf(stderr, "Passed %d tests\n\nPASS\n", (int)g_testlist.size());
  return 0;
}

_END_GOOGLE_NAMESPACE_

's avatar
committed
195
#endif  // ! HAVE_LIB_GTEST
's avatar
committed
196 197 198

_START_GOOGLE_NAMESPACE_

's avatar
committed
199 200
static bool g_called_abort;
static jmp_buf g_jmp_buf;
201
static inline void CalledAbort() {
's avatar
committed
202 203 204 205
  g_called_abort = true;
  longjmp(g_jmp_buf, 1);
}

206 207 208 209
#ifdef OS_WINDOWS
// TODO(hamaji): Death test somehow doesn't work in Windows.
#define ASSERT_DEATH(fn, msg)
#else
's avatar
committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
#define ASSERT_DEATH(fn, msg)                                           \
  do {                                                                  \
    g_called_abort = false;                                             \
    /* in logging.cc */                                                 \
    void (*original_logging_fail_func)() = g_logging_fail_func;         \
    g_logging_fail_func = &CalledAbort;                                 \
    if (!setjmp(g_jmp_buf)) fn;                                         \
    /* set back to their default */                                     \
    g_logging_fail_func = original_logging_fail_func;                   \
    if (!g_called_abort) {                                              \
      fprintf(stderr, "Function didn't die (%s): %s\n", msg, #fn);      \
      exit(1);                                                          \
    }                                                                   \
  } while (0)
224
#endif
's avatar
committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

#ifdef NDEBUG
#define ASSERT_DEBUG_DEATH(fn, msg)
#else
#define ASSERT_DEBUG_DEATH(fn, msg) ASSERT_DEATH(fn, msg)
#endif  // NDEBUG

// Benchmark tools.

#define BENCHMARK(n) static BenchmarkRegisterer __benchmark_ ## n (#n, &n);

map<string, void (*)(int)> g_benchlist;  // the benchmarks to run

class BenchmarkRegisterer {
 public:
  BenchmarkRegisterer(const char* name, void (*function)(int iters)) {
    EXPECT_TRUE(g_benchlist.insert(std::make_pair(name, function)).second);
  }
};

245
static inline void RunSpecifiedBenchmarks() {
246 247 248 249
  if (!FLAGS_run_benchmark) {
    return;
  }

's avatar
committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
  int iter_cnt = FLAGS_benchmark_iters;
  puts("Benchmark\tTime(ns)\tIterations");
  for (map<string, void (*)(int)>::const_iterator iter = g_benchlist.begin();
       iter != g_benchlist.end();
       ++iter) {
    clock_t start = clock();
    iter->second(iter_cnt);
    double elapsed_ns =
        ((double)clock() - start) / CLOCKS_PER_SEC * 1000*1000*1000;
    printf("%s\t%8.2lf\t%10d\n",
           iter->first.c_str(), elapsed_ns / iter_cnt, iter_cnt);
  }
  puts("");
}

// ----------------------------------------------------------------------
// Golden file functions
// ----------------------------------------------------------------------

class CapturedStream {
 public:
  CapturedStream(int fd, const string & filename) :
    fd_(fd),
    uncaptured_fd_(-1),
    filename_(filename) {
    Capture();
  }

  ~CapturedStream() {
    if (uncaptured_fd_ != -1) {
      CHECK(close(uncaptured_fd_) != -1);
    }
  }

  // Start redirecting output to a file
  void Capture() {
    // Keep original stream for later
    CHECK(uncaptured_fd_ == -1) << ", Stream " << fd_ << " already captured!";
    uncaptured_fd_ = dup(fd_);
    CHECK(uncaptured_fd_ != -1);

    // Open file to save stream to
    int cap_fd = open(filename_.c_str(),
                      O_CREAT | O_TRUNC | O_WRONLY,
                      S_IRUSR | S_IWUSR);
    CHECK(cap_fd != -1);

    // Send stdout/stderr to this file
    fflush(NULL);
    CHECK(dup2(cap_fd, fd_) != -1);
    CHECK(close(cap_fd) != -1);
  }

  // Remove output redirection
  void StopCapture() {
    // Restore original stream
    if (uncaptured_fd_ != -1) {
      fflush(NULL);
      CHECK(dup2(uncaptured_fd_, fd_) != -1);
    }
  }

  const string & filename() const { return filename_; }

 private:
  int fd_;             // file descriptor being captured
  int uncaptured_fd_;  // where the stream was originally being sent to
  string filename_;    // file where stream is being saved
};
static CapturedStream * s_captured_streams[STDERR_FILENO+1];
// Redirect a file descriptor to a file.
//   fd       - Should be STDOUT_FILENO or STDERR_FILENO
//   filename - File where output should be stored
323
static inline void CaptureTestOutput(int fd, const string & filename) {
's avatar
committed
324 325 326 327
  CHECK((fd == STDOUT_FILENO) || (fd == STDERR_FILENO));
  CHECK(s_captured_streams[fd] == NULL);
  s_captured_streams[fd] = new CapturedStream(fd, filename);
}
328
static inline void CaptureTestStderr() {
's avatar
committed
329 330 331
  CaptureTestOutput(STDERR_FILENO, FLAGS_test_tmpdir + "/captured.err");
}
// Return the size (in bytes) of a file
332
static inline size_t GetFileSize(FILE * file) {
's avatar
committed
333 334 335 336
  fseek(file, 0, SEEK_END);
  return static_cast<size_t>(ftell(file));
}
// Read the entire content of a file as a string
337
static inline string ReadEntireFile(FILE * file) {
's avatar
committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  const size_t file_size = GetFileSize(file);
  char * const buffer = new char[file_size];

  size_t bytes_last_read = 0;  // # of bytes read in the last fread()
  size_t bytes_read = 0;       // # of bytes read so far

  fseek(file, 0, SEEK_SET);

  // Keep reading the file until we cannot read further or the
  // pre-determined file size is reached.
  do {
    bytes_last_read = fread(buffer+bytes_read, 1, file_size-bytes_read, file);
    bytes_read += bytes_last_read;
  } while (bytes_last_read > 0 && bytes_read < file_size);

  const string content = string(buffer, buffer+bytes_read);
  delete[] buffer;

  return content;
}
// Get the captured stdout (when fd is STDOUT_FILENO) or stderr (when
// fd is STDERR_FILENO) as a string
360
static inline string GetCapturedTestOutput(int fd) {
's avatar
committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
  CHECK(fd == STDOUT_FILENO || fd == STDERR_FILENO);
  CapturedStream * const cap = s_captured_streams[fd];
  CHECK(cap)
    << ": did you forget CaptureTestStdout() or CaptureTestStderr()?";

  // Make sure everything is flushed.
  cap->StopCapture();

  // Read the captured file.
  FILE * const file = fopen(cap->filename().c_str(), "r");
  const string content = ReadEntireFile(file);
  fclose(file);

  delete cap;
  s_captured_streams[fd] = NULL;

  return content;
}
// Get the captured stderr of a test as a string.
380
static inline string GetCapturedTestStderr() {
's avatar
committed
381 382 383 384
  return GetCapturedTestOutput(STDERR_FILENO);
}

// Check if the string is [IWEF](\d{4}|DATE)
385
static inline bool IsLoggingPrefix(const string& s) {
's avatar
committed
386 387 388 389 390 391 392 393 394 395 396 397 398
  if (s.size() != 5) return false;
  if (!strchr("IWEF", s[0])) return false;
  for (int i = 1; i <= 4; ++i) {
    if (!isdigit(s[i]) && s[i] != "DATE"[i-1]) return false;
  }
  return true;
}

// Convert log output into normalized form.
//
// Example:
//     I0102 030405 logging_unittest.cc:345] RAW: vlog -1
//  => IDATE TIME__ logging_unittest.cc:LINE] RAW: vlog -1
399
static inline string MungeLine(const string& line) {
's avatar
committed
400 401 402 403 404
  std::istringstream iss(line);
  string before, logcode_date, time, thread_lineinfo;
  iss >> logcode_date;
  while (!IsLoggingPrefix(logcode_date)) {
    before += " " + logcode_date;
405
    if (!(iss >> logcode_date)) {
's avatar
committed
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
      // We cannot find the header of log output.
      return before;
    }
  }
  if (!before.empty()) before += " ";
  iss >> time;
  iss >> thread_lineinfo;
  CHECK(!thread_lineinfo.empty());
  if (thread_lineinfo[thread_lineinfo.size() - 1] != ']') {
    // We found thread ID.
    string tmp;
    iss >> tmp;
    CHECK(!tmp.empty());
    CHECK_EQ(']', tmp[tmp.size() - 1]);
    thread_lineinfo = "THREADID " + tmp;
  }
  size_t index = thread_lineinfo.find(':');
  CHECK_NE(string::npos, index);
  thread_lineinfo = thread_lineinfo.substr(0, index+1) + "LINE]";
  string rest;
  std::getline(iss, rest);
  return (before + logcode_date[0] + "DATE TIME__ " + thread_lineinfo +
          MungeLine(rest));
}

431
static inline void StringReplace(string* str,
's avatar
committed
432 433 434 435 436 437 438 439
                          const string& oldsub,
                          const string& newsub) {
  size_t pos = str->find(oldsub);
  if (pos != string::npos) {
    str->replace(pos, oldsub.size(), newsub.c_str());
  }
}

440
static inline string Munge(const string& filename) {
's avatar
committed
441 442 443 444 445 446 447
  FILE* fp = fopen(filename.c_str(), "rb");
  CHECK(fp != NULL) << filename << ": couldn't open";
  char buf[4096];
  string result;
  while (fgets(buf, 4095, fp)) {
    string line = MungeLine(buf);
    char null_str[256];
448
    sprintf(null_str, "%p", static_cast<void*>(NULL));
's avatar
committed
449
    StringReplace(&line, "__NULLP__", null_str);
450 451
    // Remove 0x prefix produced by %p. VC++ doesn't put the prefix.
    StringReplace(&line, " 0x", " ");
's avatar
committed
452

453 454 455 456 457
    StringReplace(&line, "__SUCCESS__", StrError(0));
    StringReplace(&line, "__ENOENT__", StrError(ENOENT));
    StringReplace(&line, "__EINTR__", StrError(EINTR));
    StringReplace(&line, "__ENXIO__", StrError(ENXIO));
    StringReplace(&line, "__ENOEXEC__", StrError(ENOEXEC));
's avatar
committed
458 459 460 461 462 463
    result += line + "\n";
  }
  fclose(fp);
  return result;
}

464
static inline void WriteToFile(const string& body, const string& file) {
's avatar
committed
465 466 467 468 469
  FILE* fp = fopen(file.c_str(), "wb");
  fwrite(body.data(), 1, body.size(), fp);
  fclose(fp);
}

470
static inline bool MungeAndDiffTestStderr(const string& golden_filename) {
's avatar
committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
  CapturedStream* cap = s_captured_streams[STDERR_FILENO];
  CHECK(cap) << ": did you forget CaptureTestStderr()?";

  cap->StopCapture();

  // Run munge
  const string captured = Munge(cap->filename());
  const string golden = Munge(golden_filename);
  if (captured != golden) {
    fprintf(stderr,
            "Test with golden file failed. We'll try to show the diff:\n");
    string munged_golden = golden_filename + ".munged";
    WriteToFile(golden, munged_golden);
    string munged_captured = cap->filename() + ".munged";
    WriteToFile(captured, munged_captured);
    string diffcmd("diff -u " + munged_golden + " " + munged_captured);
    if (system(diffcmd.c_str()) != 0) {
      fprintf(stderr, "diff command was failed.\n");
    }
    unlink(munged_golden.c_str());
    unlink(munged_captured.c_str());
    return false;
  }
  LOG(INFO) << "Diff was successful";
  return true;
}

// Save flags used from logging_unittest.cc.
#ifndef HAVE_LIB_GFLAGS
struct FlagSaver {
  FlagSaver()
      : v_(FLAGS_v),
        stderrthreshold_(FLAGS_stderrthreshold),
        logtostderr_(FLAGS_logtostderr),
        alsologtostderr_(FLAGS_alsologtostderr) {}
  ~FlagSaver() {
    FLAGS_v = v_;
    FLAGS_stderrthreshold = stderrthreshold_;
    FLAGS_logtostderr = logtostderr_;
    FLAGS_alsologtostderr = alsologtostderr_;
  }
  int v_;
  int stderrthreshold_;
  bool logtostderr_;
  bool alsologtostderr_;
};
#endif

class Thread {
 public:
521 522
  virtual ~Thread() {}

523
  void SetJoinable(bool) {}
524
#if defined(OS_WINDOWS) || defined(OS_CYGWIN)
525 526 527 528 529 530 531 532 533 534 535 536
  void Start() {
    handle_ = CreateThread(NULL,
                           0,
                           (LPTHREAD_START_ROUTINE)&Thread::InvokeThread,
                           (LPVOID)this,
                           0,
                           &th_);
    CHECK(handle_) << "CreateThread";
  }
  void Join() {
    WaitForSingleObject(handle_, INFINITE);
  }
537 538 539 540 541 542 543
#elif defined(HAVE_PTHREAD)
  void Start() {
    pthread_create(&th_, NULL, &Thread::InvokeThread, this);
  }
  void Join() {
    pthread_join(th_, NULL);
  }
544 545 546
#else
# error No thread implementation.
#endif
's avatar
committed
547 548 549 550 551 552 553 554 555 556

 protected:
  virtual void Run() = 0;

 private:
  static void* InvokeThread(void* self) {
    ((Thread*)self)->Run();
    return NULL;
  }

's avatar
committed
557
#if defined(OS_WINDOWS) || defined(OS_CYGWIN)
558
  HANDLE handle_;
's avatar
committed
559 560 561
  DWORD th_;
#else
  pthread_t th_;
562
#endif
's avatar
committed
563 564
};

565
static inline void SleepForMilliseconds(int t) {
566
#ifndef OS_WINDOWS
's avatar
committed
567
  usleep(t * 1000);
568 569 570
#else
  Sleep(t);
#endif
's avatar
committed
571 572 573 574 575 576 577 578
}

// Add hook for operator new to ensure there are no memory allocation.

void (*g_new_hook)() = NULL;

_END_GOOGLE_NAMESPACE_

579
void* operator new(size_t size) throw(std::bad_alloc) {
's avatar
committed
580 581 582 583 584 585
  if (GOOGLE_NAMESPACE::g_new_hook) {
    GOOGLE_NAMESPACE::g_new_hook();
  }
  return malloc(size);
}

586
void* operator new[](size_t size) throw(std::bad_alloc) {
's avatar
committed
587 588 589
  return ::operator new(size);
}

590
void operator delete(void* p) throw() {
's avatar
committed
591 592 593
  free(p);
}

594
void operator delete[](void* p) throw() {
's avatar
committed
595 596
  ::operator delete(p);
}