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