| OLD | NEW |
| 1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #ifndef CCTEST_H_ | 28 #ifndef CCTEST_H_ |
| 29 #define CCTEST_H_ | 29 #define CCTEST_H_ |
| 30 | 30 |
| 31 #include "v8.h" | |
| 32 | |
| 33 #ifndef TEST | 31 #ifndef TEST |
| 34 #define TEST(Name) \ | 32 #define TEST(Name) \ |
| 35 static void Test##Name(); \ | 33 static void Test##Name(); \ |
| 36 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true); \ | 34 CcTest register_test_##Name(Test##Name, __FILE__, #Name, NULL, true); \ |
| 37 static void Test##Name() | 35 static void Test##Name() |
| 38 #endif | 36 #endif |
| 39 | 37 |
| 40 #ifndef DEPENDENT_TEST | 38 #ifndef DEPENDENT_TEST |
| 41 #define DEPENDENT_TEST(Name, Dep) \ | 39 #define DEPENDENT_TEST(Name, Dep) \ |
| 42 static void Test##Name(); \ | 40 static void Test##Name(); \ |
| (...skipping 24 matching lines...) Expand all Loading... |
| 67 private: | 65 private: |
| 68 TestFunction* callback_; | 66 TestFunction* callback_; |
| 69 const char* file_; | 67 const char* file_; |
| 70 const char* name_; | 68 const char* name_; |
| 71 const char* dependency_; | 69 const char* dependency_; |
| 72 bool enabled_; | 70 bool enabled_; |
| 73 static CcTest* last_; | 71 static CcTest* last_; |
| 74 CcTest* prev_; | 72 CcTest* prev_; |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 // 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. | |
| 87 class ApiTestFuzzer: public v8::internal::Thread { | |
| 88 public: | |
| 89 void CallTest(); | |
| 90 explicit ApiTestFuzzer(int num) | |
| 91 : test_number_(num), | |
| 92 gate_(v8::internal::OS::CreateSemaphore(0)), | |
| 93 active_(true) { | |
| 94 } | |
| 95 ~ApiTestFuzzer() { delete gate_; } | |
| 96 | |
| 97 // The ApiTestFuzzer is also a Thread, so it has a Run method. | |
| 98 virtual void Run(); | |
| 99 | |
| 100 enum PartOfTest { FIRST_PART, SECOND_PART }; | |
| 101 | |
| 102 static void Setup(PartOfTest part); | |
| 103 static void RunAllTests(); | |
| 104 static void TearDown(); | |
| 105 // This method switches threads if we are running the Threading test. | |
| 106 // Otherwise it does nothing. | |
| 107 static void Fuzz(); | |
| 108 private: | |
| 109 static bool fuzzing_; | |
| 110 static int tests_being_run_; | |
| 111 static int current_; | |
| 112 static int active_tests_; | |
| 113 static bool NextThread(); | |
| 114 int test_number_; | |
| 115 v8::internal::Semaphore* gate_; | |
| 116 bool active_; | |
| 117 void ContextSwitch(); | |
| 118 static int GetNextTestNumber(); | |
| 119 static v8::internal::Semaphore* all_tests_done_; | |
| 120 }; | |
| 121 | |
| 122 | |
| 123 #define THREADED_TEST(Name) \ | |
| 124 static void Test##Name(); \ | |
| 125 RegisterThreadedTest register_##Name(Test##Name, #Name); \ | |
| 126 /* */ TEST(Name) | |
| 127 | |
| 128 | |
| 129 class RegisterThreadedTest { | |
| 130 public: | |
| 131 explicit RegisterThreadedTest(CcTest::TestFunction* callback, | |
| 132 const char* name) | |
| 133 : fuzzer_(NULL), callback_(callback), name_(name) { | |
| 134 prev_ = first_; | |
| 135 first_ = this; | |
| 136 count_++; | |
| 137 } | |
| 138 static int count() { return count_; } | |
| 139 static RegisterThreadedTest* nth(int i) { | |
| 140 CHECK(i < count()); | |
| 141 RegisterThreadedTest* current = first_; | |
| 142 while (i > 0) { | |
| 143 i--; | |
| 144 current = current->prev_; | |
| 145 } | |
| 146 return current; | |
| 147 } | |
| 148 CcTest::TestFunction* callback() { return callback_; } | |
| 149 ApiTestFuzzer* fuzzer_; | |
| 150 const char* name() { return name_; } | |
| 151 | |
| 152 private: | |
| 153 static RegisterThreadedTest* first_; | |
| 154 static int count_; | |
| 155 CcTest::TestFunction* callback_; | |
| 156 RegisterThreadedTest* prev_; | |
| 157 const char* name_; | |
| 158 }; | |
| 159 | |
| 160 | |
| 161 // A LocalContext holds a reference to a v8::Context. | |
| 162 class LocalContext { | |
| 163 public: | |
| 164 LocalContext(v8::ExtensionConfiguration* extensions = 0, | |
| 165 v8::Handle<v8::ObjectTemplate> global_template = | |
| 166 v8::Handle<v8::ObjectTemplate>(), | |
| 167 v8::Handle<v8::Value> global_object = v8::Handle<v8::Value>()) | |
| 168 : context_(v8::Context::New(extensions, global_template, global_object)) { | |
| 169 context_->Enter(); | |
| 170 } | |
| 171 | |
| 172 virtual ~LocalContext() { | |
| 173 context_->Exit(); | |
| 174 context_.Dispose(); | |
| 175 } | |
| 176 | |
| 177 v8::Context* operator->() { return *context_; } | |
| 178 v8::Context* operator*() { return *context_; } | |
| 179 bool IsReady() { return !context_.IsEmpty(); } | |
| 180 | |
| 181 v8::Local<v8::Context> local() { | |
| 182 return v8::Local<v8::Context>::New(context_); | |
| 183 } | |
| 184 | |
| 185 private: | |
| 186 v8::Persistent<v8::Context> context_; | |
| 187 }; | |
| 188 | |
| 189 | |
| 190 static inline v8::Local<v8::Value> v8_num(double x) { | |
| 191 return v8::Number::New(x); | |
| 192 } | |
| 193 | |
| 194 | |
| 195 static inline v8::Local<v8::String> v8_str(const char* x) { | |
| 196 return v8::String::New(x); | |
| 197 } | |
| 198 | |
| 199 | |
| 200 static inline v8::Local<v8::Script> v8_compile(const char* x) { | |
| 201 return v8::Script::Compile(v8_str(x)); | |
| 202 } | |
| 203 | |
| 204 | |
| 205 // Helper function that compiles and runs the source. | |
| 206 static inline v8::Local<v8::Value> CompileRun(const char* source) { | |
| 207 return v8::Script::Compile(v8::String::New(source))->Run(); | |
| 208 } | |
| 209 | |
| 210 | |
| 211 #endif // ifndef CCTEST_H_ | 75 #endif // ifndef CCTEST_H_ |
| OLD | NEW |