Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma / Expressions / 11.7.3.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.3.js
42    ECMA Section:       11.7.3  The unsigned right shift operator ( >>> )
43    Description:
44    11.7.3 The unsigned right shift operator ( >>> )
45    Performs a zero-filling bitwise right shift operation on the left argument
46    by the amount specified by the right argument.
47
48    The production ShiftExpression : ShiftExpression >>> AdditiveExpression is
49    evaluated as follows:
50
51    1.  Evaluate ShiftExpression.
52    2.  Call GetValue(Result(1)).
53    3.  Evaluate AdditiveExpression.
54    4.  Call GetValue(Result(3)).
55    5.  Call ToUint32(Result(2)).
56    6.  Call ToUint32(Result(4)).
57    7.  Mask out all but the least significant 5 bits of Result(6), that is,
58    compute Result(6) & 0x1F.
59    8.  Perform zero-filling right shift of Result(5) by Result(7) bits.
60    Vacated bits are filled with zero. The result is an unsigned 32 bit
61    integer.
62    9.  Return Result(8).
63
64    Author:             christine@netscape.com
65    Date:               12 november 1997
66 */
67 var SECTION = "11.7.3";
68 var VERSION = "ECMA_1";
69 startTest();
70
71 writeHeaderToLog( SECTION + "  The unsigned right shift operator ( >>> )");
72
73 var addexp = 0;
74 var power = 0;
75
76 for ( power = 0; power <= 32; power++ ) {
77   shiftexp = Math.pow( 2, power );
78
79   for ( addexp = 0; addexp <= 32; addexp++ ) {
80     new TestCase( SECTION,
81                   shiftexp + " >>> " + addexp,
82                   UnsignedRightShift( shiftexp, addexp ),
83                   shiftexp >>> addexp );
84   }
85 }
86
87 test();
88
89
90 function ToInteger( n ) {
91   n = Number( n );
92   var sign = ( n < 0 ) ? -1 : 1;
93
94   if ( n != n ) {
95     return 0;
96   }
97   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
98     return n;
99   }
100   return ( sign * Math.floor(Math.abs(n)) );
101 }
102 function ToInt32( n ) {
103   n = Number( n );
104   var sign = ( n < 0 ) ? -1 : 1;
105
106   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
107     return 0;
108   }
109
110   n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
111   n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
112
113   return ( n );
114 }
115 function ToUint32( n ) {
116   n = Number( n );
117   var sign = ( n < 0 ) ? -1 : 1;
118
119   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
120     return 0;
121   }
122   n = sign * Math.floor( Math.abs(n) )
123
124     n = n % Math.pow(2,32);
125
126   if ( n < 0 ){
127     n += Math.pow(2,32);
128   }
129
130   return ( n );
131 }
132 function ToUint16( n ) {
133   var sign = ( n < 0 ) ? -1 : 1;
134
135   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
136     return 0;
137   }
138
139   n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
140
141   if (n <0) {
142     n += Math.pow(2,16);
143   }
144
145   return ( n );
146 }
147 function Mask( b, n ) {
148   b = ToUint32BitString( b );
149   b = b.substring( b.length - n );
150   b = ToUint32Decimal( b );
151   return ( b );
152 }
153 function ToUint32BitString( n ) {
154   var b = "";
155   for ( p = 31; p >=0; p-- ) {
156     if ( n >= Math.pow(2,p) ) {
157       b += "1";
158       n -= Math.pow(2,p);
159     } else {
160       b += "0";
161     }
162   }
163   return b;
164 }
165 function ToInt32BitString( n ) {
166   var b = "";
167   var sign = ( n < 0 ) ? -1 : 1;
168
169   b += ( sign == 1 ) ? "0" : "1";
170
171   for ( p = 30; p >=0; p-- ) {
172     if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
173       b += ( sign == 1 ) ? "1" : "0";
174       n -= sign * Math.pow( 2, p );
175     } else {
176       b += ( sign == 1 ) ? "0" : "1";
177     }
178   }
179
180   return b;
181 }
182 function ToInt32Decimal( bin ) {
183   var r = 0;
184   var sign;
185
186   if ( Number(bin.charAt(0)) == 0 ) {
187     sign = 1;
188     r = 0;
189   } else {
190     sign = -1;
191     r = -(Math.pow(2,31));
192   }
193
194   for ( var j = 0; j < 31; j++ ) {
195     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
196   }
197
198   return r;
199 }
200 function ToUint32Decimal( bin ) {
201   var r = 0;
202
203
204   for ( l = bin.length; l < 32; l++ ) {
205     bin = "0" + bin;
206   }
207
208   for ( j = 0; j < 32; j++ ) {
209     r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
210
211   }
212
213   return r;
214 }
215 function RShift( s, a ) {
216   s = ToUint32BitString( s );
217   for ( z = 0; z < a; z++ ) {
218     s = "0" + s;
219   }
220   s = s.substring( 0, s.length - a );
221
222   return ToUint32Decimal(s);
223 }
224 function UnsignedRightShift( s, a ) {
225   s = ToUint32( s );
226   a = ToUint32( a );
227   a = Mask( a, 5 );
228   return ( RShift( s, a ) );
229 }