js/src/tests/ecma/Expressions/11.7.1.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 /**
     8    File Name:          11.7.1.js
     9    ECMA Section:       11.7.1 The Left Shift Operator ( << )
    10    Description:
    11    Performs a bitwise left shift operation on the left argument by the amount
    12    specified by the right argument.
    14    The production ShiftExpression : ShiftExpression << AdditiveExpression is
    15    evaluated as follows:
    17    1.  Evaluate ShiftExpression.
    18    2.  Call GetValue(Result(1)).
    19    3.  Evaluate AdditiveExpression.
    20    4.  Call GetValue(Result(3)).
    21    5.  Call ToInt32(Result(2)).
    22    6.  Call ToUint32(Result(4)).
    23    7.  Mask out all but the least significant 5 bits of Result(6), that is,
    24    compute Result(6) & 0x1F.
    25    8.  Left shift Result(5) by Result(7) bits. The result is a signed 32 bit
    26    integer.
    27    9.  Return Result(8).
    29    Author:             christine@netscape.com
    30    Date:               12 november 1997
    31 */
    32 var SECTION = "11.7.1";
    33 var VERSION = "ECMA_1";
    34 startTest();
    36 writeHeaderToLog( SECTION + " The left shift operator ( << )");
    38 for ( power = 0; power < 33; power++ ) {
    39   shiftexp = Math.pow( 2, power );
    41   for ( addexp = 0; addexp < 33; addexp++ ) {
    42     new TestCase( SECTION,
    43 		  shiftexp + " << " + addexp,
    44 		  LeftShift( shiftexp, addexp ),
    45 		  shiftexp << addexp );
    46   }
    47 }
    49 test();
    51 function ToInteger( n ) {
    52   n = Number( n );
    53   var sign = ( n < 0 ) ? -1 : 1;
    55   if ( n != n ) {
    56     return 0;
    57   }
    58   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
    59     return n;
    60   }
    61   return ( sign * Math.floor(Math.abs(n)) );
    62 }
    63 function ToInt32( n ) {
    64   n = Number( n );
    65   var sign = ( n < 0 ) ? -1 : 1;
    67   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    68     return 0;
    69   }
    71   n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
    72   n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
    74   return ( n );
    75 }
    76 function ToUint32( n ) {
    77   n = Number( n );
    78   var sign = ( n < 0 ) ? -1 : 1;
    80   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    81     return 0;
    82   }
    83   n = sign * Math.floor( Math.abs(n) )
    85     n = n % Math.pow(2,32);
    87   if ( n < 0 ){
    88     n += Math.pow(2,32);
    89   }
    91   return ( n );
    92 }
    93 function ToUint16( n ) {
    94   var sign = ( n < 0 ) ? -1 : 1;
    96   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
    97     return 0;
    98   }
   100   n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
   102   if (n <0) {
   103     n += Math.pow(2,16);
   104   }
   106   return ( n );
   107 }
   108 function Mask( b, n ) {
   109   b = ToUint32BitString( b );
   110   b = b.substring( b.length - n );
   111   b = ToUint32Decimal( b );
   112   return ( b );
   113 }
   114 function ToUint32BitString( n ) {
   115   var b = "";
   116   for ( p = 31; p >=0; p-- ) {
   117     if ( n >= Math.pow(2,p) ) {
   118       b += "1";
   119       n -= Math.pow(2,p);
   120     } else {
   121       b += "0";
   122     }
   123   }
   124   return b;
   125 }
   126 function ToInt32BitString( n ) {
   127   var b = "";
   128   var sign = ( n < 0 ) ? -1 : 1;
   130   b += ( sign == 1 ) ? "0" : "1";
   132   for ( p = 30; p >=0; p-- ) {
   133     if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
   134       b += ( sign == 1 ) ? "1" : "0";
   135       n -= sign * Math.pow( 2, p );
   136     } else {
   137       b += ( sign == 1 ) ? "0" : "1";
   138     }
   139   }
   141   return b;
   142 }
   143 function ToInt32Decimal( bin ) {
   144   var r = 0;
   145   var sign;
   147   if ( Number(bin.charAt(0)) == 0 ) {
   148     sign = 1;
   149     r = 0;
   150   } else {
   151     sign = -1;
   152     r = -(Math.pow(2,31));
   153   }
   155   for ( var j = 0; j < 31; j++ ) {
   156     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
   157   }
   159   return r;
   160 }
   161 function ToUint32Decimal( bin ) {
   162   var r = 0;
   165   for ( l = bin.length; l < 32; l++ ) {
   166     bin = "0" + bin;
   167   }
   169   for ( j = 0; j < 31; j++ ) {
   170     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
   172   }
   174   return r;
   175 }
   176 function LeftShift( s, a ) {
   177   var shift = ToInt32( s );
   178   var add = ToUint32( a );
   179   add = Mask( add, 5 );
   180   var exp = LShift( shift, add );
   182   return ( exp );
   183 }
   184 function LShift( s, a ) {
   185   s = ToInt32BitString( s );
   187   for ( var z = 0; z < a; z++ ) {
   188     s += "0";
   189   }
   191   s = s.substring( a, s.length);
   193   return ToInt32(ToInt32Decimal(s));
   194 }

mercurial