blob: a0091ff6f803503d841f1b804daa7890630ae001 [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#include <v8.h>
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000029#include "cctest.h"
ager@chromium.org41826e72009-03-30 13:30:57 +000030#include "debug.h"
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000031
32
33CcTest* CcTest::last_ = NULL;
34
35
iposva@chromium.org245aa852009-02-10 00:49:54 +000036CcTest::CcTest(TestFunction* callback, const char* file, const char* name,
37 const char* dependency, bool enabled)
38 : callback_(callback), name_(name), dependency_(dependency), prev_(last_) {
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000039 // Find the base name of this test (const_cast required on Windows).
40 char *basename = strrchr(const_cast<char *>(file), '/');
41 if (!basename) {
42 basename = strrchr(const_cast<char *>(file), '\\');
43 }
44 if (!basename) {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000045 basename = v8::internal::StrDup(file);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000046 } else {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +000047 basename = v8::internal::StrDup(basename + 1);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000048 }
49 // Drop the extension, if there is one.
50 char *extension = strrchr(basename, '.');
51 if (extension) *extension = 0;
52 // Install this test in the list of tests
53 file_ = basename;
ager@chromium.org9258b6b2008-09-11 09:11:10 +000054 enabled_ = enabled;
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000055 prev_ = last_;
56 last_ = this;
57}
58
59
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000060v8::Persistent<v8::Context> CcTest::context_;
61
62
63void CcTest::InitializeVM(CcTestExtensionFlags extensions) {
64 const char* extension_names[kMaxExtensions];
65 int extension_count = 0;
66#define CHECK_EXTENSION_FLAG(Name, Id) \
67 if (extensions.Contains(Name##_ID)) extension_names[extension_count++] = Id;
68 EXTENSION_LIST(CHECK_EXTENSION_FLAG)
69#undef CHECK_EXTENSION_FLAG
70 if (context_.IsEmpty()) {
71 v8::Isolate* isolate = default_isolate();
72 v8::HandleScope scope(isolate);
73 v8::ExtensionConfiguration config(extension_count, extension_names);
74 v8::Local<v8::Context> context = v8::Context::New(isolate, &config);
75 context_ = v8::Persistent<v8::Context>::New(isolate, context);
76 }
77 context_->Enter();
78}
79
80
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000081static void PrintTestList(CcTest* current) {
82 if (current == NULL) return;
83 PrintTestList(current->prev());
iposva@chromium.org245aa852009-02-10 00:49:54 +000084 if (current->dependency() != NULL) {
85 printf("%s/%s<%s\n",
86 current->file(), current->name(), current->dependency());
87 } else {
88 printf("%s/%s<\n", current->file(), current->name());
89 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000090}
91
92
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000093v8::Isolate* CcTest::default_isolate_;
94
mstarzinger@chromium.orge27d6172013-04-17 11:51:44 +000095
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +000096int main(int argc, char* argv[]) {
97 v8::internal::FlagList::SetFlagsFromCommandLine(&argc, argv, true);
yangguo@chromium.org46a2a512013-01-18 16:29:40 +000098 CcTest::set_default_isolate(v8::Isolate::GetCurrent());
99 CHECK(CcTest::default_isolate() != NULL);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000100 int tests_run = 0;
101 bool print_run_count = true;
102 for (int i = 1; i < argc; i++) {
103 char* arg = argv[i];
104 if (strcmp(arg, "--list") == 0) {
105 PrintTestList(CcTest::last());
106 print_run_count = false;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000107
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000108 } else {
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000109 char* arg_copy = v8::internal::StrDup(arg);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000110 char* testname = strchr(arg_copy, '/');
111 if (testname) {
112 // Split the string in two by nulling the slash and then run
113 // exact matches.
114 *testname = 0;
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000115 char* file = arg_copy;
116 char* name = testname + 1;
117 CcTest* test = CcTest::last();
118 while (test != NULL) {
119 if (test->enabled()
120 && strcmp(test->file(), file) == 0
121 && strcmp(test->name(), name) == 0) {
122 test->Run();
123 tests_run++;
124 }
125 test = test->prev();
126 }
127
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000128 } else {
129 // Run all tests with the specified file or test name.
ager@chromium.org9258b6b2008-09-11 09:11:10 +0000130 char* file_or_name = arg_copy;
131 CcTest* test = CcTest::last();
132 while (test != NULL) {
133 if (test->enabled()
134 && (strcmp(test->file(), file_or_name) == 0
135 || strcmp(test->name(), file_or_name) == 0)) {
136 test->Run();
137 tests_run++;
138 }
139 test = test->prev();
140 }
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000141 }
ager@chromium.orgbb29dc92009-03-24 13:25:23 +0000142 v8::internal::DeleteArray<char>(arg_copy);
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000143 }
144 }
145 if (print_run_count && tests_run != 1)
146 printf("Ran %i tests.\n", tests_run);
ager@chromium.org41826e72009-03-30 13:30:57 +0000147 v8::V8::Dispose();
mads.s.ager@gmail.com9a4089a2008-09-01 08:55:01 +0000148 return 0;
149}
ager@chromium.orgc4c92722009-11-18 14:12:51 +0000150
151RegisterThreadedTest *RegisterThreadedTest::first_ = NULL;
152int RegisterThreadedTest::count_ = 0;