js/src/tests/ecma_2/String/split-002.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: String/split-002.js
michael@0 9 * ECMA Section: 15.6.4.9
michael@0 10 * Description: Based on ECMA 2 Draft 7 February 1999
michael@0 11 *
michael@0 12 * Author: christine@netscape.com
michael@0 13 * Date: 19 February 1999
michael@0 14 */
michael@0 15
michael@0 16 /*
michael@0 17 * Since regular expressions have been part of JavaScript since 1.2, there
michael@0 18 * are already tests for regular expressions in the js1_2/regexp folder.
michael@0 19 *
michael@0 20 * These new tests try to supplement the existing tests, and verify that
michael@0 21 * our implementation of RegExp conforms to the ECMA specification, but
michael@0 22 * does not try to be as exhaustive as in previous tests.
michael@0 23 *
michael@0 24 * The [,limit] argument to String.split is new, and not covered in any
michael@0 25 * existing tests.
michael@0 26 *
michael@0 27 * String.split cases are covered in ecma/String/15.5.4.8-*.js.
michael@0 28 * String.split where separator is a RegExp are in
michael@0 29 * js1_2/regexp/string_split.js
michael@0 30 *
michael@0 31 */
michael@0 32
michael@0 33 var SECTION = "ecma_2/String/split-002.js";
michael@0 34 var VERSION = "ECMA_2";
michael@0 35 var TITLE = "String.prototype.split( regexp, [,limit] )";
michael@0 36
michael@0 37 startTest();
michael@0 38
michael@0 39 // the separator is not supplied
michael@0 40 // separator is undefined
michael@0 41 // separator is an empty string
michael@0 42
michael@0 43 // AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] );
michael@0 44 // AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] );
michael@0 45
michael@0 46 // separator is an empty regexp
michael@0 47 // separator is not supplied
michael@0 48
michael@0 49 CompareSplit( "hello", "ll" );
michael@0 50
michael@0 51 CompareSplit( "hello", "l" );
michael@0 52 CompareSplit( "hello", "x" );
michael@0 53 CompareSplit( "hello", "h" );
michael@0 54 CompareSplit( "hello", "o" );
michael@0 55 CompareSplit( "hello", "hello" );
michael@0 56 CompareSplit( "hello", undefined );
michael@0 57
michael@0 58 CompareSplit( "hello", "");
michael@0 59 CompareSplit( "hello", "hellothere" );
michael@0 60
michael@0 61 CompareSplit( new String("hello" ) );
michael@0 62
michael@0 63
michael@0 64 Number.prototype.split = String.prototype.split;
michael@0 65
michael@0 66 CompareSplit( new Number(100111122133144155), 1 );
michael@0 67 CompareSplitWithLimit(new Number(100111122133144155), 1, 1 );
michael@0 68
michael@0 69 CompareSplitWithLimit(new Number(100111122133144155), 1, 2 );
michael@0 70 CompareSplitWithLimit(new Number(100111122133144155), 1, 0 );
michael@0 71 CompareSplitWithLimit(new Number(100111122133144155), 1, 100 );
michael@0 72 CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 );
michael@0 73 CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 );
michael@0 74 CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" );
michael@0 75 CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) );
michael@0 76 CompareSplitWithLimit( "hello", "l", NaN );
michael@0 77 CompareSplitWithLimit( "hello", "l", 0 );
michael@0 78 CompareSplitWithLimit( "hello", "l", 1 );
michael@0 79 CompareSplitWithLimit( "hello", "l", 2 );
michael@0 80 CompareSplitWithLimit( "hello", "l", 3 );
michael@0 81 CompareSplitWithLimit( "hello", "l", 4 );
michael@0 82
michael@0 83
michael@0 84 /*
michael@0 85 CompareSplitWithLimit( "hello", "ll", 0 );
michael@0 86 CompareSplitWithLimit( "hello", "ll", 1 );
michael@0 87 CompareSplitWithLimit( "hello", "ll", 2 );
michael@0 88 CompareSplit( "", " " );
michael@0 89 CompareSplit( "" );
michael@0 90 */
michael@0 91
michael@0 92 // separartor is a regexp
michael@0 93 // separator regexp value global setting is set
michael@0 94 // string is an empty string
michael@0 95 // if separator is an empty string, split each by character
michael@0 96
michael@0 97 // this is not a String object
michael@0 98
michael@0 99 // limit is not a number
michael@0 100 // limit is undefined
michael@0 101 // limit is larger than 2^32-1
michael@0 102 // limit is a negative number
michael@0 103
michael@0 104 test();
michael@0 105
michael@0 106 function CompareSplit( string, separator ) {
michael@0 107 split_1 = string.split( separator );
michael@0 108 split_2 = string_split( string, separator );
michael@0 109
michael@0 110 AddTestCase(
michael@0 111 "( " + string +".split(" + separator + ") ).length" ,
michael@0 112 split_2.length,
michael@0 113 split_1.length );
michael@0 114
michael@0 115 var limit = split_1.length > split_2.length ?
michael@0 116 split_1.length : split_2.length;
michael@0 117
michael@0 118 for ( var split_item = 0; split_item < limit; split_item++ ) {
michael@0 119 AddTestCase(
michael@0 120 string + ".split(" + separator + ")["+split_item+"]",
michael@0 121 split_2[split_item],
michael@0 122 split_1[split_item] );
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 function CompareSplitWithLimit( string, separator, splitlimit ) {
michael@0 127 split_1 = string.split( separator, splitlimit );
michael@0 128 split_2 = string_split( string, separator, splitlimit );
michael@0 129
michael@0 130 AddTestCase(
michael@0 131 "( " + string +".split(" + separator + ", " + splitlimit+") ).length" ,
michael@0 132 split_2.length,
michael@0 133 split_1.length );
michael@0 134
michael@0 135 var limit = split_1.length > split_2.length ?
michael@0 136 split_1.length : split_2.length;
michael@0 137
michael@0 138 for ( var split_item = 0; split_item < limit; split_item++ ) {
michael@0 139 AddTestCase(
michael@0 140 string + ".split(" + separator + ", " + splitlimit+")["+split_item+"]",
michael@0 141 split_2[split_item],
michael@0 142 split_1[split_item] );
michael@0 143 }
michael@0 144 }
michael@0 145
michael@0 146 function string_split ( __this, separator, limit ) {
michael@0 147 var S = String(__this ); // 1
michael@0 148
michael@0 149 var A = new Array(); // 2
michael@0 150
michael@0 151 if ( limit == undefined ) { // 3
michael@0 152 lim = Math.pow(2, 31 ) -1;
michael@0 153 } else {
michael@0 154 lim = ToUint32( limit );
michael@0 155 }
michael@0 156
michael@0 157 var s = S.length; // 4
michael@0 158 var p = 0; // 5
michael@0 159
michael@0 160 if ( separator == undefined ) { // 8
michael@0 161 A[0] = S;
michael@0 162 return A;
michael@0 163 }
michael@0 164
michael@0 165 if ( separator.constructor == RegExp ) // 6
michael@0 166 R = separator;
michael@0 167 else
michael@0 168 R = separator.toString();
michael@0 169
michael@0 170 if (lim == 0) return A; // 7
michael@0 171
michael@0 172 if ( separator == undefined ) { // 8
michael@0 173 A[0] = S;
michael@0 174 return A;
michael@0 175 }
michael@0 176
michael@0 177 if (s == 0) { // 9
michael@0 178 z = SplitMatch(R, S, 0);
michael@0 179 if (z != false) return A;
michael@0 180 A[0] = S;
michael@0 181 return A;
michael@0 182 }
michael@0 183
michael@0 184 var q = p; // 10
michael@0 185 loop:
michael@0 186 while (true ) {
michael@0 187
michael@0 188 if ( q == s ) break; // 11
michael@0 189
michael@0 190 z = SplitMatch(R, S, q); // 12
michael@0 191
michael@0 192 //print("Returned ", z);
michael@0 193
michael@0 194 if (z != false) { // 13
michael@0 195 e = z.endIndex; // 14
michael@0 196 cap = z.captures; // 14
michael@0 197 if (e != p) { // 15
michael@0 198 //print("S = ", S, ", p = ", p, ", q = ", q);
michael@0 199 T = S.slice(p, q); // 16
michael@0 200 //print("T = ", T);
michael@0 201 A[A.length] = T; // 17
michael@0 202 if (A.length == lim) return A; // 18
michael@0 203 p = e; // 19
michael@0 204 i = 0; // 20
michael@0 205 while (true) { // 25
michael@0 206 if (i == cap.length) { // 21
michael@0 207 q = p; // 10
michael@0 208 continue loop;
michael@0 209 }
michael@0 210 i = i + 1; // 22
michael@0 211 A[A.length] = cap[i] // 23
michael@0 212 if (A.length == lim) return A; // 24
michael@0 213 }
michael@0 214 }
michael@0 215 }
michael@0 216
michael@0 217 q = q + 1; // 26
michael@0 218 }
michael@0 219
michael@0 220 T = S.slice(p, q);
michael@0 221 A[A.length] = T;
michael@0 222 return A;
michael@0 223 }
michael@0 224
michael@0 225 function SplitMatch(R, S, q)
michael@0 226 {
michael@0 227 if (R.constructor == RegExp) { // 1
michael@0 228 var reResult = R.match(S, q); // 8
michael@0 229 if (reResult == undefined)
michael@0 230 return false;
michael@0 231 else {
michael@0 232 a = new Array(reResult.length - 1);
michael@0 233 for (var i = 1; i < reResult.length; i++)
michael@0 234 a[a.length] = reResult[i];
michael@0 235 return { endIndex : reResult.index + reResult[0].length, captures : cap };
michael@0 236 }
michael@0 237 }
michael@0 238 else {
michael@0 239 var r = R.length; // 2
michael@0 240 s = S.length; // 3
michael@0 241 if ((q + r) > s) return false; // 4
michael@0 242 for (var i = 0; i < r; i++) {
michael@0 243 //print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i));
michael@0 244 if (S.charAt(q + i) != R.charAt(i)) // 5
michael@0 245 return false;
michael@0 246 }
michael@0 247 cap = new Array(); // 6
michael@0 248 return { endIndex : q + r, captures : cap }; // 7
michael@0 249 }
michael@0 250 }
michael@0 251
michael@0 252 function ToUint32( n ) {
michael@0 253 n = Number( n );
michael@0 254 var sign = ( n < 0 ) ? -1 : 1;
michael@0 255
michael@0 256 if ( Math.abs( n ) == 0
michael@0 257 || Math.abs( n ) == Number.POSITIVE_INFINITY
michael@0 258 || n != n) {
michael@0 259 return 0;
michael@0 260 }
michael@0 261 n = sign * Math.floor( Math.abs(n) )
michael@0 262
michael@0 263 n = n % Math.pow(2,32);
michael@0 264
michael@0 265 if ( n < 0 ){
michael@0 266 n += Math.pow(2,32);
michael@0 267 }
michael@0 268
michael@0 269 return ( n );
michael@0 270 }

mercurial