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