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
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/
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
15 * The Original Code is Mozilla Communicator client code, released
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.
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.
37 * ***** END LICENSE BLOCK ***** */
43 ECMA Section: 9.6 Type Conversion: ToUint32
44 Description: rules for converting an argument to an unsigned
47 this test uses >>> 0 to convert the argument to
48 an unsigned 32bit integer.
50 1 call ToNumber on argument
51 2 if result is NaN, 0, -0, Infinity, -Infinity
53 3 compute (sign (result(1)) * floor(abs(result 1)))
54 4 compute result(3) modulo 2^32:
62 ToInt32(ToUint32(x)) == ToInt32(x) for all values of x
63 ** NEED TO DO THIS PART IN A SEPARATE TEST FILE **
66 Author: christine@netscape.com
71 var VERSION = "ECMA_1";
74 writeHeaderToLog( SECTION + " Type Conversion: ToUint32");
76 new TestCase( SECTION, "0 >>> 0", 0, 0 >>> 0 );
77 // new TestCase( SECTION, "+0 >>> 0", 0, +0 >>> 0);
78 new TestCase( SECTION, "-0 >>> 0", 0, -0 >>> 0 );
79 new TestCase( SECTION, "'Infinity' >>> 0", 0, "Infinity" >>> 0 );
80 new TestCase( SECTION, "'-Infinity' >>> 0", 0, "-Infinity" >>> 0);
81 new TestCase( SECTION, "'+Infinity' >>> 0", 0, "+Infinity" >>> 0 );
82 new TestCase( SECTION, "Number.POSITIVE_INFINITY >>> 0", 0, Number.POSITIVE_INFINITY >>> 0 );
83 new TestCase( SECTION, "Number.NEGATIVE_INFINITY >>> 0", 0, Number.NEGATIVE_INFINITY >>> 0 );
84 new TestCase( SECTION, "Number.NaN >>> 0", 0, Number.NaN >>> 0 );
86 new TestCase( SECTION, "Number.MIN_VALUE >>> 0", 0, Number.MIN_VALUE >>> 0 );
87 new TestCase( SECTION, "-Number.MIN_VALUE >>> 0", 0, Number.MIN_VALUE >>> 0 );
88 new TestCase( SECTION, "0.1 >>> 0", 0, 0.1 >>> 0 );
89 new TestCase( SECTION, "-0.1 >>> 0", 0, -0.1 >>> 0 );
90 new TestCase( SECTION, "1 >>> 0", 1, 1 >>> 0 );
91 new TestCase( SECTION, "1.1 >>> 0", 1, 1.1 >>> 0 );
93 new TestCase( SECTION, "-1.1 >>> 0", ToUint32(-1.1), -1.1 >>> 0 );
94 new TestCase( SECTION, "-1 >>> 0", ToUint32(-1), -1 >>> 0 );
96 new TestCase( SECTION, "2147483647 >>> 0", ToUint32(2147483647), 2147483647 >>> 0 );
97 new TestCase( SECTION, "2147483648 >>> 0", ToUint32(2147483648), 2147483648 >>> 0 );
98 new TestCase( SECTION, "2147483649 >>> 0", ToUint32(2147483649), 2147483649 >>> 0 );
100 new TestCase( SECTION, "4294967295 >>> 0", ToUint32(4294967295), 4294967295 >>> 0 );
101 new TestCase( SECTION, "4294967296 >>> 0", ToUint32(4294967296), 4294967296 >>> 0 );
102 new TestCase( SECTION, "4294967297 >>> 0", ToUint32(4294967297), 4294967297 >>> 0 );
104 new TestCase( SECTION, "-2147483647 >>> 0", ToUint32(-2147483647), -2147483647 >>> 0 );
105 new TestCase( SECTION, "-2147483648 >>> 0", ToUint32(-2147483648), -2147483648 >>> 0 );
106 new TestCase( SECTION, "-2147483649 >>> 0", ToUint32(-2147483649), -2147483649 >>> 0 );
108 new TestCase( SECTION, "-4294967295 >>> 0", ToUint32(-4294967295), -4294967295 >>> 0 );
109 new TestCase( SECTION, "-4294967296 >>> 0", ToUint32(-4294967296), -4294967296 >>> 0 );
110 new TestCase( SECTION, "-4294967297 >>> 0", ToUint32(-4294967297), -4294967297 >>> 0 );
112 new TestCase( SECTION, "'2147483647' >>> 0", ToUint32(2147483647), '2147483647' >>> 0 );
113 new TestCase( SECTION, "'2147483648' >>> 0", ToUint32(2147483648), '2147483648' >>> 0 );
114 new TestCase( SECTION, "'2147483649' >>> 0", ToUint32(2147483649), '2147483649' >>> 0 );
116 new TestCase( SECTION, "'4294967295' >>> 0", ToUint32(4294967295), '4294967295' >>> 0 );
117 new TestCase( SECTION, "'4294967296' >>> 0", ToUint32(4294967296), '4294967296' >>> 0 );
118 new TestCase( SECTION, "'4294967297' >>> 0", ToUint32(4294967297), '4294967297' >>> 0 );
123 function ToUint32( n ) {
125 var sign = ( n < 0 ) ? -1 : 1;
127 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
130 n = sign * Math.floor( Math.abs(n) )
132 n = n % Math.pow(2,32);