Debian Codegraph @

rhino/testsrc/tests/ecma/TypeConversion/9.6.js

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 gTestfile = '9.6.js';
7
8 /**
9 File Name: 9.6.js
10 ECMA Section: 9.6 Type Conversion: ToUint32
11 Description: rules for converting an argument to an unsigned
12 32 bit integer
13
14 this test uses >>> 0 to convert the argument to
15 an unsigned 32bit integer.
16
17 1 call ToNumber on argument
18 2 if result is NaN, 0, -0, Infinity, -Infinity
19 return 0
20 3 compute (sign (result(1)) * floor(abs(result 1)))
21 4 compute result(3) modulo 2^32:
22 5 return result(4)
23
24 special cases:
25 -0 returns 0
26 Infinity returns 0
27 -Infinity returns 0
28 0 returns 0
29 ToInt32(ToUint32(x)) == ToInt32(x) for all values of x
30 ** NEED TO DO THIS PART IN A SEPARATE TEST FILE **
31
32
33 Author: [email protected]
34 Date: 17 july 1997
35 */
36
37 var SECTION = "9.6";
38 var VERSION = "ECMA_1";
39 startTest();
40
41 writeHeaderToLog( SECTION + " Type Conversion: ToUint32");
42
43 new TestCase( SECTION, "0 >>> 0", 0, 0 >>> 0 );
44 // new TestCase( SECTION, "+0 >>> 0", 0, +0 >>> 0);
45 new TestCase( SECTION, "-0 >>> 0", 0, -0 >>> 0 );
46 new TestCase( SECTION, "'Infinity' >>> 0", 0, "Infinity" >>> 0 );
47 new TestCase( SECTION, "'-Infinity' >>> 0", 0, "-Infinity" >>> 0);
48 new TestCase( SECTION, "'+Infinity' >>> 0", 0, "+Infinity" >>> 0 );
49 new TestCase( SECTION, "Number.POSITIVE_INFINITY >>> 0", 0, Number.POSITIVE_INFINITY >>> 0 );
50 new TestCase( SECTION, "Number.NEGATIVE_INFINITY >>> 0", 0, Number.NEGATIVE_INFINITY >>> 0 );
51 new TestCase( SECTION, "Number.NaN >>> 0", 0, Number.NaN >>> 0 );
52
53 new TestCase( SECTION, "Number.MIN_VALUE >>> 0", 0, Number.MIN_VALUE >>> 0 );
54 new TestCase( SECTION, "-Number.MIN_VALUE >>> 0", 0, Number.MIN_VALUE >>> 0 );
55 new TestCase( SECTION, "0.1 >>> 0", 0, 0.1 >>> 0 );
56 new TestCase( SECTION, "-0.1 >>> 0", 0, -0.1 >>> 0 );
57 new TestCase( SECTION, "1 >>> 0", 1, 1 >>> 0 );
58 new TestCase( SECTION, "1.1 >>> 0", 1, 1.1 >>> 0 );
59
60 new TestCase( SECTION, "-1.1 >>> 0", ToUint32(-1.1), -1.1 >>> 0 );
61 new TestCase( SECTION, "-1 >>> 0", ToUint32(-1), -1 >>> 0 );
62
63 new TestCase( SECTION, "2147483647 >>> 0", ToUint32(2147483647), 2147483647 >>> 0 );
64 new TestCase( SECTION, "2147483648 >>> 0", ToUint32(2147483648), 2147483648 >>> 0 );
65 new TestCase( SECTION, "2147483649 >>> 0", ToUint32(2147483649), 2147483649 >>> 0 );
66
67 new TestCase( SECTION, "4294967295 >>> 0", ToUint32(4294967295), 4294967295 >>> 0 );
68 new TestCase( SECTION, "4294967296 >>> 0", ToUint32(4294967296), 4294967296 >>> 0 );
69 new TestCase( SECTION, "4294967297 >>> 0", ToUint32(4294967297), 4294967297 >>> 0 );
70
71 new TestCase( SECTION, "-2147483647 >>> 0", ToUint32(-2147483647), -2147483647 >>> 0 );
72 new TestCase( SECTION, "-2147483648 >>> 0", ToUint32(-2147483648), -2147483648 >>> 0 );
73 new TestCase( SECTION, "-2147483649 >>> 0", ToUint32(-2147483649), -2147483649 >>> 0 );
74
75 new TestCase( SECTION, "-4294967295 >>> 0", ToUint32(-4294967295), -4294967295 >>> 0 );
76 new TestCase( SECTION, "-4294967296 >>> 0", ToUint32(-4294967296), -4294967296 >>> 0 );
77 new TestCase( SECTION, "-4294967297 >>> 0", ToUint32(-4294967297), -4294967297 >>> 0 );
78
79 new TestCase( SECTION, "'2147483647' >>> 0", ToUint32(2147483647), '2147483647' >>> 0 );
80 new TestCase( SECTION, "'2147483648' >>> 0", ToUint32(2147483648), '2147483648' >>> 0 );
81 new TestCase( SECTION, "'2147483649' >>> 0", ToUint32(2147483649), '2147483649' >>> 0 );
82
83 new TestCase( SECTION, "'4294967295' >>> 0", ToUint32(4294967295), '4294967295' >>> 0 );
84 new TestCase( SECTION, "'4294967296' >>> 0", ToUint32(4294967296), '4294967296' >>> 0 );
85 new TestCase( SECTION, "'4294967297' >>> 0", ToUint32(4294967297), '4294967297' >>> 0 );
86
87
88 test();
89
90 function ToUint32( n ) {
91 n = Number( n );
92 var sign = ( n < 0 ) ? -1 : 1;
93
94 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
95 return 0;
96 }
97 n = sign * Math.floor( Math.abs(n) )
98
99 n = n % Math.pow(2,32);
100
101 if ( n < 0 ){
102 n += Math.pow(2,32);
103 }
104
105 return ( n );
106 }
107