Home | History | Annotate | Download | only in String
      1 /* The contents of this file are subject to the Netscape Public
      2  * License Version 1.1 (the "License"); you may not use this file
      3  * except in compliance with the License. You may obtain a copy of
      4  * the License at http://www.mozilla.org/NPL/
      5  *
      6  * Software distributed under the License is distributed on an "AS
      7  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
      8  * implied. See the License for the specific language governing
      9  * rights and limitations under the License.
     10  *
     11  * The Original Code is Mozilla Communicator client code, released March
     12  * 31, 1998.
     13  *
     14  * The Initial Developer of the Original Code is Netscape Communications
     15  * Corporation. Portions created by Netscape are
     16  * Copyright (C) 1998 Netscape Communications Corporation. All
     17  * Rights Reserved.
     18  *
     19  * Contributor(s):
     20  *
     21  */
     22 /**
     23     File Name:          15.5.4.5.1.js
     24     ECMA Section:       15.5.4.5 String.prototype.charCodeAt(pos)
     25     Description:        Returns a number (a nonnegative integer less than 2^16)
     26                         representing the Unicode encoding of the character at
     27                         position pos in this string.  If there is no character
     28                         at that position, the number is NaN.
     29 
     30                         When the charCodeAt method is called with one argument
     31                         pos, the following steps are taken:
     32                         1. Call ToString, giving it the theis value as its
     33                            argument
     34                         2. Call ToInteger(pos)
     35                         3. Compute the number of characters in result(1).
     36                         4. If Result(2) is less than 0 or is not less than
     37                            Result(3), return NaN.
     38                         5. Return a value of Number type, of positive sign, whose
     39                            magnitude is the Unicode encoding of one character
     40                            from result 1, namely the characer at position Result
     41                            (2), where the first character in Result(1) is
     42                            considered to be at position 0.
     43 
     44                         Note that the charCodeAt funciton is intentionally
     45                         generic; it does not require that its this value be a
     46                         String object.  Therefore it can be transferred to other
     47                         kinds of objects for use as a method.
     48 
     49     Author:             christine (at) netscape.com
     50     Date:               2 october 1997
     51 */
     52     var SECTION = "15.5.4.5-5";
     53     var VERSION = "ECMA_1";
     54     startTest();
     55     var TITLE   = "String.prototype.charCodeAt";
     56 
     57     writeHeaderToLog( SECTION + " "+ TITLE);
     58 
     59     var TEST_STRING = "";
     60 
     61     for ( var i = 0x0000; i < 255; i++ ) {
     62         TEST_STRING += String.fromCharCode( i );
     63     }
     64 
     65 
     66 
     67     var testcases = getTestCases();
     68     test();
     69 
     70 function getTestCases() {
     71     var array = new Array();
     72     var item = 0;
     73     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(0)", 0x0074,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(0)") );
     74     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(1)", 0x0072,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(1)") );
     75     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(2)", 0x0075,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(2)") );
     76     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(3)", 0x0065,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(3)") );
     77     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(4)", Number.NaN,     eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(4)") );
     78     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(-1)", Number.NaN,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(-1)") );
     79 
     80     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(true)",  0x0072,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(true)") );
     81     array[item++] = new TestCase( SECTION,     "x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(false)", 0x0074,    eval("x = new Boolean(true); x.charCodeAt=String.prototype.charCodeAt;x.charCodeAt(false)") );
     82 
     83     array[item++] = new TestCase( SECTION,     "x = new String(); x.charCodeAt(0)",    Number.NaN,     eval("x=new String();x.charCodeAt(0)") );
     84     array[item++] = new TestCase( SECTION,     "x = new String(); x.charCodeAt(1)",    Number.NaN,     eval("x=new String();x.charCodeAt(1)") );
     85     array[item++] = new TestCase( SECTION,     "x = new String(); x.charCodeAt(-1)",   Number.NaN,     eval("x=new String();x.charCodeAt(-1)") );
     86 
     87     array[item++] = new TestCase( SECTION,     "x = new String(); x.charCodeAt(NaN)",  Number.NaN,     eval("x=new String();x.charCodeAt(Number.NaN)") );
     88     array[item++] = new TestCase( SECTION,     "x = new String(); x.charCodeAt(Number.POSITIVE_INFINITY)",   Number.NaN,     eval("x=new String();x.charCodeAt(Number.POSITIVE_INFINITY)") );
     89     array[item++] = new TestCase( SECTION,     "x = new String(); x.charCodeAt(Number.NEGATIVE_INFINITY)",   Number.NaN,     eval("x=new String();x.charCodeAt(Number.NEGATIVE_INFINITY)") );
     90 
     91     for ( var j = 0; j < 255; j++ ) {
     92         array[item++] = new TestCase( SECTION,  "TEST_STRING.charCodeAt("+j+")",    j,     TEST_STRING.charCodeAt(j) );
     93     }
     94     return (array );
     95 }
     96 
     97 function test() {
     98     for ( tc = 0; tc < testcases.length; tc++ ) {
     99     testcases[tc].passed = writeTestCaseResult(
    100         testcases[tc].expect,
    101         testcases[tc].actual,
    102         testcases[tc].description +" = "+
    103         testcases[tc].actual );
    104 
    105         testcases[tc].reason += ( testcases[tc].passed )
    106                                     ? ""
    107                                     : "wrong value "
    108     }
    109     stopTest();
    110     return ( testcases );
    111 }
    112