cctest.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2008 the V8 project authors. All rights reserved.
  2. // Redistribution and use in source and binary forms, with or without
  3. // modification, are permitted provided that the following conditions are
  4. // met:
  5. //
  6. // * Redistributions of source code must retain the above copyright
  7. // notice, this list of conditions and the following disclaimer.
  8. // * Redistributions in binary form must reproduce the above
  9. // copyright notice, this list of conditions and the following
  10. // disclaimer in the documentation and/or other materials provided
  11. // with the distribution.
  12. // * Neither the name of Google Inc. nor the names of its
  13. // contributors may be used to endorse or promote products derived
  14. // from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  17. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  18. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  19. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  20. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  26. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #include "cctest.h"
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. CcTest* CcTest::last_ = NULL;
  32. CcTest::CcTest(TestFunction* callback, const char* file, const char* name,
  33. const char* dependency, bool enabled)
  34. : callback_(callback), name_(name), dependency_(dependency), prev_(last_) {
  35. // Find the base name of this test (const_cast required on Windows).
  36. char *basename = strrchr(const_cast<char *>(file), '/');
  37. if (!basename) {
  38. basename = strrchr(const_cast<char *>(file), '\\');
  39. }
  40. if (!basename) {
  41. basename = strdup(file);
  42. } else {
  43. basename = strdup(basename + 1);
  44. }
  45. // Drop the extension, if there is one.
  46. char *extension = strrchr(basename, '.');
  47. if (extension) *extension = 0;
  48. // Install this test in the list of tests
  49. file_ = basename;
  50. enabled_ = enabled;
  51. prev_ = last_;
  52. last_ = this;
  53. }
  54. static void PrintTestList(CcTest* current) {
  55. if (current == NULL) return;
  56. PrintTestList(current->prev());
  57. if (current->dependency() != NULL) {
  58. printf("%s/%s<%s\n",
  59. current->file(), current->name(), current->dependency());
  60. } else {
  61. printf("%s/%s<\n", current->file(), current->name());
  62. }
  63. }
  64. int main(int argc, char* argv[]) {
  65. int tests_run = 0;
  66. bool print_run_count = true;
  67. for (int i = 1; i < argc; i++) {
  68. char* arg = argv[i];
  69. if (strcmp(arg, "--list") == 0) {
  70. PrintTestList(CcTest::last());
  71. print_run_count = false;
  72. } else {
  73. char* arg_copy = strdup(arg);
  74. char* testname = strchr(arg_copy, '/');
  75. if (testname) {
  76. // Split the string in two by nulling the slash and then run
  77. // exact matches.
  78. *testname = 0;
  79. char* file = arg_copy;
  80. char* name = testname + 1;
  81. CcTest* test = CcTest::last();
  82. while (test != NULL) {
  83. if (test->enabled()
  84. && strcmp(test->file(), file) == 0
  85. && strcmp(test->name(), name) == 0) {
  86. test->Run();
  87. tests_run++;
  88. }
  89. test = test->prev();
  90. }
  91. } else {
  92. // Run all tests with the specified file or test name.
  93. char* file_or_name = arg_copy;
  94. CcTest* test = CcTest::last();
  95. while (test != NULL) {
  96. if (test->enabled()
  97. && (strcmp(test->file(), file_or_name) == 0
  98. || strcmp(test->name(), file_or_name) == 0)) {
  99. test->Run();
  100. tests_run++;
  101. }
  102. test = test->prev();
  103. }
  104. }
  105. delete[] arg_copy;
  106. }
  107. }
  108. if (print_run_count && tests_run != 1)
  109. printf("Ran %i tests.\n", tests_run);
  110. return 0;
  111. }