2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include <wtf/CurrentTime.h>
26 #include "InitializeThreading.h"
27 #include "JSGlobalObject.h"
28 #include "Operations.h"
33 #include <wtf/text/StringBuilder.h>
43 #if COMPILER(MSVC) && !OS(WINCE)
50 #include <QCoreApplication>
54 const int MaxLineLength = 100 * 1024;
68 Vector<String> arguments;
76 long getElapsedMS(); // call stop() first
83 void StopWatch::start()
85 m_startTime = monotonicallyIncreasingTime();
88 void StopWatch::stop()
90 m_stopTime = monotonicallyIncreasingTime();
93 long StopWatch::getElapsedMS()
95 return static_cast<long>((m_stopTime - m_startTime) * 1000);
108 Vector<int, 32> expectVector;
111 class GlobalObject : public JSGlobalObject {
113 GlobalObject(VM&, Structure*, const Vector<String>& arguments);
116 typedef JSGlobalObject Base;
118 static GlobalObject* create(VM& vm, Structure* structure, const Vector<String>& arguments)
120 GlobalObject* globalObject = new (NotNull, allocateCell<GlobalObject>(vm.heap)) GlobalObject(vm, structure, arguments);
121 vm.heap.addFinalizer(globalObject, destroy);
127 static const bool needsDestructor = false;
129 static Structure* createStructure(VM& vm, JSValue prototype)
131 return Structure::create(vm, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), info());
135 void finishCreation(VM& vm, const Vector<String>& arguments)
137 Base::finishCreation(vm);
138 UNUSED_PARAM(arguments);
142 const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
144 GlobalObject::GlobalObject(VM& vm, Structure* structure, const Vector<String>& arguments)
145 : JSGlobalObject(vm, structure)
147 finishCreation(vm, arguments);
150 // Use SEH for Release builds only to get rid of the crash report dialog
151 // (luckily the same tests fail in Release and Debug builds so far). Need to
152 // be in a separate main function because the realMain function requires object
155 #if COMPILER(MSVC) && !COMPILER(INTEL) && !defined(_DEBUG) && !OS(WINCE)
157 #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
163 int realMain(int argc, char** argv);
165 int main(int argc, char** argv)
169 // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
170 // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
171 // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
176 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
177 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
178 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
179 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
180 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
181 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
188 QCoreApplication app(argc, argv);
191 // Initialize JSC before getting VM.
192 JSC::initializeThreading();
194 // We can't use destructors in the following code because it uses Windows
195 // Structured Exception Handling
198 res = realMain(argc, argv);
203 static bool testOneRegExp(VM& vm, RegExp* regexp, RegExpTest* regExpTest, bool verbose, unsigned int lineNumber)
206 Vector<int, 32> outVector;
207 outVector.resize(regExpTest->expectVector.size());
208 int matchResult = regexp->match(vm, regExpTest->subject, regExpTest->offset, outVector);
210 if (matchResult != regExpTest->result) {
213 printf("Line %d: results mismatch - expected %d got %d\n", lineNumber, regExpTest->result, matchResult);
214 } else if (matchResult != -1) {
215 if (outVector.size() != regExpTest->expectVector.size()) {
218 printf("Line %d: output vector size mismatch - expected %lu got %lu\n", lineNumber, regExpTest->expectVector.size(), outVector.size());
219 } else if (outVector.size() % 2) {
222 printf("Line %d: output vector size is odd (%lu), should be even\n", lineNumber, outVector.size());
224 // Check in pairs since the first value of the pair could be -1 in which case the second doesn't matter.
225 size_t pairCount = outVector.size() / 2;
226 for (size_t i = 0; i < pairCount; ++i) {
227 size_t startIndex = i*2;
228 if (outVector[startIndex] != regExpTest->expectVector[startIndex]) {
231 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex, regExpTest->expectVector[startIndex], outVector[startIndex]);
233 if ((i > 0) && (regExpTest->expectVector[startIndex] != -1) && (outVector[startIndex+1] != regExpTest->expectVector[startIndex+1])) {
236 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex+1, regExpTest->expectVector[startIndex+1], outVector[startIndex+1]);
245 static int scanString(char* buffer, int bufferLength, StringBuilder& builder, char termChar)
249 for (int i = 0; i < bufferLength; ++i) {
285 if ((i + 4) >= bufferLength)
287 unsigned int charValue;
288 if (sscanf(buffer+i+1, "%04x", &charValue) != 1)
290 c = static_cast<UChar>(charValue);
311 static RegExp* parseRegExpLine(VM& vm, char* line, int lineLength)
313 StringBuilder pattern;
318 int i = scanString(line + 1, lineLength - 1, pattern, '/') + 1;
320 if ((i >= lineLength) || (line[i] != '/'))
325 return RegExp::create(vm, pattern.toString(), regExpFlags(line + i));
328 static RegExpTest* parseTestLine(char* line, int lineLength)
330 StringBuilder subjectString;
332 if ((line[0] != ' ') || (line[1] != '"'))
335 int i = scanString(line + 2, lineLength - 2, subjectString, '"') + 2;
337 if ((i >= (lineLength - 2)) || (line[i] != '"') || (line[i+1] != ',') || (line[i+2] != ' '))
344 if (sscanf(line + i, "%d, ", &offset) != 1)
347 while (line[i] && line[i] != ' ')
354 if (sscanf(line + i, "%d, ", &matchResult) != 1)
357 while (line[i] && line[i] != ' ')
362 if (line[i++] != '(')
367 RegExpTest* result = new RegExpTest();
369 result->subject = subjectString.toString();
370 result->offset = offset;
371 result->result = matchResult;
373 while (line[i] && line[i] != ')') {
374 if (sscanf(line + i, "%d, %d", &start, &end) != 2) {
379 result->expectVector.append(start);
380 result->expectVector.append(end);
382 while (line[i] && (line[i] != ',') && (line[i] != ')'))
385 while (line[i] && (line[i] != ',') && (line[i] != ')'))
390 if (!line[i] || (line[i] != ',')) {
400 static bool runFromFiles(GlobalObject* globalObject, const Vector<String>& files, bool verbose)
404 Vector<char> scriptBuffer;
406 unsigned failures = 0;
407 char* lineBuffer = new char[MaxLineLength + 1];
409 VM& vm = globalObject->vm();
412 for (size_t i = 0; i < files.size(); i++) {
413 FILE* testCasesFile = fopen(files[i].utf8().data(), "rb");
415 if (!testCasesFile) {
416 printf("Unable to open test data file \"%s\"\n", files[i].utf8().data());
421 size_t lineLength = 0;
423 unsigned int lineNumber = 0;
425 while ((linePtr = fgets(&lineBuffer[0], MaxLineLength, testCasesFile))) {
426 lineLength = strlen(linePtr);
427 if (linePtr[lineLength - 1] == '\n') {
428 linePtr[lineLength - 1] = '\0';
433 if (linePtr[0] == '#')
436 if (linePtr[0] == '/') {
437 regexp = parseRegExpLine(vm, linePtr, lineLength);
438 } else if (linePtr[0] == ' ') {
439 RegExpTest* regExpTest = parseTestLine(linePtr, lineLength);
441 if (regexp && regExpTest) {
443 if (!testOneRegExp(vm, regexp, regExpTest, verbose, lineNumber)) {
445 printf("Failure on line %u\n", lineNumber);
454 fclose(testCasesFile);
458 printf("%u tests run, %u failures\n", tests, failures);
460 printf("%u tests passed\n", tests);
464 vm.dumpSampleData(globalObject->globalExec());
465 #if ENABLE(REGEXP_TRACING)
466 vm.dumpRegExpTrace();
471 #define RUNNING_FROM_XCODE 0
473 static NO_RETURN void printUsageStatement(bool help = false)
475 fprintf(stderr, "Usage: regexp_test [options] file\n");
476 fprintf(stderr, " -h|--help Prints this help message\n");
477 fprintf(stderr, " -v|--verbose Verbose output\n");
479 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
482 static void parseArguments(int argc, char** argv, CommandLine& options)
485 for (; i < argc; ++i) {
486 const char* arg = argv[i];
487 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
488 printUsageStatement(true);
489 if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose"))
490 options.verbose = true;
492 options.files.append(argv[i]);
495 for (; i < argc; ++i)
496 options.arguments.append(argv[i]);
499 int realMain(int argc, char** argv)
501 VM* vm = VM::create(LargeHeap).leakRef();
502 APIEntryShim shim(vm);
505 parseArguments(argc, argv, options);
507 GlobalObject* globalObject = GlobalObject::create(*vm, GlobalObject::createStructure(*vm, jsNull()), options.arguments);
508 bool success = runFromFiles(globalObject, options.files, options.verbose);
510 return success ? 0 : 3;