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