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.3.2-1.js
     24     ECMA Section:       15.5.3.2  String.fromCharCode( char0, char1, ... )
     25     Description:        Return a string value containing as many characters
     26                         as the number of arguments.  Each argument specifies
     27                         one character of the resulting string, with the first
     28                         argument specifying the first character, and so on,
     29                         from left to right.  An argument is converted to a
     30                         character by applying the operation ToUint16 and
     31                         regarding the resulting 16bit integeras the Unicode
     32                         encoding of a character.  If no arguments are supplied,
     33                         the result is the empty string.
     34 
     35                         This test covers Basic Latin (range U+0020 - U+007F)
     36 
     37     Author:             christine (at) netscape.com
     38     Date:               2 october 1997
     39 */
     40 
     41     var SECTION = "15.5.3.2-3";
     42     var VERSION = "ECMA_1";
     43     startTest();
     44     var TITLE   = "String.fromCharCode()";
     45 
     46     writeHeaderToLog( SECTION + " "+ TITLE);
     47 
     48     var testcases = getTestCases();
     49     test();
     50 
     51 function getTestCases() {
     52     var array = new Array();
     53     var item = 0;
     54 
     55     for ( CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) {
     56         array[item++] = new TestCase(   SECTION,
     57                                         "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
     58                                         ToUint16(CHARCODE),
     59                                         (String.fromCharCode(CHARCODE)).charCodeAt(0)
     60                                      );
     61     }
     62     for ( CHARCODE = 256; CHARCODE < 65536; CHARCODE+=333 ) {
     63         array[item++] = new TestCase(   SECTION,
     64                                         "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
     65                                         ToUint16(CHARCODE),
     66                                         (String.fromCharCode(CHARCODE)).charCodeAt(0)
     67                                      );
     68     }
     69     for ( CHARCODE = 65535; CHARCODE < 65538; CHARCODE++ ) {
     70         array[item++] = new TestCase(   SECTION,
     71                                         "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
     72                                         ToUint16(CHARCODE),
     73                                         (String.fromCharCode(CHARCODE)).charCodeAt(0)
     74                                      );
     75     }
     76     for ( CHARCODE = Math.pow(2,32)-1; CHARCODE < Math.pow(2,32)+1; CHARCODE++ ) {
     77         array[item++] = new TestCase(   SECTION,
     78                                         "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
     79                                         ToUint16(CHARCODE),
     80                                         (String.fromCharCode(CHARCODE)).charCodeAt(0)
     81                                      );
     82     }
     83     for ( CHARCODE = 0; CHARCODE > -65536; CHARCODE-=3333 ) {
     84         array[item++] = new TestCase(   SECTION,
     85                                         "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
     86                                         ToUint16(CHARCODE),
     87                                         (String.fromCharCode(CHARCODE)).charCodeAt(0)
     88                                      );
     89     }
     90     array[item++] = new TestCase( SECTION, "(String.fromCharCode(65535)).charCodeAt(0)",    65535,  (String.fromCharCode(65535)).charCodeAt(0) );
     91     array[item++] = new TestCase( SECTION, "(String.fromCharCode(65536)).charCodeAt(0)",    0,      (String.fromCharCode(65536)).charCodeAt(0) );
     92     array[item++] = new TestCase( SECTION, "(String.fromCharCode(65537)).charCodeAt(0)",    1,      (String.fromCharCode(65537)).charCodeAt(0) );
     93 
     94      return array;
     95 }
     96 function ToUint16( num ) {
     97     num = Number( num );
     98     if ( isNaN( num ) || num == 0 || num == Number.POSITIVE_INFINITY || num == Number.NEGATIVE_INFINITY ) {
     99         return 0;
    100     }
    101 
    102     var sign = ( num < 0 ) ? -1 : 1;
    103 
    104     num = sign * Math.floor( Math.abs( num ) );
    105     num = num % Math.pow(2,16);
    106     num = ( num > -65536 && num < 0) ? 65536 + num : num;
    107     return num;
    108 }
    109 
    110 function test() {
    111     for ( tc=0; tc < testcases.length; tc++ ) {
    112         testcases[tc].passed = writeTestCaseResult(
    113                             testcases[tc].expect,
    114                             testcases[tc].actual,
    115                             testcases[tc].description +" = "+
    116                             testcases[tc].actual );
    117 
    118         testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
    119     }
    120     stopTest();
    121     return ( testcases );
    122 }
    123