]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/Expressions/11.7.1.js
   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/ 
   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. 
  11  * The Original Code is Mozilla Communicator client code, released March 
  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 
  24     ECMA Section:       11.7.1 The Left Shift Operator ( << ) 
  26     Performs a bitwise left shift operation on the left argument by the amount 
  27     specified by the right argument. 
  29     The production ShiftExpression : ShiftExpression << AdditiveExpression is 
  32     1.  Evaluate ShiftExpression. 
  33     2.  Call GetValue(Result(1)). 
  34     3.  Evaluate AdditiveExpression. 
  35     4.  Call GetValue(Result(3)). 
  36     5.  Call ToInt32(Result(2)). 
  37     6.  Call ToUint32(Result(4)). 
  38     7.  Mask out all but the least significant 5 bits of Result(6), that is, 
  39         compute Result(6) & 0x1F. 
  40     8.  Left shift Result(5) by Result(7) bits. The result is a signed 32 bit 
  44     Author:             christine@netscape.com 
  45     Date:               12 november 1997 
  47     var SECTION 
= "11.7.1"; 
  48     var VERSION 
= "ECMA_1"; 
  51     var testcases 
= getTestCases(); 
  53     writeHeaderToLog( SECTION 
+ " The left shift operator ( << )"); 
  57     for ( tc
=0; tc 
< testcases
.length
; tc
++ ) { 
  58         testcases
[tc
].passed 
= writeTestCaseResult( 
  61                             testcases
[tc
].description 
+" = "+ 
  62                             testcases
[tc
].actual 
); 
  64         testcases
[tc
].reason 
+= ( testcases
[tc
].passed 
) ? "" : "wrong value "; 
  69 function getTestCases() { 
  70     var array 
= new Array(); 
  73     for ( power 
= 0; power 
< 33; power
++ ) { 
  74         shiftexp 
= Math
.pow( 2, power 
); 
  76         for ( addexp 
= 0; addexp 
< 33; addexp
++ ) { 
  77             array
[item
++] = new TestCase( SECTION
, 
  78                                     shiftexp 
+ " << " + addexp
, 
  79                                     LeftShift( shiftexp
, addexp 
), 
  86 function ToInteger( n 
) { 
  88     var sign 
= ( n 
< 0 ) ? -1 : 1; 
  93     if ( Math
.abs( n 
) == 0 || Math
.abs( n 
) == Number
.POSITIVE_INFINITY 
) { 
  96     return ( sign 
* Math
.floor(Math
.abs(n
)) ); 
  98 function ToInt32( n 
) { 
 100     var sign 
= ( n 
< 0 ) ? -1 : 1; 
 102     if ( Math
.abs( n 
) == 0 || Math
.abs( n 
) == Number
.POSITIVE_INFINITY
) { 
 106     n 
= (sign 
* Math
.floor( Math
.abs(n
) )) % Math
.pow(2,32); 
 107     n 
= ( n 
>= Math
.pow(2,31) ) ? n 
- Math
.pow(2,32) : n
; 
 111 function ToUint32( n 
) { 
 113     var sign 
= ( n 
< 0 ) ? -1 : 1; 
 115     if ( Math
.abs( n 
) == 0 || Math
.abs( n 
) == Number
.POSITIVE_INFINITY
) { 
 118     n 
= sign 
* Math
.floor( Math
.abs(n
) ) 
 120     n 
= n 
% Math
.pow(2,32); 
 128 function ToUint16( n 
) { 
 129     var sign 
= ( n 
< 0 ) ? -1 : 1; 
 131     if ( Math
.abs( n 
) == 0 || Math
.abs( n 
) == Number
.POSITIVE_INFINITY
) { 
 135     n 
= ( sign 
* Math
.floor( Math
.abs(n
) ) ) % Math
.pow(2,16); 
 143 function Mask( b
, n 
) { 
 144     b 
= ToUint32BitString( b 
); 
 145     b 
= b
.substring( b
.length 
- n 
); 
 146     b 
= ToUint32Decimal( b 
); 
 149 function ToUint32BitString( n 
) { 
 151     for ( p 
= 31; p 
>=0; p
-- ) { 
 152         if ( n 
>= Math
.pow(2,p
) ) { 
 161 function ToInt32BitString( n 
) { 
 163     var sign 
= ( n 
< 0 ) ? -1 : 1; 
 165     b 
+= ( sign 
== 1 ) ? "0" : "1"; 
 167     for ( p 
= 30; p 
>=0; p
-- ) { 
 168         if ( (sign 
== 1 ) ? sign 
* n 
>= Math
.pow(2,p
) : sign 
* n 
> Math
.pow(2,p
) ) { 
 169             b 
+= ( sign 
== 1 ) ? "1" : "0"; 
 170             n 
-= sign 
* Math
.pow( 2, p 
); 
 172             b 
+= ( sign 
== 1 ) ? "0" : "1"; 
 178 function ToInt32Decimal( bin 
) { 
 182     if ( Number(bin
.charAt(0)) == 0 ) { 
 187         r 
= -(Math
.pow(2,31)); 
 190     for ( var j 
= 0; j 
< 31; j
++ ) { 
 191         r 
+= Math
.pow( 2, j 
) * Number(bin
.charAt(31-j
)); 
 196 function ToUint32Decimal( bin 
) { 
 200     for ( l 
= bin
.length
; l 
< 32; l
++ ) { 
 204     for ( j 
= 0; j 
< 31; j
++ ) { 
 205         r 
+= Math
.pow( 2, j 
) * Number(bin
.charAt(31-j
)); 
 211 function LeftShift( s
, a 
) { 
 212     var shift 
= ToInt32( s 
); 
 213     var add 
= ToUint32( a 
); 
 214     add 
= Mask( add
, 5 ); 
 215     var exp 
= LShift( shift
, add 
); 
 219 function LShift( s
, a 
) { 
 220     s 
= ToInt32BitString( s 
); 
 222     for ( var z 
= 0; z 
< a
; z
++ ) { 
 226     s 
= s
.substring( a
, s
.length
); 
 228     return ToInt32(ToInt32Decimal(s
));