PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/gold/testsuite/test.h

https://bitbucket.org/zlwu/cegcc-binutils
C Header | 145 lines | 68 code | 35 blank | 42 comment | 0 complexity | 61a1a39977549e93f347220686ce4f9d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. // test.h -- simplistic test framework for gold unittests -*- C++ -*-
  2. // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #ifndef GOLD_TESTSUITE_TEST_H
  18. #define GOLD_TESTSUITE_TEST_H
  19. namespace gold_testsuite
  20. {
  21. class Test_report;
  22. // This class handles basic test framework functionality.
  23. class Test_framework
  24. {
  25. public:
  26. Test_framework()
  27. : testname_(NULL), current_fail_(0), passes_(0), failures_(0)
  28. { }
  29. // Return number of failures.
  30. unsigned int
  31. failures() const
  32. { return this->failures_; }
  33. // Run a test.
  34. void
  35. run(const char* name, bool (*pfn)(Test_report*));
  36. // Get the current Test_report. This is used by the test support
  37. // macros.
  38. static Test_report*
  39. report()
  40. { return Test_framework::current_report; }
  41. private:
  42. friend class Test_report;
  43. // Cause the current test to fail.
  44. void
  45. fail(const char* filename, int lineno);
  46. // Report an error from the current test.
  47. void
  48. error(const char* message);
  49. // Current Test_report. This is a static variable valid while a
  50. // test is being run.
  51. static Test_report* current_report;
  52. // Current test being run.
  53. const char* testname_;
  54. // Whether the current test is failing.
  55. bool current_fail_;
  56. // Total number of passeed tests.
  57. unsigned int passes_;
  58. // Total number of failed tests.
  59. unsigned int failures_;
  60. };
  61. // An instance of this class is passed to each test function.
  62. class Test_report
  63. {
  64. public:
  65. Test_report(Test_framework* tf)
  66. : tf_(tf)
  67. { }
  68. // Mark the test as failing.
  69. void
  70. fail(const char* filename, int lineno)
  71. { this->tf_->fail(filename, lineno); }
  72. // Report an error.
  73. void
  74. error(const char* message)
  75. { this->tf_->error(message); }
  76. private:
  77. Test_framework* tf_;
  78. };
  79. // This class registers a test function so that the testsuite runs it.
  80. class Register_test
  81. {
  82. public:
  83. Register_test(const char* name, bool (*pfn)(Test_report*));
  84. // Run all registered tests.
  85. static void
  86. run_tests(Test_framework*);
  87. private:
  88. // Linked list of all tests.
  89. static Register_test* all_tests;
  90. // Test name.
  91. const char* name_;
  92. // Function to call. It should return true if the test passes,
  93. // false if it fails.
  94. bool (*pfn_)(Test_report*);
  95. // Next test in linked list.
  96. Register_test* next_;
  97. };
  98. } // End namespace gold_testsuite.
  99. // These macros are for convenient use in tests.
  100. // Check that a condition is true. If it is false, report a failure.
  101. #define CHECK(cond) \
  102. ((void) \
  103. ((cond) \
  104. ? 0 \
  105. : (::gold_testsuite::Test_framework::report()->fail(__FILE__, \
  106. __LINE__), \
  107. 0)))
  108. // Report an error during a test.
  109. #define ERROR(msg) (::gold_testsuite::Test_framework::report()->error(msg))
  110. #endif // !defined(GOLD_TESTSUITE_TEST_H)