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
28#include <v8.h>
29#include "cctest.h"
30#include "debug.h"
31
32enum InitializationState {kUnset, kUnintialized, kInitialized};
33static InitializationState initialization_state_  = kUnset;
34static bool disable_automatic_dispose_ = false;
35
36CcTest* CcTest::last_ = NULL;
37bool CcTest::initialize_called_ = false;
38bool CcTest::isolate_used_ = false;
39v8::Isolate* CcTest::isolate_ = NULL;
40
41
42CcTest::CcTest(TestFunction* callback, const char* file, const char* name,
43               const char* dependency, bool enabled, bool initialize)
44    : callback_(callback), name_(name), dependency_(dependency),
45      enabled_(enabled), initialize_(initialize), prev_(last_) {
46  // Find the base name of this test (const_cast required on Windows).
47  char *basename = strrchr(const_cast<char *>(file), '/');
48  if (!basename) {
49    basename = strrchr(const_cast<char *>(file), '\\');
50  }
51  if (!basename) {
52    basename = v8::internal::StrDup(file);
53  } else {
54    basename = v8::internal::StrDup(basename + 1);
55  }
56  // Drop the extension, if there is one.
57  char *extension = strrchr(basename, '.');
58  if (extension) *extension = 0;
59  // Install this test in the list of tests
60  file_ = basename;
61  prev_ = last_;
62  last_ = this;
63}
64
65
66void CcTest::Run() {
67  if (!initialize_) {
68    CHECK(initialization_state_ != kInitialized);
69    initialization_state_ = kUnintialized;
70    CHECK(CcTest::isolate_ == NULL);
71  } else {
72    CHECK(initialization_state_ != kUnintialized);
73    initialization_state_ = kInitialized;
74    if (isolate_ == NULL) {
75      isolate_ = v8::Isolate::New();
76    }
77    isolate_->Enter();
78  }
79  callback_();
80  if (initialize_) {
81    isolate_->Exit();
82  }
83}
84
85
86v8::Local<v8::Context> CcTest::NewContext(CcTestExtensionFlags extensions,
87                                          v8::Isolate* isolate) {
88    const char* extension_names[kMaxExtensions];
89    int extension_count = 0;
90  #define CHECK_EXTENSION_FLAG(Name, Id) \
91    if (extensions.Contains(Name##_ID)) extension_names[extension_count++] = Id;
92    EXTENSION_LIST(CHECK_EXTENSION_FLAG)
93  #undef CHECK_EXTENSION_FLAG
94    v8::ExtensionConfiguration config(extension_count, extension_names);
95    v8::Local<v8::Context> context = v8::Context::New(isolate, &config);
96    CHECK(!context.IsEmpty());
97    return context;
98}
99
100
101void CcTest::DisableAutomaticDispose() {
102  CHECK_EQ(kUnintialized, initialization_state_);
103  disable_automatic_dispose_ = true;
104}
105
106
107static void PrintTestList(CcTest* current) {
108  if (current == NULL) return;
109  PrintTestList(current->prev());
110  if (current->dependency() != NULL) {
111    printf("%s/%s<%s\n",
112           current->file(), current->name(), current->dependency());
113  } else {
114    printf("%s/%s<\n", current->file(), current->name());
115  }
116}
117
118
119class CcTestArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
120  virtual void* Allocate(size_t length) { return malloc(length); }
121  virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
122  virtual void Free(void* data, size_t length) { free(data); }
123  // TODO(dslomov): Remove when v8:2823 is fixed.
124  virtual void Free(void* data) { UNREACHABLE(); }
125};
126
127
128static void SuggestTestHarness(int tests) {
129  if (tests == 0) return;
130  printf("Running multiple tests in sequence is deprecated and may cause "
131         "bogus failure.  Consider using tools/run-tests.py instead.\n");
132}
133
134
135int main(int argc, char* argv[]) {
136  v8::V8::InitializeICU();
137  i::Isolate::SetCrashIfDefaultIsolateInitialized();
138
139  v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
140
141  CcTestArrayBufferAllocator array_buffer_allocator;
142  v8::V8::SetArrayBufferAllocator(&array_buffer_allocator);
143
144  int tests_run = 0;
145  bool print_run_count = true;
146  for (int i = 1; i < argc; i++) {
147    char* arg = argv[i];
148    if (strcmp(arg, "--list") == 0) {
149      PrintTestList(CcTest::last());
150      print_run_count = false;
151
152    } else {
153      char* arg_copy = v8::internal::StrDup(arg);
154      char* testname = strchr(arg_copy, '/');
155      if (testname) {
156        // Split the string in two by nulling the slash and then run
157        // exact matches.
158        *testname = 0;
159        char* file = arg_copy;
160        char* name = testname + 1;
161        CcTest* test = CcTest::last();
162        while (test != NULL) {
163          if (test->enabled()
164              && strcmp(test->file(), file) == 0
165              && strcmp(test->name(), name) == 0) {
166            SuggestTestHarness(tests_run++);
167            test->Run();
168          }
169          test = test->prev();
170        }
171
172      } else {
173        // Run all tests with the specified file or test name.
174        char* file_or_name = arg_copy;
175        CcTest* test = CcTest::last();
176        while (test != NULL) {
177          if (test->enabled()
178              && (strcmp(test->file(), file_or_name) == 0
179                  || strcmp(test->name(), file_or_name) == 0)) {
180            SuggestTestHarness(tests_run++);
181            test->Run();
182          }
183          test = test->prev();
184        }
185      }
186      v8::internal::DeleteArray<char>(arg_copy);
187    }
188  }
189  if (print_run_count && tests_run != 1)
190    printf("Ran %i tests.\n", tests_run);
191  if (!disable_automatic_dispose_) v8::V8::Dispose();
192  return 0;
193}
194
195RegisterThreadedTest *RegisterThreadedTest::first_ = NULL;
196int RegisterThreadedTest::count_ = 0;
197