OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include "bin/dartutils.h" | 7 #include "bin/dartutils.h" |
8 #include "bin/file.h" | 8 #include "bin/file.h" |
9 #include "bin/loader.h" | |
10 #include "bin/platform.h" | 9 #include "bin/platform.h" |
11 #include "bin/snapshot_utils.h" | |
12 #include "platform/assert.h" | 10 #include "platform/assert.h" |
13 #include "vm/benchmark_test.h" | 11 #include "vm/benchmark_test.h" |
14 #include "vm/dart.h" | 12 #include "vm/dart.h" |
15 #include "vm/unit_test.h" | 13 #include "vm/unit_test.h" |
16 | 14 |
17 | 15 |
18 // TODO(iposva, asiva): This is a placeholder for the real unittest framework. | 16 // TODO(iposva, asiva): This is a placeholder for the real unittest framework. |
19 namespace dart { | 17 namespace dart { |
20 | 18 |
21 // Defined in vm/os_thread_win.cc | 19 // Defined in vm/os_thread_win.cc |
22 extern bool private_flag_windows_run_tls_destructors; | 20 extern bool private_flag_windows_run_tls_destructors; |
23 | 21 |
24 // vm_snapshot_data_buffer points to a snapshot for the vm isolate if we | 22 // vm_snapshot_data_buffer points to a snapshot for the vm isolate if we |
25 // link in a snapshot otherwise it is initialized to NULL. | 23 // link in a snapshot otherwise it is initialized to NULL. |
26 extern const uint8_t* bin::vm_snapshot_data; | 24 extern const uint8_t* bin::vm_snapshot_data; |
27 extern const uint8_t* bin::vm_snapshot_instructions; | 25 extern const uint8_t* bin::vm_snapshot_instructions; |
28 | 26 |
29 // Only run tests that match the filter string. The default does not match any | 27 // Only run tests that match the filter string. The default does not match any |
30 // tests. | 28 // tests. |
31 static const char* const kNone = "No Test or Benchmarks"; | 29 static const char* const kNone = "No Test or Benchmarks"; |
32 static const char* const kList = "List all Tests and Benchmarks"; | 30 static const char* const kList = "List all Tests and Benchmarks"; |
33 static const char* const kAllBenchmarks = "All Benchmarks"; | 31 static const char* const kAllBenchmarks = "All Benchmarks"; |
34 static const char* run_filter = kNone; | 32 static const char* run_filter = kNone; |
35 static const char* kernel_snapshot = NULL; | |
36 | 33 |
37 static int run_matches = 0; | 34 static int run_matches = 0; |
38 | 35 |
39 | 36 |
40 void TestCase::Run() { | 37 void TestCase::Run() { |
41 OS::Print("Running test: %s\n", name()); | 38 OS::Print("Running test: %s\n", name()); |
42 (*run_)(); | 39 (*run_)(); |
43 OS::Print("Done: %s\n", name()); | 40 OS::Print("Done: %s\n", name()); |
44 } | 41 } |
45 | 42 |
(...skipping 25 matching lines...) Expand all Loading... |
71 run_matches++; | 68 run_matches++; |
72 } else if (run_filter == kList) { | 69 } else if (run_filter == kList) { |
73 OS::Print("%s\n", this->name()); | 70 OS::Print("%s\n", this->name()); |
74 run_matches++; | 71 run_matches++; |
75 } | 72 } |
76 } | 73 } |
77 | 74 |
78 | 75 |
79 static void PrintUsage() { | 76 static void PrintUsage() { |
80 OS::PrintErr( | 77 OS::PrintErr( |
81 "Usage: one of the following\n" | 78 "run_vm_tests [--list | --benchmarks | " |
82 " run_vm_tests --list\n" | 79 "<test name> | <benchmark name>]\n"); |
83 " run_vm_tests [--dfe=<snapshot file name>] --benchmarks\n" | 80 OS::PrintErr("run_vm_tests [vm-flags ...] <test name>\n"); |
84 " run_vm_tests [--dfe=<snapshot file name>] [vm-flags ...] <test name>\n" | 81 OS::PrintErr("run_vm_tests [vm-flags ...] <benchmark name>\n"); |
85 " run_vm_tests [--dfe=<snapshot file name>] [vm-flags ...] <benchmark " | |
86 "name>\n"); | |
87 } | 82 } |
88 | 83 |
89 #define CHECK_RESULT(result) \ | |
90 if (Dart_IsError(result)) { \ | |
91 *error = strdup(Dart_GetError(result)); \ | |
92 Dart_ExitScope(); \ | |
93 Dart_ShutdownIsolate(); \ | |
94 return NULL; \ | |
95 } | |
96 | |
97 | |
98 static Dart_Isolate CreateIsolateAndSetup(const char* script_uri, | |
99 const char* main, | |
100 const char* package_root, | |
101 const char* packages_config, | |
102 Dart_IsolateFlags* flags, | |
103 void* data, | |
104 char** error) { | |
105 ASSERT(script_uri != NULL); | |
106 const bool is_service_isolate = | |
107 strcmp(script_uri, DART_VM_SERVICE_ISOLATE_NAME) == 0; | |
108 if (is_service_isolate) { | |
109 // We don't need service isolate for VM tests. | |
110 return NULL; | |
111 } | |
112 const bool is_kernel_isolate = | |
113 strcmp(script_uri, DART_KERNEL_ISOLATE_NAME) == 0; | |
114 if (!is_kernel_isolate) { | |
115 *error = | |
116 strdup("Spawning of only Kernel isolate is supported in run_vm_tests."); | |
117 return NULL; | |
118 } | |
119 if (kernel_snapshot == NULL) { | |
120 *error = | |
121 strdup("Kernel snapshot location has to be specified via --dfe option"); | |
122 return NULL; | |
123 } | |
124 script_uri = kernel_snapshot; | |
125 | |
126 bin::AppSnapshot* app_snapshot = | |
127 bin::Snapshot::TryReadAppSnapshot(script_uri); | |
128 if (app_snapshot == NULL) { | |
129 *error = strdup("Failed to read kernel service app snapshot"); | |
130 return NULL; | |
131 } | |
132 | |
133 const uint8_t* isolate_snapshot_data = bin::core_isolate_snapshot_data; | |
134 const uint8_t* isolate_snapshot_instructions = | |
135 bin::core_isolate_snapshot_instructions; | |
136 | |
137 const uint8_t* ignore_vm_snapshot_data; | |
138 const uint8_t* ignore_vm_snapshot_instructions; | |
139 app_snapshot->SetBuffers( | |
140 &ignore_vm_snapshot_data, &ignore_vm_snapshot_instructions, | |
141 &isolate_snapshot_data, &isolate_snapshot_instructions); | |
142 | |
143 bin::IsolateData* isolate_data = new bin::IsolateData( | |
144 script_uri, package_root, packages_config, NULL /* app_snapshot */); | |
145 Dart_Isolate isolate = Dart_CreateIsolate( | |
146 script_uri, main, isolate_snapshot_data, isolate_snapshot_instructions, | |
147 flags, isolate_data, error); | |
148 if (isolate == NULL) { | |
149 *error = strdup("Failed to create isolate"); | |
150 delete isolate_data; | |
151 return NULL; | |
152 } | |
153 | |
154 Dart_EnterScope(); | |
155 | |
156 bin::DartUtils::SetOriginalWorkingDirectory(); | |
157 Dart_Handle result = bin::DartUtils::PrepareForScriptLoading( | |
158 false /* is_service_isolate */, false /* trace_loading */); | |
159 CHECK_RESULT(result); | |
160 | |
161 Dart_ExitScope(); | |
162 Dart_ExitIsolate(); | |
163 bool retval = Dart_IsolateMakeRunnable(isolate); | |
164 if (!retval) { | |
165 *error = strdup("Invalid isolate state - Unable to make it runnable"); | |
166 Dart_EnterIsolate(isolate); | |
167 Dart_ShutdownIsolate(); | |
168 return NULL; | |
169 } | |
170 | |
171 return isolate; | |
172 } | |
173 | 84 |
174 static int Main(int argc, const char** argv) { | 85 static int Main(int argc, const char** argv) { |
175 // Flags being passed to the Dart VM. | 86 // Flags being passed to the Dart VM. |
176 int dart_argc = 0; | 87 int dart_argc = 0; |
177 const char** dart_argv = NULL; | 88 const char** dart_argv = NULL; |
178 | 89 |
179 // Perform platform specific initialization. | 90 // Perform platform specific initialization. |
180 if (!dart::bin::Platform::Initialize()) { | 91 if (!dart::bin::Platform::Initialize()) { |
181 OS::PrintErr("Initialization failed\n"); | 92 OS::PrintErr("Initialization failed\n"); |
182 } | 93 } |
183 | 94 |
184 if (argc < 2) { | 95 if (argc < 2) { |
185 // Bad parameter count. | 96 // Bad parameter count. |
186 PrintUsage(); | 97 PrintUsage(); |
187 return 1; | 98 return 1; |
188 } | 99 } else if (argc == 2) { |
189 | 100 if (strcmp(argv[1], "--list") == 0) { |
190 if (argc == 2 && strcmp(argv[1], "--list") == 0) { | 101 run_filter = kList; |
191 run_filter = kList; | 102 // List all tests and benchmarks and exit without initializing the VM. |
192 // List all tests and benchmarks and exit without initializing the VM. | 103 TestCaseBase::RunAll(); |
193 TestCaseBase::RunAll(); | 104 Benchmark::RunAll(argv[0]); |
194 Benchmark::RunAll(argv[0]); | 105 TestCaseBase::RunAllRaw(); |
195 TestCaseBase::RunAllRaw(); | 106 fflush(stdout); |
196 fflush(stdout); | 107 return 0; |
197 return 0; | 108 } else if (strcmp(argv[1], "--benchmarks") == 0) { |
198 } | 109 run_filter = kAllBenchmarks; |
199 | 110 } else { |
200 int arg_pos = 1; | 111 run_filter = argv[1]; |
201 if (strstr(argv[arg_pos], "--dfe") == argv[arg_pos]) { | |
202 const char* delim = strstr(argv[1], "="); | |
203 if (delim == NULL || strlen(delim + 1) == 0) { | |
204 OS::PrintErr("Invalid value for the option: %s\n", argv[1]); | |
205 PrintUsage(); | |
206 return 1; | |
207 } | 112 } |
208 kernel_snapshot = strdup(delim + 1); | |
209 // VM needs '--use-dart-frontend' option, which we will insert in place | |
210 // of '--dfe' option. | |
211 argv[arg_pos] = strdup("--use-dart-frontend"); | |
212 ++arg_pos; | |
213 } | |
214 | |
215 if (arg_pos == argc - 1 && strcmp(argv[arg_pos], "--benchmarks") == 0) { | |
216 // "--benchmarks" is the last argument. | |
217 run_filter = kAllBenchmarks; | |
218 } else { | 113 } else { |
219 // Last argument is the test name, the rest are vm flags. | 114 // Last argument is the test name, the rest are vm flags. |
220 run_filter = argv[argc - 1]; | 115 run_filter = argv[argc - 1]; |
221 // Remove the first value (executable) from the arguments and | 116 // Remove the first value (executable) from the arguments and |
222 // exclude the last argument which is the test name. | 117 // exclude the last argument which is the test name. |
223 dart_argc = argc - 2; | 118 dart_argc = argc - 2; |
224 dart_argv = &argv[1]; | 119 dart_argv = &argv[1]; |
225 } | 120 } |
226 | |
227 bool set_vm_flags_success = | 121 bool set_vm_flags_success = |
228 Flags::ProcessCommandLineFlags(dart_argc, dart_argv); | 122 Flags::ProcessCommandLineFlags(dart_argc, dart_argv); |
229 ASSERT(set_vm_flags_success); | 123 ASSERT(set_vm_flags_success); |
230 const char* err_msg = Dart::InitOnce( | 124 const char* err_msg = Dart::InitOnce( |
231 dart::bin::vm_snapshot_data, dart::bin::vm_snapshot_instructions, | 125 dart::bin::vm_snapshot_data, dart::bin::vm_snapshot_instructions, NULL, |
232 CreateIsolateAndSetup /* create */, NULL /* shutdown */, | 126 NULL, NULL, NULL, dart::bin::DartUtils::OpenFile, |
233 NULL /* cleanup */, NULL /* thread_exit */, | 127 dart::bin::DartUtils::ReadFile, dart::bin::DartUtils::WriteFile, |
234 dart::bin::DartUtils::OpenFile, dart::bin::DartUtils::ReadFile, | 128 dart::bin::DartUtils::CloseFile, NULL, NULL); |
235 dart::bin::DartUtils::WriteFile, dart::bin::DartUtils::CloseFile, | |
236 NULL /* entropy_source */, NULL /* get_service_assets */); | |
237 | |
238 ASSERT(err_msg == NULL); | 129 ASSERT(err_msg == NULL); |
239 // Apply the filter to all registered tests. | 130 // Apply the filter to all registered tests. |
240 TestCaseBase::RunAll(); | 131 TestCaseBase::RunAll(); |
241 // Apply the filter to all registered benchmarks. | 132 // Apply the filter to all registered benchmarks. |
242 Benchmark::RunAll(argv[0]); | 133 Benchmark::RunAll(argv[0]); |
243 | 134 |
244 err_msg = Dart::Cleanup(); | 135 err_msg = Dart::Cleanup(); |
245 ASSERT(err_msg == NULL); | 136 ASSERT(err_msg == NULL); |
246 | 137 |
247 TestCaseBase::RunAllRaw(); | 138 TestCaseBase::RunAllRaw(); |
248 // Print a warning message if no tests or benchmarks were matched. | 139 // Print a warning message if no tests or benchmarks were matched. |
249 if (run_matches == 0) { | 140 if (run_matches == 0) { |
250 OS::PrintErr("No tests matched: %s\n", run_filter); | 141 OS::PrintErr("No tests matched: %s\n", run_filter); |
251 return 1; | 142 return 1; |
252 } | 143 } |
253 if (DynamicAssertionHelper::failed()) { | 144 if (DynamicAssertionHelper::failed()) { |
254 return 255; | 145 return 255; |
255 } | 146 } |
256 return 0; | 147 return 0; |
257 } | 148 } |
258 | 149 |
259 } // namespace dart | 150 } // namespace dart |
260 | 151 |
261 | 152 |
262 int main(int argc, const char** argv) { | 153 int main(int argc, const char** argv) { |
263 dart::bin::Platform::Exit(dart::Main(argc, argv)); | 154 dart::bin::Platform::Exit(dart::Main(argc, argv)); |
264 } | 155 } |
OLD | NEW |