michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * michael@0: * Date: 18 Feb 2002 michael@0: * SUMMARY: Testing re.exec(str) when re.lastIndex is < 0 or > str.length michael@0: * michael@0: * Case 1: If re has the global flag set, then re(str) should be null michael@0: * Case 2: If re doesn't have this set, then re(str) should be unaffected michael@0: * michael@0: * See http://bugzilla.mozilla.org/show_bug.cgi?id=76717 michael@0: * michael@0: * michael@0: * From the ECMA-262 Final spec: michael@0: * michael@0: * 15.10.6.2 RegExp.prototype.exec(string) michael@0: * Performs a regular expression match of string against the regular michael@0: * expression and returns an Array object containing the results of michael@0: * the match, or null if the string did not match. michael@0: * michael@0: * The string ToString(string) is searched for an occurrence of the michael@0: * regular expression pattern as follows: michael@0: * michael@0: * 1. Let S be the value of ToString(string). michael@0: * 2. Let length be the length of S. michael@0: * 3. Let lastIndex be the value of the lastIndex property. michael@0: * 4. Let i be the value of ToInteger(lastIndex). michael@0: * 5. If the global property is false, let i = 0. michael@0: * 6. If i < 0 or i > length then set lastIndex to 0 and return null. michael@0: * 7. Call [[Match]], giving it the arguments S and i. michael@0: * If [[Match]] returned failure, go to step 8; michael@0: * otherwise let r be its State result and go to step 10. michael@0: * 8. Let i = i+1. michael@0: * 9. Go to step 6. michael@0: * 10. Let e be r's endIndex value. michael@0: * 11. If the global property is true, set lastIndex to e. michael@0: * michael@0: * etc. michael@0: * michael@0: * michael@0: * So: michael@0: * michael@0: * A. If the global flag is not set, |lastIndex| is set to 0 michael@0: * before the match is attempted; thus the match is unaffected. michael@0: * michael@0: * B. If the global flag IS set and re.lastIndex is >= 0 and <= str.length, michael@0: * |lastIndex| is incremented every time there is a match; not from michael@0: * i to i+1, but from i to "endIndex" e: michael@0: * michael@0: * e = (index of last input character matched so far by the pattern) + 1 michael@0: * michael@0: * The match is then attempted from this position in the string (Step 7). michael@0: * michael@0: * C. When the global flag IS set and re.lastIndex is < 0 or > str.length, michael@0: * |lastIndex| is set to 0 and the match returns null. michael@0: * michael@0: * michael@0: * Note the |lastIndex| property is writeable, and may be set arbitrarily michael@0: * by the programmer - and we will do that below. michael@0: * michael@0: */ michael@0: //----------------------------------------------------------------------------- michael@0: var i = 0; michael@0: var BUGNUMBER = 76717; michael@0: var summary = 'Testing re.exec(str) when re.lastIndex is < 0 or > str.length'; michael@0: var status = ''; michael@0: var statusmessages = new Array(); michael@0: var pattern = ''; michael@0: var patterns = new Array(); michael@0: var string = ''; michael@0: var strings = new Array(); michael@0: var actualmatch = ''; michael@0: var actualmatches = new Array(); michael@0: var expectedmatch = ''; michael@0: var expectedmatches = new Array(); michael@0: michael@0: michael@0: /****************************************************************************** michael@0: * michael@0: * Case 1 : when the global flag is set - michael@0: * michael@0: *****************************************************************************/ michael@0: pattern = /abc/gi; michael@0: string = 'AbcaBcabC'; michael@0: michael@0: status = inSection(1); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc'); michael@0: addThis(); michael@0: michael@0: status = inSection(2); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('aBc'); michael@0: addThis(); michael@0: michael@0: status = inSection(3); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('abC'); michael@0: addThis(); michael@0: michael@0: /* michael@0: * At this point |lastIndex| is > string.length, so the match should be null - michael@0: */ michael@0: status = inSection(4); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: /* michael@0: * Now let's set |lastIndex| to -1, so the match should again be null - michael@0: */ michael@0: status = inSection(5); michael@0: pattern.lastIndex = -1; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: /* michael@0: * Now try some edge-case values. Thanks to the work done in michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex| michael@0: * is now stored as a double instead of a uint32_t (unsigned integer). michael@0: * michael@0: * Note 2^32 -1 is the upper bound for uint32's, but doubles can go michael@0: * all the way up to Number.MAX_VALUE. So that's why we need cases michael@0: * between those two numbers. michael@0: */ michael@0: status = inSection(6); michael@0: pattern.lastIndex = Math.pow(2,32); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(7); michael@0: pattern.lastIndex = -Math.pow(2,32); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(8); michael@0: pattern.lastIndex = Math.pow(2,32) + 1; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(9); michael@0: pattern.lastIndex = -(Math.pow(2,32) + 1); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(10); michael@0: pattern.lastIndex = Math.pow(2,32) * 2; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(11); michael@0: pattern.lastIndex = -Math.pow(2,32) * 2; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(12); michael@0: pattern.lastIndex = Math.pow(2,40); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(13); michael@0: pattern.lastIndex = -Math.pow(2,40); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(14); michael@0: pattern.lastIndex = Number.MAX_VALUE; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: status = inSection(15); michael@0: pattern.lastIndex = -Number.MAX_VALUE; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = null; michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: /****************************************************************************** michael@0: * michael@0: * Case 2: repeat all the above cases WITHOUT the global flag set. michael@0: * According to EMCA. |lastIndex| should get set to 0 before the match. michael@0: * michael@0: * Therefore re.exec(str) should be unaffected; thus our expected values michael@0: * below are now DIFFERENT when |lastIndex| is < 0 or > str.length michael@0: * michael@0: *****************************************************************************/ michael@0: michael@0: pattern = /abc/i; michael@0: string = 'AbcaBcabC'; michael@0: michael@0: status = inSection(16); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc'); michael@0: addThis(); michael@0: michael@0: status = inSection(17); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc'); // NOT Array('aBc') as before - michael@0: addThis(); michael@0: michael@0: status = inSection(18); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc'); // NOT Array('abC') as before - michael@0: addThis(); michael@0: michael@0: /* michael@0: * At this point above, |lastIndex| WAS > string.length, but not here - michael@0: */ michael@0: status = inSection(19); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: /* michael@0: * Now let's set |lastIndex| to -1 michael@0: */ michael@0: status = inSection(20); michael@0: pattern.lastIndex = -1; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: /* michael@0: * Now try some edge-case values. Thanks to the work done in michael@0: * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex| michael@0: * is now stored as a double instead of a uint32_t (unsigned integer). michael@0: * michael@0: * Note 2^32 -1 is the upper bound for uint32's, but doubles can go michael@0: * all the way up to Number.MAX_VALUE. So that's why we need cases michael@0: * between those two numbers. michael@0: */ michael@0: status = inSection(21); michael@0: pattern.lastIndex = Math.pow(2,32); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(22); michael@0: pattern.lastIndex = -Math.pow(2,32); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(23); michael@0: pattern.lastIndex = Math.pow(2,32) + 1; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(24); michael@0: pattern.lastIndex = -(Math.pow(2,32) + 1); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(25); michael@0: pattern.lastIndex = Math.pow(2,32) * 2; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(26); michael@0: pattern.lastIndex = -Math.pow(2,32) * 2; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(27); michael@0: pattern.lastIndex = Math.pow(2,40); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before -; michael@0: addThis(); michael@0: michael@0: status = inSection(28); michael@0: pattern.lastIndex = -Math.pow(2,40); michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(29); michael@0: pattern.lastIndex = Number.MAX_VALUE; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: status = inSection(30); michael@0: pattern.lastIndex = -Number.MAX_VALUE; michael@0: actualmatch = pattern.exec(string); michael@0: expectedmatch = Array('Abc') // NOT null as before - michael@0: addThis(); michael@0: michael@0: michael@0: michael@0: michael@0: //------------------------------------------------------------------------------------------------- michael@0: test(); michael@0: //------------------------------------------------------------------------------------------------- michael@0: michael@0: michael@0: michael@0: function addThis() michael@0: { michael@0: statusmessages[i] = status; michael@0: patterns[i] = pattern; michael@0: strings[i] = string; michael@0: actualmatches[i] = actualmatch; michael@0: expectedmatches[i] = expectedmatch; michael@0: i++; michael@0: } michael@0: michael@0: michael@0: function test() michael@0: { michael@0: enterFunc ('test'); michael@0: printBugNumber(BUGNUMBER); michael@0: printStatus (summary); michael@0: testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches); michael@0: exitFunc ('test'); michael@0: }