initial import
[vuplus_webkit] / Source / JavaScriptCore / tests / mozilla / ecma / Expressions / 11.10-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/
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-2.js
24     ECMA Section:       11.10-2 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-2";
45     var VERSION = "ECMA_1";
46     startTest();
47
48     var testcases = getTestCases();
49
50     writeHeaderToLog( SECTION + " Binary Bitwise Operators:  |");
51     test();
52
53 function 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 }
66 function 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         shiftexp += Math.pow( 2, shiftpow );
74
75         for ( addpow = 0; addpow < 33; addpow++ ) {
76             addexp += Math.pow(2, addpow);
77
78             array[item++] = new TestCase( SECTION,
79                                     shiftexp + " | " + addexp,
80                                     Or( shiftexp, addexp ),
81                                     shiftexp | addexp );
82         }
83     }
84
85     return ( array );
86 }
87 function ToInteger( n ) {
88     n = Number( n );
89     var sign = ( n < 0 ) ? -1 : 1;
90
91     if ( n != n ) {
92         return 0;
93     }
94     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
95         return n;
96     }
97     return ( sign * Math.floor(Math.abs(n)) );
98 }
99 function ToInt32( n ) {
100     n = Number( n );
101     var sign = ( n < 0 ) ? -1 : 1;
102
103     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
104         return 0;
105     }
106
107     n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
108     n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
109
110     return ( n );
111 }
112 function ToUint32( n ) {
113     n = Number( n );
114     var sign = ( n < 0 ) ? -1 : 1;
115
116     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
117         return 0;
118     }
119     n = sign * Math.floor( Math.abs(n) )
120
121     n = n % Math.pow(2,32);
122
123     if ( n < 0 ){
124         n += Math.pow(2,32);
125     }
126
127     return ( n );
128 }
129 function ToUint16( n ) {
130     var sign = ( n < 0 ) ? -1 : 1;
131
132     if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
133         return 0;
134     }
135
136     n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
137
138     if (n <0) {
139         n += Math.pow(2,16);
140     }
141
142     return ( n );
143 }
144 function Mask( b, n ) {
145     b = ToUint32BitString( b );
146     b = b.substring( b.length - n );
147     b = ToUint32Decimal( b );
148     return ( b );
149 }
150 function ToUint32BitString( n ) {
151     var b = "";
152     for ( p = 31; p >=0; p-- ) {
153         if ( n >= Math.pow(2,p) ) {
154             b += "1";
155             n -= Math.pow(2,p);
156         } else {
157             b += "0";
158         }
159     }
160     return b;
161 }
162 function ToInt32BitString( n ) {
163     var b = "";
164     var sign = ( n < 0 ) ? -1 : 1;
165
166     b += ( sign == 1 ) ? "0" : "1";
167
168     for ( p = 30; p >=0; p-- ) {
169         if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
170             b += ( sign == 1 ) ? "1" : "0";
171             n -= sign * Math.pow( 2, p );
172         } else {
173             b += ( sign == 1 ) ? "0" : "1";
174         }
175     }
176
177     return b;
178 }
179 function ToInt32Decimal( bin ) {
180     var r = 0;
181     var sign;
182
183     if ( Number(bin.charAt(0)) == 0 ) {
184         sign = 1;
185         r = 0;
186     } else {
187         sign = -1;
188         r = -(Math.pow(2,31));
189     }
190
191     for ( var j = 0; j < 31; j++ ) {
192         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
193     }
194
195     return r;
196 }
197 function ToUint32Decimal( bin ) {
198     var r = 0;
199
200
201     for ( l = bin.length; l < 32; l++ ) {
202         bin = "0" + bin;
203     }
204
205     for ( j = 0; j < 31; j++ ) {
206         r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
207
208     }
209
210     return r;
211 }
212 function And( s, a ) {
213     s = ToInt32( s );
214     a = ToInt32( a );
215
216     var bs = ToInt32BitString( s );
217     var ba = ToInt32BitString( a );
218
219     var result = "";
220
221     for ( var bit = 0; bit < bs.length; bit++ ) {
222         if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
223             result += "1";
224         } else {
225             result += "0";
226         }
227     }
228     return ToInt32Decimal(result);
229 }
230 function Xor( s, a ) {
231     s = ToInt32( s );
232     a = ToInt32( a );
233
234     var bs = ToInt32BitString( s );
235     var ba = ToInt32BitString( a );
236
237     var result = "";
238
239     for ( var bit = 0; bit < bs.length; bit++ ) {
240         if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") ||
241              (bs.charAt(bit) == "0" && ba.charAt(bit) == "1")
242            ) {
243             result += "1";
244         } else {
245             result += "0";
246         }
247     }
248
249     return ToInt32Decimal(result);
250 }
251 function Or( s, a ) {
252     s = ToInt32( s );
253     a = ToInt32( a );
254
255     var bs = ToInt32BitString( s );
256     var ba = ToInt32BitString( a );
257
258     var result = "";
259
260     for ( var bit = 0; bit < bs.length; bit++ ) {
261         if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
262             result += "1";
263         } else {
264             result += "0";
265         }
266     }
267
268     return ToInt32Decimal(result);
269 }