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/
5 *
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.
10 *
11 * The Original Code is Mozilla Communicator client code, released March
12 * 31, 1998.
13 *
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
17 * Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22/**
23    File Name:          11.10-1.js
24    ECMA Section:       11.10-1 Binary Bitwise Operators:  &
25    Description:
26    Semantics
27
28    The production A : A @ B, where @ is one of the bitwise operators in the
29    productions &, ^, | , is evaluated as follows:
30
31    1.  Evaluate A.
32    2.  Call GetValue(Result(1)).
33    3.  Evaluate B.
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.
39    8.  Return Result(7).
40
41    Author:             christine@netscape.com
42    Date:               12 november 1997
43*/
44    var SECTION = "11.10-1";
45    var VERSION = "ECMA_1";
46    startTest();
47
48    var testcases = getTestCases();
49
50    writeHeaderToLog( SECTION + " Binary Bitwise Operators:  &");
51    test();
52
53function test() {
54    for ( tc=0; tc < testcases.length; tc++ ) {
55        testcases[tc].passed = writeTestCaseResult(
56                            testcases[tc].expect,
57                            testcases[tc].actual,
58                            testcases[tc].description +" = "+
59                            testcases[tc].actual );
60
61        testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
62    }
63    stopTest();
64    return ( testcases );
65}
66function getTestCases() {
67    var array = new Array();
68    var item = 0;
69    var shiftexp = 0;
70    var addexp = 0;
71
72//    for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
73    for ( shiftpow = 0; shiftpow < 1; shiftpow++ ) {
74        shiftexp += Math.pow( 2, shiftpow );
75
76        for ( addpow = 0; addpow < 33; addpow++ ) {
77            addexp += Math.pow(2, addpow);
78
79            array[item++] = new TestCase( SECTION,
80                                    shiftexp + " & " + addexp,
81                                    And( shiftexp, addexp ),
82                                    shiftexp & addexp );
83        }
84    }
85
86    return ( array );
87}
88function ToInteger( n ) {
89    n = Number( n );
90    var sign = ( n < 0 ) ? -1 : 1;
91
92    if ( n != n ) {
93        return 0;
94    }
95    if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
96        return n;
97    }
98    return ( sign * Math.floor(Math.abs(n)) );
99}
100function ToInt32( n ) {
101    n = Number( n );
102    var sign = ( n < 0 ) ? -1 : 1;
103
104    if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
105        return 0;
106    }
107
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;
110
111    return ( n );
112}
113function ToUint32( n ) {
114    n = Number( n );
115    var sign = ( n < 0 ) ? -1 : 1;
116
117    if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
118        return 0;
119    }
120    n = sign * Math.floor( Math.abs(n) )
121
122    n = n % Math.pow(2,32);
123
124    if ( n < 0 ){
125        n += Math.pow(2,32);
126    }
127
128    return ( n );
129}
130function ToUint16( n ) {
131    var sign = ( n < 0 ) ? -1 : 1;
132
133    if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
134        return 0;
135    }
136
137    n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
138
139    if (n <0) {
140        n += Math.pow(2,16);
141    }
142
143    return ( n );
144}
145function Mask( b, n ) {
146    b = ToUint32BitString( b );
147    b = b.substring( b.length - n );
148    b = ToUint32Decimal( b );
149    return ( b );
150}
151function ToUint32BitString( n ) {
152    var b = "";
153    for ( p = 31; p >=0; p-- ) {
154        if ( n >= Math.pow(2,p) ) {
155            b += "1";
156            n -= Math.pow(2,p);
157        } else {
158            b += "0";
159        }
160    }
161    return b;
162}
163function ToInt32BitString( n ) {
164    var b = "";
165    var sign = ( n < 0 ) ? -1 : 1;
166
167    b += ( sign == 1 ) ? "0" : "1";
168
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 );
173        } else {
174            b += ( sign == 1 ) ? "0" : "1";
175        }
176    }
177
178    return b;
179}
180function ToInt32Decimal( bin ) {
181    var r = 0;
182    var sign;
183
184    if ( Number(bin.charAt(0)) == 0 ) {
185        sign = 1;
186        r = 0;
187    } else {
188        sign = -1;
189        r = -(Math.pow(2,31));
190    }
191
192    for ( var j = 0; j < 31; j++ ) {
193        r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
194    }
195
196    return r;
197}
198function ToUint32Decimal( bin ) {
199    var r = 0;
200
201
202    for ( l = bin.length; l < 32; l++ ) {
203        bin = "0" + bin;
204    }
205
206    for ( j = 0; j < 31; j++ ) {
207        r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
208
209    }
210
211    return r;
212}
213function And( s, a ) {
214    s = ToInt32( s );
215    a = ToInt32( a );
216
217    var bs = ToInt32BitString( s );
218    var ba = ToInt32BitString( a );
219
220    var result = "";
221
222    for ( var bit = 0; bit < bs.length; bit++ ) {
223        if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
224            result += "1";
225        } else {
226            result += "0";
227        }
228    }
229    return ToInt32Decimal(result);
230}
231function Xor( s, a ) {
232    s = ToInt32( s );
233    a = ToInt32( a );
234
235    var bs = ToInt32BitString( s );
236    var ba = ToInt32BitString( a );
237
238    var result = "";
239
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")
243           ) {
244            result += "1";
245        } else {
246            result += "0";
247        }
248    }
249
250    return ToInt32Decimal(result);
251}
252function Or( s, a ) {
253    s = ToInt32( s );
254    a = ToInt32( a );
255
256    var bs = ToInt32BitString( s );
257    var ba = ToInt32BitString( a );
258
259    var result = "";
260
261    for ( var bit = 0; bit < bs.length; bit++ ) {
262        if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
263            result += "1";
264        } else {
265            result += "0";
266        }
267    }
268
269    return ToInt32Decimal(result);
270}
271