test.cc 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // test.cc -- simplistic test framework for gold.
  2. // Copyright (C) 2006-2015 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. #include "gold.h"
  18. #include <cstdio>
  19. #include "test.h"
  20. namespace gold_testsuite
  21. {
  22. // Test_framework methods.
  23. // The current test being run.
  24. Test_report* Test_framework::current_report;
  25. // Run a test.
  26. void
  27. Test_framework::run(const char *name, bool (*pfn)(Test_report*))
  28. {
  29. this->testname_ = name;
  30. this->current_fail_ = false;
  31. Test_report tr(this);
  32. Test_framework::current_report = &tr;
  33. if ((*pfn)(&tr) && !this->current_fail_)
  34. {
  35. printf("PASS: %s\n", name);
  36. ++this->passes_;
  37. }
  38. else
  39. {
  40. printf("FAIL: %s\n", name);
  41. ++this->failures_;
  42. }
  43. Test_framework::current_report = NULL;
  44. this->testname_ = NULL;
  45. }
  46. // Report a failure.
  47. void
  48. Test_framework::fail(const char* filename, int lineno)
  49. {
  50. printf("FAIL: %s: %s: %d\n", this->testname_, filename, lineno);
  51. this->current_fail_ = true;
  52. }
  53. // Let a test report an error.
  54. void
  55. Test_framework::error(const char* message)
  56. {
  57. printf("ERROR: %s: %s\n", this->testname_, message);
  58. this->current_fail_ = true;
  59. }
  60. // Register_test methods.
  61. // Linked list of all registered tests.
  62. Register_test* Register_test::all_tests;
  63. // Register a test.
  64. Register_test::Register_test(const char* name, bool (*pfn)(Test_report*))
  65. : name_(name), pfn_(pfn), next_(Register_test::all_tests)
  66. {
  67. Register_test::all_tests = this;
  68. }
  69. // Run all registered tests.
  70. void
  71. Register_test::run_tests(Test_framework* tf)
  72. {
  73. for (Register_test* p = Register_test::all_tests;
  74. p != NULL;
  75. p = p->next_)
  76. tf->run(p->name_, p->pfn_);
  77. }
  78. } // End namespace gold_testsuite.