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)
49 const int MaxLineLength = 100 * 1024;
63 Vector<String> arguments;
71 long getElapsedMS(); // call stop() first
78 void StopWatch::start()
80 m_startTime = monotonicallyIncreasingTime();
83 void StopWatch::stop()
85 m_stopTime = monotonicallyIncreasingTime();
88 long StopWatch::getElapsedMS()
90 return static_cast<long>((m_stopTime - m_startTime) * 1000);
103 Vector<int, 32> expectVector;
106 class GlobalObject : public JSGlobalObject {
108 GlobalObject(VM&, Structure*, const Vector<String>& arguments);
111 typedef JSGlobalObject Base;
113 static GlobalObject* create(VM& vm, Structure* structure, const Vector<String>& arguments)
115 GlobalObject* globalObject = new (NotNull, allocateCell<GlobalObject>(vm.heap)) GlobalObject(vm, structure, arguments);
116 vm.heap.addFinalizer(globalObject, destroy);
122 static const bool needsDestructor = false;
124 static Structure* createStructure(VM& vm, JSValue prototype)
126 return Structure::create(vm, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), info());
130 void finishCreation(VM& vm, const Vector<String>& arguments)
132 Base::finishCreation(vm);
133 UNUSED_PARAM(arguments);
137 const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
139 GlobalObject::GlobalObject(VM& vm, Structure* structure, const Vector<String>& arguments)
140 : JSGlobalObject(vm, structure)
142 finishCreation(vm, arguments);
145 // Use SEH for Release builds only to get rid of the crash report dialog
146 // (luckily the same tests fail in Release and Debug builds so far). Need to
147 // be in a separate main function because the realMain function requires object
150 #if COMPILER(MSVC) && !defined(_DEBUG) && !OS(WINCE)
152 #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
158 int realMain(int argc, char** argv);
160 int main(int argc, char** argv)
164 // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
165 // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
166 // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
171 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
172 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
173 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
174 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
175 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
176 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
182 // Initialize JSC before getting VM.
183 JSC::initializeThreading();
185 // We can't use destructors in the following code because it uses Windows
186 // Structured Exception Handling
189 res = realMain(argc, argv);
194 static bool testOneRegExp(VM& vm, RegExp* regexp, RegExpTest* regExpTest, bool verbose, unsigned int lineNumber)
197 Vector<int, 32> outVector;
198 outVector.resize(regExpTest->expectVector.size());
199 int matchResult = regexp->match(vm, regExpTest->subject, regExpTest->offset, outVector);
201 if (matchResult != regExpTest->result) {
204 printf("Line %d: results mismatch - expected %d got %d\n", lineNumber, regExpTest->result, matchResult);
205 } else if (matchResult != -1) {
206 if (outVector.size() != regExpTest->expectVector.size()) {
209 printf("Line %d: output vector size mismatch - expected %lu got %lu\n", lineNumber, regExpTest->expectVector.size(), outVector.size());
210 } else if (outVector.size() % 2) {
213 printf("Line %d: output vector size is odd (%lu), should be even\n", lineNumber, outVector.size());
215 // Check in pairs since the first value of the pair could be -1 in which case the second doesn't matter.
216 size_t pairCount = outVector.size() / 2;
217 for (size_t i = 0; i < pairCount; ++i) {
218 size_t startIndex = i*2;
219 if (outVector[startIndex] != regExpTest->expectVector[startIndex]) {
222 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex, regExpTest->expectVector[startIndex], outVector[startIndex]);
224 if ((i > 0) && (regExpTest->expectVector[startIndex] != -1) && (outVector[startIndex+1] != regExpTest->expectVector[startIndex+1])) {
227 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex+1, regExpTest->expectVector[startIndex+1], outVector[startIndex+1]);
236 static int scanString(char* buffer, int bufferLength, StringBuilder& builder, char termChar)
240 for (int i = 0; i < bufferLength; ++i) {
276 if ((i + 4) >= bufferLength)
278 unsigned int charValue;
279 if (sscanf(buffer+i+1, "%04x", &charValue) != 1)
281 c = static_cast<UChar>(charValue);
302 static RegExp* parseRegExpLine(VM& vm, char* line, int lineLength)
304 StringBuilder pattern;
309 int i = scanString(line + 1, lineLength - 1, pattern, '/') + 1;
311 if ((i >= lineLength) || (line[i] != '/'))
316 return RegExp::create(vm, pattern.toString(), regExpFlags(line + i));
319 static RegExpTest* parseTestLine(char* line, int lineLength)
321 StringBuilder subjectString;
323 if ((line[0] != ' ') || (line[1] != '"'))
326 int i = scanString(line + 2, lineLength - 2, subjectString, '"') + 2;
328 if ((i >= (lineLength - 2)) || (line[i] != '"') || (line[i+1] != ',') || (line[i+2] != ' '))
335 if (sscanf(line + i, "%d, ", &offset) != 1)
338 while (line[i] && line[i] != ' ')
345 if (sscanf(line + i, "%d, ", &matchResult) != 1)
348 while (line[i] && line[i] != ' ')
353 if (line[i++] != '(')
358 RegExpTest* result = new RegExpTest();
360 result->subject = subjectString.toString();
361 result->offset = offset;
362 result->result = matchResult;
364 while (line[i] && line[i] != ')') {
365 if (sscanf(line + i, "%d, %d", &start, &end) != 2) {
370 result->expectVector.append(start);
371 result->expectVector.append(end);
373 while (line[i] && (line[i] != ',') && (line[i] != ')'))
376 while (line[i] && (line[i] != ',') && (line[i] != ')'))
381 if (!line[i] || (line[i] != ',')) {
391 static bool runFromFiles(GlobalObject* globalObject, const Vector<String>& files, bool verbose)
395 Vector<char> scriptBuffer;
397 unsigned failures = 0;
398 char* lineBuffer = new char[MaxLineLength + 1];
400 VM& vm = globalObject->vm();
403 for (size_t i = 0; i < files.size(); i++) {
404 FILE* testCasesFile = fopen(files[i].utf8().data(), "rb");
406 if (!testCasesFile) {
407 printf("Unable to open test data file \"%s\"\n", files[i].utf8().data());
412 size_t lineLength = 0;
414 unsigned int lineNumber = 0;
416 while ((linePtr = fgets(&lineBuffer[0], MaxLineLength, testCasesFile))) {
417 lineLength = strlen(linePtr);
418 if (linePtr[lineLength - 1] == '\n') {
419 linePtr[lineLength - 1] = '\0';
424 if (linePtr[0] == '#')
427 if (linePtr[0] == '/') {
428 regexp = parseRegExpLine(vm, linePtr, lineLength);
429 } else if (linePtr[0] == ' ') {
430 RegExpTest* regExpTest = parseTestLine(linePtr, lineLength);
432 if (regexp && regExpTest) {
434 if (!testOneRegExp(vm, regexp, regExpTest, verbose, lineNumber)) {
436 printf("Failure on line %u\n", lineNumber);
445 fclose(testCasesFile);
449 printf("%u tests run, %u failures\n", tests, failures);
451 printf("%u tests passed\n", tests);
455 vm.dumpSampleData(globalObject->globalExec());
456 #if ENABLE(REGEXP_TRACING)
457 vm.dumpRegExpTrace();
462 #define RUNNING_FROM_XCODE 0
464 static NO_RETURN void printUsageStatement(bool help = false)
466 fprintf(stderr, "Usage: regexp_test [options] file\n");
467 fprintf(stderr, " -h|--help Prints this help message\n");
468 fprintf(stderr, " -v|--verbose Verbose output\n");
470 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
473 static void parseArguments(int argc, char** argv, CommandLine& options)
476 for (; i < argc; ++i) {
477 const char* arg = argv[i];
478 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
479 printUsageStatement(true);
480 if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose"))
481 options.verbose = true;
483 options.files.append(argv[i]);
486 for (; i < argc; ++i)
487 options.arguments.append(argv[i]);
490 int realMain(int argc, char** argv)
492 VM* vm = VM::create(LargeHeap).leakRef();
493 APIEntryShim shim(vm);
496 parseArguments(argc, argv, options);
498 GlobalObject* globalObject = GlobalObject::create(*vm, GlobalObject::createStructure(*vm, jsNull()), options.arguments);
499 bool success = runFromFiles(globalObject, options.files, options.verbose);
501 return success ? 0 : 3;