]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/Expressions/11.7.2.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.2 The signed right shift operator ( >> )
26 Performs a sign-filling bitwise right shift operation on the left argument
27 by the amount 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. Perform sign-extending right shift of Result(5) by Result(7) bits. The
41 most significant bit is propagated. The result is a signed 32 bit
45 Author: christine@netscape.com
46 Date: 12 november 1997
48 var SECTION
= "11.7.2";
49 var VERSION
= "ECMA_1";
51 var testcases
= getTestCases();
53 writeHeaderToLog( SECTION
+ " The signed right 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();
75 for ( power
= 0; power
<= 32; power
++ ) {
76 shiftexp
= Math
.pow( 2, power
);
78 for ( addexp
= 0; addexp
<= 32; addexp
++ ) {
79 array
[item
++] = new TestCase( SECTION
,
80 shiftexp
+ " >> " + addexp
,
81 SignedRightShift( shiftexp
, addexp
),
86 for ( power
= 0; power
<= 32; power
++ ) {
87 shiftexp
= -Math
.pow( 2, power
);
89 for ( addexp
= 0; addexp
<= 32; addexp
++ ) {
90 array
[item
++] = new TestCase( SECTION
,
91 shiftexp
+ " >> " + addexp
,
92 SignedRightShift( shiftexp
, addexp
),
100 function ToInteger( n
) {
102 var sign
= ( n
< 0 ) ? -1 : 1;
107 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
110 return ( sign
* Math
.floor(Math
.abs(n
)) );
112 function ToInt32( n
) {
114 var sign
= ( n
< 0 ) ? -1 : 1;
116 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
120 n
= (sign
* Math
.floor( Math
.abs(n
) )) % Math
.pow(2,32);
121 n
= ( n
>= Math
.pow(2,31) ) ? n
- Math
.pow(2,32) : n
;
125 function ToUint32( n
) {
127 var sign
= ( n
< 0 ) ? -1 : 1;
129 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
132 n
= sign
* Math
.floor( Math
.abs(n
) )
134 n
= n
% Math
.pow(2,32);
142 function ToUint16( n
) {
143 var sign
= ( n
< 0 ) ? -1 : 1;
145 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
149 n
= ( sign
* Math
.floor( Math
.abs(n
) ) ) % Math
.pow(2,16);
157 function Mask( b
, n
) {
158 b
= ToUint32BitString( b
);
159 b
= b
.substring( b
.length
- n
);
160 b
= ToUint32Decimal( b
);
163 function ToUint32BitString( n
) {
165 for ( p
= 31; p
>=0; p
-- ) {
166 if ( n
>= Math
.pow(2,p
) ) {
175 function ToInt32BitString( n
) {
177 var sign
= ( n
< 0 ) ? -1 : 1;
179 b
+= ( sign
== 1 ) ? "0" : "1";
181 for ( p
= 30; p
>=0; p
-- ) {
182 if ( (sign
== 1 ) ? sign
* n
>= Math
.pow(2,p
) : sign
* n
> Math
.pow(2,p
) ) {
183 b
+= ( sign
== 1 ) ? "1" : "0";
184 n
-= sign
* Math
.pow( 2, p
);
186 b
+= ( sign
== 1 ) ? "0" : "1";
192 function ToInt32Decimal( bin
) {
196 if ( Number(bin
.charAt(0)) == 0 ) {
201 r
= -(Math
.pow(2,31));
204 for ( var j
= 0; j
< 31; j
++ ) {
205 r
+= Math
.pow( 2, j
) * Number(bin
.charAt(31-j
));
210 function ToUint32Decimal( bin
) {
213 for ( l
= bin
.length
; l
< 32; l
++ ) {
217 for ( j
= 0; j
< 31; j
++ ) {
218 r
+= Math
.pow( 2, j
) * Number(bin
.charAt(31-j
));
223 function SignedRightShift( s
, a
) {
227 return ( SignedRShift( s
, a
) );
229 function SignedRShift( s
, a
) {
230 s
= ToInt32BitString( s
);
232 var firstbit
= s
.substring(0,1);
234 s
= s
.substring( 1, s
.length
);
236 for ( var z
= 0; z
< a
; z
++ ) {
240 s
= s
.substring( 0, s
.length
- a
);
245 return ToInt32(ToInt32Decimal(s
));