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-6.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-6";
     53     var VERSION = "ECMA_2";
     54     startTest();
     55     var TITLE   = "String.prototype.charCodeAt";
     56 
     57     writeHeaderToLog( SECTION + " "+ TITLE);
     58 
     59     var testcases = getTestCases();
     60     test();
     61 
     62 function getTestCases() {
     63     var array = new Array();
     64     var item = 0;
     65 
     66     array[item++] = new TestCase( SECTION,
     67                                   "var obj = true; obj.__proto__.charCodeAt = String.prototype.charCodeAt; var s = ''; for ( var i = 0; i < 4; i++ ) s+= String.fromCharCode( obj.charCodeAt(i) ); s",
     68                                   "true",
     69                                   eval("var obj = true; obj.__proto__.charCodeAt = String.prototype.charCodeAt; var s = ''; for ( var i = 0; i < 4; i++ ) s+= String.fromCharCode( obj.charCodeAt(i) ); s") );
     70 
     71     array[item++] = new TestCase( SECTION,
     72                                   "var obj = 1234; obj.__proto__.charCodeAt = String.prototype.charCodeAt; var s = ''; for ( var i = 0; i < 4; i++ ) s+= String.fromCharCode( obj.charCodeAt(i) ); s",
     73                                   "1234",
     74                                   eval("var obj = 1234; obj.__proto__.charCodeAt = String.prototype.charCodeAt; var s = ''; for ( var i = 0; i < 4; i++ ) s+= String.fromCharCode( obj.charCodeAt(i) ); s") );
     75 
     76     array[item++] = new TestCase( SECTION,
     77                                   "var obj = 'hello'; obj.__proto__.charCodeAt = String.prototype.charCodeAt; var s = ''; for ( var i = 0; i < 5; i++ ) s+= String.fromCharCode( obj.charCodeAt(i) ); s",
     78                                   "hello",
     79                                   eval("var obj = 'hello'; obj.__proto__.charCodeAt = String.prototype.charCodeAt; var s = ''; for ( var i = 0; i < 5; i++ ) s+= String.fromCharCode( obj.charCodeAt(i) ); s") );
     80     return (array );
     81 }
     82 
     83 function test() {
     84     for ( tc = 0; tc < testcases.length; tc++ ) {
     85     testcases[tc].passed = writeTestCaseResult(
     86         testcases[tc].expect,
     87         testcases[tc].actual,
     88         testcases[tc].description +" = "+
     89         testcases[tc].actual );
     90 
     91         testcases[tc].reason += ( testcases[tc].passed )
     92                                     ? ""
     93                                     : "wrong value "
     94     }
     95     stopTest();
     96     return ( testcases );
     97 }
     98