2773052340f71a541997a9dcebf2eea7df4e15b7
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / parserstress / tests / ecma / TypeConversion / 9.5-2.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 gTestfile = '9.5-2.js';
40
41 /**
42    File Name:          9.5-2.js
43    ECMA Section:       9.5  Type Conversion:  ToInt32
44    Description:        rules for converting an argument to a signed 32 bit integer
45
46    this test uses << 0 to convert the argument to a 32bit
47    integer.
48
49    The operator ToInt32 converts its argument to one of 2^32
50    integer values in the range -2^31 through 2^31 inclusive.
51    This operator functions as follows:
52
53    1 call ToNumber on argument
54    2 if result is NaN, 0, -0, return 0
55    3 compute (sign (result(1)) * floor(abs(result 1)))
56    4 compute result(3) modulo 2^32:
57    5 if result(4) is greater than or equal to 2^31, return
58    result(5)-2^32.  otherwise, return result(5)
59
60    special cases:
61    -0          returns 0
62    Infinity    returns 0
63    -Infinity   returns 0
64    ToInt32(ToUint32(x)) == ToInt32(x) for all values of x
65    Numbers greater than 2^31 (see step 5 above)
66    (note http://bugzilla.mozilla.org/show_bug.cgi?id=120083)
67
68    Author:             christine@netscape.com
69    Date:               17 july 1997
70 */
71 var SECTION = "9.5-2";
72 var VERSION = "ECMA_1";
73 startTest();
74
75 writeHeaderToLog( SECTION + " ToInt32");
76
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,   "Number.POSITIVE_INFINITY << 0", 0,              Number.POSITIVE_INFINITY << 0 );
82 new TestCase( SECTION,   "Number.NEGATIVE_INFINITY << 0", 0,              Number.NEGATIVE_INFINITY << 0 );
83 new TestCase( SECTION,   "Number.NaN << 0",               0,              Number.NaN << 0 );
84
85 new TestCase( SECTION,   "Number.MIN_VALUE << 0",         0,              Number.MIN_VALUE << 0 );
86 new TestCase( SECTION,   "-Number.MIN_VALUE << 0",        0,              -Number.MIN_VALUE << 0 );
87 new TestCase( SECTION,   "0.1 << 0",                      0,              0.1 << 0 );
88 new TestCase( SECTION,   "-0.1 << 0",                     0,              -0.1 << 0 );
89 new TestCase( SECTION,   "1 << 0",                        1,              1 << 0 );
90 new TestCase( SECTION,   "1.1 << 0",                      1,              1.1 << 0 );
91 new TestCase( SECTION,   "-1 << 0",                     ToInt32(-1),             -1 << 0 );
92
93
94 new TestCase( SECTION,   "2147483647 << 0",     ToInt32(2147483647),    2147483647 << 0 );
95 new TestCase( SECTION,   "2147483648 << 0",     ToInt32(2147483648),    2147483648 << 0 );
96 new TestCase( SECTION,   "2147483649 << 0",     ToInt32(2147483649),    2147483649 << 0 );
97
98 new TestCase( SECTION,   "(Math.pow(2,31)-1) << 0", ToInt32(2147483647),    (Math.pow(2,31)-1) << 0 );
99 new TestCase( SECTION,   "Math.pow(2,31) << 0",     ToInt32(2147483648),    Math.pow(2,31) << 0 );
100 new TestCase( SECTION,   "(Math.pow(2,31)+1) << 0", ToInt32(2147483649),    (Math.pow(2,31)+1) << 0 );
101
102 new TestCase( SECTION,   "(Math.pow(2,32)-1) << 0",   ToInt32(4294967295),    (Math.pow(2,32)-1) << 0 );
103 new TestCase( SECTION,   "(Math.pow(2,32)) << 0",     ToInt32(4294967296),    (Math.pow(2,32)) << 0 );
104 new TestCase( SECTION,   "(Math.pow(2,32)+1) << 0",   ToInt32(4294967297),    (Math.pow(2,32)+1) << 0 );
105
106 new TestCase( SECTION,   "4294967295 << 0",     ToInt32(4294967295),    4294967295 << 0 );
107 new TestCase( SECTION,   "4294967296 << 0",     ToInt32(4294967296),    4294967296 << 0 );
108 new TestCase( SECTION,   "4294967297 << 0",     ToInt32(4294967297),    4294967297 << 0 );
109
110 new TestCase( SECTION,   "'2147483647' << 0",   ToInt32(2147483647),    '2147483647' << 0 );
111 new TestCase( SECTION,   "'2147483648' << 0",   ToInt32(2147483648),    '2147483648' << 0 );
112 new TestCase( SECTION,   "'2147483649' << 0",   ToInt32(2147483649),    '2147483649' << 0 );
113
114 new TestCase( SECTION,   "'4294967295' << 0",   ToInt32(4294967295),    '4294967295' << 0 );
115 new TestCase( SECTION,   "'4294967296' << 0",   ToInt32(4294967296),    '4294967296' << 0 );
116 new TestCase( SECTION,   "'4294967297' << 0",   ToInt32(4294967297),    '4294967297' << 0 );
117
118 new TestCase( SECTION,   "-2147483647 << 0",    ToInt32(-2147483647),   -2147483647     << 0 );
119 new TestCase( SECTION,   "-2147483648 << 0",    ToInt32(-2147483648),   -2147483648 << 0 );
120 new TestCase( SECTION,   "-2147483649 << 0",    ToInt32(-2147483649),   -2147483649 << 0 );
121
122 new TestCase( SECTION,   "-4294967295 << 0",    ToInt32(-4294967295),   -4294967295 << 0 );
123 new TestCase( SECTION,   "-4294967296 << 0",    ToInt32(-4294967296),   -4294967296 << 0 );
124 new TestCase( SECTION,   "-4294967297 << 0",    ToInt32(-4294967297),   -4294967297 << 0 );
125
126 /*
127  * Numbers between 2^31 and 2^32 will have a negative ToInt32 per ECMA (see step 5 of introduction)
128  * (These are by stevechapel@earthlink.net; cf. http://bugzilla.mozilla.org/show_bug.cgi?id=120083)
129  */
130 new TestCase( SECTION,   "2147483648.25 << 0",  ToInt32(2147483648.25),   2147483648.25 << 0 );
131 new TestCase( SECTION,   "2147483648.5 << 0",   ToInt32(2147483648.5),    2147483648.5 << 0 );
132 new TestCase( SECTION,   "2147483648.75 << 0",  ToInt32(2147483648.75),   2147483648.75 << 0 );
133 new TestCase( SECTION,   "4294967295.25 << 0",  ToInt32(4294967295.25),   4294967295.25 << 0 );
134 new TestCase( SECTION,   "4294967295.5 << 0",   ToInt32(4294967295.5),    4294967295.5 << 0 );
135 new TestCase( SECTION,   "4294967295.75 << 0",  ToInt32(4294967295.75),   4294967295.75 << 0 );
136 new TestCase( SECTION,   "3000000000.25 << 0",  ToInt32(3000000000.25),   3000000000.25 << 0 );
137 new TestCase( SECTION,   "3000000000.5 << 0",   ToInt32(3000000000.5),    3000000000.5 << 0 );
138 new TestCase( SECTION,   "3000000000.75 << 0",  ToInt32(3000000000.75),   3000000000.75 << 0 );
139
140 /*
141  * Numbers between - 2^31 and - 2^32
142  */
143 new TestCase( SECTION,   "-2147483648.25 << 0",  ToInt32(-2147483648.25),   -2147483648.25 << 0 );
144 new TestCase( SECTION,   "-2147483648.5 << 0",   ToInt32(-2147483648.5),    -2147483648.5 << 0 );
145 new TestCase( SECTION,   "-2147483648.75 << 0",  ToInt32(-2147483648.75),   -2147483648.75 << 0 );
146 new TestCase( SECTION,   "-4294967295.25 << 0",  ToInt32(-4294967295.25),   -4294967295.25 << 0 );
147 new TestCase( SECTION,   "-4294967295.5 << 0",   ToInt32(-4294967295.5),    -4294967295.5 << 0 );
148 new TestCase( SECTION,   "-4294967295.75 << 0",  ToInt32(-4294967295.75),   -4294967295.75 << 0 );
149 new TestCase( SECTION,   "-3000000000.25 << 0",  ToInt32(-3000000000.25),   -3000000000.25 << 0 );
150 new TestCase( SECTION,   "-3000000000.5 << 0",   ToInt32(-3000000000.5),    -3000000000.5 << 0 );
151 new TestCase( SECTION,   "-3000000000.75 << 0",  ToInt32(-3000000000.75),   -3000000000.75 << 0 );
152
153
154 test();
155
156 function ToInt32( n ) {
157   n = Number( n );
158   var sign = ( n < 0 ) ? -1 : 1;
159
160   if ( Math.abs( n ) == 0 || Math.abs( n ) == Number.POSITIVE_INFINITY) {
161     return 0;
162   }
163
164   n = (sign * Math.floor( Math.abs(n) )) % Math.pow(2,32);
165   if ( sign == -1 ) {
166     n = ( n < -Math.pow(2,31) ) ? n + Math.pow(2,32) : n;
167   } else{
168     n = ( n >= Math.pow(2,31) ) ? n - Math.pow(2,32) : n;
169   }
170
171   return ( n );
172 }
173