Debian Codegraph @

rhino/testsrc/tests/ecma/Expressions/11.10-1.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = '11.10-1.js';
7
8 /**
9 File Name: 11.10-1.js
10 ECMA Section: 11.10-1 Binary Bitwise Operators: &
11 Description:
12 Semantics
13
14 The production A : A @ B, where @ is one of the bitwise operators in the
15 productions &, ^, | , is evaluated as follows:
16
17 1. Evaluate A.
18 2. Call GetValue(Result(1)).
19 3. Evaluate B.
20 4. Call GetValue(Result(3)).
21 5. Call ToInt32(Result(2)).
22 6. Call ToInt32(Result(4)).
23 7. Apply the bitwise operator @ to Result(5) and Result(6). The result is
24 a signed 32 bit integer.
25 8. Return Result(7).
26
27 Author: [email protected]
28 Date: 12 november 1997
29 */
30 var SECTION = "11.10-1";
31 var VERSION = "ECMA_1";
32 startTest();
33
34 writeHeaderToLog( SECTION + " Binary Bitwise Operators: &");
35
36 var shiftexp = 0;
37 var addexp = 0;
38
39 // for ( shiftpow = 0; shiftpow < 33; shiftpow++ ) {
40 for ( shiftpow = 0; shiftpow < 1; shiftpow++ ) {
41 shiftexp += Math.pow( 2, shiftpow );
42
43 for ( addpow = 0; addpow < 33; addpow++ ) {
44 addexp += Math.pow(2, addpow);
45
46 new TestCase( SECTION,
47 shiftexp + " & " + addexp,
48 And( shiftexp, addexp ),
49 shiftexp & addexp );
50 }
51 }
52
53 test();
54
55 function ToInteger( n ) {
56 n = Number( n );
57 var sign = ( n < 0 ) ? -1 : 1;
58
59 if ( n != n ) {
60 return 0;
61 }
62 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
63 return n;
64 }
65 return ( sign * Math.floor(Math.abs(n)) );
66 }
67 function ToInt32( n ) {
68 n = Number( n );
69 var sign = ( n < 0 ) ? -1 : 1;
70
71 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
72 return 0;
73 }
74
75 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
76 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
77
78 return ( n );
79 }
80 function ToUint32( n ) {
81 n = Number( n );
82 var sign = ( n < 0 ) ? -1 : 1;
83
84 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
85 return 0;
86 }
87 n = sign * Math.floor( Math.abs(n) )
88
89 n = n % Math.pow(2,32);
90
91 if ( n < 0 ){
92 n += Math.pow(2,32);
93 }
94
95 return ( n );
96 }
97 function ToUint16( n ) {
98 var sign = ( n < 0 ) ? -1 : 1;
99
100 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
101 return 0;
102 }
103
104 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
105
106 if (n <0) {
107 n += Math.pow(2,16);
108 }
109
110 return ( n );
111 }
112 function Mask( b, n ) {
113 b = ToUint32BitString( b );
114 b = b.substring( b.length - n );
115 b = ToUint32Decimal( b );
116 return ( b );
117 }
118 function ToUint32BitString( n ) {
119 var b = "";
120 for ( p = 31; p >=0; p-- ) {
121 if ( n >= Math.pow(2,p) ) {
122 b += "1";
123 n -= Math.pow(2,p);
124 } else {
125 b += "0";
126 }
127 }
128 return b;
129 }
130 function ToInt32BitString( n ) {
131 var b = "";
132 var sign = ( n < 0 ) ? -1 : 1;
133
134 b += ( sign == 1 ) ? "0" : "1";
135
136 for ( p = 30; p >=0; p-- ) {
137 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
138 b += ( sign == 1 ) ? "1" : "0";
139 n -= sign * Math.pow( 2, p );
140 } else {
141 b += ( sign == 1 ) ? "0" : "1";
142 }
143 }
144
145 return b;
146 }
147 function ToInt32Decimal( bin ) {
148 var r = 0;
149 var sign;
150
151 if ( Number(bin.charAt(0)) == 0 ) {
152 sign = 1;
153 r = 0;
154 } else {
155 sign = -1;
156 r = -(Math.pow(2,31));
157 }
158
159 for ( var j = 0; j < 31; j++ ) {
160 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
161 }
162
163 return r;
164 }
165 function ToUint32Decimal( bin ) {
166 var r = 0;
167
168
169 for ( l = bin.length; l < 32; l++ ) {
170 bin = "0" + bin;
171 }
172
173 for ( j = 0; j < 31; j++ ) {
174 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
175
176 }
177
178 return r;
179 }
180 function And( s, a ) {
181 s = ToInt32( s );
182 a = ToInt32( a );
183
184 var bs = ToInt32BitString( s );
185 var ba = ToInt32BitString( a );
186
187 var result = "";
188
189 for ( var bit = 0; bit < bs.length; bit++ ) {
190 if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
191 result += "1";
192 } else {
193 result += "0";
194 }
195 }
196 return ToInt32Decimal(result);
197 }
198 function Xor( s, a ) {
199 s = ToInt32( s );
200 a = ToInt32( a );
201
202 var bs = ToInt32BitString( s );
203 var ba = ToInt32BitString( a );
204
205 var result = "";
206
207 for ( var bit = 0; bit < bs.length; bit++ ) {
208 if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") ||
209 (bs.charAt(bit) == "0" && ba.charAt(bit) == "1")
210 ) {
211 result += "1";
212 } else {
213 result += "0";
214 }
215 }
216
217 return ToInt32Decimal(result);
218 }
219 function Or( s, a ) {
220 s = ToInt32( s );
221 a = ToInt32( a );
222
223 var bs = ToInt32BitString( s );
224 var ba = ToInt32BitString( a );
225
226 var result = "";
227
228 for ( var bit = 0; bit < bs.length; bit++ ) {
229 if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
230 result += "1";
231 } else {
232 result += "0";
233 }
234 }
235
236 return ToInt32Decimal(result);
237 }