blob: b0b8eb720a8d83a7b10ee297ab5e0c600678d4c1 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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#ifndef CCTEST_H_
29#define CCTEST_H_
30
Steve Blockd0582a62009-12-15 09:54:21 +000031#include "v8.h"
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033#ifndef TEST
34#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); \
44 static void Test##Name()
45#endif
46
47#ifndef DISABLED_TEST
48#define DISABLED_TEST(Name) \
49 static void Test##Name(); \
50 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, false); \
51 static void Test##Name()
52#endif
53
54class CcTest {
55 public:
56 typedef void (TestFunction)();
57 CcTest(TestFunction* callback, const char* file, const char* name,
58 const char* dependency, bool enabled);
59 void Run() { callback_(); }
60 static int test_count();
61 static CcTest* last() { return last_; }
62 CcTest* prev() { return prev_; }
63 const char* file() { return file_; }
64 const char* name() { return name_; }
65 const char* dependency() { return dependency_; }
66 bool enabled() { return enabled_; }
67 private:
68 TestFunction* callback_;
69 const char* file_;
70 const char* name_;
71 const char* dependency_;
72 bool enabled_;
73 static CcTest* last_;
74 CcTest* prev_;
75};
76
Steve Blockd0582a62009-12-15 09:54:21 +000077// Switches between all the Api tests using the threading support.
78// In order to get a surprising but repeatable pattern of thread
79// switching it has extra semaphores to control the order in which
80// the tests alternate, not relying solely on the big V8 lock.
81//
82// A test is augmented with calls to ApiTestFuzzer::Fuzz() in its
83// callbacks. This will have no effect when we are not running the
84// thread fuzzing test. In the thread fuzzing test it will
85// pseudorandomly select a successor thread and switch execution
86// to that thread, suspending the current test.
87class ApiTestFuzzer: public v8::internal::Thread {
88 public:
89 void CallTest();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000090 explicit ApiTestFuzzer(int num)
91 : Thread("ApiTestFuzzer"),
Steve Block44f0eee2011-05-26 01:26:41 +010092 test_number_(num),
Steve Blockd0582a62009-12-15 09:54:21 +000093 gate_(v8::internal::OS::CreateSemaphore(0)),
94 active_(true) {
95 }
96 ~ApiTestFuzzer() { delete gate_; }
97
98 // The ApiTestFuzzer is also a Thread, so it has a Run method.
99 virtual void Run();
100
Ben Murdoch257744e2011-11-30 15:57:28 +0000101 enum PartOfTest { FIRST_PART,
102 SECOND_PART,
103 THIRD_PART,
104 FOURTH_PART,
105 LAST_PART = FOURTH_PART };
Steve Blockd0582a62009-12-15 09:54:21 +0000106
107 static void Setup(PartOfTest part);
108 static void RunAllTests();
109 static void TearDown();
110 // This method switches threads if we are running the Threading test.
111 // Otherwise it does nothing.
112 static void Fuzz();
113 private:
114 static bool fuzzing_;
115 static int tests_being_run_;
116 static int current_;
117 static int active_tests_;
118 static bool NextThread();
119 int test_number_;
120 v8::internal::Semaphore* gate_;
121 bool active_;
122 void ContextSwitch();
123 static int GetNextTestNumber();
124 static v8::internal::Semaphore* all_tests_done_;
125};
126
127
128#define THREADED_TEST(Name) \
129 static void Test##Name(); \
130 RegisterThreadedTest register_##Name(Test##Name, #Name); \
131 /* */ TEST(Name)
132
133
134class RegisterThreadedTest {
135 public:
136 explicit RegisterThreadedTest(CcTest::TestFunction* callback,
137 const char* name)
138 : fuzzer_(NULL), callback_(callback), name_(name) {
139 prev_ = first_;
140 first_ = this;
141 count_++;
142 }
143 static int count() { return count_; }
144 static RegisterThreadedTest* nth(int i) {
145 CHECK(i < count());
146 RegisterThreadedTest* current = first_;
147 while (i > 0) {
148 i--;
149 current = current->prev_;
150 }
151 return current;
152 }
153 CcTest::TestFunction* callback() { return callback_; }
154 ApiTestFuzzer* fuzzer_;
155 const char* name() { return name_; }
156
157 private:
158 static RegisterThreadedTest* first_;
159 static int count_;
160 CcTest::TestFunction* callback_;
161 RegisterThreadedTest* prev_;
162 const char* name_;
163};
164
165
166// A LocalContext holds a reference to a v8::Context.
167class LocalContext {
168 public:
169 LocalContext(v8::ExtensionConfiguration* extensions = 0,
170 v8::Handle<v8::ObjectTemplate> global_template =
171 v8::Handle<v8::ObjectTemplate>(),
172 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>())
173 : context_(v8::Context::New(extensions, global_template, global_object)) {
174 context_->Enter();
175 }
176
177 virtual ~LocalContext() {
178 context_->Exit();
179 context_.Dispose();
180 }
181
182 v8::Context* operator->() { return *context_; }
183 v8::Context* operator*() { return *context_; }
184 bool IsReady() { return !context_.IsEmpty(); }
185
186 v8::Local<v8::Context> local() {
187 return v8::Local<v8::Context>::New(context_);
188 }
189
190 private:
191 v8::Persistent<v8::Context> context_;
192};
193
194
195static inline v8::Local<v8::Value> v8_num(double x) {
196 return v8::Number::New(x);
197}
198
199
200static inline v8::Local<v8::String> v8_str(const char* x) {
201 return v8::String::New(x);
202}
203
204
205static inline v8::Local<v8::Script> v8_compile(const char* x) {
206 return v8::Script::Compile(v8_str(x));
207}
208
209
210// Helper function that compiles and runs the source.
211static inline v8::Local<v8::Value> CompileRun(const char* source) {
212 return v8::Script::Compile(v8::String::New(source))->Run();
213}
214
215
Steve Blocka7e24c12009-10-30 11:49:00 +0000216#endif // ifndef CCTEST_H_