~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/lang/js/test/ecma/Expressions/11.7.3.js

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
   File Name:          11.7.3.js
 
41
   ECMA Section:       11.7.3  The unsigned right shift operator ( >>> )
 
42
   Description:
 
43
   11.7.3 The unsigned right shift operator ( >>> )
 
44
   Performs a zero-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 ToUint32(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 zero-filling right shift of Result(5) by Result(7) bits.
 
59
   Vacated bits are filled with zero. The result is an unsigned 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.3";
 
67
var VERSION = "ECMA_1";
 
68
startTest();
 
69
 
 
70
writeHeaderToLog( SECTION + "  The unsigned right shift operator ( >>> )");
 
71
 
 
72
var addexp = 0;
 
73
var power = 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
                  UnsignedRightShift( shiftexp, addexp ),
 
82
                  shiftexp >>> addexp );
 
83
  }
 
84
}
 
85
 
 
86
test();
 
87
 
 
88
 
 
89
function ToInteger( n ) {
 
90
  n = Number( n );
 
91
  var sign = ( n < 0 ) ? -1 : 1;
 
92
 
 
93
  if ( n != n ) {
 
94
    return 0;
 
95
  }
 
96
  if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY ) {
 
97
    return n;
 
98
  }
 
99
  return ( sign * Math.floor(Math.abs(n)) );
 
100
}
 
101
function ToInt32( n ) {
 
102
  n = Number( n );
 
103
  var sign = ( n < 0 ) ? -1 : 1;
 
104
 
 
105
  if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
 
106
    return 0;
 
107
  }
 
108
 
 
109
  n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
 
110
  n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
 
111
 
 
112
  return ( n );
 
113
}
 
114
function ToUint32( n ) {
 
115
  n = Number( n );
 
116
  var sign = ( n < 0 ) ? -1 : 1;
 
117
 
 
118
  if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
 
119
    return 0;
 
120
  }
 
121
  n = sign * Math.floor( Math.abs(n) )
 
122
 
 
123
    n = n % Math.pow(2,32);
 
124
 
 
125
  if ( n < 0 ){
 
126
    n += Math.pow(2,32);
 
127
  }
 
128
 
 
129
  return ( n );
 
130
}
 
131
function ToUint16( n ) {
 
132
  var sign = ( n < 0 ) ? -1 : 1;
 
133
 
 
134
  if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
 
135
    return 0;
 
136
  }
 
137
 
 
138
  n = ( sign * Math.floor( Math.abs(n) ) ) % Math.pow(2,16);
 
139
 
 
140
  if (n <0) {
 
141
    n += Math.pow(2,16);
 
142
  }
 
143
 
 
144
  return ( n );
 
145
}
 
146
function Mask( b, n ) {
 
147
  b = ToUint32BitString( b );
 
148
  b = b.substring( b.length - n );
 
149
  b = ToUint32Decimal( b );
 
150
  return ( b );
 
151
}
 
152
function ToUint32BitString( n ) {
 
153
  var b = "";
 
154
  for ( p = 31; p >=0; p-- ) {
 
155
    if ( n >= Math.pow(2,p) ) {
 
156
      b += "1";
 
157
      n -= Math.pow(2,p);
 
158
    } else {
 
159
      b += "0";
 
160
    }
 
161
  }
 
162
  return b;
 
163
}
 
164
function ToInt32BitString( n ) {
 
165
  var b = "";
 
166
  var sign = ( n < 0 ) ? -1 : 1;
 
167
 
 
168
  b += ( sign == 1 ) ? "0" : "1";
 
169
 
 
170
  for ( p = 30; p >=0; p-- ) {
 
171
    if ( (sign == 1 ) ? sign * n >= Math.pow(2,p) : sign * n > Math.pow(2,p) ) {
 
172
      b += ( sign == 1 ) ? "1" : "0";
 
173
      n -= sign * Math.pow( 2, p );
 
174
    } else {
 
175
      b += ( sign == 1 ) ? "0" : "1";
 
176
    }
 
177
  }
 
178
 
 
179
  return b;
 
180
}
 
181
function ToInt32Decimal( bin ) {
 
182
  var r = 0;
 
183
  var sign;
 
184
 
 
185
  if ( Number(bin.charAt(0)) == 0 ) {
 
186
    sign = 1;
 
187
    r = 0;
 
188
  } else {
 
189
    sign = -1;
 
190
    r = -(Math.pow(2,31));
 
191
  }
 
192
 
 
193
  for ( var j = 0; j < 31; j++ ) {
 
194
    r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
 
195
  }
 
196
 
 
197
  return r;
 
198
}
 
199
function ToUint32Decimal( bin ) {
 
200
  var r = 0;
 
201
 
 
202
 
 
203
  for ( l = bin.length; l < 32; l++ ) {
 
204
    bin = "0" + bin;
 
205
  }
 
206
 
 
207
  for ( j = 0; j < 32; j++ ) {
 
208
    r += Math.pow( 2, j ) * Number(bin.charAt(31-j));
 
209
 
 
210
  }
 
211
 
 
212
  return r;
 
213
}
 
214
function RShift( s, a ) {
 
215
  s = ToUint32BitString( s );
 
216
  for ( z = 0; z < a; z++ ) {
 
217
    s = "0" + s;
 
218
  }
 
219
  s = s.substring( 0, s.length - a );
 
220
 
 
221
  return ToUint32Decimal(s);
 
222
}
 
223
function UnsignedRightShift( s, a ) {
 
224
  s = ToUint32( s );
 
225
  a = ToUint32( a );
 
226
  a = Mask( a, 5 );
 
227
  return ( RShift( s, a ) );
 
228
}