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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial