js/src/tests/ecma/String/15.5.4.6-2.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.6-2.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,220 @@
     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.6-1.js
    1.12 +   ECMA Section:       15.5.4.6 String.prototype.indexOf( searchString, pos)
    1.13 +   Description:        If the given searchString appears as a substring of the
    1.14 +   result of converting this object to a string, at one or
    1.15 +   more positions that are at or to the right of the
    1.16 +   specified position, then the index of the leftmost such
    1.17 +   position is returned; otherwise -1 is returned.  If
    1.18 +   positionis undefined or not supplied, 0 is assumed, so
    1.19 +   as to search all of the string.
    1.20 +
    1.21 +   When the indexOf method is called with two arguments,
    1.22 +   searchString and pos, the following steps are taken:
    1.23 +
    1.24 +   1. Call ToString, giving it the this value as its
    1.25 +   argument.
    1.26 +   2. Call ToString(searchString).
    1.27 +   3. Call ToInteger(position). (If position is undefined
    1.28 +   or not supplied, this step produces the value 0).
    1.29 +   4. Compute the number of characters in Result(1).
    1.30 +   5. Compute min(max(Result(3), 0), Result(4)).
    1.31 +   6. Compute the number of characters in the string that
    1.32 +   is Result(2).
    1.33 +   7. Compute the smallest possible integer k not smaller
    1.34 +   than Result(5) such that k+Result(6) is not greater
    1.35 +   than Result(4), and for all nonnegative integers j
    1.36 +   less than Result(6), the character at position k+j
    1.37 +   of Result(1) is the same as the character at position
    1.38 +   j of Result(2); but if there is no such integer k,
    1.39 +   then compute the value -1.
    1.40 +   8. Return Result(7).
    1.41 +
    1.42 +   Note that the indexOf function is intentionally generic;
    1.43 +   it does not require that its this value be a String object.
    1.44 +   Therefore it can be transferred to other kinds of objects
    1.45 +   for use as a method.
    1.46 +
    1.47 +   Author:             christine@netscape.com, pschwartau@netscape.com
    1.48 +   Date:               02 October 1997
    1.49 +   Modified:           14 July 2002
    1.50 +   Reason:             See http://bugzilla.mozilla.org/show_bug.cgi?id=155289
    1.51 +   ECMA-262 Ed.3  Section 15.5.4.7
    1.52 +   The length property of the indexOf method is 1
    1.53 +   *
    1.54 +   */
    1.55 +var SECTION = "15.5.4.6-2";
    1.56 +var VERSION = "ECMA_1";
    1.57 +var TITLE   = "String.protoype.indexOf";
    1.58 +var BUGNUMBER="105721";
    1.59 +
    1.60 +startTest();
    1.61 +
    1.62 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.63 +
    1.64 +
    1.65 +// the following test regresses http://scopus/bugsplat/show_bug.cgi?id=105721
    1.66 +
    1.67 +// regress http://scopus/bugsplat/show_bug.cgi?id=105721
    1.68 +
    1.69 +new TestCase( SECTION,
    1.70 +              "function f() { return this; }; function g() { var h = f; return h(); }; g().toString()",   
    1.71 +              GLOBAL, 
    1.72 +              g().toString()
    1.73 +  );
    1.74 +
    1.75 +
    1.76 +new TestCase( SECTION, "String.prototype.indexOf.length",                                               1,     String.prototype.indexOf.length );
    1.77 +new TestCase( SECTION, "String.prototype.indexOf.length = null; String.prototype.indexOf.length",       1,     eval("String.prototype.indexOf.length = null; String.prototype.indexOf.length") );
    1.78 +new TestCase( SECTION, "delete String.prototype.indexOf.length",                                        false,  delete String.prototype.indexOf.length );
    1.79 +new TestCase( SECTION, "delete String.prototype.indexOf.length; String.prototype.indexOf.length",       1,      eval("delete String.prototype.indexOf.length; String.prototype.indexOf.length") );
    1.80 +
    1.81 +new TestCase( SECTION,
    1.82 +              "var s = new String(); s.indexOf()",    
    1.83 +              -1,    
    1.84 +              eval("var s = new String(); s.indexOf()") );
    1.85 +
    1.86 +// some Unicode tests.
    1.87 +
    1.88 +// generate a test string.
    1.89 +
    1.90 +var TEST_STRING = "";
    1.91 +
    1.92 +for ( var u = 0x00A1; u <= 0x00FF; u++ ) {
    1.93 +  TEST_STRING += String.fromCharCode( u );
    1.94 +}
    1.95 +
    1.96 +for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
    1.97 +  new TestCase(   SECTION,
    1.98 +		  "TEST_STRING.indexOf( " + String.fromCharCode(u) + " )",
    1.99 +		  i,
   1.100 +		  TEST_STRING.indexOf( String.fromCharCode(u) ) );
   1.101 +}
   1.102 +for ( var u = 0x00A1, i = 0; u <= 0x00FF; u++, i++ ) {
   1.103 +  new TestCase(   SECTION,
   1.104 +		  "TEST_STRING.indexOf( " + String.fromCharCode(u) + ", void 0 )",
   1.105 +		  i,
   1.106 +		  TEST_STRING.indexOf( String.fromCharCode(u), void 0 ) );
   1.107 +}
   1.108 +
   1.109 +
   1.110 +
   1.111 +var foo = new MyObject('hello');
   1.112 +
   1.113 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('h')", 0, foo.indexOf("h")  );
   1.114 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('e')", 1, foo.indexOf("e")  );
   1.115 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l")  );
   1.116 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('l')", 2, foo.indexOf("l")  );
   1.117 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('o')", 4, foo.indexOf("o")  );
   1.118 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf('X')", -1,  foo.indexOf("X")  );
   1.119 +new TestCase( SECTION, "var foo = new MyObject('hello');foo.indexOf(5) ", -1,  foo.indexOf(5)  );
   1.120 +
   1.121 +var boo = new MyObject(true);
   1.122 +
   1.123 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('t')", 0, boo.indexOf("t")  );
   1.124 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('r')", 1, boo.indexOf("r")  );
   1.125 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('u')", 2, boo.indexOf("u")  );
   1.126 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('e')", 3, boo.indexOf("e")  );
   1.127 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('true')", 0, boo.indexOf("true")  );
   1.128 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('rue')", 1, boo.indexOf("rue")  );
   1.129 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('ue')", 2, boo.indexOf("ue")  );
   1.130 +new TestCase( SECTION, "var boo = new MyObject(true);boo.indexOf('oy')", -1, boo.indexOf("oy")  );
   1.131 +
   1.132 +
   1.133 +var noo = new MyObject( Math.PI );
   1.134 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('3') ", 0, noo.indexOf('3')  );
   1.135 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('.') ", 1, noo.indexOf('.')  );
   1.136 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1')  );
   1.137 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('4') ", 3, noo.indexOf('4')  );
   1.138 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('1') ", 2, noo.indexOf('1')  );
   1.139 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('5') ", 5, noo.indexOf('5')  );
   1.140 +new TestCase( SECTION, "var noo = new MyObject(Math.PI); noo.indexOf('9') ", 6, noo.indexOf('9')  );
   1.141 +
   1.142 +new TestCase( SECTION,
   1.143 +	      "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')",
   1.144 +	      0,
   1.145 +	      eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf('new')") );
   1.146 +
   1.147 +new TestCase( SECTION,
   1.148 +	      "var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')",
   1.149 +	      3,
   1.150 +	      eval("var arr = new Array('new','zoo','revue'); arr.indexOf = String.prototype.indexOf; arr.indexOf(',zoo,')") );
   1.151 +
   1.152 +new TestCase( SECTION,
   1.153 +	      "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')",
   1.154 +	      0,
   1.155 +	      eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('[object Object]')") );
   1.156 +
   1.157 +new TestCase( SECTION,
   1.158 +	      "var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')",
   1.159 +	      2,
   1.160 +	      eval("var obj = new Object(); obj.indexOf = String.prototype.indexOf; obj.indexOf('bject')") );
   1.161 +
   1.162 +new TestCase( SECTION,
   1.163 +	      "var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')",
   1.164 +	      0,
   1.165 +	      eval("var f = new Function(); f.toString = Object.prototype.toString; f.indexOf = String.prototype.indexOf; f.indexOf('[object Function]')") );
   1.166 +
   1.167 +new TestCase( SECTION,
   1.168 +	      "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')",
   1.169 +	      -1,
   1.170 +	      eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('true')") );
   1.171 +
   1.172 +new TestCase( SECTION,
   1.173 +	      "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)",
   1.174 +	      -1,
   1.175 +	      eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 1)") );
   1.176 +
   1.177 +new TestCase( SECTION,
   1.178 +	      "var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)",
   1.179 +	      0,
   1.180 +	      eval("var b = new Boolean(); b.indexOf = String.prototype.indexOf; b.indexOf('false', 0)") );
   1.181 +
   1.182 +new TestCase( SECTION,
   1.183 +	      "var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')",
   1.184 +	      1,
   1.185 +	      eval("var n = new Number(1e21); n.indexOf = String.prototype.indexOf; n.indexOf('e')") );
   1.186 +
   1.187 +new TestCase( SECTION,
   1.188 +	      "var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')",
   1.189 +	      0,
   1.190 +	      eval("var n = new Number(-Infinity); n.indexOf = String.prototype.indexOf; n.indexOf('-')") );
   1.191 +
   1.192 +new TestCase( SECTION,
   1.193 +	      "var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')",
   1.194 +	      1,
   1.195 +	      eval("var n = new Number(0xFF); n.indexOf = String.prototype.indexOf; n.indexOf('5')") );
   1.196 +
   1.197 +new TestCase( SECTION,
   1.198 +	      "var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )",
   1.199 +	      8,
   1.200 +	      eval("var m = Math; m.indexOf = String.prototype.indexOf; m.indexOf( 'Math' )") );
   1.201 +
   1.202 +// new Date(0) has '31' or '01' at index 8 depending on whether tester is (GMT-) or (GMT+), respectively
   1.203 +new TestCase( SECTION,
   1.204 +	      "var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')",
   1.205 +	      8,
   1.206 +	      eval("var d = new Date(0); d.indexOf = String.prototype.indexOf; d.getTimezoneOffset()>0 ? d.indexOf('31') : d.indexOf('01')") );
   1.207 +
   1.208 +test();
   1.209 +
   1.210 +function f() {
   1.211 +  return this;
   1.212 +}
   1.213 +function g() {
   1.214 +  var h = f;
   1.215 +  return h();
   1.216 +}
   1.217 +
   1.218 +function MyObject (v) {
   1.219 +  this.value      = v;
   1.220 +  this.toString   = new Function ( "return this.value +\"\"");
   1.221 +  this.indexOf     = String.prototype.indexOf;
   1.222 +}
   1.223 +

mercurial