js/src/tests/ecma/String/15.5.4.8-3.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-3.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,170 @@
     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-3.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-3";
    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 +var TEST_STRING = "";
    1.75 +var EXPECT = new Array();
    1.76 +
    1.77 +// this.toString is the empty string.
    1.78 +
    1.79 +new TestCase(   SECTION,
    1.80 +		"var s = new String(); s.split().length",
    1.81 +		1,
    1.82 +		eval("var s = new String(); s.split().length") );
    1.83 +
    1.84 +new TestCase(   SECTION,
    1.85 +		"var s = new String(); s.split()[0]",
    1.86 +		"",
    1.87 +		eval("var s = new String(); s.split()[0]") );
    1.88 +
    1.89 +// this.toString() is the empty string, separator is specified.
    1.90 +
    1.91 +new TestCase(   SECTION,
    1.92 +		"var s = new String(); s.split('').length",
    1.93 +		0,
    1.94 +		eval("var s = new String(); s.split('').length") );
    1.95 +
    1.96 +new TestCase(   SECTION,
    1.97 +		"var s = new String(); s.split(' ').length",
    1.98 +		1,
    1.99 +		eval("var s = new String(); s.split(' ').length") );
   1.100 +
   1.101 +// this to string is " "
   1.102 +new TestCase(   SECTION,
   1.103 +		"var s = new String(' '); s.split().length",
   1.104 +		1,
   1.105 +		eval("var s = new String(' '); s.split().length") );
   1.106 +
   1.107 +new TestCase(   SECTION,
   1.108 +		"var s = new String(' '); s.split()[0]",
   1.109 +		" ",
   1.110 +		eval("var s = new String(' '); s.split()[0]") );
   1.111 +
   1.112 +new TestCase(   SECTION,
   1.113 +		"var s = new String(' '); s.split('').length",
   1.114 +		1,
   1.115 +		eval("var s = new String(' '); s.split('').length") );
   1.116 +
   1.117 +new TestCase(   SECTION,
   1.118 +		"var s = new String(' '); s.split('')[0]",
   1.119 +		" ",
   1.120 +		eval("var s = new String(' '); s.split('')[0]") );
   1.121 +
   1.122 +new TestCase(   SECTION,
   1.123 +		"var s = new String(' '); s.split(' ').length",
   1.124 +		2,
   1.125 +		eval("var s = new String(' '); s.split(' ').length") );
   1.126 +
   1.127 +new TestCase(   SECTION,
   1.128 +		"var s = new String(' '); s.split(' ')[0]",
   1.129 +		"",
   1.130 +		eval("var s = new String(' '); s.split(' ')[0]") );
   1.131 +
   1.132 +new TestCase(   SECTION,
   1.133 +		"\"\".split(\"\").length",
   1.134 +		0,
   1.135 +		("".split("")).length );
   1.136 +
   1.137 +new TestCase(   SECTION,
   1.138 +		"\"\".split(\"x\").length",
   1.139 +		1,
   1.140 +		("".split("x")).length );
   1.141 +
   1.142 +new TestCase(   SECTION,
   1.143 +		"\"\".split(\"x\")[0]",
   1.144 +		"",
   1.145 +		("".split("x"))[0] );
   1.146 +
   1.147 +test();
   1.148 +
   1.149 +function Split( string, separator ) {
   1.150 +  string = String( string );
   1.151 +
   1.152 +  var A = new Array();
   1.153 +
   1.154 +  if ( arguments.length < 2 ) {
   1.155 +    A[0] = string;
   1.156 +    return A;
   1.157 +  }
   1.158 +
   1.159 +  separator = String( separator );
   1.160 +
   1.161 +  var str_len = String( string ).length;
   1.162 +  var sep_len = String( separator ).length;
   1.163 +
   1.164 +  var p = 0;
   1.165 +  var k = 0;
   1.166 +
   1.167 +  if ( sep_len == 0 ) {
   1.168 +    for ( ; p < str_len; p++ ) {
   1.169 +      A[A.length] = String( string.charAt(p) );
   1.170 +    }
   1.171 +  }
   1.172 +  return A;
   1.173 +}

mercurial