js/src/tests/ecma/String/15.5.4.6-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.6-1.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,121 @@
     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
    1.48 +   Date:               2 october 1997
    1.49 +*/
    1.50 +var SECTION = "15.5.4.6-1";
    1.51 +var VERSION = "ECMA_1";
    1.52 +startTest();
    1.53 +var TITLE   = "String.protoype.indexOf";
    1.54 +
    1.55 +var TEST_STRING = new String( " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" );
    1.56 +
    1.57 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.58 +
    1.59 +var j = 0;
    1.60 +
    1.61 +for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
    1.62 +  new TestCase( SECTION,
    1.63 +		"String.indexOf(" +String.fromCharCode(i)+ ", 0)",
    1.64 +		k,
    1.65 +		TEST_STRING.indexOf( String.fromCharCode(i), 0 ) );
    1.66 +}
    1.67 +
    1.68 +for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
    1.69 +  new TestCase( SECTION,
    1.70 +		"String.indexOf("+String.fromCharCode(i)+ ", "+ k +")",
    1.71 +		k,
    1.72 +		TEST_STRING.indexOf( String.fromCharCode(i), k ) );
    1.73 +}
    1.74 +
    1.75 +for ( k = 0, i = 0x0020; i < 0x007e; i++, j++, k++ ) {
    1.76 +  new TestCase( SECTION,
    1.77 +		"String.indexOf("+String.fromCharCode(i)+ ", "+k+1+")",
    1.78 +		-1,
    1.79 +		TEST_STRING.indexOf( String.fromCharCode(i), k+1 ) );
    1.80 +}
    1.81 +
    1.82 +for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
    1.83 +  new TestCase( SECTION,
    1.84 +		"String.indexOf("+(String.fromCharCode(i) +
    1.85 +				   String.fromCharCode(i+1)+
    1.86 +				   String.fromCharCode(i+2)) +", "+0+")",
    1.87 +		k,
    1.88 +		TEST_STRING.indexOf( (String.fromCharCode(i)+
    1.89 +				      String.fromCharCode(i+1)+
    1.90 +				      String.fromCharCode(i+2)),
    1.91 +				     0 ) );
    1.92 +}
    1.93 +
    1.94 +for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
    1.95 +  new TestCase( SECTION,
    1.96 +		"String.indexOf("+(String.fromCharCode(i) +
    1.97 +				   String.fromCharCode(i+1)+
    1.98 +				   String.fromCharCode(i+2)) +", "+ k +")",
    1.99 +		k,
   1.100 +		TEST_STRING.indexOf( (String.fromCharCode(i)+
   1.101 +				      String.fromCharCode(i+1)+
   1.102 +				      String.fromCharCode(i+2)),
   1.103 +				     k ) );
   1.104 +}
   1.105 +for ( k = 0, i = 0x0020; i < 0x007d; i++, j++, k++ ) {
   1.106 +  new TestCase( SECTION,
   1.107 +		"String.indexOf("+(String.fromCharCode(i) +
   1.108 +				   String.fromCharCode(i+1)+
   1.109 +				   String.fromCharCode(i+2)) +", "+ k+1 +")",
   1.110 +		-1,
   1.111 +		TEST_STRING.indexOf( (String.fromCharCode(i)+
   1.112 +				      String.fromCharCode(i+1)+
   1.113 +				      String.fromCharCode(i+2)),
   1.114 +				     k+1 ) );
   1.115 +}
   1.116 +
   1.117 +new TestCase( SECTION,  "String.indexOf(" +TEST_STRING + ", 0 )", 0, TEST_STRING.indexOf( TEST_STRING, 0 ) );
   1.118 +
   1.119 +new TestCase( SECTION,  "String.indexOf(" +TEST_STRING + ", 1 )", -1, TEST_STRING.indexOf( TEST_STRING, 1 ));
   1.120 +
   1.121 +print( "TEST_STRING = new String(\" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\")" );
   1.122 +
   1.123 +
   1.124 +test();

mercurial