blob: c249b5e92127c0adff760758a3470cc9644db95c [file] [log] [blame]
ager@chromium.org9258b6b2008-09-11 09:11:10 +00001// Copyright 2008 the V8 project authors. All rights reserved.
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +00002// 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#ifndef CCTEST_H_
29#define CCTEST_H_
30
ager@chromium.orgc4c92722009-11-18 14:12:51 +000031#include "v8.h"
32
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000033#ifndef TEST
iposva@chromium.org245aa852009-02-10 00:49:54 +000034#define TEST(Name) \
35 static void Test##Name(); \
36 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true); \
37 static void Test##Name()
38#endif
39
40#ifndef DEPENDENT_TEST
41#define DEPENDENT_TEST(Name, Dep) \
42 static void Test##Name(); \
43 CcTest register_test_##Name(Test##Name, __FILE__, #Name, #Dep, true); \
ager@chromium.org9258b6b2008-09-11 09:11:10 +000044 static void Test##Name()
45#endif
46
47#ifndef DISABLED_TEST
iposva@chromium.org245aa852009-02-10 00:49:54 +000048#define DISABLED_TEST(Name) \
49 static void Test##Name(); \
50 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false); \
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000051 static void Test##Name()
52#endif
53
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000054#define EXTENSION_LIST(V) \
55 V(GC_EXTENSION, "v8/gc") \
56 V(PRINT_EXTENSION, "v8/print") \
57 V(TRACE_EXTENSION, "v8/trace")
58
59#define DEFINE_EXTENSION_ID(Name, Ident) Name##_ID,
60enum CcTestExtensionIds {
61 EXTENSION_LIST(DEFINE_EXTENSION_ID)
62 kMaxExtensions
63};
64#undef DEFINE_EXTENSION_ID
65
66typedef v8::internal::EnumSet<CcTestExtensionIds> CcTestExtensionFlags;
67#define DEFINE_EXTENSION_FLAG(Name, Ident) \
68 static const CcTestExtensionFlags Name(1 << Name##_ID);
69 static const CcTestExtensionFlags NO_EXTENSIONS(0);
70 static const CcTestExtensionFlags ALL_EXTENSIONS((1 << kMaxExtensions) - 1);
71 EXTENSION_LIST(DEFINE_EXTENSION_FLAG)
72#undef DEFINE_EXTENSION_FLAG
73
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000074class CcTest {
75 public:
76 typedef void (TestFunction)();
ager@chromium.org9258b6b2008-09-11 09:11:10 +000077 CcTest(TestFunction* callback, const char* file, const char* name,
iposva@chromium.org245aa852009-02-10 00:49:54 +000078 const char* dependency, bool enabled);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000079 void Run() { callback_(); }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080 static CcTest* last() { return last_; }
81 CcTest* prev() { return prev_; }
82 const char* file() { return file_; }
83 const char* name() { return name_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +000084 const char* dependency() { return dependency_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +000085 bool enabled() { return enabled_; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000086 static v8::Isolate* default_isolate() { return default_isolate_; }
ulan@chromium.org57ff8812013-05-10 08:16:55 +000087
88 static v8::Handle<v8::Context> env() {
89 return v8::Local<v8::Context>::New(default_isolate_, context_);
90 }
91
92 static v8::Isolate* isolate() { return default_isolate_; }
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000093
94 // Helper function to initialize the VM.
95 static void InitializeVM(CcTestExtensionFlags extensions = NO_EXTENSIONS);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000096
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000097 private:
ulan@chromium.org57ff8812013-05-10 08:16:55 +000098 friend int main(int argc, char** argv);
99 static void set_default_isolate(v8::Isolate* default_isolate) {
100 default_isolate_ = default_isolate;
101 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000102 TestFunction* callback_;
103 const char* file_;
104 const char* name_;
iposva@chromium.org245aa852009-02-10 00:49:54 +0000105 const char* dependency_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000106 bool enabled_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000107 CcTest* prev_;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000108 static CcTest* last_;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000109 static v8::Isolate* default_isolate_;
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +0000110 static v8::Persistent<v8::Context> context_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000111};
112
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000113// Switches between all the Api tests using the threading support.
114// In order to get a surprising but repeatable pattern of thread
115// switching it has extra semaphores to control the order in which
116// the tests alternate, not relying solely on the big V8 lock.
117//
118// A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
119// callbacks. This will have no effect when we are not running the
120// thread fuzzing test. In the thread fuzzing test it will
121// pseudorandomly select a successor thread and switch execution
122// to that thread, suspending the current test.
123class ApiTestFuzzer: public v8::internal::Thread {
124 public:
125 void CallTest();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000126
127 // The ApiTestFuzzer is also a Thread, so it has a Run method.
128 virtual void Run();
129
lrn@chromium.org1c092762011-05-09 09:42:16 +0000130 enum PartOfTest { FIRST_PART,
131 SECOND_PART,
132 THIRD_PART,
133 FOURTH_PART,
134 LAST_PART = FOURTH_PART };
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000135
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000136 static void SetUp(PartOfTest part);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000137 static void RunAllTests();
138 static void TearDown();
139 // This method switches threads if we are running the Threading test.
140 // Otherwise it does nothing.
141 static void Fuzz();
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000142
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000143 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000144 explicit ApiTestFuzzer(int num)
145 : Thread("ApiTestFuzzer"),
146 test_number_(num),
147 gate_(v8::internal::OS::CreateSemaphore(0)),
148 active_(true) {
149 }
150 ~ApiTestFuzzer() { delete gate_; }
151
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000152 static bool fuzzing_;
153 static int tests_being_run_;
154 static int current_;
155 static int active_tests_;
156 static bool NextThread();
157 int test_number_;
158 v8::internal::Semaphore* gate_;
159 bool active_;
160 void ContextSwitch();
161 static int GetNextTestNumber();
162 static v8::internal::Semaphore* all_tests_done_;
163};
164
165
166#define THREADED_TEST(Name) \
167 static void Test##Name(); \
168 RegisterThreadedTest register_##Name(Test##Name, #Name); \
169 /* */ TEST(Name)
170
171
172class RegisterThreadedTest {
173 public:
174 explicit RegisterThreadedTest(CcTest::TestFunction* callback,
175 const char* name)
176 : fuzzer_(NULL), callback_(callback), name_(name) {
177 prev_ = first_;
178 first_ = this;
179 count_++;
180 }
181 static int count() { return count_; }
182 static RegisterThreadedTest* nth(int i) {
183 CHECK(i < count());
184 RegisterThreadedTest* current = first_;
185 while (i > 0) {
186 i--;
187 current = current->prev_;
188 }
189 return current;
190 }
191 CcTest::TestFunction* callback() { return callback_; }
192 ApiTestFuzzer* fuzzer_;
193 const char* name() { return name_; }
194
195 private:
196 static RegisterThreadedTest* first_;
197 static int count_;
198 CcTest::TestFunction* callback_;
199 RegisterThreadedTest* prev_;
200 const char* name_;
201};
202
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000203namespace v8 {
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000204// A LocalContext holds a reference to a v8::Context.
205class LocalContext {
206 public:
207 LocalContext(v8::ExtensionConfiguration* extensions = 0,
208 v8::Handle<v8::ObjectTemplate> global_template =
209 v8::Handle<v8::ObjectTemplate>(),
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000210 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) {
211 v8::Isolate* isolate = v8::Isolate::GetCurrent();
212 HandleScope scope(isolate);
213 context_.Reset(isolate,
214 Context::New(isolate,
215 extensions,
216 global_template,
217 global_object));
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000218 context_->Enter();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000219 // We can't do this later perhaps because of a fatal error.
220 isolate_ = context_->GetIsolate();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000221 }
222
223 virtual ~LocalContext() {
224 context_->Exit();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000225 context_.Dispose(isolate_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000226 }
227
228 v8::Context* operator->() { return *context_; }
229 v8::Context* operator*() { return *context_; }
230 bool IsReady() { return !context_.IsEmpty(); }
231
232 v8::Local<v8::Context> local() {
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000233 return v8::Local<v8::Context>::New(isolate_, context_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000234 }
235
236 private:
237 v8::Persistent<v8::Context> context_;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000238 v8::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000239};
ulan@chromium.org57ff8812013-05-10 08:16:55 +0000240}
241typedef v8::LocalContext LocalContext;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000242
243static inline v8::Local<v8::Value> v8_num(double x) {
244 return v8::Number::New(x);
245}
246
247
248static inline v8::Local<v8::String> v8_str(const char* x) {
249 return v8::String::New(x);
250}
251
252
253static inline v8::Local<v8::Script> v8_compile(const char* x) {
254 return v8::Script::Compile(v8_str(x));
255}
256
257
258// Helper function that compiles and runs the source.
259static inline v8::Local<v8::Value> CompileRun(const char* source) {
260 return v8::Script::Compile(v8::String::New(source))->Run();
261}
262
263
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000264// Helper function that compiles and runs the source with given origin.
265static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
266 const char* origin_url,
267 int line_number,
268 int column_number) {
269 v8::ScriptOrigin origin(v8::String::New(origin_url),
270 v8::Integer::New(line_number),
271 v8::Integer::New(column_number));
272 return v8::Script::Compile(v8::String::New(source), &origin)->Run();
273}
274
275
276// Pick a slightly different port to allow tests to be run in parallel.
277static inline int FlagDependentPortOffset() {
278 return ::v8::internal::FLAG_crankshaft == false ? 100 :
279 ::v8::internal::FLAG_always_opt ? 200 : 0;
280}
281
282
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000283// Helper function that simulates a full new-space in the heap.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000284static inline void SimulateFullSpace(v8::internal::NewSpace* space) {
285 int new_linear_size = static_cast<int>(
286 *space->allocation_limit_address() - *space->allocation_top_address());
287 v8::internal::MaybeObject* maybe = space->AllocateRaw(new_linear_size);
288 v8::internal::FreeListNode* node = v8::internal::FreeListNode::cast(maybe);
289 node->set_size(space->heap(), new_linear_size);
290}
291
292
293// Helper function that simulates a full old-space in the heap.
294static inline void SimulateFullSpace(v8::internal::PagedSpace* space) {
295 int old_linear_size = static_cast<int>(space->limit() - space->top());
296 space->Free(space->top(), old_linear_size);
297 space->SetTop(space->limit(), space->limit());
298 space->ResetFreeList();
299 space->ClearStats();
300}
301
302
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000303#endif // ifndef CCTEST_H_