/tests/auto/qscriptjstestsuite/tests/ecma_3/RegExp/15.10.6.2-2.js

https://bitbucket.org/ultra_iter/qt-vtl
JavaScript | 367 lines | 178 code | 46 blank | 143 comment | 0 complexity | 1e31812e0cb647ee99b954b9932cc385 MD5 | raw file
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is JavaScript Engine testing utilities.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corp.
  19. * Portions created by the Initial Developer are Copyright (C) 2002
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * pschwartau@netscape.com
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either the GNU General Public License Version 2 or later (the "GPL"), or
  27. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. /*
  39. *
  40. * Date: 18 Feb 2002
  41. * SUMMARY: Testing re.exec(str) when re.lastIndex is < 0 or > str.length
  42. *
  43. * Case 1: If re has the global flag set, then re(str) should be null
  44. * Case 2: If re doesn't have this set, then re(str) should be unaffected
  45. *
  46. * See http://bugzilla.mozilla.org/show_bug.cgi?id=76717
  47. *
  48. *
  49. * From the ECMA-262 Final spec:
  50. *
  51. * 15.10.6.2 RegExp.prototype.exec(string)
  52. * Performs a regular expression match of string against the regular
  53. * expression and returns an Array object containing the results of
  54. * the match, or null if the string did not match.
  55. *
  56. * The string ToString(string) is searched for an occurrence of the
  57. * regular expression pattern as follows:
  58. *
  59. * 1. Let S be the value of ToString(string).
  60. * 2. Let length be the length of S.
  61. * 3. Let lastIndex be the value of the lastIndex property.
  62. * 4. Let i be the value of ToInteger(lastIndex).
  63. * 5. If the global property is false, let i = 0.
  64. * 6. If i < 0 or i > length then set lastIndex to 0 and return null.
  65. * 7. Call [[Match]], giving it the arguments S and i.
  66. * If [[Match]] returned failure, go to step 8;
  67. * otherwise let r be its State result and go to step 10.
  68. * 8. Let i = i+1.
  69. * 9. Go to step 6.
  70. * 10. Let e be r's endIndex value.
  71. * 11. If the global property is true, set lastIndex to e.
  72. *
  73. * etc.
  74. *
  75. *
  76. * So:
  77. *
  78. * A. If the global flag is not set, |lastIndex| is set to 0
  79. * before the match is attempted; thus the match is unaffected.
  80. *
  81. * B. If the global flag IS set and re.lastIndex is >= 0 and <= str.length,
  82. * |lastIndex| is incremented every time there is a match; not from
  83. * i to i+1, but from i to "endIndex" e:
  84. *
  85. * e = (index of last input character matched so far by the pattern) + 1
  86. *
  87. * The match is then attempted from this position in the string (Step 7).
  88. *
  89. * C. When the global flag IS set and re.lastIndex is < 0 or > str.length,
  90. * |lastIndex| is set to 0 and the match returns null.
  91. *
  92. *
  93. * Note the |lastIndex| property is writeable, and may be set arbitrarily
  94. * by the programmer - and we will do that below.
  95. *
  96. */
  97. //-----------------------------------------------------------------------------
  98. var gTestfile = '15.10.6.2-2.js';
  99. var i = 0;
  100. var BUGNUMBER = 76717;
  101. var summary = 'Testing re.exec(str) when re.lastIndex is < 0 or > str.length';
  102. var status = '';
  103. var statusmessages = new Array();
  104. var pattern = '';
  105. var patterns = new Array();
  106. var string = '';
  107. var strings = new Array();
  108. var actualmatch = '';
  109. var actualmatches = new Array();
  110. var expectedmatch = '';
  111. var expectedmatches = new Array();
  112. /******************************************************************************
  113. *
  114. * Case 1 : when the global flag is set -
  115. *
  116. *****************************************************************************/
  117. pattern = /abc/gi;
  118. string = 'AbcaBcabC';
  119. status = inSection(1);
  120. actualmatch = pattern.exec(string);
  121. expectedmatch = Array('Abc');
  122. addThis();
  123. status = inSection(2);
  124. actualmatch = pattern.exec(string);
  125. expectedmatch = Array('aBc');
  126. addThis();
  127. status = inSection(3);
  128. actualmatch = pattern.exec(string);
  129. expectedmatch = Array('abC');
  130. addThis();
  131. /*
  132. * At this point |lastIndex| is > string.length, so the match should be null -
  133. */
  134. status = inSection(4);
  135. actualmatch = pattern.exec(string);
  136. expectedmatch = null;
  137. addThis();
  138. /*
  139. * Now let's set |lastIndex| to -1, so the match should again be null -
  140. */
  141. status = inSection(5);
  142. pattern.lastIndex = -1;
  143. actualmatch = pattern.exec(string);
  144. expectedmatch = null;
  145. addThis();
  146. /*
  147. * Now try some edge-case values. Thanks to the work done in
  148. * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex|
  149. * is now stored as a double instead of a uint32 (unsigned integer).
  150. *
  151. * Note 2^32 -1 is the upper bound for uint32's, but doubles can go
  152. * all the way up to Number.MAX_VALUE. So that's why we need cases
  153. * between those two numbers.
  154. */
  155. status = inSection(6);
  156. pattern.lastIndex = Math.pow(2,32);
  157. actualmatch = pattern.exec(string);
  158. expectedmatch = null;
  159. addThis();
  160. status = inSection(7);
  161. pattern.lastIndex = -Math.pow(2,32);
  162. actualmatch = pattern.exec(string);
  163. expectedmatch = null;
  164. addThis();
  165. status = inSection(8);
  166. pattern.lastIndex = Math.pow(2,32) + 1;
  167. actualmatch = pattern.exec(string);
  168. expectedmatch = null;
  169. addThis();
  170. status = inSection(9);
  171. pattern.lastIndex = -(Math.pow(2,32) + 1);
  172. actualmatch = pattern.exec(string);
  173. expectedmatch = null;
  174. addThis();
  175. status = inSection(10);
  176. pattern.lastIndex = Math.pow(2,32) * 2;
  177. actualmatch = pattern.exec(string);
  178. expectedmatch = null;
  179. addThis();
  180. status = inSection(11);
  181. pattern.lastIndex = -Math.pow(2,32) * 2;
  182. actualmatch = pattern.exec(string);
  183. expectedmatch = null;
  184. addThis();
  185. status = inSection(12);
  186. pattern.lastIndex = Math.pow(2,40);
  187. actualmatch = pattern.exec(string);
  188. expectedmatch = null;
  189. addThis();
  190. status = inSection(13);
  191. pattern.lastIndex = -Math.pow(2,40);
  192. actualmatch = pattern.exec(string);
  193. expectedmatch = null;
  194. addThis();
  195. status = inSection(14);
  196. pattern.lastIndex = Number.MAX_VALUE;
  197. actualmatch = pattern.exec(string);
  198. expectedmatch = null;
  199. addThis();
  200. status = inSection(15);
  201. pattern.lastIndex = -Number.MAX_VALUE;
  202. actualmatch = pattern.exec(string);
  203. expectedmatch = null;
  204. addThis();
  205. /******************************************************************************
  206. *
  207. * Case 2: repeat all the above cases WITHOUT the global flag set.
  208. * According to EMCA. |lastIndex| should get set to 0 before the match.
  209. *
  210. * Therefore re.exec(str) should be unaffected; thus our expected values
  211. * below are now DIFFERENT when |lastIndex| is < 0 or > str.length
  212. *
  213. *****************************************************************************/
  214. pattern = /abc/i;
  215. string = 'AbcaBcabC';
  216. status = inSection(16);
  217. actualmatch = pattern.exec(string);
  218. expectedmatch = Array('Abc');
  219. addThis();
  220. status = inSection(17);
  221. actualmatch = pattern.exec(string);
  222. expectedmatch = Array('Abc'); // NOT Array('aBc') as before -
  223. addThis();
  224. status = inSection(18);
  225. actualmatch = pattern.exec(string);
  226. expectedmatch = Array('Abc'); // NOT Array('abC') as before -
  227. addThis();
  228. /*
  229. * At this point above, |lastIndex| WAS > string.length, but not here -
  230. */
  231. status = inSection(19);
  232. actualmatch = pattern.exec(string);
  233. expectedmatch = Array('Abc') // NOT null as before -
  234. addThis();
  235. /*
  236. * Now let's set |lastIndex| to -1
  237. */
  238. status = inSection(20);
  239. pattern.lastIndex = -1;
  240. actualmatch = pattern.exec(string);
  241. expectedmatch = Array('Abc') // NOT null as before -
  242. addThis();
  243. /*
  244. * Now try some edge-case values. Thanks to the work done in
  245. * http://bugzilla.mozilla.org/show_bug.cgi?id=124339, |lastIndex|
  246. * is now stored as a double instead of a uint32 (unsigned integer).
  247. *
  248. * Note 2^32 -1 is the upper bound for uint32's, but doubles can go
  249. * all the way up to Number.MAX_VALUE. So that's why we need cases
  250. * between those two numbers.
  251. */
  252. status = inSection(21);
  253. pattern.lastIndex = Math.pow(2,32);
  254. actualmatch = pattern.exec(string);
  255. expectedmatch = Array('Abc') // NOT null as before -
  256. addThis();
  257. status = inSection(22);
  258. pattern.lastIndex = -Math.pow(2,32);
  259. actualmatch = pattern.exec(string);
  260. expectedmatch = Array('Abc') // NOT null as before -
  261. addThis();
  262. status = inSection(23);
  263. pattern.lastIndex = Math.pow(2,32) + 1;
  264. actualmatch = pattern.exec(string);
  265. expectedmatch = Array('Abc') // NOT null as before -
  266. addThis();
  267. status = inSection(24);
  268. pattern.lastIndex = -(Math.pow(2,32) + 1);
  269. actualmatch = pattern.exec(string);
  270. expectedmatch = Array('Abc') // NOT null as before -
  271. addThis();
  272. status = inSection(25);
  273. pattern.lastIndex = Math.pow(2,32) * 2;
  274. actualmatch = pattern.exec(string);
  275. expectedmatch = Array('Abc') // NOT null as before -
  276. addThis();
  277. status = inSection(26);
  278. pattern.lastIndex = -Math.pow(2,32) * 2;
  279. actualmatch = pattern.exec(string);
  280. expectedmatch = Array('Abc') // NOT null as before -
  281. addThis();
  282. status = inSection(27);
  283. pattern.lastIndex = Math.pow(2,40);
  284. actualmatch = pattern.exec(string);
  285. expectedmatch = Array('Abc') // NOT null as before -;
  286. addThis();
  287. status = inSection(28);
  288. pattern.lastIndex = -Math.pow(2,40);
  289. actualmatch = pattern.exec(string);
  290. expectedmatch = Array('Abc') // NOT null as before -
  291. addThis();
  292. status = inSection(29);
  293. pattern.lastIndex = Number.MAX_VALUE;
  294. actualmatch = pattern.exec(string);
  295. expectedmatch = Array('Abc') // NOT null as before -
  296. addThis();
  297. status = inSection(30);
  298. pattern.lastIndex = -Number.MAX_VALUE;
  299. actualmatch = pattern.exec(string);
  300. expectedmatch = Array('Abc') // NOT null as before -
  301. addThis();
  302. //-------------------------------------------------------------------------------------------------
  303. test();
  304. //-------------------------------------------------------------------------------------------------
  305. function addThis()
  306. {
  307. statusmessages[i] = status;
  308. patterns[i] = pattern;
  309. strings[i] = string;
  310. actualmatches[i] = actualmatch;
  311. expectedmatches[i] = expectedmatch;
  312. i++;
  313. }
  314. function test()
  315. {
  316. enterFunc ('test');
  317. printBugNumber(BUGNUMBER);
  318. printStatus (summary);
  319. testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
  320. exitFunc ('test');
  321. }