]>
git.saurik.com Git - apple/javascriptcore.git/blob - tests/mozilla/ecma/Expressions/11.10-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.10-1 Binary Bitwise Operators: &
28 The production A : A @ B, where @ is one of the bitwise operators in the
29 productions &, ^, | , is evaluated as follows:
32 2. Call GetValue(Result(1)).
34 4. Call GetValue(Result(3)).
35 5. Call ToInt32(Result(2)).
36 6. Call ToInt32(Result(4)).
37 7. Apply the bitwise operator @ to Result(5) and Result(6). The result is
38 a signed 32 bit integer.
41 Author: christine@netscape.com
42 Date: 12 november 1997
44 var SECTION
= "11.10-1";
45 var VERSION
= "ECMA_1";
48 var testcases
= getTestCases();
50 writeHeaderToLog( SECTION
+ " Binary Bitwise Operators: &");
54 for ( tc
=0; tc
< testcases
.length
; tc
++ ) {
55 testcases
[tc
].passed
= writeTestCaseResult(
58 testcases
[tc
].description
+" = "+
59 testcases
[tc
].actual
);
61 testcases
[tc
].reason
+= ( testcases
[tc
].passed
) ? "" : "wrong value ";
66 function getTestCases() {
67 var array
= new Array();
72 // for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
73 for ( shiftpow
= 0; shiftpow
< 1; shiftpow
++ ) {
74 shiftexp
+= Math
.pow( 2, shiftpow
);
76 for ( addpow
= 0; addpow
< 33; addpow
++ ) {
77 addexp
+= Math
.pow(2, addpow
);
79 array
[item
++] = new TestCase( SECTION
,
80 shiftexp
+ " & " + addexp
,
81 And( shiftexp
, addexp
),
88 function ToInteger( n
) {
90 var sign
= ( n
< 0 ) ? -1 : 1;
95 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
98 return ( sign
* Math
.floor(Math
.abs(n
)) );
100 function ToInt32( n
) {
102 var sign
= ( n
< 0 ) ? -1 : 1;
104 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
108 n
= (sign
* Math
.floor( Math
.abs(n
) )) % Math
.pow(2,32);
109 n
= ( n
>= Math
.pow(2,31) ) ? n
- Math
.pow(2,32) : n
;
113 function ToUint32( n
) {
115 var sign
= ( n
< 0 ) ? -1 : 1;
117 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
120 n
= sign
* Math
.floor( Math
.abs(n
) )
122 n
= n
% Math
.pow(2,32);
130 function ToUint16( n
) {
131 var sign
= ( n
< 0 ) ? -1 : 1;
133 if ( Math
.abs( n
) == 0 || Math
.abs( n
) == Number
.POSITIVE_INFINITY
) {
137 n
= ( sign
* Math
.floor( Math
.abs(n
) ) ) % Math
.pow(2,16);
145 function Mask( b
, n
) {
146 b
= ToUint32BitString( b
);
147 b
= b
.substring( b
.length
- n
);
148 b
= ToUint32Decimal( b
);
151 function ToUint32BitString( n
) {
153 for ( p
= 31; p
>=0; p
-- ) {
154 if ( n
>= Math
.pow(2,p
) ) {
163 function ToInt32BitString( n
) {
165 var sign
= ( n
< 0 ) ? -1 : 1;
167 b
+= ( sign
== 1 ) ? "0" : "1";
169 for ( p
= 30; p
>=0; p
-- ) {
170 if ( (sign
== 1 ) ? sign
* n
>= Math
.pow(2,p
) : sign
* n
> Math
.pow(2,p
) ) {
171 b
+= ( sign
== 1 ) ? "1" : "0";
172 n
-= sign
* Math
.pow( 2, p
);
174 b
+= ( sign
== 1 ) ? "0" : "1";
180 function ToInt32Decimal( bin
) {
184 if ( Number(bin
.charAt(0)) == 0 ) {
189 r
= -(Math
.pow(2,31));
192 for ( var j
= 0; j
< 31; j
++ ) {
193 r
+= Math
.pow( 2, j
) * Number(bin
.charAt(31-j
));
198 function ToUint32Decimal( bin
) {
202 for ( l
= bin
.length
; l
< 32; l
++ ) {
206 for ( j
= 0; j
< 31; j
++ ) {
207 r
+= Math
.pow( 2, j
) * Number(bin
.charAt(31-j
));
213 function And( s
, a
) {
217 var bs
= ToInt32BitString( s
);
218 var ba
= ToInt32BitString( a
);
222 for ( var bit
= 0; bit
< bs
.length
; bit
++ ) {
223 if ( bs
.charAt(bit
) == "1" && ba
.charAt(bit
) == "1" ) {
229 return ToInt32Decimal(result
);
231 function Xor( s
, a
) {
235 var bs
= ToInt32BitString( s
);
236 var ba
= ToInt32BitString( a
);
240 for ( var bit
= 0; bit
< bs
.length
; bit
++ ) {
241 if ( (bs
.charAt(bit
) == "1" && ba
.charAt(bit
) == "0") ||
242 (bs
.charAt(bit
) == "0" && ba
.charAt(bit
) == "1")
250 return ToInt32Decimal(result
);
252 function Or( s
, a
) {
256 var bs
= ToInt32BitString( s
);
257 var ba
= ToInt32BitString( a
);
261 for ( var bit
= 0; bit
< bs
.length
; bit
++ ) {
262 if ( bs
.charAt(bit
) == "1" || ba
.charAt(bit
) == "1" ) {
269 return ToInt32Decimal(result
);