TestMain.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===--- utils/unittest/SPIRV/TestMain.cpp - unittest driver --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include "gmock/gmock.h"
  10. #include "gtest/gtest.h"
  11. #include "llvm/Support/Signals.h"
  12. #include "SpirvTestOptions.h"
  13. #include "dxc/Support/Global.h"
  14. #if defined(_WIN32)
  15. #include <windows.h>
  16. #if defined(_MSC_VER)
  17. #include <crtdbg.h>
  18. #endif
  19. #endif
  20. namespace {
  21. using namespace ::testing;
  22. /// A GoogleTest event printer that only prints test failures.
  23. class FailurePrinter : public TestEventListener {
  24. public:
  25. explicit FailurePrinter(TestEventListener *listener)
  26. : defaultListener(listener) {}
  27. ~FailurePrinter() override { delete defaultListener; }
  28. void OnTestProgramStart(const UnitTest &ut) override {
  29. defaultListener->OnTestProgramStart(ut);
  30. }
  31. void OnTestIterationStart(const UnitTest &ut, int iteration) override {
  32. defaultListener->OnTestIterationStart(ut, iteration);
  33. }
  34. void OnEnvironmentsSetUpStart(const UnitTest &ut) override {
  35. defaultListener->OnEnvironmentsSetUpStart(ut);
  36. }
  37. void OnEnvironmentsSetUpEnd(const UnitTest &ut) override {
  38. defaultListener->OnEnvironmentsSetUpEnd(ut);
  39. }
  40. void OnTestCaseStart(const TestCase &tc) override {
  41. defaultListener->OnTestCaseStart(tc);
  42. }
  43. void OnTestStart(const TestInfo &ti) override {
  44. // Do not output on test start
  45. // defaultListener->OnTestStart(ti);
  46. }
  47. void OnTestPartResult(const TestPartResult &result) override {
  48. defaultListener->OnTestPartResult(result);
  49. }
  50. void OnTestEnd(const TestInfo &ti) override {
  51. // Only output if failure on test end
  52. if (ti.result()->Failed())
  53. defaultListener->OnTestEnd(ti);
  54. }
  55. void OnTestCaseEnd(const TestCase &tc) override {
  56. defaultListener->OnTestCaseEnd(tc);
  57. }
  58. void OnEnvironmentsTearDownStart(const UnitTest &ut) override {
  59. defaultListener->OnEnvironmentsTearDownStart(ut);
  60. }
  61. void OnEnvironmentsTearDownEnd(const UnitTest &ut) override {
  62. defaultListener->OnEnvironmentsTearDownEnd(ut);
  63. }
  64. void OnTestIterationEnd(const UnitTest &ut, int iteration) override {
  65. defaultListener->OnTestIterationEnd(ut, iteration);
  66. }
  67. void OnTestProgramEnd(const UnitTest &ut) override {
  68. defaultListener->OnTestProgramEnd(ut);
  69. }
  70. private:
  71. TestEventListener *defaultListener;
  72. };
  73. } // namespace
  74. const char *TestMainArgv0;
  75. int main(int argc, char **argv) {
  76. llvm::sys::PrintStackTraceOnErrorSignal(true /* Disable crash reporting */);
  77. for (int i = 1; i < argc; ++i) {
  78. if (std::string("--spirv-test-root") == argv[i]) {
  79. // Allow the user set the root directory for test input files.
  80. if (i + 1 < argc) {
  81. clang::spirv::testOptions::inputDataDir = argv[++i];
  82. } else {
  83. fprintf(stderr, "Error: --spirv-test-root requires an argument\n");
  84. return 1;
  85. }
  86. }
  87. }
  88. // Initialize both gmock and gtest.
  89. testing::InitGoogleMock(&argc, argv);
  90. // Switch event listener to one that only prints failures.
  91. testing::TestEventListeners &listeners =
  92. ::testing::UnitTest::GetInstance()->listeners();
  93. auto *defaultPrinter = listeners.Release(listeners.default_result_printer());
  94. // Google Test takes the ownership.
  95. listeners.Append(new FailurePrinter(defaultPrinter));
  96. // Make it easy for a test to re-execute itself by saving argv[0].
  97. TestMainArgv0 = argv[0];
  98. #if defined(_WIN32)
  99. // Disable all of the possible ways Windows conspires to make automated
  100. // testing impossible.
  101. ::SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
  102. #if defined(_MSC_VER)
  103. ::_set_error_mode(_OUT_TO_STDERR);
  104. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  105. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
  106. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  107. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
  108. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
  109. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
  110. #endif
  111. #endif
  112. // DxcInitThreadMalloc()/DxcCleanupThreadMalloc() only once for module.
  113. DxcInitThreadMalloc();
  114. int result = RUN_ALL_TESTS();
  115. DxcCleanupThreadMalloc();
  116. return result;
  117. }