TestRunner.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2016, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef RS2SPIRV_TEST_RUNNER
  17. #define RS2SPIRV_TEST_RUNNER
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <cassert>
  21. #include <vector>
  22. namespace rs2spirv {
  23. struct TestCase {
  24. void (*testPtr)(void);
  25. const char *const description;
  26. };
  27. class TestRunnerContext {
  28. public:
  29. static TestRunnerContext &getInstance() {
  30. static TestRunnerContext ctx;
  31. return ctx;
  32. }
  33. static void addTest(TestCase TC) { getInstance().tests.push_back(TC); }
  34. static size_t &getCheckSuccessNum() { return getInstance().checkSuccessNum; }
  35. static size_t &getTotalCheckNum() { return getInstance().totalCheckNum; }
  36. static int runTests() {
  37. bool Failed = false;
  38. for (auto &TC : getInstance().tests) {
  39. getCheckSuccessNum() = getTotalCheckNum() = 0;
  40. llvm::outs() << "Test(" << TC.description << ") {\n";
  41. TC.testPtr();
  42. llvm::outs() << "\n} (" << TC.description << ") [" << getCheckSuccessNum()
  43. << "/" << getTotalCheckNum() << "]\n\n";
  44. Failed |= getCheckSuccessNum() != getTotalCheckNum();
  45. }
  46. return static_cast<int>(Failed);
  47. }
  48. private:
  49. TestRunnerContext() = default;
  50. std::vector<TestCase> tests;
  51. size_t checkSuccessNum;
  52. size_t totalCheckNum;
  53. };
  54. struct TestAdder {
  55. TestAdder(TestCase TC) { TestRunnerContext::addTest(TC); }
  56. };
  57. #define RS2SPIRV_CONCAT_IMPL(S1, S2) S1##S2
  58. #define RS2SPIRV_CONCAT(S1, S2) RS2SPIRV_CONCAT_IMPL(S1, S2)
  59. #define RS2SPIRV_ANONYMOUS(X) RS2SPIRV_CONCAT(X, __COUNTER__)
  60. #if RS2SPIRV_DEBUG
  61. #define RS2SPIRV_TEST_CASE_ADD_IMPL(FNAME, VNAME, DESCRIPTION) \
  62. static void FNAME(); \
  63. static rs2spirv::TestAdder VNAME({FNAME, DESCRIPTION}); \
  64. inline void FNAME()
  65. #elif defined(__GNUC__) || defined(__clang__)
  66. #define RS2SPIRV_TEST_CASE_ADD_IMPL(FNAME, VNAME, DESCRIPTION) \
  67. static inline void __attribute__((unused)) FNAME()
  68. #else
  69. #define RS2SPIRV_TEST_CASE_ADD_IMPL(FNAME, VNAME, DESCRIPTION) \
  70. static inline void FNAME()
  71. #endif
  72. #define RS2SPIRV_TEST_CASE_ADD(NAME, DESCRIPTION) \
  73. RS2SPIRV_TEST_CASE_ADD_IMPL(RS2SPIRV_ANONYMOUS(NAME), \
  74. RS2SPIRV_ANONYMOUS(NAME), DESCRIPTION)
  75. #define TEST_CASE(DESCRIPTION) RS2SPIRV_TEST_CASE_ADD(TC, DESCRIPTION)
  76. #define CHECK(CONDITION) \
  77. ++rs2spirv::TestRunnerContext::getTotalCheckNum(); \
  78. if (!(CONDITION)) \
  79. llvm::errs() << "\nCHECK <( " #CONDITION " )> failed!\n"; \
  80. else \
  81. ++rs2spirv::TestRunnerContext::getCheckSuccessNum(); \
  82. (void)0
  83. } // namespace rs2spirv
  84. #endif