js/src/tests/ecma/String/15.5.3.2-3.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.3.2-3.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     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.3.2-1.js
    1.12 +   ECMA Section:       15.5.3.2  String.fromCharCode( char0, char1, ... )
    1.13 +   Description:        Return a string value containing as many characters
    1.14 +   as the number of arguments.  Each argument specifies
    1.15 +   one character of the resulting string, with the first
    1.16 +   argument specifying the first character, and so on,
    1.17 +   from left to right.  An argument is converted to a
    1.18 +   character by applying the operation ToUint16_t and
    1.19 +   regarding the resulting 16bit integeras the Unicode
    1.20 +   encoding of a character.  If no arguments are supplied,
    1.21 +   the result is the empty string.
    1.22 +
    1.23 +   This test covers Basic Latin (range U+0020 - U+007F)
    1.24 +
    1.25 +   Author:             christine@netscape.com
    1.26 +   Date:               2 october 1997
    1.27 +*/
    1.28 +
    1.29 +var SECTION = "15.5.3.2-3";
    1.30 +var VERSION = "ECMA_1";
    1.31 +startTest();
    1.32 +var TITLE   = "String.fromCharCode()";
    1.33 +
    1.34 +writeHeaderToLog( SECTION + " "+ TITLE);
    1.35 +
    1.36 +for ( CHARCODE = 0; CHARCODE < 256; CHARCODE++ ) {
    1.37 +  new TestCase(   SECTION,
    1.38 +		  "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
    1.39 +		  ToUint16(CHARCODE),
    1.40 +		  (String.fromCharCode(CHARCODE)).charCodeAt(0)
    1.41 +    );
    1.42 +}
    1.43 +for ( CHARCODE = 256; CHARCODE < 65536; CHARCODE+=333 ) {
    1.44 +  new TestCase(   SECTION,
    1.45 +		  "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
    1.46 +		  ToUint16(CHARCODE),
    1.47 +		  (String.fromCharCode(CHARCODE)).charCodeAt(0)
    1.48 +    );
    1.49 +}
    1.50 +for ( CHARCODE = 65535; CHARCODE < 65538; CHARCODE++ ) {
    1.51 +  new TestCase(   SECTION,
    1.52 +		  "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
    1.53 +		  ToUint16(CHARCODE),
    1.54 +		  (String.fromCharCode(CHARCODE)).charCodeAt(0)
    1.55 +    );
    1.56 +}
    1.57 +for ( CHARCODE = Math.pow(2,32)-1; CHARCODE < Math.pow(2,32)+1; CHARCODE++ ) {
    1.58 +  new TestCase(   SECTION,
    1.59 +		  "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
    1.60 +		  ToUint16(CHARCODE),
    1.61 +		  (String.fromCharCode(CHARCODE)).charCodeAt(0)
    1.62 +    );
    1.63 +}
    1.64 +for ( CHARCODE = 0; CHARCODE > -65536; CHARCODE-=3333 ) {
    1.65 +  new TestCase(   SECTION,
    1.66 +		  "(String.fromCharCode(" + CHARCODE +")).charCodeAt(0)",
    1.67 +		  ToUint16(CHARCODE),
    1.68 +		  (String.fromCharCode(CHARCODE)).charCodeAt(0)
    1.69 +    );
    1.70 +}
    1.71 +new TestCase( SECTION, "(String.fromCharCode(65535)).charCodeAt(0)",    65535,  (String.fromCharCode(65535)).charCodeAt(0) );
    1.72 +new TestCase( SECTION, "(String.fromCharCode(65536)).charCodeAt(0)",    0,      (String.fromCharCode(65536)).charCodeAt(0) );
    1.73 +new TestCase( SECTION, "(String.fromCharCode(65537)).charCodeAt(0)",    1,      (String.fromCharCode(65537)).charCodeAt(0) );
    1.74 +
    1.75 +test();
    1.76 +
    1.77 +function ToUint16( num ) {
    1.78 +  num = Number( num );
    1.79 +  if ( isNaN( num ) || num == 0 || num == Number.POSITIVE_INFINITY || num == Number.NEGATIVE_INFINITY ) {
    1.80 +    return 0;
    1.81 +  }
    1.82 +
    1.83 +  var sign = ( num < 0 ) ? -1 : 1;
    1.84 +
    1.85 +  num = sign * Math.floor( Math.abs( num ) );
    1.86 +  num = num % Math.pow(2,16);
    1.87 +  num = ( num > -65536 && num < 0) ? 65536 + num : num;
    1.88 +  return num;
    1.89 +}
    1.90 +

mercurial