js/src/tests/ecma/String/15.5.4.8-2.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/tests/ecma/String/15.5.4.8-2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,213 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +/**
    1.11 +   File Name:          15.5.4.8-2.js
    1.12 +   ECMA Section:       15.5.4.8 String.prototype.split( separator )
    1.13 +   Description:
    1.14 +
    1.15 +   Returns an Array object into which substrings of the result of converting
    1.16 +   this object to a string have been stored. The substrings are determined by
    1.17 +   searching from left to right for occurrences of the given separator; these
    1.18 +   occurrences are not part of any substring in the returned array, but serve
    1.19 +   to divide up this string value. The separator may be a string of any length.
    1.20 +
    1.21 +   As a special case, if the separator is the empty string, the string is split
    1.22 +   up into individual characters; the length of the result array equals the
    1.23 +   length of the string, and each substring contains one character.
    1.24 +
    1.25 +   If the separator is not supplied, then the result array contains just one
    1.26 +   string, which is the string.
    1.27 +
    1.28 +   When the split method is called with one argument separator, the following steps are taken:
    1.29 +
    1.30 +   1.   Call ToString, giving it the this value as its argument.
    1.31 +   2.   Create a new Array object of length 0 and call it A.
    1.32 +   3.   If separator is not supplied, call the [[Put]] method of A with 0 and
    1.33 +   Result(1) as arguments, and then return A.
    1.34 +   4.   Call ToString(separator).
    1.35 +   5.   Compute the number of characters in Result(1).
    1.36 +   6.   Compute the number of characters in the string that is Result(4).
    1.37 +   7.   Let p be 0.
    1.38 +   8.   If Result(6) is zero (the separator string is empty), go to step 17.
    1.39 +   9.   Compute the smallest possible integer k not smaller than p such that
    1.40 +   k+Result(6) is not greater than Result(5), and for all nonnegative
    1.41 +   integers j less than Result(6), the character at position k+j of
    1.42 +   Result(1) is the same as the character at position j of Result(2);
    1.43 +   but if there is no such integer k, then go to step 14.
    1.44 +   10.   Compute a string value equal to the substring of Result(1), consisting
    1.45 +   of the characters at positions p through k1, inclusive.
    1.46 +   11.   Call the [[Put]] method of A with A.length and Result(10) as arguments.
    1.47 +   12.   Let p be k+Result(6).
    1.48 +   13.   Go to step 9.
    1.49 +   14.   Compute a string value equal to the substring of Result(1), consisting
    1.50 +   of the characters from position p to the end of Result(1).
    1.51 +   15.   Call the [[Put]] method of A with A.length and Result(14) as arguments.
    1.52 +   16.   Return A.
    1.53 +   17.   If p equals Result(5), return A.
    1.54 +   18.   Compute a string value equal to the substring of Result(1), consisting of
    1.55 +   the single character at position p.
    1.56 +   19.   Call the [[Put]] method of A with A.length and Result(18) as arguments.
    1.57 +   20.   Increase p by 1.
    1.58 +   21.   Go to step 17.
    1.59 +
    1.60 +   Note that the split function is intentionally generic; it does not require that its this value be a String
    1.61 +   object. Therefore it can be transferred to other kinds of objects for use as a method.
    1.62 +
    1.63 +   Author:             christine@netscape.com
    1.64 +   Date:               12 november 1997
    1.65 +*/
    1.66 +
    1.67 +var SECTION = "15.5.4.8-2";
    1.68 +var VERSION = "ECMA_1";
    1.69 +startTest();
    1.70 +var TITLE   = "String.prototype.split";
    1.71 +
    1.72 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.73 +
    1.74 +// case where separator is the empty string.
    1.75 +
    1.76 +var TEST_STRING = "this is a string object";
    1.77 +
    1.78 +new TestCase(   SECTION,
    1.79 +		"var s = new String( "+ TEST_STRING +" ); s.split('').length",
    1.80 +		TEST_STRING.length,
    1.81 +		eval("var s = new String( TEST_STRING ); s.split('').length") );
    1.82 +
    1.83 +for ( var i = 0; i < TEST_STRING.length; i++ ) {
    1.84 +
    1.85 +  new TestCase(   SECTION,
    1.86 +		  "var s = new String( "+TEST_STRING+" ); s.split('')["+i+"]",
    1.87 +		  TEST_STRING.charAt(i),
    1.88 +		  eval("var s = new String( TEST_STRING ); s.split('')["+i+"]") );
    1.89 +}
    1.90 +
    1.91 +// case where the value of the separator is undefined.  in this case,
    1.92 +// the this value is returned.
    1.93 +
    1.94 +var TEST_STRING = "thisundefinedisundefinedaundefinedstringundefinedobject";
    1.95 +var EXPECT_STRING = new Array( TEST_STRING );
    1.96 +
    1.97 +new TestCase(   SECTION,
    1.98 +		"var s = new String( "+ TEST_STRING +" ); s.split(void 0).length",
    1.99 +		EXPECT_STRING.length,
   1.100 +		eval("var s = new String( TEST_STRING ); s.split(void 0).length") );
   1.101 +
   1.102 +for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
   1.103 +  new TestCase(   SECTION,
   1.104 +		  "var s = new String( "+TEST_STRING+" ); s.split(void 0)["+i+"]",
   1.105 +		  EXPECT_STRING[i],
   1.106 +		  eval("var s = new String( TEST_STRING ); s.split(void 0)["+i+"]") );
   1.107 +}
   1.108 +
   1.109 +// case where the value of the separator is null.  in this case the value of the separator is "null".
   1.110 +TEST_STRING = "thisnullisnullanullstringnullobject";
   1.111 +var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
   1.112 +
   1.113 +new TestCase(   SECTION,
   1.114 +		"var s = new String( "+ TEST_STRING +" ); s.split(null).length",
   1.115 +		EXPECT_STRING.length,
   1.116 +		eval("var s = new String( TEST_STRING ); s.split(null).length") );
   1.117 +
   1.118 +for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
   1.119 +  new TestCase(   SECTION,
   1.120 +		  "var s = new String( "+TEST_STRING+" ); s.split(null)["+i+"]",
   1.121 +		  EXPECT_STRING[i],
   1.122 +		  eval("var s = new String( TEST_STRING ); s.split(null)["+i+"]") );
   1.123 +}
   1.124 +
   1.125 +// case where the value of the separator is a boolean.
   1.126 +TEST_STRING = "thistrueistrueatruestringtrueobject";
   1.127 +var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
   1.128 +
   1.129 +new TestCase(   SECTION,
   1.130 +		"var s = new String( "+ TEST_STRING +" ); s.split(true).length",
   1.131 +		EXPECT_STRING.length,
   1.132 +		eval("var s = new String( TEST_STRING ); s.split(true).length") );
   1.133 +
   1.134 +for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
   1.135 +  new TestCase(   SECTION,
   1.136 +		  "var s = new String( "+TEST_STRING+" ); s.split(true)["+i+"]",
   1.137 +		  EXPECT_STRING[i],
   1.138 +		  eval("var s = new String( TEST_STRING ); s.split(true)["+i+"]") );
   1.139 +}
   1.140 +
   1.141 +// case where the value of the separator is a number
   1.142 +TEST_STRING = "this123is123a123string123object";
   1.143 +var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
   1.144 +
   1.145 +new TestCase(   SECTION,
   1.146 +		"var s = new String( "+ TEST_STRING +" ); s.split(123).length",
   1.147 +		EXPECT_STRING.length,
   1.148 +		eval("var s = new String( TEST_STRING ); s.split(123).length") );
   1.149 +
   1.150 +for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
   1.151 +  new TestCase(   SECTION,
   1.152 +		  "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]",
   1.153 +		  EXPECT_STRING[i],
   1.154 +		  eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") );
   1.155 +}
   1.156 +
   1.157 +
   1.158 +// case where the value of the separator is a number
   1.159 +TEST_STRING = "this123is123a123string123object";
   1.160 +var EXPECT_STRING = new Array( "this", "is", "a", "string", "object" );
   1.161 +
   1.162 +new TestCase(   SECTION,
   1.163 +		"var s = new String( "+ TEST_STRING +" ); s.split(123).length",
   1.164 +		EXPECT_STRING.length,
   1.165 +		eval("var s = new String( TEST_STRING ); s.split(123).length") );
   1.166 +
   1.167 +for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
   1.168 +  new TestCase(   SECTION,
   1.169 +		  "var s = new String( "+TEST_STRING+" ); s.split(123)["+i+"]",
   1.170 +		  EXPECT_STRING[i],
   1.171 +		  eval("var s = new String( TEST_STRING ); s.split(123)["+i+"]") );
   1.172 +}
   1.173 +
   1.174 +// case where the separator is not in the string
   1.175 +TEST_STRING = "this is a string";
   1.176 +EXPECT_STRING = new Array( "this is a string" );
   1.177 +
   1.178 +new TestCase(   SECTION,
   1.179 +		"var s = new String( " + TEST_STRING + " ); s.split(':').length",
   1.180 +		1,
   1.181 +		eval("var s = new String( TEST_STRING ); s.split(':').length") );
   1.182 +
   1.183 +new TestCase(   SECTION,
   1.184 +		"var s = new String( " + TEST_STRING + " ); s.split(':')[0]",
   1.185 +		TEST_STRING,
   1.186 +		eval("var s = new String( TEST_STRING ); s.split(':')[0]") );
   1.187 +
   1.188 +// case where part but not all of separator is in the string.
   1.189 +TEST_STRING = "this is a string";
   1.190 +EXPECT_STRING = new Array( "this is a string" );
   1.191 +new TestCase(   SECTION,
   1.192 +		"var s = new String( " + TEST_STRING + " ); s.split('strings').length",
   1.193 +		1,
   1.194 +		eval("var s = new String( TEST_STRING ); s.split('strings').length") );
   1.195 +
   1.196 +new TestCase(   SECTION,
   1.197 +		"var s = new String( " + TEST_STRING + " ); s.split('strings')[0]",
   1.198 +		TEST_STRING,
   1.199 +		eval("var s = new String( TEST_STRING ); s.split('strings')[0]") );
   1.200 +
   1.201 +// case where the separator is at the end of the string
   1.202 +TEST_STRING = "this is a string";
   1.203 +EXPECT_STRING = new Array( "this is a " );
   1.204 +new TestCase(   SECTION,
   1.205 +		"var s = new String( " + TEST_STRING + " ); s.split('string').length",
   1.206 +		2,
   1.207 +		eval("var s = new String( TEST_STRING ); s.split('string').length") );
   1.208 +
   1.209 +for ( var i = 0; i < EXPECT_STRING.length; i++ ) {
   1.210 +  new TestCase(   SECTION,
   1.211 +		  "var s = new String( "+TEST_STRING+" ); s.split('string')["+i+"]",
   1.212 +		  EXPECT_STRING[i],
   1.213 +		  eval("var s = new String( TEST_STRING ); s.split('string')["+i+"]") );
   1.214 +}
   1.215 +
   1.216 +test();

mercurial