Debian Codegraph @

rhino/testsrc/tests/ecma_2/String/split-002.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = 'split-002.js';
7
8 /**
9 * File Name: String/split-002.js
10 * ECMA Section: 15.6.4.9
11 * Description: Based on ECMA 2 Draft 7 February 1999
12 *
13 * Author: [email protected]
14 * Date: 19 February 1999
15 */
16
17 /*
18 * Since regular expressions have been part of JavaScript since 1.2, there
19 * are already tests for regular expressions in the js1_2/regexp folder.
20 *
21 * These new tests try to supplement the existing tests, and verify that
22 * our implementation of RegExp conforms to the ECMA specification, but
23 * does not try to be as exhaustive as in previous tests.
24 *
25 * The [,limit] argument to String.split is new, and not covered in any
26 * existing tests.
27 *
28 * String.split cases are covered in ecma/String/15.5.4.8-*.js.
29 * String.split where separator is a RegExp are in
30 * js1_2/regexp/string_split.js
31 *
32 */
33
34 var SECTION = "ecma_2/String/split-002.js";
35 var VERSION = "ECMA_2";
36 var TITLE = "String.prototype.split( regexp, [,limit] )";
37
38 startTest();
39
40 // the separator is not supplied
41 // separator is undefined
42 // separator is an empty string
43
44 // AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
45 // AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
46
47 // separator is an empty regexp
48 // separator is not supplied
49
50 CompareSplit( "hello", "ll" );
51
52 CompareSplit( "hello", "l" );
53 CompareSplit( "hello", "x" );
54 CompareSplit( "hello", "h" );
55 CompareSplit( "hello", "o" );
56 CompareSplit( "hello", "hello" );
57 CompareSplit( "hello", undefined );
58
59 CompareSplit( "hello", "");
60 CompareSplit( "hello", "hellothere" );
61
62 CompareSplit( new String("hello" ) );
63
64
65 Number.prototype.split = String.prototype.split;
66
67 CompareSplit( new Number(100111122133144155), 1 );
68 CompareSplitWithLimit(new Number(100111122133144155), 1, 1 );
69
70 CompareSplitWithLimit(new Number(100111122133144155), 1, 2 );
71 CompareSplitWithLimit(new Number(100111122133144155), 1, 0 );
72 CompareSplitWithLimit(new Number(100111122133144155), 1, 100 );
73 CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 );
74 CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 );
75 CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" );
76 CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) );
77 CompareSplitWithLimit( "hello", "l", NaN );
78 CompareSplitWithLimit( "hello", "l", 0 );
79 CompareSplitWithLimit( "hello", "l", 1 );
80 CompareSplitWithLimit( "hello", "l", 2 );
81 CompareSplitWithLimit( "hello", "l", 3 );
82 CompareSplitWithLimit( "hello", "l", 4 );
83
84
85 /*
86 CompareSplitWithLimit( "hello", "ll", 0 );
87 CompareSplitWithLimit( "hello", "ll", 1 );
88 CompareSplitWithLimit( "hello", "ll", 2 );
89 CompareSplit( "", " " );
90 CompareSplit( "" );
91 */
92
93 // separartor is a regexp
94 // separator regexp value global setting is set
95 // string is an empty string
96 // if separator is an empty string, split each by character
97
98 // this is not a String object
99
100 // limit is not a number
101 // limit is undefined
102 // limit is larger than 2^32-1
103 // limit is a negative number
104
105 test();
106
107 function CompareSplit( string, separator ) {
108 split_1 = string.split( separator );
109 split_2 = string_split( string, separator );
110
111 AddTestCase(
112 "( " + string +".split(" + separator + ") ).length" ,
113 split_2.length,
114 split_1.length );
115
116 var limit = split_1.length > split_2.length ?
117 split_1.length : split_2.length;
118
119 for ( var split_item = 0; split_item < limit; split_item++ ) {
120 AddTestCase(
121 string + ".split(" + separator + ")["+split_item+"]",
122 split_2[split_item],
123 split_1[split_item] );
124 }
125 }
126
127 function CompareSplitWithLimit( string, separator, splitlimit ) {
128 split_1 = string.split( separator, splitlimit );
129 split_2 = string_split( string, separator, splitlimit );
130
131 AddTestCase(
132 "( " + string +".split(" + separator + ", " + splitlimit+") ).length" ,
133 split_2.length,
134 split_1.length );
135
136 var limit = split_1.length > split_2.length ?
137 split_1.length : split_2.length;
138
139 for ( var split_item = 0; split_item < limit; split_item++ ) {
140 AddTestCase(
141 string + ".split(" + separator + ", " + splitlimit+")["+split_item+"]",
142 split_2[split_item],
143 split_1[split_item] );
144 }
145 }
146
147 function string_split ( __this, separator, limit ) {
148 var S = String(__this ); // 1
149
150 var A = new Array(); // 2
151
152 if ( limit == undefined ) { // 3
153 lim = Math.pow(2, 31 ) -1;
154 } else {
155 lim = ToUint32( limit );
156 }
157
158 var s = S.length; // 4
159 var p = 0; // 5
160
161 if ( separator == undefined ) { // 8
162 A[0] = S;
163 return A;
164 }
165
166 if ( separator.constructor == RegExp ) // 6
167 R = separator;
168 else
169 R = separator.toString();
170
171 if (lim == 0) return A; // 7
172
173 if ( separator == undefined ) { // 8
174 A[0] = S;
175 return A;
176 }
177
178 if (s == 0) { // 9
179 z = SplitMatch(R, S, 0);
180 if (z != false) return A;
181 A[0] = S;
182 return A;
183 }
184
185 var q = p; // 10
186 loop:
187 while (true ) {
188
189 if ( q == s ) break; // 11
190
191 z = SplitMatch(R, S, q); // 12
192
193 //print("Returned ", z);
194
195 if (z != false) { // 13
196 e = z.endIndex; // 14
197 cap = z.captures; // 14
198 if (e != p) { // 15
199 //print("S = ", S, ", p = ", p, ", q = ", q);
200 T = S.slice(p, q); // 16
201 //print("T = ", T);
202 A[A.length] = T; // 17
203 if (A.length == lim) return A; // 18
204 p = e; // 19
205 i = 0; // 20
206 while (true) { // 25
207 if (i == cap.length) { // 21
208 q = p; // 10
209 continue loop;
210 }
211 i = i + 1; // 22
212 A[A.length] = cap[i] // 23
213 if (A.length == lim) return A; // 24
214 }
215 }
216 }
217
218 q = q + 1; // 26
219 }
220
221 T = S.slice(p, q);
222 A[A.length] = T;
223 return A;
224 }
225
226 function SplitMatch(R, S, q)
227 {
228 if (R.constructor == RegExp) { // 1
229 var reResult = R.match(S, q); // 8
230 if (reResult == undefined)
231 return false;
232 else {
233 a = new Array(reResult.length - 1);
234 for (var i = 1; i < reResult.length; i++)
235 a[a.length] = reResult[i];
236 return { endIndex : reResult.index + reResult[0].length, captures : cap };
237 }
238 }
239 else {
240 var r = R.length; // 2
241 s = S.length; // 3
242 if ((q + r) > s) return false; // 4
243 for (var i = 0; i < r; i++) {
244 //print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i));
245 if (S.charAt(q + i) != R.charAt(i)) // 5
246 return false;
247 }
248 cap = new Array(); // 6
249 return { endIndex : q + r, captures : cap }; // 7
250 }
251 }
252
253 function ToUint32( n ) {
254 n = Number( n );
255 var sign = ( n < 0 ) ? -1 : 1;
256
257 if ( Math.abs( n ) == 0
258 || Math.abs( n ) == Number.POSITIVE_INFINITY
259 || n != n) {
260 return 0;
261 }
262 n = sign * Math.floor( Math.abs(n) )
263
264 n = n % Math.pow(2,32);
265
266 if ( n < 0 ){
267 n += Math.pow(2,32);
268 }
269
270 return ( n );
271 }