Debian Codegraph @

rhino/testsrc/tests/ecma/Expressions/11.10-3.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-3.js';
7
8 /**
9 File Name: 11.10-3.js
10 ECMA Section: 11.10-3 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-3";
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 shiftexp += Math.pow( 2, shiftpow );
41
42 for ( addpow = 0; addpow < 33; addpow++ ) {
43 addexp += Math.pow(2, addpow);
44
45 new TestCase( SECTION,
46 shiftexp + " ^ " + addexp,
47 Xor( shiftexp, addexp ),
48 shiftexp ^ addexp );
49 }
50 }
51
52 test();
53
54 function ToInteger( n ) {
55 n = Number( n );
56 var sign = ( n < 0 ) ? -1 : 1;
57
58 if ( n != n ) {
59 return 0;
60 }
61 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
62 return n;
63 }
64 return ( sign * Math.floor(Math.abs(n)) );
65 }
66 function ToInt32( n ) {
67 n = Number( n );
68 var sign = ( n < 0 ) ? -1 : 1;
69
70 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
71 return 0;
72 }
73
74 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
75 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
76
77 return ( n );
78 }
79 function ToUint32( n ) {
80 n = Number( n );
81 var sign = ( n < 0 ) ? -1 : 1;
82
83 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
84 return 0;
85 }
86 n = sign * Math.floor( Math.abs(n) )
87
88 n = n % Math.pow(2,32);
89
90 if ( n < 0 ){
91 n += Math.pow(2,32);
92 }
93
94 return ( n );
95 }
96 function ToUint16( n ) {
97 var sign = ( n < 0 ) ? -1 : 1;
98
99 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
100 return 0;
101 }
102
103 n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
104
105 if (n <0) {
106 n += Math.pow(2,16);
107 }
108
109 return ( n );
110 }
111 function Mask( b, n ) {
112 b = ToUint32BitString( b );
113 b = b.substring( b.length - n );
114 b = ToUint32Decimal( b );
115 return ( b );
116 }
117 function ToUint32BitString( n ) {
118 var b = "";
119 for ( p = 31; p >=0; p-- ) {
120 if ( n >= Math.pow(2,p) ) {
121 b += "1";
122 n -= Math.pow(2,p);
123 } else {
124 b += "0";
125 }
126 }
127 return b;
128 }
129 function ToInt32BitString( n ) {
130 var b = "";
131 var sign = ( n < 0 ) ? -1 : 1;
132
133 b += ( sign == 1 ) ? "0" : "1";
134
135 for ( p = 30; p >=0; p-- ) {
136 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
137 b += ( sign == 1 ) ? "1" : "0";
138 n -= sign * Math.pow( 2, p );
139 } else {
140 b += ( sign == 1 ) ? "0" : "1";
141 }
142 }
143
144 return b;
145 }
146 function ToInt32Decimal( bin ) {
147 var r = 0;
148 var sign;
149
150 if ( Number(bin.charAt(0)) == 0 ) {
151 sign = 1;
152 r = 0;
153 } else {
154 sign = -1;
155 r = -(Math.pow(2,31));
156 }
157
158 for ( var j = 0; j < 31; j++ ) {
159 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
160 }
161
162 return r;
163 }
164 function ToUint32Decimal( bin ) {
165 var r = 0;
166
167 for ( l = bin.length; l < 32; l++ ) {
168 bin = "0" + bin;
169 }
170
171 for ( j = 0; j < 31; j++ ) {
172 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
173
174 }
175
176 return r;
177 }
178 function And( s, a ) {
179 s = ToInt32( s );
180 a = ToInt32( a );
181
182 var bs = ToInt32BitString( s );
183 var ba = ToInt32BitString( a );
184
185 var result = "";
186
187 for ( var bit = 0; bit < bs.length; bit++ ) {
188 if ( bs.charAt(bit) == "1" && ba.charAt(bit) == "1" ) {
189 result += "1";
190 } else {
191 result += "0";
192 }
193 }
194 return ToInt32Decimal(result);
195 }
196 function Xor( s, a ) {
197 s = ToInt32( s );
198 a = ToInt32( a );
199
200 var bs = ToInt32BitString( s );
201 var ba = ToInt32BitString( a );
202
203 var result = "";
204
205 for ( var bit = 0; bit < bs.length; bit++ ) {
206 if ( (bs.charAt(bit) == "1" && ba.charAt(bit) == "0") ||
207 (bs.charAt(bit) == "0" && ba.charAt(bit) == "1")
208 ) {
209 result += "1";
210 } else {
211 result += "0";
212 }
213 }
214
215 return ToInt32Decimal(result);
216 }
217 function Or( s, a ) {
218 s = ToInt32( s );
219 a = ToInt32( a );
220
221 var bs = ToInt32BitString( s );
222 var ba = ToInt32BitString( a );
223
224 var result = "";
225
226 for ( var bit = 0; bit < bs.length; bit++ ) {
227 if ( bs.charAt(bit) == "1" || ba.charAt(bit) == "1" ) {
228 result += "1";
229 } else {
230 result += "0";
231 }
232 }
233
234 return ToInt32Decimal(result);
235 }