Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Expressions / 11.7.2.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is Mozilla Communicator client code, released
16  * March 31, 1998.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39
40 /**
41    File Name:          11.7.2.js
42    ECMA Section:       11.7.2  The signed right shift operator ( >> )
43    Description:
44    Performs a sign-filling bitwise right shift operation on the left argument
45    by the amount specified by the right argument.
46
47    The production ShiftExpression : ShiftExpression >> AdditiveExpression is
48    evaluated as follows:
49
50    1.  Evaluate ShiftExpression.
51    2.  Call GetValue(Result(1)).
52    3.  Evaluate AdditiveExpression.
53    4.  Call GetValue(Result(3)).
54    5.  Call ToInt32(Result(2)).
55    6.  Call ToUint32(Result(4)).
56    7.  Mask out all but the least significant 5 bits of Result(6), that is,
57    compute Result(6) & 0x1F.
58    8.  Perform sign-extending right shift of Result(5) by Result(7) bits. The
59    most significant bit is propagated. The result is a signed 32 bit
60    integer.
61    9.  Return Result(8).
62
63    Author:             christine@netscape.com
64    Date:               12 november 1997
65 */
66 var SECTION = "11.7.2";
67 var VERSION = "ECMA_1";
68 startTest();
69
70 writeHeaderToLog( SECTION + "  The signed right shift operator ( >> )");
71
72 var power = 0;
73 var addexp = 0;
74
75 for ( power = 0; power <= 32; power++ ) {
76   shiftexp = Math.pow( 2, power );
77
78   for ( addexp = 0; addexp <= 32; addexp++ ) {
79     new TestCase( SECTION,
80                   shiftexp + " >> " + addexp,
81                   SignedRightShift( shiftexp, addexp ),
82                   shiftexp >> addexp );
83   }
84 }
85
86 for ( power = 0; power <= 32; power++ ) {
87   shiftexp = -Math.pow( 2, power );
88
89   for ( addexp = 0; addexp <= 32; addexp++ ) {
90     new TestCase( SECTION,
91                   shiftexp + " >> " + addexp,
92                   SignedRightShift( shiftexp, addexp ),
93                   shiftexp >> addexp );
94   }
95 }
96
97 test();
98
99 function ToInteger( n ) {
100   n = Number( n );
101   var sign = ( n < 0 ) ? -1 : 1;
102
103   if ( n != n ) {
104     return 0;
105   }
106   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
107     return n;
108   }
109   return ( sign * Math.floor(Math.abs(n)) );
110 }
111 function ToInt32( n ) {
112   n = Number( n );
113   var sign = ( n < 0 ) ? -1 : 1;
114
115   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
116     return 0;
117   }
118
119   n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
120   n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
121
122   return ( n );
123 }
124 function ToUint32( n ) {
125   n = Number( n );
126   var sign = ( n < 0 ) ? -1 : 1;
127
128   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
129     return 0;
130   }
131   n = sign * Math.floor( Math.abs(n) )
132
133     n = n % Math.pow(2,32);
134
135   if ( n < 0 ){
136     n += Math.pow(2,32);
137   }
138
139   return ( n );
140 }
141 function ToUint16( n ) {
142   var sign = ( n < 0 ) ? -1 : 1;
143
144   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
145     return 0;
146   }
147
148   n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
149
150   if (n <0) {
151     n += Math.pow(2,16);
152   }
153
154   return ( n );
155 }
156 function Mask( b, n ) {
157   b = ToUint32BitString( b );
158   b = b.substring( b.length - n );
159   b = ToUint32Decimal( b );
160   return ( b );
161 }
162 function ToUint32BitString( n ) {
163   var b = "";
164   for ( p = 31; p >=0; p-- ) {
165     if ( n >= Math.pow(2,p) ) {
166       b += "1";
167       n -= Math.pow(2,p);
168     } else {
169       b += "0";
170     }
171   }
172   return b;
173 }
174 function ToInt32BitString( n ) {
175   var b = "";
176   var sign = ( n < 0 ) ? -1 : 1;
177
178   b += ( sign == 1 ) ? "0" : "1";
179
180   for ( p = 30; p >=0; p-- ) {
181     if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
182       b += ( sign == 1 ) ? "1" : "0";
183       n -= sign * Math.pow( 2, p );
184     } else {
185       b += ( sign == 1 ) ? "0" : "1";
186     }
187   }
188
189   return b;
190 }
191 function ToInt32Decimal( bin ) {
192   var r = 0;
193   var sign;
194
195   if ( Number(bin.charAt(0)) == 0 ) {
196     sign = 1;
197     r = 0;
198   } else {
199     sign = -1;
200     r = -(Math.pow(2,31));
201   }
202
203   for ( var j = 0; j < 31; j++ ) {
204     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
205   }
206
207   return r;
208 }
209 function ToUint32Decimal( bin ) {
210   var r = 0;
211
212   for ( l = bin.length; l < 32; l++ ) {
213     bin = "0" + bin;
214   }
215
216   for ( j = 0; j < 31; j++ ) {
217     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
218   }
219
220   return r;
221 }
222 function SignedRightShift( s, a ) {
223   s = ToInt32( s );
224   a = ToUint32( a );
225   a = Mask( a, 5 );
226   return ( SignedRShift( s, a ) );
227 }
228 function SignedRShift( s, a ) {
229   s = ToInt32BitString( s );
230
231   var firstbit = s.substring(0,1);
232
233   s = s.substring( 1, s.length );
234
235   for ( var z = 0; z < a; z++ ) {
236     s = firstbit + s;
237   }
238
239   s = s.substring( 0, s.length - a);
240
241   s = firstbit +s;
242
243
244   return ToInt32(ToInt32Decimal(s));
245 }