Debian Codegraph @

rhino/testsrc/tests/ecma/Expressions/11.7.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.7.3.js';
7
8 /**
9 File Name: 11.7.3.js
10 ECMA Section: 11.7.3 The unsigned right shift operator ( >>> )
11 Description:
12 11.7.3 The unsigned right shift operator ( >>> )
13 Performs a zero-filling bitwise right shift operation on the left argument
14 by the amount specified by the right argument.
15
16 The production ShiftExpression : ShiftExpression >>> AdditiveExpression is
17 evaluated as follows:
18
19 1. Evaluate ShiftExpression.
20 2. Call GetValue(Result(1)).
21 3. Evaluate AdditiveExpression.
22 4. Call GetValue(Result(3)).
23 5. Call ToUint32(Result(2)).
24 6. Call ToUint32(Result(4)).
25 7. Mask out all but the least significant 5 bits of Result(6), that is,
26 compute Result(6) & 0x1F.
27 8. Perform zero-filling right shift of Result(5) by Result(7) bits.
28 Vacated bits are filled with zero. The result is an unsigned 32 bit
29 integer.
30 9. Return Result(8).
31
32 Author: [email protected]
33 Date: 12 november 1997
34 */
35 var SECTION = "11.7.3";
36 var VERSION = "ECMA_1";
37 startTest();
38
39 writeHeaderToLog( SECTION + " The unsigned right shift operator ( >>> )");
40
41 var addexp = 0;
42 var power = 0;
43
44 for ( power = 0; power <= 32; power++ ) {
45 shiftexp = Math.pow( 2, power );
46
47 for ( addexp = 0; addexp <= 32; addexp++ ) {
48 new TestCase( SECTION,
49 shiftexp + " >>> " + addexp,
50 UnsignedRightShift( shiftexp, addexp ),
51 shiftexp >>> addexp );
52 }
53 }
54
55 test();
56
57
58 function ToInteger( n ) {
59 n = Number( n );
60 var sign = ( n < 0 ) ? -1 : 1;
61
62 if ( n != n ) {
63 return 0;
64 }
65 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
66 return n;
67 }
68 return ( sign * Math.floor(Math.abs(n)) );
69 }
70 function ToInt32( n ) {
71 n = Number( n );
72 var sign = ( n < 0 ) ? -1 : 1;
73
74 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
75 return 0;
76 }
77
78 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
79 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
80
81 return ( n );
82 }
83 function ToUint32( n ) {
84 n = Number( n );
85 var sign = ( n < 0 ) ? -1 : 1;
86
87 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
88 return 0;
89 }
90 n = sign * Math.floor( Math.abs(n) )
91
92 n = n % Math.pow(2,32);
93
94 if ( n < 0 ){
95 n += Math.pow(2,32);
96 }
97
98 return ( n );
99 }
100 function ToUint16( 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,16);
108
109 if (n <0) {
110 n += Math.pow(2,16);
111 }
112
113 return ( n );
114 }
115 function Mask( b, n ) {
116 b = ToUint32BitString( b );
117 b = b.substring( b.length - n );
118 b = ToUint32Decimal( b );
119 return ( b );
120 }
121 function ToUint32BitString( n ) {
122 var b = "";
123 for ( p = 31; p >=0; p-- ) {
124 if ( n >= Math.pow(2,p) ) {
125 b += "1";
126 n -= Math.pow(2,p);
127 } else {
128 b += "0";
129 }
130 }
131 return b;
132 }
133 function ToInt32BitString( n ) {
134 var b = "";
135 var sign = ( n < 0 ) ? -1 : 1;
136
137 b += ( sign == 1 ) ? "0" : "1";
138
139 for ( p = 30; p >=0; p-- ) {
140 if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
141 b += ( sign == 1 ) ? "1" : "0";
142 n -= sign * Math.pow( 2, p );
143 } else {
144 b += ( sign == 1 ) ? "0" : "1";
145 }
146 }
147
148 return b;
149 }
150 function ToInt32Decimal( bin ) {
151 var r = 0;
152 var sign;
153
154 if ( Number(bin.charAt(0)) == 0 ) {
155 sign = 1;
156 r = 0;
157 } else {
158 sign = -1;
159 r = -(Math.pow(2,31));
160 }
161
162 for ( var j = 0; j < 31; j++ ) {
163 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
164 }
165
166 return r;
167 }
168 function ToUint32Decimal( bin ) {
169 var r = 0;
170
171
172 for ( l = bin.length; l < 32; l++ ) {
173 bin = "0" + bin;
174 }
175
176 for ( j = 0; j < 32; j++ ) {
177 r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
178
179 }
180
181 return r;
182 }
183 function RShift( s, a ) {
184 s = ToUint32BitString( s );
185 for ( z = 0; z < a; z++ ) {
186 s = "0" + s;
187 }
188 s = s.substring( 0, s.length - a );
189
190 return ToUint32Decimal(s);
191 }
192 function UnsignedRightShift( s, a ) {
193 s = ToUint32( s );
194 a = ToUint32( a );
195 a = Mask( a, 5 );
196 return ( RShift( s, a ) );
197 }