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.
24 #include "CurrentTime.h"
25 #include "InitializeThreading.h"
26 #include "JSGlobalObject.h"
27 #include "UStringBuilder.h"
41 #if COMPILER(MSVC) && !OS(WINCE)
48 #include <QCoreApplication>
52 const int MaxLineLength = 100 * 1024;
57 static void cleanupGlobalData(JSGlobalData*);
68 Vector<UString> arguments;
69 Vector<UString> files;
76 long getElapsedMS(); // call stop() first
83 void StopWatch::start()
85 m_startTime = currentTime();
88 void StopWatch::stop()
90 m_stopTime = currentTime();
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(JSGlobalData&, Structure*, const Vector<UString>& arguments);
116 typedef JSGlobalObject Base;
118 static GlobalObject* create(JSGlobalData& globalData, Structure* structure, const Vector<UString>& arguments)
120 return new (allocateCell<GlobalObject>(globalData.heap)) GlobalObject(globalData, structure, arguments);
122 virtual UString className() const { return "global"; }
125 void finishCreation(JSGlobalData& globalData, const Vector<UString>& arguments)
127 Base::finishCreation(globalData, this);
128 UNUSED_PARAM(arguments);
132 COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
133 ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
135 GlobalObject::GlobalObject(JSGlobalData& globalData, Structure* structure, const Vector<UString>& arguments)
136 : JSGlobalObject(globalData, structure)
138 finishCreation(globalData, arguments);
141 // Use SEH for Release builds only to get rid of the crash report dialog
142 // (luckily the same tests fail in Release and Debug builds so far). Need to
143 // be in a separate main function because the realMain function requires object
146 #if COMPILER(MSVC) && !COMPILER(INTEL) && !defined(_DEBUG) && !OS(WINCE)
148 #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
154 int realMain(int argc, char** argv, JSGlobalData*);
156 int main(int argc, char** argv)
160 // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
161 // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
162 // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
167 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
168 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
169 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
170 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
171 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
172 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
179 QCoreApplication app(argc, argv);
182 // Initialize JSC before getting JSGlobalData.
183 JSC::initializeThreading();
185 // We can't use destructors in the following code because it uses Windows
186 // Structured Exception Handling
188 JSGlobalData* globalData = JSGlobalData::create(ThreadStackTypeLarge, LargeHeap).leakRef();
190 res = realMain(argc, argv, globalData);
193 cleanupGlobalData(globalData);
197 static void cleanupGlobalData(JSGlobalData* globalData)
199 JSLock lock(SilenceAssertionsOnly);
200 globalData->clearBuiltinStructures();
201 globalData->heap.destroy();
205 static bool testOneRegExp(JSGlobalData& globalData, RegExp* regexp, RegExpTest* regExpTest, bool verbose, unsigned int lineNumber)
208 Vector<int, 32> outVector;
209 outVector.resize(regExpTest->expectVector.size());
210 int matchResult = regexp->match(globalData, regExpTest->subject, regExpTest->offset, &outVector);
212 if (matchResult != regExpTest->result) {
215 printf("Line %d: results mismatch - expected %d got %d\n", lineNumber, regExpTest->result, matchResult);
216 } else if (matchResult != -1) {
217 if (outVector.size() != regExpTest->expectVector.size()) {
220 printf("Line %d: output vector size mismatch - expected %lu got %lu\n", lineNumber, regExpTest->expectVector.size(), outVector.size());
221 } else if (outVector.size() % 2) {
224 printf("Line %d: output vector size is odd (%lu), should be even\n", lineNumber, outVector.size());
226 // Check in pairs since the first value of the pair could be -1 in which case the second doesn't matter.
227 size_t pairCount = outVector.size() / 2;
228 for (size_t i = 0; i < pairCount; ++i) {
229 size_t startIndex = i*2;
230 if (outVector[startIndex] != regExpTest->expectVector[startIndex]) {
233 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex, regExpTest->expectVector[startIndex], outVector[startIndex]);
235 if ((i > 0) && (regExpTest->expectVector[startIndex] != -1) && (outVector[startIndex+1] != regExpTest->expectVector[startIndex+1])) {
238 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex+1, regExpTest->expectVector[startIndex+1], outVector[startIndex+1]);
247 static int scanString(char* buffer, int bufferLength, UStringBuilder& builder, char termChar)
251 for (int i = 0; i < bufferLength; ++i) {
287 if ((i + 4) >= bufferLength)
289 unsigned int charValue;
290 if (sscanf(buffer+i+1, "%04x", &charValue) != 1)
292 c = static_cast<UChar>(charValue);
313 static RegExp* parseRegExpLine(JSGlobalData& globalData, char* line, int lineLength)
315 UStringBuilder pattern;
320 int i = scanString(line + 1, lineLength - 1, pattern, '/') + 1;
322 if ((i >= lineLength) || (line[i] != '/'))
327 return RegExp::create(globalData, pattern.toUString(), regExpFlags(line + i));
330 static RegExpTest* parseTestLine(char* line, int lineLength)
332 UStringBuilder subjectString;
334 if ((line[0] != ' ') || (line[1] != '"'))
337 int i = scanString(line + 2, lineLength - 2, subjectString, '"') + 2;
339 if ((i >= (lineLength - 2)) || (line[i] != '"') || (line[i+1] != ',') || (line[i+2] != ' '))
346 if (sscanf(line + i, "%d, ", &offset) != 1)
349 while (line[i] && line[i] != ' ')
356 if (sscanf(line + i, "%d, ", &matchResult) != 1)
359 while (line[i] && line[i] != ' ')
364 if (line[i++] != '(')
369 RegExpTest* result = new RegExpTest();
371 result->subject = subjectString.toUString();
372 result->offset = offset;
373 result->result = matchResult;
375 while (line[i] && line[i] != ')') {
376 if (sscanf(line + i, "%d, %d", &start, &end) != 2) {
381 result->expectVector.append(start);
382 result->expectVector.append(end);
384 while (line[i] && (line[i] != ',') && (line[i] != ')'))
387 while (line[i] && (line[i] != ',') && (line[i] != ')'))
392 if (!line[i] || (line[i] != ',')) {
402 static bool runFromFiles(GlobalObject* globalObject, const Vector<UString>& files, bool verbose)
406 Vector<char> scriptBuffer;
408 unsigned failures = 0;
409 char* lineBuffer = new char[MaxLineLength + 1];
411 JSGlobalData& globalData = globalObject->globalData();
414 for (size_t i = 0; i < files.size(); i++) {
415 FILE* testCasesFile = fopen(files[i].utf8().data(), "rb");
417 if (!testCasesFile) {
418 printf("Unable to open test data file \"%s\"\n", files[i].utf8().data());
423 size_t lineLength = 0;
425 unsigned int lineNumber = 0;
427 while ((linePtr = fgets(&lineBuffer[0], MaxLineLength, testCasesFile))) {
428 lineLength = strlen(linePtr);
429 if (linePtr[lineLength - 1] == '\n') {
430 linePtr[lineLength - 1] = '\0';
435 if (linePtr[0] == '#')
438 if (linePtr[0] == '/') {
439 regexp = parseRegExpLine(globalData, linePtr, lineLength);
440 } else if (linePtr[0] == ' ') {
441 RegExpTest* regExpTest = parseTestLine(linePtr, lineLength);
443 if (regexp && regExpTest) {
445 if (!testOneRegExp(globalData, regexp, regExpTest, verbose, lineNumber)) {
447 printf("Failure on line %u\n", lineNumber);
456 fclose(testCasesFile);
460 printf("%u tests run, %u failures\n", tests, failures);
462 printf("%u tests passed\n", tests);
466 globalData.dumpSampleData(globalObject->globalExec());
467 #if ENABLE(REGEXP_TRACING)
468 globalData.dumpRegExpTrace();
473 #define RUNNING_FROM_XCODE 0
475 static NO_RETURN void printUsageStatement(JSGlobalData* globalData, bool help = false)
477 fprintf(stderr, "Usage: regexp_test [options] file\n");
478 fprintf(stderr, " -h|--help Prints this help message\n");
479 fprintf(stderr, " -v|--verbose Verbose output\n");
481 cleanupGlobalData(globalData);
482 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
485 static void parseArguments(int argc, char** argv, Options& options, JSGlobalData* globalData)
488 for (; i < argc; ++i) {
489 const char* arg = argv[i];
490 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
491 printUsageStatement(globalData, true);
492 if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose"))
493 options.verbose = true;
495 options.files.append(argv[i]);
498 for (; i < argc; ++i)
499 options.arguments.append(argv[i]);
502 int realMain(int argc, char** argv, JSGlobalData* globalData)
504 JSLock lock(SilenceAssertionsOnly);
507 parseArguments(argc, argv, options, globalData);
509 GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);
510 bool success = runFromFiles(globalObject, options.files, options.verbose);
512 return success ? 0 : 3;