Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_2 / String / split-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 Communication Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38
39 /**
40  *  File Name:          String/split-002.js
41  *  ECMA Section:       15.6.4.9
42  *  Description:        Based on ECMA 2 Draft 7 February 1999
43  *
44  *  Author:             christine@netscape.com
45  *  Date:               19 February 1999
46  */
47
48 /*
49  * Since regular expressions have been part of JavaScript since 1.2, there
50  * are already tests for regular expressions in the js1_2/regexp folder.
51  *
52  * These new tests try to supplement the existing tests, and verify that
53  * our implementation of RegExp conforms to the ECMA specification, but
54  * does not try to be as exhaustive as in previous tests.
55  *
56  * The [,limit] argument to String.split is new, and not covered in any
57  * existing tests.
58  *
59  * String.split cases are covered in ecma/String/15.5.4.8-*.js.
60  * String.split where separator is a RegExp are in
61  * js1_2/regexp/string_split.js
62  *
63  */
64
65 var SECTION = "ecma_2/String/split-002.js";
66 var VERSION = "ECMA_2";
67 var TITLE   = "String.prototype.split( regexp, [,limit] )";
68
69 startTest();
70
71 // the separator is not supplied
72 // separator is undefined
73 // separator is an empty string
74
75 //    AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
76 //    AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
77
78 // separator is an empty regexp
79 // separator is not supplied
80
81 CompareSplit( "hello", "ll" );
82
83 CompareSplit( "hello", "l" );
84 CompareSplit( "hello", "x" );
85 CompareSplit( "hello", "h" );
86 CompareSplit( "hello", "o" );
87 CompareSplit( "hello", "hello" );
88 CompareSplit( "hello", undefined );
89
90 CompareSplit( "hello", "");
91 CompareSplit( "hello", "hellothere" );
92
93 CompareSplit( new String("hello" ) );
94
95
96 Number.prototype.split = String.prototype.split;
97
98 CompareSplit( new Number(100111122133144155), 1 );
99 CompareSplitWithLimit(new Number(100111122133144155), 1, 1 );
100
101 CompareSplitWithLimit(new Number(100111122133144155), 1, 2 );
102 CompareSplitWithLimit(new Number(100111122133144155), 1, 0 );
103 CompareSplitWithLimit(new Number(100111122133144155), 1, 100 );
104 CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 );
105 CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 );
106 CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" );
107 CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) );
108 CompareSplitWithLimit( "hello", "l", NaN );
109 CompareSplitWithLimit( "hello", "l", 0 );
110 CompareSplitWithLimit( "hello", "l", 1 );
111 CompareSplitWithLimit( "hello", "l", 2 );
112 CompareSplitWithLimit( "hello", "l", 3 );
113 CompareSplitWithLimit( "hello", "l", 4 );
114
115
116 /*
117   CompareSplitWithLimit( "hello", "ll", 0 );
118   CompareSplitWithLimit( "hello", "ll", 1 );
119   CompareSplitWithLimit( "hello", "ll", 2 );
120   CompareSplit( "", " " );
121   CompareSplit( "" );
122 */
123
124 // separartor is a regexp
125 // separator regexp value global setting is set
126 // string is an empty string
127 // if separator is an empty string, split each by character
128
129 // this is not a String object
130
131 // limit is not a number
132 // limit is undefined
133 // limit is larger than 2^32-1
134 // limit is a negative number
135
136 test();
137
138 function CompareSplit( string, separator ) {
139   split_1 = string.split( separator );
140   split_2 = string_split( string, separator );
141
142   AddTestCase(
143     "( " + string +".split(" + separator + ") ).length" ,
144     split_2.length,
145     split_1.length );
146
147   var limit = split_1.length > split_2.length ?
148     split_1.length : split_2.length;
149
150   for ( var split_item = 0; split_item < limit; split_item++ ) {
151     AddTestCase(
152       string + ".split(" + separator + ")["+split_item+"]",
153       split_2[split_item],
154       split_1[split_item] );
155   }
156 }
157
158 function CompareSplitWithLimit( string, separator, splitlimit ) {
159   split_1 = string.split( separator, splitlimit );
160   split_2 = string_split( string, separator, splitlimit );
161
162   AddTestCase(
163     "( " + string +".split(" + separator + ", " + splitlimit+") ).length" ,
164     split_2.length,
165     split_1.length );
166
167   var limit = split_1.length > split_2.length ?
168     split_1.length : split_2.length;
169
170   for ( var split_item = 0; split_item < limit; split_item++ ) {
171     AddTestCase(
172       string + ".split(" + separator  + ", " + splitlimit+")["+split_item+"]",
173       split_2[split_item],
174       split_1[split_item] );
175   }
176 }
177
178 function string_split ( __this, separator, limit ) {
179   var S = String(__this );                                        // 1
180
181   var A = new Array();                          // 2
182
183   if ( limit == undefined ) {                   // 3
184     lim = Math.pow(2, 31 ) -1;
185   } else {
186     lim = ToUint32( limit );
187   }
188
189   var s = S.length;                              // 4
190   var p = 0;                                     // 5
191
192   if  ( separator == undefined ) {              // 8
193     A[0] = S;
194     return A;
195   }
196
197   if ( separator.constructor == RegExp )         // 6
198     R = separator;
199   else
200     R = separator.toString();
201
202   if (lim == 0) return A;                       // 7
203
204   if  ( separator == undefined ) {              // 8
205     A[0] = S;
206     return A;
207   }
208
209   if (s == 0) {                                   // 9
210     z = SplitMatch(R, S, 0);
211     if (z != false) return A;
212     A[0] = S;
213     return A;
214   }
215
216   var q = p;                                                                      // 10
217 loop:
218   while (true ) {
219         
220     if ( q == s ) break;                                          // 11
221
222     z = SplitMatch(R, S, q);                  // 12
223
224 //print("Returned ", z);
225
226     if (z != false) {                                                   // 13
227       e = z.endIndex;                                                   // 14
228       cap = z.captures;                                         // 14
229       if (e != p) {                                                     // 15
230 //print("S = ", S, ", p = ", p, ", q = ", q);
231         T = S.slice(p, q);                                      // 16
232 //print("T = ", T);
233         A[A.length] = T;                                        // 17
234         if (A.length == lim) return A;          // 18
235         p = e;                                                          // 19
236         i = 0;                                                          // 20
237         while (true) {                                          // 25
238           if (i == cap.length) {              // 21
239             q = p;                          // 10
240             continue loop;
241           }
242           i = i + 1;                                                    // 22
243           A[A.length] = cap[i]                          // 23
244             if (A.length == lim) return A;              // 24
245         }
246       }
247     }
248
249     q = q + 1;                               // 26
250   }
251
252   T = S.slice(p, q);
253   A[A.length] = T;
254   return A;
255 }
256
257 function SplitMatch(R, S, q)
258 {
259   if (R.constructor == RegExp) {                        // 1
260     var reResult = R.match(S, q);               // 8
261     if (reResult == undefined)
262       return false;
263     else {
264       a = new Array(reResult.length - 1);
265       for (var i = 1; i < reResult.length; i++)
266         a[a.length] = reResult[i];
267       return { endIndex : reResult.index + reResult[0].length, captures : cap };
268     }
269   }
270   else {
271     var r = R.length;                                   // 2
272     s = S.length;                                               // 3
273     if ((q + r) > s) return false;              // 4
274     for (var i = 0; i < r; i++) {
275 //print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i));
276       if (S.charAt(q + i) != R.charAt(i))                       // 5
277         return false;
278     }
279     cap = new Array();                                                          // 6
280     return { endIndex : q + r, captures : cap };        // 7
281   }
282 }
283
284 function ToUint32( n ) {
285   n = Number( n );
286   var sign = ( n < 0 ) ? -1 : 1;
287
288   if ( Math.abs( n ) == 0
289        || Math.abs( n ) == Number.POSITIVE_INFINITY
290        || n != n) {
291     return 0;
292   }
293   n = sign * Math.floor( Math.abs(n) )
294
295     n = n % Math.pow(2,32);
296
297   if ( n < 0 ){
298     n += Math.pow(2,32);
299   }
300
301   return ( n );
302 }