1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_2/String/split-002.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,270 @@ 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: String/split-002.js 1.12 + * ECMA Section: 15.6.4.9 1.13 + * Description: Based on ECMA 2 Draft 7 February 1999 1.14 + * 1.15 + * Author: christine@netscape.com 1.16 + * Date: 19 February 1999 1.17 + */ 1.18 + 1.19 +/* 1.20 + * Since regular expressions have been part of JavaScript since 1.2, there 1.21 + * are already tests for regular expressions in the js1_2/regexp folder. 1.22 + * 1.23 + * These new tests try to supplement the existing tests, and verify that 1.24 + * our implementation of RegExp conforms to the ECMA specification, but 1.25 + * does not try to be as exhaustive as in previous tests. 1.26 + * 1.27 + * The [,limit] argument to String.split is new, and not covered in any 1.28 + * existing tests. 1.29 + * 1.30 + * String.split cases are covered in ecma/String/15.5.4.8-*.js. 1.31 + * String.split where separator is a RegExp are in 1.32 + * js1_2/regexp/string_split.js 1.33 + * 1.34 + */ 1.35 + 1.36 +var SECTION = "ecma_2/String/split-002.js"; 1.37 +var VERSION = "ECMA_2"; 1.38 +var TITLE = "String.prototype.split( regexp, [,limit] )"; 1.39 + 1.40 +startTest(); 1.41 + 1.42 +// the separator is not supplied 1.43 +// separator is undefined 1.44 +// separator is an empty string 1.45 + 1.46 +// AddSplitCases( "splitme", "", "''", ["s", "p", "l", "i", "t", "m", "e"] ); 1.47 +// AddSplitCases( "splitme", new RegExp(), "new RegExp()", ["s", "p", "l", "i", "t", "m", "e"] ); 1.48 + 1.49 +// separator is an empty regexp 1.50 +// separator is not supplied 1.51 + 1.52 +CompareSplit( "hello", "ll" ); 1.53 + 1.54 +CompareSplit( "hello", "l" ); 1.55 +CompareSplit( "hello", "x" ); 1.56 +CompareSplit( "hello", "h" ); 1.57 +CompareSplit( "hello", "o" ); 1.58 +CompareSplit( "hello", "hello" ); 1.59 +CompareSplit( "hello", undefined ); 1.60 + 1.61 +CompareSplit( "hello", ""); 1.62 +CompareSplit( "hello", "hellothere" ); 1.63 + 1.64 +CompareSplit( new String("hello" ) ); 1.65 + 1.66 + 1.67 +Number.prototype.split = String.prototype.split; 1.68 + 1.69 +CompareSplit( new Number(100111122133144155), 1 ); 1.70 +CompareSplitWithLimit(new Number(100111122133144155), 1, 1 ); 1.71 + 1.72 +CompareSplitWithLimit(new Number(100111122133144155), 1, 2 ); 1.73 +CompareSplitWithLimit(new Number(100111122133144155), 1, 0 ); 1.74 +CompareSplitWithLimit(new Number(100111122133144155), 1, 100 ); 1.75 +CompareSplitWithLimit(new Number(100111122133144155), 1, void 0 ); 1.76 +CompareSplitWithLimit(new Number(100111122133144155), 1, Math.pow(2,32)-1 ); 1.77 +CompareSplitWithLimit(new Number(100111122133144155), 1, "boo" ); 1.78 +CompareSplitWithLimit(new Number(100111122133144155), 1, -(Math.pow(2,32)-1) ); 1.79 +CompareSplitWithLimit( "hello", "l", NaN ); 1.80 +CompareSplitWithLimit( "hello", "l", 0 ); 1.81 +CompareSplitWithLimit( "hello", "l", 1 ); 1.82 +CompareSplitWithLimit( "hello", "l", 2 ); 1.83 +CompareSplitWithLimit( "hello", "l", 3 ); 1.84 +CompareSplitWithLimit( "hello", "l", 4 ); 1.85 + 1.86 + 1.87 +/* 1.88 + CompareSplitWithLimit( "hello", "ll", 0 ); 1.89 + CompareSplitWithLimit( "hello", "ll", 1 ); 1.90 + CompareSplitWithLimit( "hello", "ll", 2 ); 1.91 + CompareSplit( "", " " ); 1.92 + CompareSplit( "" ); 1.93 +*/ 1.94 + 1.95 +// separartor is a regexp 1.96 +// separator regexp value global setting is set 1.97 +// string is an empty string 1.98 +// if separator is an empty string, split each by character 1.99 + 1.100 +// this is not a String object 1.101 + 1.102 +// limit is not a number 1.103 +// limit is undefined 1.104 +// limit is larger than 2^32-1 1.105 +// limit is a negative number 1.106 + 1.107 +test(); 1.108 + 1.109 +function CompareSplit( string, separator ) { 1.110 + split_1 = string.split( separator ); 1.111 + split_2 = string_split( string, separator ); 1.112 + 1.113 + AddTestCase( 1.114 + "( " + string +".split(" + separator + ") ).length" , 1.115 + split_2.length, 1.116 + split_1.length ); 1.117 + 1.118 + var limit = split_1.length > split_2.length ? 1.119 + split_1.length : split_2.length; 1.120 + 1.121 + for ( var split_item = 0; split_item < limit; split_item++ ) { 1.122 + AddTestCase( 1.123 + string + ".split(" + separator + ")["+split_item+"]", 1.124 + split_2[split_item], 1.125 + split_1[split_item] ); 1.126 + } 1.127 +} 1.128 + 1.129 +function CompareSplitWithLimit( string, separator, splitlimit ) { 1.130 + split_1 = string.split( separator, splitlimit ); 1.131 + split_2 = string_split( string, separator, splitlimit ); 1.132 + 1.133 + AddTestCase( 1.134 + "( " + string +".split(" + separator + ", " + splitlimit+") ).length" , 1.135 + split_2.length, 1.136 + split_1.length ); 1.137 + 1.138 + var limit = split_1.length > split_2.length ? 1.139 + split_1.length : split_2.length; 1.140 + 1.141 + for ( var split_item = 0; split_item < limit; split_item++ ) { 1.142 + AddTestCase( 1.143 + string + ".split(" + separator + ", " + splitlimit+")["+split_item+"]", 1.144 + split_2[split_item], 1.145 + split_1[split_item] ); 1.146 + } 1.147 +} 1.148 + 1.149 +function string_split ( __this, separator, limit ) { 1.150 + var S = String(__this ); // 1 1.151 + 1.152 + var A = new Array(); // 2 1.153 + 1.154 + if ( limit == undefined ) { // 3 1.155 + lim = Math.pow(2, 31 ) -1; 1.156 + } else { 1.157 + lim = ToUint32( limit ); 1.158 + } 1.159 + 1.160 + var s = S.length; // 4 1.161 + var p = 0; // 5 1.162 + 1.163 + if ( separator == undefined ) { // 8 1.164 + A[0] = S; 1.165 + return A; 1.166 + } 1.167 + 1.168 + if ( separator.constructor == RegExp ) // 6 1.169 + R = separator; 1.170 + else 1.171 + R = separator.toString(); 1.172 + 1.173 + if (lim == 0) return A; // 7 1.174 + 1.175 + if ( separator == undefined ) { // 8 1.176 + A[0] = S; 1.177 + return A; 1.178 + } 1.179 + 1.180 + if (s == 0) { // 9 1.181 + z = SplitMatch(R, S, 0); 1.182 + if (z != false) return A; 1.183 + A[0] = S; 1.184 + return A; 1.185 + } 1.186 + 1.187 + var q = p; // 10 1.188 +loop: 1.189 + while (true ) { 1.190 + 1.191 + if ( q == s ) break; // 11 1.192 + 1.193 + z = SplitMatch(R, S, q); // 12 1.194 + 1.195 +//print("Returned ", z); 1.196 + 1.197 + if (z != false) { // 13 1.198 + e = z.endIndex; // 14 1.199 + cap = z.captures; // 14 1.200 + if (e != p) { // 15 1.201 +//print("S = ", S, ", p = ", p, ", q = ", q); 1.202 + T = S.slice(p, q); // 16 1.203 +//print("T = ", T); 1.204 + A[A.length] = T; // 17 1.205 + if (A.length == lim) return A; // 18 1.206 + p = e; // 19 1.207 + i = 0; // 20 1.208 + while (true) { // 25 1.209 + if (i == cap.length) { // 21 1.210 + q = p; // 10 1.211 + continue loop; 1.212 + } 1.213 + i = i + 1; // 22 1.214 + A[A.length] = cap[i] // 23 1.215 + if (A.length == lim) return A; // 24 1.216 + } 1.217 + } 1.218 + } 1.219 + 1.220 + q = q + 1; // 26 1.221 + } 1.222 + 1.223 + T = S.slice(p, q); 1.224 + A[A.length] = T; 1.225 + return A; 1.226 +} 1.227 + 1.228 +function SplitMatch(R, S, q) 1.229 +{ 1.230 + if (R.constructor == RegExp) { // 1 1.231 + var reResult = R.match(S, q); // 8 1.232 + if (reResult == undefined) 1.233 + return false; 1.234 + else { 1.235 + a = new Array(reResult.length - 1); 1.236 + for (var i = 1; i < reResult.length; i++) 1.237 + a[a.length] = reResult[i]; 1.238 + return { endIndex : reResult.index + reResult[0].length, captures : cap }; 1.239 + } 1.240 + } 1.241 + else { 1.242 + var r = R.length; // 2 1.243 + s = S.length; // 3 1.244 + if ((q + r) > s) return false; // 4 1.245 + for (var i = 0; i < r; i++) { 1.246 +//print("S.charAt(", q + i, ") = ", S.charAt(q + i), ", R.charAt(", i, ") = ", R.charAt(i)); 1.247 + if (S.charAt(q + i) != R.charAt(i)) // 5 1.248 + return false; 1.249 + } 1.250 + cap = new Array(); // 6 1.251 + return { endIndex : q + r, captures : cap }; // 7 1.252 + } 1.253 +} 1.254 + 1.255 +function ToUint32( n ) { 1.256 + n = Number( n ); 1.257 + var sign = ( n < 0 ) ? -1 : 1; 1.258 + 1.259 + if ( Math.abs( n ) == 0 1.260 + || Math.abs( n ) == Number.POSITIVE_INFINITY 1.261 + || n != n) { 1.262 + return 0; 1.263 + } 1.264 + n = sign * Math.floor( Math.abs(n) ) 1.265 + 1.266 + n = n % Math.pow(2,32); 1.267 + 1.268 + if ( n < 0 ){ 1.269 + n += Math.pow(2,32); 1.270 + } 1.271 + 1.272 + return ( n ); 1.273 +}