]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/String/15.5.4.8-2.js
1 /* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
11 * The Original Code is Mozilla Communicator client code, released March
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
23 File Name: 15.5.4.8-2.js
24 ECMA Section: 15.5.4.8 String.prototype.split( separator )
27 Returns an Array object into which substrings of the result of converting
28 this object to a string have been stored. The substrings are determined by
29 searching from left to right for occurrences of the given separator; these
30 occurrences are not part of any substring in the returned array, but serve
31 to divide up this string value. The separator may be a string of any length.
33 As a special case, if the separator is the empty string, the string is split
34 up into individual characters; the length of the result array equals the
35 length of the string, and each substring contains one character.
37 If the separator is not supplied, then the result array contains just one
38 string, which is the string.
40 When the split method is called with one argument separator, the following steps are taken:
42 1. Call ToString, giving it the this value as its argument.
43 2. Create a new Array object of length 0 and call it A.
44 3. If separator is not supplied, call the [[Put]] method of A with 0 and
45 Result(1) as arguments, and then return A.
46 4. Call ToString(separator).
47 5. Compute the number of characters in Result(1).
48 6. Compute the number of characters in the string that is Result(4).
50 8. If Result(6) is zero (the separator string is empty), go to step 17.
51 9. Compute the smallest possible integer k not smaller than p such that
52 k+Result(6) is not greater than Result(5), and for all nonnegative
53 integers j less than Result(6), the character at position k+j of
54 Result(1) is the same as the character at position j of Result(2);
55 but if there is no such integer k, then go to step 14.
56 10. Compute a string value equal to the substring of Result(1), consisting
57 of the characters at positions p through k1, inclusive.
58 11. Call the [[Put]] method of A with A.length and Result(10) as arguments.
59 12. Let p be k+Result(6).
61 14. Compute a string value equal to the substring of Result(1), consisting
62 of the characters from position p to the end of Result(1).
63 15. Call the [[Put]] method of A with A.length and Result(14) as arguments.
65 17. If p equals Result(5), return A.
66 18. Compute a string value equal to the substring of Result(1), consisting of
67 the single character at position p.
68 19. Call the [[Put]] method of A with A.length and Result(18) as arguments.
72 Note that the split function is intentionally generic; it does not require that its this value be a String
73 object. Therefore it can be transferred to other kinds of objects for use as a method.
75 Author: christine@netscape.com
76 Date: 12 november 1997
79 var SECTION
= "15.5.4.8-2";
80 var VERSION
= "ECMA_1";
82 var TITLE
= "String.prototype.split";
84 writeHeaderToLog( SECTION
+ " "+ TITLE
);
86 var testcases
= getTestCases();
89 function getTestCases() {
90 var array
= new Array();
93 // case where separator is the empty string.
95 var TEST_STRING
= "this is a string object";
97 array
[item
++] = new TestCase( SECTION
,
98 "var s = new String( "+ TEST_STRING
+" ); s.split('').length",
100 eval("var s = new String( TEST_STRING ); s.split('').length") );
102 for ( var i
= 0; i
< TEST_STRING
.length
; i
++ ) {
104 array
[item
++] = new TestCase( SECTION
,
105 "var s = new String( "+TEST_STRING
+" ); s.split('')["+i
+"]",
106 TEST_STRING
.charAt(i
),
107 eval("var s = new String( TEST_STRING ); s.split('')["+i
+"]") );
110 // Case where the value of the separator is undefined.
111 // Per ES5 15.5.4.14 step 10 this returns the input (unless limit is non-zero).
113 var TEST_STRING
= "thisundefinedisundefinedaundefinedstringundefinedobject";
114 var EXPECT_STRING
= new Array( "thisundefinedisundefinedaundefinedstringundefinedobject" );
116 array
[item
++] = new TestCase( SECTION
,
117 "var s = new String( "+ TEST_STRING
+" ); s.split(void 0).length",
118 EXPECT_STRING
.length
,
119 eval("var s = new String( TEST_STRING ); s.split(void 0).length") );
121 for ( var i
= 0; i
< EXPECT_STRING
.length
; i
++ ) {
122 array
[item
++] = new TestCase( SECTION
,
123 "var s = new String( "+TEST_STRING
+" ); s.split(void 0)["+i
+"]",
125 eval("var s = new String( TEST_STRING ); s.split(void 0)["+i
+"]") );
128 // case where the value of the separator is null. in this case the value of the separator is "null".
129 TEST_STRING
= "thisnullisnullanullstringnullobject";
130 var EXPECT_STRING
= new Array( "this", "is", "a", "string", "object" );
132 array
[item
++] = new TestCase( SECTION
,
133 "var s = new String( "+ TEST_STRING
+" ); s.split(null).length",
134 EXPECT_STRING
.length
,
135 eval("var s = new String( TEST_STRING ); s.split(null).length") );
137 for ( var i
= 0; i
< EXPECT_STRING
.length
; i
++ ) {
138 array
[item
++] = new TestCase( SECTION
,
139 "var s = new String( "+TEST_STRING
+" ); s.split(null)["+i
+"]",
141 eval("var s = new String( TEST_STRING ); s.split(null)["+i
+"]") );
144 // case where the value of the separator is a boolean.
145 TEST_STRING
= "thistrueistrueatruestringtrueobject";
146 var EXPECT_STRING
= new Array( "this", "is", "a", "string", "object" );
148 array
[item
++] = new TestCase( SECTION
,
149 "var s = new String( "+ TEST_STRING
+" ); s.split(true).length",
150 EXPECT_STRING
.length
,
151 eval("var s = new String( TEST_STRING ); s.split(true).length") );
153 for ( var i
= 0; i
< EXPECT_STRING
.length
; i
++ ) {
154 array
[item
++] = new TestCase( SECTION
,
155 "var s = new String( "+TEST_STRING
+" ); s.split(true)["+i
+"]",
157 eval("var s = new String( TEST_STRING ); s.split(true)["+i
+"]") );
160 // case where the value of the separator is a number
161 TEST_STRING
= "this123is123a123string123object";
162 var EXPECT_STRING
= new Array( "this", "is", "a", "string", "object" );
164 array
[item
++] = new TestCase( SECTION
,
165 "var s = new String( "+ TEST_STRING
+" ); s.split(123).length",
166 EXPECT_STRING
.length
,
167 eval("var s = new String( TEST_STRING ); s.split(123).length") );
169 for ( var i
= 0; i
< EXPECT_STRING
.length
; i
++ ) {
170 array
[item
++] = new TestCase( SECTION
,
171 "var s = new String( "+TEST_STRING
+" ); s.split(123)["+i
+"]",
173 eval("var s = new String( TEST_STRING ); s.split(123)["+i
+"]") );
177 // case where the value of the separator is a number
178 TEST_STRING
= "this123is123a123string123object";
179 var EXPECT_STRING
= new Array( "this", "is", "a", "string", "object" );
181 array
[item
++] = new TestCase( SECTION
,
182 "var s = new String( "+ TEST_STRING
+" ); s.split(123).length",
183 EXPECT_STRING
.length
,
184 eval("var s = new String( TEST_STRING ); s.split(123).length") );
186 for ( var i
= 0; i
< EXPECT_STRING
.length
; i
++ ) {
187 array
[item
++] = new TestCase( SECTION
,
188 "var s = new String( "+TEST_STRING
+" ); s.split(123)["+i
+"]",
190 eval("var s = new String( TEST_STRING ); s.split(123)["+i
+"]") );
193 // case where the separator is not in the string
194 TEST_STRING
= "this is a string";
195 EXPECT_STRING
= new Array( "this is a string" );
197 array
[item
++] = new TestCase( SECTION
,
198 "var s = new String( " + TEST_STRING
+ " ); s.split(':').length",
200 eval("var s = new String( TEST_STRING ); s.split(':').length") );
202 array
[item
++] = new TestCase( SECTION
,
203 "var s = new String( " + TEST_STRING
+ " ); s.split(':')[0]",
205 eval("var s = new String( TEST_STRING ); s.split(':')[0]") );
207 // case where part but not all of separator is in the string.
208 TEST_STRING
= "this is a string";
209 EXPECT_STRING
= new Array( "this is a string" );
210 array
[item
++] = new TestCase( SECTION
,
211 "var s = new String( " + TEST_STRING
+ " ); s.split('strings').length",
213 eval("var s = new String( TEST_STRING ); s.split('strings').length") );
215 array
[item
++] = new TestCase( SECTION
,
216 "var s = new String( " + TEST_STRING
+ " ); s.split('strings')[0]",
218 eval("var s = new String( TEST_STRING ); s.split('strings')[0]") );
220 // case where the separator is at the end of the string
221 TEST_STRING
= "this is a string";
222 EXPECT_STRING
= new Array( "this is a " );
223 array
[item
++] = new TestCase( SECTION
,
224 "var s = new String( " + TEST_STRING
+ " ); s.split('string').length",
226 eval("var s = new String( TEST_STRING ); s.split('string').length") );
228 for ( var i
= 0; i
< EXPECT_STRING
.length
; i
++ ) {
229 array
[item
++] = new TestCase( SECTION
,
230 "var s = new String( "+TEST_STRING
+" ); s.split('string')["+i
+"]",
232 eval("var s = new String( TEST_STRING ); s.split('string')["+i
+"]") );
237 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
238 testcases
[tc
].passed
= writeTestCaseResult(
239 testcases
[tc
].expect
,
240 testcases
[tc
].actual
,
241 testcases
[tc
].description
+" = "+
242 testcases
[tc
].actual
);
244 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
247 return ( testcases
);