blob: 30d9d7e20d2f85aa9946e7b70afe3d025a548261 [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
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000054class CcTest {
55 public:
56 typedef void (TestFunction)();
ager@chromium.org9258b6b2008-09-11 09:11:10 +000057 CcTest(TestFunction* callback, const char* file, const char* name,
iposva@chromium.org245aa852009-02-10 00:49:54 +000058 const char* dependency, bool enabled);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000059 void Run() { callback_(); }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000060 static CcTest* last() { return last_; }
61 CcTest* prev() { return prev_; }
62 const char* file() { return file_; }
63 const char* name() { return name_; }
iposva@chromium.org245aa852009-02-10 00:49:54 +000064 const char* dependency() { return dependency_; }
ager@chromium.org9258b6b2008-09-11 09:11:10 +000065 bool enabled() { return enabled_; }
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000066 static void set_default_isolate(v8::Isolate* default_isolate) {
67 default_isolate_ = default_isolate;
68 }
69 static v8::Isolate* default_isolate() { return default_isolate_; }
70
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000071 private:
72 TestFunction* callback_;
73 const char* file_;
74 const char* name_;
iposva@chromium.org245aa852009-02-10 00:49:54 +000075 const char* dependency_;
ager@chromium.org9258b6b2008-09-11 09:11:10 +000076 bool enabled_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000077 static CcTest* last_;
78 CcTest* prev_;
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000079 static v8::Isolate* default_isolate_;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000080};
81
ager@chromium.orgc4c92722009-11-18 14:12:51 +000082// Switches between all the Api tests using the threading support.
83// In order to get a surprising but repeatable pattern of thread
84// switching it has extra semaphores to control the order in which
85// the tests alternate, not relying solely on the big V8 lock.
86//
87// A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
88// callbacks. This will have no effect when we are not running the
89// thread fuzzing test. In the thread fuzzing test it will
90// pseudorandomly select a successor thread and switch execution
91// to that thread, suspending the current test.
92class ApiTestFuzzer: public v8::internal::Thread {
93 public:
94 void CallTest();
ager@chromium.orgc4c92722009-11-18 14:12:51 +000095
96 // The ApiTestFuzzer is also a Thread, so it has a Run method.
97 virtual void Run();
98
lrn@chromium.org1c092762011-05-09 09:42:16 +000099 enum PartOfTest { FIRST_PART,
100 SECOND_PART,
101 THIRD_PART,
102 FOURTH_PART,
103 LAST_PART = FOURTH_PART };
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000104
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000105 static void SetUp(PartOfTest part);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000106 static void RunAllTests();
107 static void TearDown();
108 // This method switches threads if we are running the Threading test.
109 // Otherwise it does nothing.
110 static void Fuzz();
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000111
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000112 private:
yangguo@chromium.org46a2a512013-01-18 16:29:40 +0000113 explicit ApiTestFuzzer(int num)
114 : Thread("ApiTestFuzzer"),
115 test_number_(num),
116 gate_(v8::internal::OS::CreateSemaphore(0)),
117 active_(true) {
118 }
119 ~ApiTestFuzzer() { delete gate_; }
120
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000121 static bool fuzzing_;
122 static int tests_being_run_;
123 static int current_;
124 static int active_tests_;
125 static bool NextThread();
126 int test_number_;
127 v8::internal::Semaphore* gate_;
128 bool active_;
129 void ContextSwitch();
130 static int GetNextTestNumber();
131 static v8::internal::Semaphore* all_tests_done_;
132};
133
134
135#define THREADED_TEST(Name) \
136 static void Test##Name(); \
137 RegisterThreadedTest register_##Name(Test##Name, #Name); \
138 /* */ TEST(Name)
139
140
141class RegisterThreadedTest {
142 public:
143 explicit RegisterThreadedTest(CcTest::TestFunction* callback,
144 const char* name)
145 : fuzzer_(NULL), callback_(callback), name_(name) {
146 prev_ = first_;
147 first_ = this;
148 count_++;
149 }
150 static int count() { return count_; }
151 static RegisterThreadedTest* nth(int i) {
152 CHECK(i < count());
153 RegisterThreadedTest* current = first_;
154 while (i > 0) {
155 i--;
156 current = current->prev_;
157 }
158 return current;
159 }
160 CcTest::TestFunction* callback() { return callback_; }
161 ApiTestFuzzer* fuzzer_;
162 const char* name() { return name_; }
163
164 private:
165 static RegisterThreadedTest* first_;
166 static int count_;
167 CcTest::TestFunction* callback_;
168 RegisterThreadedTest* prev_;
169 const char* name_;
170};
171
172
173// A LocalContext holds a reference to a v8::Context.
174class LocalContext {
175 public:
176 LocalContext(v8::ExtensionConfiguration* extensions = 0,
177 v8::Handle<v8::ObjectTemplate> global_template =
178 v8::Handle<v8::ObjectTemplate>(),
179 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>())
180 : context_(v8::Context::New(extensions, global_template, global_object)) {
181 context_->Enter();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000182 // We can't do this later perhaps because of a fatal error.
183 isolate_ = context_->GetIsolate();
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000184 }
185
186 virtual ~LocalContext() {
187 context_->Exit();
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000188 context_.Dispose(isolate_);
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000189 }
190
191 v8::Context* operator->() { return *context_; }
192 v8::Context* operator*() { return *context_; }
193 bool IsReady() { return !context_.IsEmpty(); }
194
195 v8::Local<v8::Context> local() {
196 return v8::Local<v8::Context>::New(context_);
197 }
198
199 private:
200 v8::Persistent<v8::Context> context_;
mvstanton@chromium.orgd16d8532013-01-25 13:29:10 +0000201 v8::Isolate* isolate_;
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000202};
203
204
205static inline v8::Local<v8::Value> v8_num(double x) {
206 return v8::Number::New(x);
207}
208
209
210static inline v8::Local<v8::String> v8_str(const char* x) {
211 return v8::String::New(x);
212}
213
214
215static inline v8::Local<v8::Script> v8_compile(const char* x) {
216 return v8::Script::Compile(v8_str(x));
217}
218
219
220// Helper function that compiles and runs the source.
221static inline v8::Local<v8::Value> CompileRun(const char* source) {
222 return v8::Script::Compile(v8::String::New(source))->Run();
223}
224
225
verwaest@chromium.org33e09c82012-10-10 17:07:22 +0000226// Helper function that compiles and runs the source with given origin.
227static inline v8::Local<v8::Value> CompileRunWithOrigin(const char* source,
228 const char* origin_url,
229 int line_number,
230 int column_number) {
231 v8::ScriptOrigin origin(v8::String::New(origin_url),
232 v8::Integer::New(line_number),
233 v8::Integer::New(column_number));
234 return v8::Script::Compile(v8::String::New(source), &origin)->Run();
235}
236
237
238// Pick a slightly different port to allow tests to be run in parallel.
239static inline int FlagDependentPortOffset() {
240 return ::v8::internal::FLAG_crankshaft == false ? 100 :
241 ::v8::internal::FLAG_always_opt ? 200 : 0;
242}
243
244
mmassi@chromium.org2f0efde2013-02-06 14:12:58 +0000245// Helper function that simulates a full new-space in the heap.
ulan@chromium.org8e8d8822012-11-23 14:36:46 +0000246static inline void SimulateFullSpace(v8::internal::NewSpace* space) {
247 int new_linear_size = static_cast<int>(
248 *space->allocation_limit_address() - *space->allocation_top_address());
249 v8::internal::MaybeObject* maybe = space->AllocateRaw(new_linear_size);
250 v8::internal::FreeListNode* node = v8::internal::FreeListNode::cast(maybe);
251 node->set_size(space->heap(), new_linear_size);
252}
253
254
255// Helper function that simulates a full old-space in the heap.
256static inline void SimulateFullSpace(v8::internal::PagedSpace* space) {
257 int old_linear_size = static_cast<int>(space->limit() - space->top());
258 space->Free(space->top(), old_linear_size);
259 space->SetTop(space->limit(), space->limit());
260 space->ResetFreeList();
261 space->ClearStats();
262}
263
264
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000265#endif // ifndef CCTEST_H_