js/src/tests/ecma/String/15.5.4.10-1.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.10-1.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,183 @@
     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.10-1.js
    1.12 +   ECMA Section:       15.5.4.10 String.prototype.substring( start, end )
    1.13 +   Description:
    1.14 +
    1.15 +   15.5.4.10 String.prototype.substring(start, end)
    1.16 +
    1.17 +   Returns a substring of the result of converting this object to a string,
    1.18 +   starting from character position start and running to character position
    1.19 +   end of the string. The result is a string value, not a String object.
    1.20 +
    1.21 +   If either argument is NaN or negative, it is replaced with zero; if either
    1.22 +   argument is larger than the length of the string, it is replaced with the
    1.23 +   length of the string.
    1.24 +
    1.25 +   If start is larger than end, they are swapped.
    1.26 +
    1.27 +   When the substring method is called with two arguments start and end, the
    1.28 +   following steps are taken:
    1.29 +
    1.30 +   1.  Call ToString, giving it the this value as its argument.
    1.31 +   2.  Call ToInteger(start).
    1.32 +   3.  Call ToInteger (end).
    1.33 +   4.  Compute the number of characters in Result(1).
    1.34 +   5.  Compute min(max(Result(2), 0), Result(4)).
    1.35 +   6.  Compute min(max(Result(3), 0), Result(4)).
    1.36 +   7.  Compute min(Result(5), Result(6)).
    1.37 +   8.  Compute max(Result(5), Result(6)).
    1.38 +   9.  Return a string whose length is the difference between Result(8) and
    1.39 +   Result(7), containing characters from Result(1), namely the characters
    1.40 +   with indices Result(7) through Result(8)1, in ascending order.
    1.41 +
    1.42 +   Note that the substring function is intentionally generic; it does not require
    1.43 +   that its this value be a String object. Therefore it can be transferred to other
    1.44 +   kinds of objects for use as a method.
    1.45 +
    1.46 +   Author:             christine@netscape.com
    1.47 +   Date:               12 november 1997
    1.48 +*/
    1.49 +
    1.50 +var SECTION = "15.5.4.10-1";
    1.51 +var VERSION = "ECMA_1";
    1.52 +startTest();
    1.53 +var TITLE   = "String.prototype.substring( start, end )";
    1.54 +
    1.55 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.56 +
    1.57 +new TestCase( SECTION,  "String.prototype.substring.length",        2,          String.prototype.substring.length );
    1.58 +new TestCase( SECTION,  "delete String.prototype.substring.length", false,      delete String.prototype.substring.length );
    1.59 +new TestCase( SECTION,  "delete String.prototype.substring.length; String.prototype.substring.length", 2,      eval("delete String.prototype.substring.length; String.prototype.substring.length") );
    1.60 +
    1.61 +// test cases for when substring is called with no arguments.
    1.62 +
    1.63 +// this is a string object
    1.64 +
    1.65 +new TestCase(   SECTION,
    1.66 +		"var s = new String('this is a string object'); typeof s.substring()",
    1.67 +		"string",
    1.68 +		eval("var s = new String('this is a string object'); typeof s.substring()") );
    1.69 +
    1.70 +new TestCase(   SECTION,
    1.71 +		"var s = new String(''); s.substring(1,0)",
    1.72 +		"",
    1.73 +		eval("var s = new String(''); s.substring(1,0)") );
    1.74 +
    1.75 +new TestCase(   SECTION,
    1.76 +		"var s = new String('this is a string object'); s.substring(true, false)",
    1.77 +		"t",
    1.78 +		eval("var s = new String('this is a string object'); s.substring(false, true)") );
    1.79 +
    1.80 +new TestCase(   SECTION,
    1.81 +		"var s = new String('this is a string object'); s.substring(NaN, Infinity)",
    1.82 +		"this is a string object",
    1.83 +		eval("var s = new String('this is a string object'); s.substring(NaN, Infinity)") );
    1.84 +
    1.85 +
    1.86 +new TestCase(   SECTION,
    1.87 +		"var s = new String('this is a string object'); s.substring(Infinity, NaN)",
    1.88 +		"this is a string object",
    1.89 +		eval("var s = new String('this is a string object'); s.substring(Infinity, NaN)") );
    1.90 +
    1.91 +
    1.92 +new TestCase(   SECTION,
    1.93 +		"var s = new String('this is a string object'); s.substring(Infinity, Infinity)",
    1.94 +		"",
    1.95 +		eval("var s = new String('this is a string object'); s.substring(Infinity, Infinity)") );
    1.96 +
    1.97 +new TestCase(   SECTION,
    1.98 +		"var s = new String('this is a string object'); s.substring(-0.01, 0)",
    1.99 +		"",
   1.100 +		eval("var s = new String('this is a string object'); s.substring(-0.01,0)") );
   1.101 +
   1.102 +
   1.103 +new TestCase(   SECTION,
   1.104 +		"var s = new String('this is a string object'); s.substring(s.length, s.length)",
   1.105 +		"",
   1.106 +		eval("var s = new String('this is a string object'); s.substring(s.length, s.length)") );
   1.107 +
   1.108 +new TestCase(   SECTION,
   1.109 +		"var s = new String('this is a string object'); s.substring(s.length+1, 0)",
   1.110 +		"this is a string object",
   1.111 +		eval("var s = new String('this is a string object'); s.substring(s.length+1, 0)") );
   1.112 +
   1.113 +
   1.114 +new TestCase(   SECTION,
   1.115 +		"var s = new String('this is a string object'); s.substring(-Infinity, -Infinity)",
   1.116 +		"",
   1.117 +		eval("var s = new String('this is a string object'); s.substring(-Infinity, -Infinity)") );
   1.118 +
   1.119 +// this is not a String object, start is not an integer
   1.120 +
   1.121 +
   1.122 +new TestCase(   SECTION,
   1.123 +		"var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(Infinity,-Infinity)",
   1.124 +		"1,2,3,4,5",
   1.125 +		eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(Infinity,-Infinity)") );
   1.126 +
   1.127 +new TestCase(   SECTION,
   1.128 +		"var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true, false)",
   1.129 +		"1",
   1.130 +		eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring(true, false)") );
   1.131 +
   1.132 +new TestCase(   SECTION,
   1.133 +		"var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4', '5')",
   1.134 +		"3",
   1.135 +		eval("var s = new Array(1,2,3,4,5); s.substring = String.prototype.substring; s.substring('4', '5')") );
   1.136 +
   1.137 +
   1.138 +// this is an object object
   1.139 +new TestCase(   SECTION,
   1.140 +		"var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,0)",
   1.141 +		"[object ",
   1.142 +		eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,0)") );
   1.143 +
   1.144 +new TestCase(   SECTION,
   1.145 +		"var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8,obj.toString().length)",
   1.146 +		"Object]",
   1.147 +		eval("var obj = new Object(); obj.substring = String.prototype.substring; obj.substring(8, obj.toString().length)") );
   1.148 +
   1.149 +// this is a function object
   1.150 +new TestCase(   SECTION,
   1.151 +		"var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8, Infinity)",
   1.152 +		"Function]",
   1.153 +		eval("var obj = new Function(); obj.substring = String.prototype.substring; obj.toString = Object.prototype.toString; obj.substring(8,Infinity)") );
   1.154 +// this is a number object
   1.155 +new TestCase(   SECTION,
   1.156 +		"var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(Infinity, NaN)",
   1.157 +		"NaN",
   1.158 +		eval("var obj = new Number(NaN); obj.substring = String.prototype.substring; obj.substring(Infinity, NaN)") );
   1.159 +
   1.160 +// this is the Math object
   1.161 +new TestCase(   SECTION,
   1.162 +		"var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI, -10)",
   1.163 +		"[ob",
   1.164 +		eval("var obj = Math; obj.substring = String.prototype.substring; obj.substring(Math.PI, -10)") );
   1.165 +
   1.166 +// this is a Boolean object
   1.167 +
   1.168 +new TestCase(   SECTION,
   1.169 +		"var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array(), new Boolean(1))",
   1.170 +		"f",
   1.171 +		eval("var obj = new Boolean(); obj.substring = String.prototype.substring; obj.substring(new Array(), new Boolean(1))") );
   1.172 +
   1.173 +// this is a user defined object
   1.174 +
   1.175 +new TestCase( SECTION,
   1.176 +	      "var obj = new MyObject( void 0 ); obj.substring(0, 100)",
   1.177 +	      "undefined",
   1.178 +	      eval( "var obj = new MyObject( void 0 ); obj.substring(0,100)") );
   1.179 +
   1.180 +test();
   1.181 +
   1.182 +function MyObject( value ) {
   1.183 +  this.value = value;
   1.184 +  this.substring = String.prototype.substring;
   1.185 +  this.toString = new Function ( "return this.value+''" );
   1.186 +}

mercurial