Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_3 / RegExp / octal-002.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 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  *
41  * Date:    31 July 2002
42  * SUMMARY: Testing regexps containing octal escape sequences
43  * This is an elaboration of mozilla/js/tests/ecma_2/RegExp/octal-003.js
44  *
45  * See http://bugzilla.mozilla.org/show_bug.cgi?id=141078
46  * for a reference on octal escape sequences in regexps.
47  *
48  * NOTE:
49  * We will use the identities '\011' === '\u0009' === '\x09' === '\t'
50  *
51  * The first is an octal escape sequence (\(0-3)OO; O an octal digit).
52  * See ECMA-262 Edition 2, Section 7.7.4 "String Literals". These were
53  * dropped in Edition 3 but we support them for backward compatibility.
54  *
55  * The second is a Unicode escape sequence (\uHHHH; H a hex digit).
56  * Since octal 11 = hex 9, the two escapes define the same character.
57  *
58  * The third is a hex escape sequence (\xHH; H a hex digit).
59  * Since hex 09 = hex 0009, this defines the same character.
60  *
61  * The fourth is the familiar escape sequence for a horizontal tab,
62  * defined in the ECMA spec as having Unicode value \u0009.
63  */
64 //-----------------------------------------------------------------------------
65 var i = 0;
66 var BUGNUMBER = 141078;
67 var summary = 'Testing regexps containing octal escape sequences';
68 var status = '';
69 var statusmessages = new Array();
70 var pattern = '';
71 var patterns = new Array();
72 var string = '';
73 var strings = new Array();
74 var actualmatch = '';
75 var actualmatches = new Array();
76 var expectedmatch = '';
77 var expectedmatches = new Array();
78
79
80 /*
81  * Test a string containing the null character '\0' followed by the string '11'
82  *
83  *               'a' + String.fromCharCode(0) + '11';
84  *
85  * Note we can't simply write 'a\011', because '\011' would be interpreted
86  * as the octal escape sequence for the tab character (see above).
87  *
88  * We should get no match from the regexp /.\011/, because it should be
89  * looking for the octal escape sequence \011, i.e. the tab character -
90  *
91  */
92 status = inSection(1);
93 pattern = /.\011/;
94 string = 'a' + String.fromCharCode(0) + '11';
95 actualmatch = string.match(pattern);
96 expectedmatch = null;
97 addThis();
98
99
100 /*
101  * Try same thing with 'xx' in place of '11'.
102  *
103  * Should get a match now, because the octal escape sequence in the regexp
104  * has been reduced from \011 to \0, and '\0' is present in the string -
105  */
106 status = inSection(2);
107 pattern = /.\0xx/;
108 string = 'a' + String.fromCharCode(0) + 'xx';
109 actualmatch = string.match(pattern);
110 expectedmatch = Array(string);
111 addThis();
112
113
114 /*
115  * Same thing; don't use |String.fromCharCode(0)| this time.
116  * There is no ambiguity in '\0xx': it is the null character
117  * followed by two x's, no other interpretation is possible.
118  */
119 status = inSection(3);
120 pattern = /.\0xx/;
121 string = 'a\0xx';
122 actualmatch = string.match(pattern);
123 expectedmatch = Array(string);
124 addThis();
125
126
127 /*
128  * This one should produce a match. The two-character string
129  * 'a' + '\011' is duplicated in the pattern and test string:
130  */
131 status = inSection(4);
132 pattern = /.\011/;
133 string = 'a\011';
134 actualmatch = string.match(pattern);
135 expectedmatch = Array(string);
136 addThis();
137
138
139 /*
140  * Same as above, only now, for the second character of the string,
141  * use the Unicode escape '\u0009' instead of the octal escape '\011'
142  */
143 status = inSection(5);
144 pattern = /.\011/;
145 string = 'a\u0009';
146 actualmatch = string.match(pattern);
147 expectedmatch = Array(string);
148 addThis();
149
150
151 /*
152  * Same as above, only now  for the second character of the string,
153  * use the hex escape '\x09' instead of the octal escape '\011'
154  */
155 status = inSection(6);
156 pattern = /.\011/;
157 string = 'a\x09';
158 actualmatch = string.match(pattern);
159 expectedmatch = Array(string);
160 addThis();
161
162
163 /*
164  * Same as above, only now  for the second character of the string,
165  * use the escape '\t' instead of the octal escape '\011'
166  */
167 status = inSection(7);
168 pattern = /.\011/;
169 string = 'a\t';
170 actualmatch = string.match(pattern);
171 expectedmatch = Array(string);
172 addThis();
173
174
175 /*
176  * Return to the string from Section 1.
177  *
178  * Unlike Section 1, use the RegExp() function to create the
179  * regexp pattern: null character followed by the string '11'.
180  *
181  * Since this is exactly what the string is, we should get a match -
182  */
183 status = inSection(8);
184 string = 'a' + String.fromCharCode(0) + '11';
185 pattern = RegExp(string);
186 actualmatch = string.match(pattern);
187 expectedmatch = Array(string);
188 addThis();
189
190
191
192
193 //-------------------------------------------------------------------------------------------------
194 test();
195 //-------------------------------------------------------------------------------------------------
196
197
198
199 function addThis()
200 {
201   statusmessages[i] = status;
202   patterns[i] = pattern;
203   strings[i] = string;
204   actualmatches[i] = actualmatch;
205   expectedmatches[i] = expectedmatch;
206   i++;
207 }
208
209
210 function test()
211 {
212   enterFunc ('test');
213   printBugNumber(BUGNUMBER);
214   printStatus (summary);
215   testRegExp(statusmessages, patterns, strings, actualmatches, expectedmatches);
216   exitFunc ('test');
217 }