Debian Codegraph @

rhino/testsrc/tests/ecma/String/15.5.4.8-2.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 = '15.5.4.8-2.js';
7
8 /**
9 File Name: 15.5.4.8-2.js
10 ECMA Section: 15.5.4.8 String.prototype.split( separator )
11 Description:
12
13 Returns an Array object into which substrings of the result of converting
14 this object to a string have been stored. The substrings are determined by
15 searching from left to right for occurrences of the given separator; these
16 occurrences are not part of any substring in the returned array, but serve
17 to divide up this string value. The separator may be a string of any length.
18
19 As a special case, if the separator is the empty string, the string is split
20 up into individual characters; the length of the result array equals the
21 length of the string, and each substring contains one character.
22
23 If the separator is not supplied, then the result array contains just one
24 string, which is the string.
25
26 When the split method is called with one argument separator, the following steps are taken:
27
28 1. Call ToString, giving it the this value as its argument.
29 2. Create a new Array object of length 0 and call it A.
30 3. If separator is not supplied, call the [[Put]] method of A with 0 and
31 Result(1) as arguments, and then return A.
32 4. Call ToString(separator).
33 5. Compute the number of characters in Result(1).
34 6. Compute the number of characters in the string that is Result(4).
35 7. Let p be 0.
36 8. If Result(6) is zero (the separator string is empty), go to step 17.
37 9. Compute the smallest possible integer k not smaller than p such that
38 k+Result(6) is not greater than Result(5), and for all nonnegative
39 integers j less than Result(6), the character at position k+j of
40 Result(1) is the same as the character at position j of Result(2);
41 but if there is no such integer k, then go to step 14.
42 10. Compute a string value equal to the substring of Result(1), consisting
43 of the characters at positions p through k1, inclusive.
44 11. Call the [[Put]] method of A with A.length and Result(10) as arguments.
45 12. Let p be k+Result(6).
46 13. Go to step 9.
47 14. Compute a string value equal to the substring of Result(1), consisting
48 of the characters from position p to the end of Result(1).
49 15. Call the [[Put]] method of A with A.length and Result(14) as arguments.
50 16. Return A.
51 17. If p equals Result(5), return A.
52 18. Compute a string value equal to the substring of Result(1), consisting of
53 the single character at position p.
54 19. Call the [[Put]] method of A with A.length and Result(18) as arguments.
55 20. Increase p by 1.
56 21. Go to step 17.
57
58 Note that the split function is intentionally generic; it does not require that its this value be a String
59 object. Therefore it can be transferred to other kinds of objects for use as a method.
60
61 Author: [email protected]
62 Date: 12 november 1997
63 */
64
65 var SECTION = "15.5.4.8-2";
66 var VERSION = "ECMA_1";
67 startTest();
68 var TITLE = "String.prototype.split";
69
70 writeHeaderToLog( SECTION + " "+ TITLE);
71
72 // case where separator is the empty string.
73
74 var TEST_STRING = "this is a string object";
75
76 new TestCase( SECTION,
77 "var s = new String( "+ TEST_STRING +" ); s.split('').length",
78 TEST_STRING.length,
79 eval("var s = new String( TEST_STRING ); s.split('').length") );
80
81 for ( var i = 0; i < TEST_STRING.length; i++ ) {
82
83 new TestCase( SECTION,
84 "var s = new String( "+TEST_STRING+" ); s.split('')["+i+"]",
85 TEST_STRING.charAt(i),
86 eval("var s = new String( TEST_STRING ); s.split('')["+i+"]") );
87 }
88
89 // case where the value of the separator is undefined. in this case. the value of the separator
90 // should be ToString( separator ), or "undefined".
91
92 var TEST_STRING = "thisundefinedisundefinedaundefinedstringundefinedobject";
93 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
94
95 new TestCase( SECTION,
96 "var s = new String( "+ TEST_STRING +" ); s.split(void 0).length",
97 EXPECT_STRING.length,
98 eval("var s = new String( TEST_STRING ); s.split(void 0).length") );
99
100 for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
101 new TestCase( SECTION,
102 "var s = new String( "+TEST_STRING+" ); s.split(void 0)["+i+"]",
103 EXPECT_STRING[i],
104 eval("var s = new String( TEST_STRING ); s.split(void 0)["+i+"]") );
105 }
106
107 // case where the value of the separator is null. in this case the value of the separator is "null".
108 TEST_STRING = "thisnullisnullanullstringnullobject";
109 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
110
111 new TestCase( SECTION,
112 "var s = new String( "+ TEST_STRING +" ); s.split(null).length",
113 EXPECT_STRING.length,
114 eval("var s = new String( TEST_STRING ); s.split(null).length") );
115
116 for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
117 new TestCase( SECTION,
118 "var s = new String( "+TEST_STRING+" ); s.split(null)["+i+"]",
119 EXPECT_STRING[i],
120 eval("var s = new String( TEST_STRING ); s.split(null)["+i+"]") );
121 }
122
123 // case where the value of the separator is a boolean.
124 TEST_STRING = "thistrueistrueatruestringtrueobject";
125 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
126
127 new TestCase( SECTION,
128 "var s = new String( "+ TEST_STRING +" ); s.split(true).length",
129 EXPECT_STRING.length,
130 eval("var s = new String( TEST_STRING ); s.split(true).length") );
131
132 for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
133 new TestCase( SECTION,
134 "var s = new String( "+TEST_STRING+" ); s.split(true)["+i+"]",
135 EXPECT_STRING[i],
136 eval("var s = new String( TEST_STRING ); s.split(true)["+i+"]") );
137 }
138
139 // case where the value of the separator is a number
140 TEST_STRING = "this123is123a123string123object";
141 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
142
143 new TestCase( SECTION,
144 "var s = new String( "+ TEST_STRING +" ); s.split(123).length",
145 EXPECT_STRING.length,
146 eval("var s = new String( TEST_STRING ); s.split(123).length") );
147
148 for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
149 new TestCase( SECTION,
150 "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]",
151 EXPECT_STRING[i],
152 eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") );
153 }
154
155
156 // case where the value of the separator is a number
157 TEST_STRING = "this123is123a123string123object";
158 var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
159
160 new TestCase( SECTION,
161 "var s = new String( "+ TEST_STRING +" ); s.split(123).length",
162 EXPECT_STRING.length,
163 eval("var s = new String( TEST_STRING ); s.split(123).length") );
164
165 for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
166 new TestCase( SECTION,
167 "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]",
168 EXPECT_STRING[i],
169 eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") );
170 }
171
172 // case where the separator is not in the string
173 TEST_STRING = "this is a string";
174 EXPECT_STRING = new Array( "this is a string" );
175
176 new TestCase( SECTION,
177 "var s = new String( " + TEST_STRING + " ); s.split(':').length",
178 1,
179 eval("var s = new String( TEST_STRING ); s.split(':').length") );
180
181 new TestCase( SECTION,
182 "var s = new String( " + TEST_STRING + " ); s.split(':')[0]",
183 TEST_STRING,
184 eval("var s = new String( TEST_STRING ); s.split(':')[0]") );
185
186 // case where part but not all of separator is in the string.
187 TEST_STRING = "this is a string";
188 EXPECT_STRING = new Array( "this is a string" );
189 new TestCase( SECTION,
190 "var s = new String( " + TEST_STRING + " ); s.split('strings').length",
191 1,
192 eval("var s = new String( TEST_STRING ); s.split('strings').length") );
193
194 new TestCase( SECTION,
195 "var s = new String( " + TEST_STRING + " ); s.split('strings')[0]",
196 TEST_STRING,
197 eval("var s = new String( TEST_STRING ); s.split('strings')[0]") );
198
199 // case where the separator is at the end of the string
200 TEST_STRING = "this is a string";
201 EXPECT_STRING = new Array( "this is a " );
202 new TestCase( SECTION,
203 "var s = new String( " + TEST_STRING + " ); s.split('string').length",
204 2,
205 eval("var s = new String( TEST_STRING ); s.split('string').length") );
206
207 for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
208 new TestCase( SECTION,
209 "var s = new String( "+TEST_STRING+" ); s.split('string')["+i+"]",
210 EXPECT_STRING[i],
211 eval("var s = new String( TEST_STRING ); s.split('string')["+i+"]") );
212 }
213
214 test();