Debian Codegraph @

rhino/testsrc/tests/ecma/TypeConversion/9.5-2.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.5-2.js';
7
8 /**
9 File Name: 9.5-2.js
10 ECMA Section: 9.5 Type Conversion: ToInt32
11 Description: rules for converting an argument to a signed 32 bit integer
12
13 this test uses << 0 to convert the argument to a 32bit
14 integer.
15
16 The operator ToInt32 converts its argument to one of 2^32
17 integer values in the range -2^31 through 2^31 inclusive.
18 This operator functions as follows:
19
20 1 call ToNumber on argument
21 2 if result is NaN, 0, -0, return 0
22 3 compute (sign (result(1)) * floor(abs(result 1)))
23 4 compute result(3) modulo 2^32:
24 5 if result(4) is greater than or equal to 2^31, return
25 result(5)-2^32. otherwise, return result(5)
26
27 special cases:
28 -0 returns 0
29 Infinity returns 0
30 -Infinity returns 0
31 ToInt32(ToUint32(x)) == ToInt32(x) for all values of x
32 Numbers greater than 2^31 (see step 5 above)
33 (note http://bugzilla.mozilla.org/show_bug.cgi?id=120083)
34
35 Author: [email protected]
36 Date: 17 july 1997
37 */
38 var SECTION = "9.5-2";
39 var VERSION = "ECMA_1";
40 startTest();
41
42 writeHeaderToLog( SECTION + " ToInt32");
43
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, "Number.POSITIVE_INFINITY << 0", 0, Number.POSITIVE_INFINITY << 0 );
49 new TestCase( SECTION, "Number.NEGATIVE_INFINITY << 0", 0, Number.NEGATIVE_INFINITY << 0 );
50 new TestCase( SECTION, "Number.NaN << 0", 0, Number.NaN << 0 );
51
52 new TestCase( SECTION, "Number.MIN_VALUE << 0", 0, Number.MIN_VALUE << 0 );
53 new TestCase( SECTION, "-Number.MIN_VALUE << 0", 0, -Number.MIN_VALUE << 0 );
54 new TestCase( SECTION, "0.1 << 0", 0, 0.1 << 0 );
55 new TestCase( SECTION, "-0.1 << 0", 0, -0.1 << 0 );
56 new TestCase( SECTION, "1 << 0", 1, 1 << 0 );
57 new TestCase( SECTION, "1.1 << 0", 1, 1.1 << 0 );
58 new TestCase( SECTION, "-1 << 0", ToInt32(-1), -1 << 0 );
59
60
61 new TestCase( SECTION, "2147483647 << 0", ToInt32(2147483647), 2147483647 << 0 );
62 new TestCase( SECTION, "2147483648 << 0", ToInt32(2147483648), 2147483648 << 0 );
63 new TestCase( SECTION, "2147483649 << 0", ToInt32(2147483649), 2147483649 << 0 );
64
65 new TestCase( SECTION, "(Math.pow(2,31)-1) << 0", ToInt32(2147483647), (Math.pow(2,31)-1) << 0 );
66 new TestCase( SECTION, "Math.pow(2,31) << 0", ToInt32(2147483648), Math.pow(2,31) << 0 );
67 new TestCase( SECTION, "(Math.pow(2,31)+1) << 0", ToInt32(2147483649), (Math.pow(2,31)+1) << 0 );
68
69 new TestCase( SECTION, "(Math.pow(2,32)-1) << 0", ToInt32(4294967295), (Math.pow(2,32)-1) << 0 );
70 new TestCase( SECTION, "(Math.pow(2,32)) << 0", ToInt32(4294967296), (Math.pow(2,32)) << 0 );
71 new TestCase( SECTION, "(Math.pow(2,32)+1) << 0", ToInt32(4294967297), (Math.pow(2,32)+1) << 0 );
72
73 new TestCase( SECTION, "4294967295 << 0", ToInt32(4294967295), 4294967295 << 0 );
74 new TestCase( SECTION, "4294967296 << 0", ToInt32(4294967296), 4294967296 << 0 );
75 new TestCase( SECTION, "4294967297 << 0", ToInt32(4294967297), 4294967297 << 0 );
76
77 new TestCase( SECTION, "'2147483647' << 0", ToInt32(2147483647), '2147483647' << 0 );
78 new TestCase( SECTION, "'2147483648' << 0", ToInt32(2147483648), '2147483648' << 0 );
79 new TestCase( SECTION, "'2147483649' << 0", ToInt32(2147483649), '2147483649' << 0 );
80
81 new TestCase( SECTION, "'4294967295' << 0", ToInt32(4294967295), '4294967295' << 0 );
82 new TestCase( SECTION, "'4294967296' << 0", ToInt32(4294967296), '4294967296' << 0 );
83 new TestCase( SECTION, "'4294967297' << 0", ToInt32(4294967297), '4294967297' << 0 );
84
85 new TestCase( SECTION, "-2147483647 << 0", ToInt32(-2147483647), -2147483647 << 0 );
86 new TestCase( SECTION, "-2147483648 << 0", ToInt32(-2147483648), -2147483648 << 0 );
87 new TestCase( SECTION, "-2147483649 << 0", ToInt32(-2147483649), -2147483649 << 0 );
88
89 new TestCase( SECTION, "-4294967295 << 0", ToInt32(-4294967295), -4294967295 << 0 );
90 new TestCase( SECTION, "-4294967296 << 0", ToInt32(-4294967296), -4294967296 << 0 );
91 new TestCase( SECTION, "-4294967297 << 0", ToInt32(-4294967297), -4294967297 << 0 );
92
93 /*
94 * Numbers between 2^31 and 2^32 will have a negative ToInt32 per ECMA (see step 5 of introduction)
95 * (These are by [email protected]; cf. http://bugzilla.mozilla.org/show_bug.cgi?id=120083)
96 */
97 new TestCase( SECTION, "2147483648.25 << 0", ToInt32(2147483648.25), 2147483648.25 << 0 );
98 new TestCase( SECTION, "2147483648.5 << 0", ToInt32(2147483648.5), 2147483648.5 << 0 );
99 new TestCase( SECTION, "2147483648.75 << 0", ToInt32(2147483648.75), 2147483648.75 << 0 );
100 new TestCase( SECTION, "4294967295.25 << 0", ToInt32(4294967295.25), 4294967295.25 << 0 );
101 new TestCase( SECTION, "4294967295.5 << 0", ToInt32(4294967295.5), 4294967295.5 << 0 );
102 new TestCase( SECTION, "4294967295.75 << 0", ToInt32(4294967295.75), 4294967295.75 << 0 );
103 new TestCase( SECTION, "3000000000.25 << 0", ToInt32(3000000000.25), 3000000000.25 << 0 );
104 new TestCase( SECTION, "3000000000.5 << 0", ToInt32(3000000000.5), 3000000000.5 << 0 );
105 new TestCase( SECTION, "3000000000.75 << 0", ToInt32(3000000000.75), 3000000000.75 << 0 );
106
107 /*
108 * Numbers between - 2^31 and - 2^32
109 */
110 new TestCase( SECTION, "-2147483648.25 << 0", ToInt32(-2147483648.25), -2147483648.25 << 0 );
111 new TestCase( SECTION, "-2147483648.5 << 0", ToInt32(-2147483648.5), -2147483648.5 << 0 );
112 new TestCase( SECTION, "-2147483648.75 << 0", ToInt32(-2147483648.75), -2147483648.75 << 0 );
113 new TestCase( SECTION, "-4294967295.25 << 0", ToInt32(-4294967295.25), -4294967295.25 << 0 );
114 new TestCase( SECTION, "-4294967295.5 << 0", ToInt32(-4294967295.5), -4294967295.5 << 0 );
115 new TestCase( SECTION, "-4294967295.75 << 0", ToInt32(-4294967295.75), -4294967295.75 << 0 );
116 new TestCase( SECTION, "-3000000000.25 << 0", ToInt32(-3000000000.25), -3000000000.25 << 0 );
117 new TestCase( SECTION, "-3000000000.5 << 0", ToInt32(-3000000000.5), -3000000000.5 << 0 );
118 new TestCase( SECTION, "-3000000000.75 << 0", ToInt32(-3000000000.75), -3000000000.75 << 0 );
119
120
121 test();
122
123 function ToInt32( n ) {
124 n = Number( n );
125 var sign = ( n < 0 ) ? -1 : 1;
126
127 if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
128 return 0;
129 }
130
131 n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
132 if ( sign == -1 ) {
133 n = ( n < -Math.pow(2,31) ) ? n + Math.pow(2,32) : n;
134 } else{
135 n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
136 }
137
138 return ( n );
139 }
140