Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / RegExp / 15.10.6.2-1.js
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 mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
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: 23 October 2001
41  *
42  * SUMMARY: Testing regexps with the global flag set.
43  * NOT every substring fitting the given pattern will be matched.
44  * The parent string is CONSUMED as successive matches are found.
45  *
46  * From the ECMA-262 Final spec:
47  *
48  * 15.10.6.2 RegExp.prototype.exec(string)
49  * Performs a regular expression match of string against the regular
50  * expression and returns an Array object containing the results of
51  * the match, or null if the string did not match.
52  *
53  * The string ToString(string) is searched for an occurrence of the
54  * regular expression pattern as follows:
55  *
56  * 1.  Let S be the value of ToString(string).
57  * 2.  Let length be the length of S.
58  * 3.  Let lastIndex be the value of the lastIndex property.
59  * 4.  Let i be the value of ToInteger(lastIndex).
60  * 5.  If the global property is false, let i = 0.
61  * 6.  If i < 0 or i > length then set lastIndex to 0 and return null.
62  * 7.  Call [[Match]], giving it the arguments S and i.
63  *     If [[Match]] returned failure, go to step 8;
64  *     otherwise let r be its State result and go to step 10.
65  * 8.  Let i = i+1.
66  * 9.  Go to step 6.
67  * 10. Let e be r's endIndex value.
68  * 11. If the global property is true, set lastIndex to e.
69  *
70  *          etc.
71  *
72  *
73  * So when the global flag is set, |lastIndex| is incremented every time
74  * there is a match; not from i to i+1, but from i to "endIndex" e:
75  *
76  * e = (index of last input character matched so far by the pattern) + 1
77  *
78  * Thus in the example below, the first endIndex e occurs after the
79  * first match 'a b'. The next match will begin AFTER this, and so
80  * will NOT be 'b c', but rather 'c d'. Similarly, 'd e' won't be matched.
81  */
82 //-----------------------------------------------------------------------------
83 var i = 0;
84 var BUGNUMBER = '(none)';
85 var summary = 'Testing regexps with the global flag set';
86 var status = '';
87 var statusmessages = new Array();
88 var pattern = '';
89 var patterns = new Array();
90 var string = '';
91 var strings = new Array();
92 var actualmatch = '';
93 var actualmatches = new Array();
94 var expectedmatch = '';
95 var expectedmatches = new Array();
96
97
98 status = inSection(1);
99 string = 'a b c d e';
100 pattern = /\w\s\w/g;
101 actualmatch = string.match(pattern);
102 expectedmatch = ['a b','c d']; // see above explanation -
103 addThis();
104
105
106 status = inSection(2);
107 string = '12345678';
108 pattern = /\d\d\d/g;
109 actualmatch = string.match(pattern);
110 expectedmatch = ['123','456'];
111 addThis();
112
113
114
115 //-----------------------------------------------------------------------------
116 test();
117 //-----------------------------------------------------------------------------
118
119
120
121 function addThis()
122 {
123   statusmessages[i] = status;
124   patterns[i] = pattern;
125   strings[i] = string;
126   actualmatches[i] = actualmatch;
127   expectedmatches[i] = expectedmatch;
128   i++;
129 }
130
131
132 function test()
133 {
134   enterFunc ('test');
135   printBugNumber(BUGNUMBER);
136   printStatus (summary);
137   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
138   exitFunc ('test');
139 }