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 <wtf/CurrentTime.h>
25 #include "InitializeThreading.h"
26 #include "JSGlobalObject.h"
31 #include <wtf/text/StringBuilder.h>
41 #if COMPILER(MSVC) && !OS(WINCE)
48 #include <QCoreApplication>
52 const int MaxLineLength = 100 * 1024;
66 Vector<String> arguments;
74 long getElapsedMS(); // call stop() first
81 void StopWatch::start()
83 m_startTime = currentTime();
86 void StopWatch::stop()
88 m_stopTime = currentTime();
91 long StopWatch::getElapsedMS()
93 return static_cast<long>((m_stopTime - m_startTime) * 1000);
106 Vector<int, 32> expectVector;
109 class GlobalObject : public JSGlobalObject {
111 GlobalObject(JSGlobalData&, Structure*, const Vector<String>& arguments);
114 typedef JSGlobalObject Base;
116 static GlobalObject* create(JSGlobalData& globalData, Structure* structure, const Vector<String>& arguments)
118 GlobalObject* globalObject = new (NotNull, allocateCell<GlobalObject>(globalData.heap)) GlobalObject(globalData, structure, arguments);
119 globalData.heap.addFinalizer(globalObject, destroy);
123 static const ClassInfo s_info;
125 static const bool needsDestructor = false;
127 static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
129 return Structure::create(globalData, 0, prototype, TypeInfo(GlobalObjectType, StructureFlags), &s_info);
133 void finishCreation(JSGlobalData& globalData, const Vector<String>& arguments)
135 Base::finishCreation(globalData);
136 UNUSED_PARAM(arguments);
140 COMPILE_ASSERT(!IsInteger<GlobalObject>::value, WTF_IsInteger_GlobalObject_false);
141 ASSERT_CLASS_FITS_IN_CELL(GlobalObject);
143 const ClassInfo GlobalObject::s_info = { "global", &JSGlobalObject::s_info, 0, ExecState::globalObjectTable, CREATE_METHOD_TABLE(GlobalObject) };
145 GlobalObject::GlobalObject(JSGlobalData& globalData, Structure* structure, const Vector<String>& arguments)
146 : JSGlobalObject(globalData, structure)
148 finishCreation(globalData, arguments);
151 // Use SEH for Release builds only to get rid of the crash report dialog
152 // (luckily the same tests fail in Release and Debug builds so far). Need to
153 // be in a separate main function because the realMain function requires object
156 #if COMPILER(MSVC) && !COMPILER(INTEL) && !defined(_DEBUG) && !OS(WINCE)
158 #define EXCEPT(x) } __except (EXCEPTION_EXECUTE_HANDLER) { x; }
164 int realMain(int argc, char** argv);
166 int main(int argc, char** argv)
170 // Cygwin calls ::SetErrorMode(SEM_FAILCRITICALERRORS), which we will inherit. This is bad for
171 // testing/debugging, as it causes the post-mortem debugger not to be invoked. We reset the
172 // error mode here to work around Cygwin's behavior. See <http://webkit.org/b/55222>.
177 _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
178 _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
179 _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
180 _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
181 _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
182 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
189 QCoreApplication app(argc, argv);
192 // Initialize JSC before getting JSGlobalData.
193 JSC::initializeThreading();
195 // We can't use destructors in the following code because it uses Windows
196 // Structured Exception Handling
199 res = realMain(argc, argv);
204 static bool testOneRegExp(JSGlobalData& globalData, RegExp* regexp, RegExpTest* regExpTest, bool verbose, unsigned int lineNumber)
207 Vector<int, 32> outVector;
208 outVector.resize(regExpTest->expectVector.size());
209 int matchResult = regexp->match(globalData, regExpTest->subject, regExpTest->offset, outVector);
211 if (matchResult != regExpTest->result) {
214 printf("Line %d: results mismatch - expected %d got %d\n", lineNumber, regExpTest->result, matchResult);
215 } else if (matchResult != -1) {
216 if (outVector.size() != regExpTest->expectVector.size()) {
219 printf("Line %d: output vector size mismatch - expected %lu got %lu\n", lineNumber, regExpTest->expectVector.size(), outVector.size());
220 } else if (outVector.size() % 2) {
223 printf("Line %d: output vector size is odd (%lu), should be even\n", lineNumber, outVector.size());
225 // Check in pairs since the first value of the pair could be -1 in which case the second doesn't matter.
226 size_t pairCount = outVector.size() / 2;
227 for (size_t i = 0; i < pairCount; ++i) {
228 size_t startIndex = i*2;
229 if (outVector[startIndex] != regExpTest->expectVector[startIndex]) {
232 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex, regExpTest->expectVector[startIndex], outVector[startIndex]);
234 if ((i > 0) && (regExpTest->expectVector[startIndex] != -1) && (outVector[startIndex+1] != regExpTest->expectVector[startIndex+1])) {
237 printf("Line %d: output vector mismatch at index %lu - expected %d got %d\n", lineNumber, startIndex+1, regExpTest->expectVector[startIndex+1], outVector[startIndex+1]);
246 static int scanString(char* buffer, int bufferLength, StringBuilder& builder, char termChar)
250 for (int i = 0; i < bufferLength; ++i) {
286 if ((i + 4) >= bufferLength)
288 unsigned int charValue;
289 if (sscanf(buffer+i+1, "%04x", &charValue) != 1)
291 c = static_cast<UChar>(charValue);
312 static RegExp* parseRegExpLine(JSGlobalData& globalData, char* line, int lineLength)
314 StringBuilder pattern;
319 int i = scanString(line + 1, lineLength - 1, pattern, '/') + 1;
321 if ((i >= lineLength) || (line[i] != '/'))
326 return RegExp::create(globalData, pattern.toString(), regExpFlags(line + i));
329 static RegExpTest* parseTestLine(char* line, int lineLength)
331 StringBuilder subjectString;
333 if ((line[0] != ' ') || (line[1] != '"'))
336 int i = scanString(line + 2, lineLength - 2, subjectString, '"') + 2;
338 if ((i >= (lineLength - 2)) || (line[i] != '"') || (line[i+1] != ',') || (line[i+2] != ' '))
345 if (sscanf(line + i, "%d, ", &offset) != 1)
348 while (line[i] && line[i] != ' ')
355 if (sscanf(line + i, "%d, ", &matchResult) != 1)
358 while (line[i] && line[i] != ' ')
363 if (line[i++] != '(')
368 RegExpTest* result = new RegExpTest();
370 result->subject = subjectString.toString();
371 result->offset = offset;
372 result->result = matchResult;
374 while (line[i] && line[i] != ')') {
375 if (sscanf(line + i, "%d, %d", &start, &end) != 2) {
380 result->expectVector.append(start);
381 result->expectVector.append(end);
383 while (line[i] && (line[i] != ',') && (line[i] != ')'))
386 while (line[i] && (line[i] != ',') && (line[i] != ')'))
391 if (!line[i] || (line[i] != ',')) {
401 static bool runFromFiles(GlobalObject* globalObject, const Vector<String>& files, bool verbose)
405 Vector<char> scriptBuffer;
407 unsigned failures = 0;
408 char* lineBuffer = new char[MaxLineLength + 1];
410 JSGlobalData& globalData = globalObject->globalData();
413 for (size_t i = 0; i < files.size(); i++) {
414 FILE* testCasesFile = fopen(files[i].utf8().data(), "rb");
416 if (!testCasesFile) {
417 printf("Unable to open test data file \"%s\"\n", files[i].utf8().data());
422 size_t lineLength = 0;
424 unsigned int lineNumber = 0;
426 while ((linePtr = fgets(&lineBuffer[0], MaxLineLength, testCasesFile))) {
427 lineLength = strlen(linePtr);
428 if (linePtr[lineLength - 1] == '\n') {
429 linePtr[lineLength - 1] = '\0';
434 if (linePtr[0] == '#')
437 if (linePtr[0] == '/') {
438 regexp = parseRegExpLine(globalData, linePtr, lineLength);
439 } else if (linePtr[0] == ' ') {
440 RegExpTest* regExpTest = parseTestLine(linePtr, lineLength);
442 if (regexp && regExpTest) {
444 if (!testOneRegExp(globalData, regexp, regExpTest, verbose, lineNumber)) {
446 printf("Failure on line %u\n", lineNumber);
455 fclose(testCasesFile);
459 printf("%u tests run, %u failures\n", tests, failures);
461 printf("%u tests passed\n", tests);
465 globalData.dumpSampleData(globalObject->globalExec());
466 #if ENABLE(REGEXP_TRACING)
467 globalData.dumpRegExpTrace();
472 #define RUNNING_FROM_XCODE 0
474 static NO_RETURN void printUsageStatement(bool help = false)
476 fprintf(stderr, "Usage: regexp_test [options] file\n");
477 fprintf(stderr, " -h|--help Prints this help message\n");
478 fprintf(stderr, " -v|--verbose Verbose output\n");
480 exit(help ? EXIT_SUCCESS : EXIT_FAILURE);
483 static void parseArguments(int argc, char** argv, CommandLine& options)
486 for (; i < argc; ++i) {
487 const char* arg = argv[i];
488 if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
489 printUsageStatement(true);
490 if (!strcmp(arg, "-v") || !strcmp(arg, "--verbose"))
491 options.verbose = true;
493 options.files.append(argv[i]);
496 for (; i < argc; ++i)
497 options.arguments.append(argv[i]);
500 int realMain(int argc, char** argv)
502 RefPtr<JSGlobalData> globalData = JSGlobalData::create(ThreadStackTypeLarge, LargeHeap);
503 JSLockHolder lock(globalData.get());
506 parseArguments(argc, argv, options);
508 GlobalObject* globalObject = GlobalObject::create(*globalData, GlobalObject::createStructure(*globalData, jsNull()), options.arguments);
509 bool success = runFromFiles(globalObject, options.files, options.verbose);
511 return success ? 0 : 3;