QML_RUNTIME_TESTING should be disabled by default.
[profile/ivi/qtdeclarative.git] / tests / auto / qml / parserstress / tests / ecma / GlobalObject / 15.1.2.2-1.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 = '15.1.2.2-1.js';
40
41 /**
42    File Name:          15.1.2.2-1.js
43    ECMA Section:       15.1.2.2 Function properties of the global object
44    parseInt( string, radix )
45
46    Description:
47
48    The parseInt function produces an integer value dictated by intepretation
49    of the contents of the string argument according to the specified radix.
50
51    When the parseInt function is called, the following steps are taken:
52
53    1.   Call ToString(string).
54    2.   Compute a substring of Result(1) consisting of the leftmost character
55    that is not a StrWhiteSpaceChar and all characters to the right of
56    that character. (In other words, remove leading whitespace.)
57    3.   Let sign be 1.
58    4.   If Result(2) is not empty and the first character of Result(2) is a
59    minus sign -, let sign be -1.
60    5.   If Result(2) is not empty and the first character of Result(2) is a
61    plus sign + or a minus sign -, then Result(5) is the substring of
62    Result(2) produced by removing the first character; otherwise, Result(5)
63    is Result(2).
64    6.   If the radix argument is not supplied, go to step 12.
65    7.   Call ToInt32(radix).
66    8.   If Result(7) is zero, go to step 12; otherwise, if Result(7) < 2 or
67    Result(7) > 36, return NaN.
68    9.   Let R be Result(7).
69    10.   If R = 16 and the length of Result(5) is at least 2 and the first two
70    characters of Result(5) are either "0x" or "0X", let S be the substring
71    of Result(5) consisting of all but the first two characters; otherwise,
72    let S be Result(5).
73    11.   Go to step 22.
74    12.   If Result(5) is empty or the first character of Result(5) is not 0,
75    go to step 20.
76    13.   If the length of Result(5) is at least 2 and the second character of
77    Result(5) is x or X, go to step 17.
78    14.   Let R be 8.
79    15.   Let S be Result(5).
80    16.   Go to step 22.
81    17.   Let R be 16.
82    18.   Let S be the substring of Result(5) consisting of all but the first
83    two characters.
84    19.   Go to step 22.
85    20.   Let R be 10.
86    21.   Let S be Result(5).
87    22.   If S contains any character that is not a radix-R digit, then let Z be
88    the substring of S consisting of all characters to the left of the
89    leftmost such character; otherwise, let Z be S.
90    23.   If Z is empty, return NaN.
91    24.   Compute the mathematical integer value that is represented by Z in
92    radix-R notation. (But if R is 10 and Z contains more than 20
93    significant digits, every digit after the 20th may be replaced by a 0
94    digit, at the option of the implementation; and if R is not 2, 4, 8,
95    10, 16, or 32, then Result(24) may be an implementation-dependent
96    approximation to the mathematical integer value that is represented
97    by Z in radix-R notation.)
98    25.   Compute the number value for Result(24).
99    26.   Return sign Result(25).
100
101    Note that parseInt may interpret only a leading portion of the string as
102    an integer value; it ignores any characters that cannot be interpreted as
103    part of the notation of an integer, and no indication is given that any
104    such characters were ignored.
105
106    Author:             christine@netscape.com
107    Date:               28 october 1997
108
109 */
110 var SECTION = "15.1.2.2-1";
111 var VERSION = "ECMA_1";
112 var TITLE   = "parseInt(string, radix)";
113 var BUGNUMBER = "none";
114
115 startTest();
116
117 writeHeaderToLog( SECTION + " "+ TITLE);
118
119 var HEX_STRING = "0x0";
120 var HEX_VALUE = 0;
121
122 new TestCase( SECTION, 
123               "parseInt.length",     
124               2,     
125               parseInt.length );
126
127 new TestCase( SECTION, 
128               "parseInt.length = 0; parseInt.length",    
129               2,     
130               eval("parseInt.length = 0; parseInt.length") );
131
132 new TestCase( SECTION, 
133               "var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS",   "prototype",
134               eval("var PROPS=''; for ( var p in parseInt ) { PROPS += p; }; PROPS") );
135
136 new TestCase( SECTION, 
137               "delete parseInt.length",  
138               false, 
139               delete parseInt.length );
140
141 new TestCase( SECTION, 
142               "delete parseInt.length; parseInt.length", 
143               2, 
144               eval("delete parseInt.length; parseInt.length") );
145
146 new TestCase( SECTION, 
147               "parseInt.length = null; parseInt.length", 
148               2, 
149               eval("parseInt.length = null; parseInt.length") );
150
151 new TestCase( SECTION, 
152               "parseInt()",      
153               NaN,   
154               parseInt() );
155
156 new TestCase( SECTION, 
157               "parseInt('')",    
158               NaN,   
159               parseInt("") );
160
161 new TestCase( SECTION, 
162               "parseInt('','')", 
163               NaN,   
164               parseInt("","") );
165
166 new TestCase( SECTION,
167               "parseInt(\"     0xabcdef     ",
168               11259375,
169               parseInt( "      0xabcdef     " ));
170
171 new TestCase( SECTION,
172               "parseInt(\"     0XABCDEF     ",
173               11259375,
174               parseInt( "      0XABCDEF     " ) );
175
176 new TestCase( SECTION,
177               "parseInt( 0xabcdef )",
178               11259375,
179               parseInt( "0xabcdef") );
180
181 new TestCase( SECTION,
182               "parseInt( 0XABCDEF )",
183               11259375,
184               parseInt( "0XABCDEF") );
185
186 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
187   new TestCase( SECTION, "parseInt("+HEX_STRING+")",    HEX_VALUE,  parseInt(HEX_STRING) );
188   HEX_VALUE += Math.pow(16,POWER)*15;
189 }
190 for ( HEX_STRING = "0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
191   new TestCase( SECTION, "parseInt("+HEX_STRING+")",    HEX_VALUE,  parseInt(HEX_STRING) );
192   HEX_VALUE += Math.pow(16,POWER)*15;
193 }
194 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
195   new TestCase( SECTION, "parseInt("+HEX_STRING+",16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
196   HEX_VALUE += Math.pow(16,POWER)*15;
197 }
198 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
199   new TestCase( SECTION, "parseInt("+HEX_STRING+",16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
200   HEX_VALUE += Math.pow(16,POWER)*15;
201 }
202 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
203   new TestCase( SECTION, "parseInt("+HEX_STRING+",null)",    HEX_VALUE,  parseInt(HEX_STRING,null) );
204   HEX_VALUE += Math.pow(16,POWER)*15;
205 }
206 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
207   new TestCase( SECTION, "parseInt("+HEX_STRING+", void 0)",    HEX_VALUE,  parseInt(HEX_STRING, void 0) );
208   HEX_VALUE += Math.pow(16,POWER)*15;
209 }
210
211 // a few tests with spaces
212
213 for ( var space = " ", HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0;
214       POWER < 15;
215       POWER++, HEX_STRING = HEX_STRING +"f", space += " ")
216 {
217   new TestCase( SECTION, "parseInt("+space+HEX_STRING+space+", void 0)",    HEX_VALUE,  parseInt(space+HEX_STRING+space, void 0) );
218   HEX_VALUE += Math.pow(16,POWER)*15;
219 }
220
221 // a few tests with negative numbers
222 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
223   new TestCase( SECTION, "parseInt("+HEX_STRING+")",    HEX_VALUE,  parseInt(HEX_STRING) );
224   HEX_VALUE -= Math.pow(16,POWER)*15;
225 }
226
227 // we should stop parsing when we get to a value that is not a numeric literal for the type we expect
228
229 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
230   new TestCase( SECTION, "parseInt("+HEX_STRING+"g,16)",    HEX_VALUE,  parseInt(HEX_STRING+"g",16) );
231   HEX_VALUE += Math.pow(16,POWER)*15;
232 }
233 for ( HEX_STRING = "0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
234   new TestCase( SECTION, "parseInt("+HEX_STRING+"g,16)",    HEX_VALUE,  parseInt(HEX_STRING+"G",16) );
235   HEX_VALUE += Math.pow(16,POWER)*15;
236 }
237
238 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
239   new TestCase( SECTION, "parseInt("+HEX_STRING+")",    HEX_VALUE,  parseInt(HEX_STRING) );
240   HEX_VALUE -= Math.pow(16,POWER)*15;
241 }
242 for ( HEX_STRING = "-0X0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
243   new TestCase( SECTION, "parseInt("+HEX_STRING+")",    HEX_VALUE,  parseInt(HEX_STRING) );
244   HEX_VALUE -= Math.pow(16,POWER)*15;
245 }
246 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
247   new TestCase( SECTION, "parseInt("+HEX_STRING+",16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
248   HEX_VALUE -= Math.pow(16,POWER)*15;
249 }
250 for ( HEX_STRING = "-0x0", HEX_VALUE = 0, POWER = 0; POWER < 15; POWER++, HEX_STRING = HEX_STRING +"f" ) {
251   new TestCase( SECTION, "parseInt("+HEX_STRING+",16)",    HEX_VALUE,  parseInt(HEX_STRING,16) );
252   HEX_VALUE -= Math.pow(16,POWER)*15;
253 }
254
255 //  let us do some octal tests.  numbers that start with 0 and do not provid a radix should
256 //  default to using "0" as a radix.
257
258 var OCT_STRING = "0";
259 var OCT_VALUE = 0;
260
261 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
262   new TestCase( SECTION, "parseInt("+OCT_STRING+")",    OCT_VALUE,  parseInt(OCT_STRING) );
263   OCT_VALUE += Math.pow(8,POWER)*7;
264 }
265
266 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
267   new TestCase( SECTION, "parseInt("+OCT_STRING+")",    OCT_VALUE,  parseInt(OCT_STRING) );
268   OCT_VALUE -= Math.pow(8,POWER)*7;
269 }
270
271 // should get the same results as above if we provid the radix of 8 (or 010)
272
273 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
274   new TestCase( SECTION, "parseInt("+OCT_STRING+",8)",    OCT_VALUE,  parseInt(OCT_STRING,8) );
275   OCT_VALUE += Math.pow(8,POWER)*7;
276 }
277 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
278   new TestCase( SECTION, "parseInt("+OCT_STRING+",010)",    OCT_VALUE,  parseInt(OCT_STRING,010) );
279   OCT_VALUE -= Math.pow(8,POWER)*7;
280 }
281
282 // we shall stop parsing digits when we get one that isn't a numeric literal of the type we think
283 // it should be.
284 for ( OCT_STRING = "0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
285   new TestCase( SECTION, "parseInt("+OCT_STRING+"8,8)",    OCT_VALUE,  parseInt(OCT_STRING+"8",8) );
286   OCT_VALUE += Math.pow(8,POWER)*7;
287 }
288 for ( OCT_STRING = "-0", OCT_VALUE = 0, POWER = 0; POWER < 15; POWER++, OCT_STRING = OCT_STRING +"7" ) {
289   new TestCase( SECTION, "parseInt("+OCT_STRING+"8,010)",    OCT_VALUE,  parseInt(OCT_STRING+"8",010) );
290   OCT_VALUE -= Math.pow(8,POWER)*7;
291 }
292
293 new TestCase( SECTION,
294               "parseInt( '0x' )",             
295               NaN,       
296               parseInt("0x") );
297
298 new TestCase( SECTION,
299               "parseInt( '0X' )",             
300               NaN,       
301               parseInt("0X") );
302
303 new TestCase( SECTION,
304               "parseInt( '11111111112222222222' )",   
305               11111111112222222222,  
306               parseInt("11111111112222222222") );
307
308 new TestCase( SECTION,
309               "parseInt( '111111111122222222223' )",   
310               111111111122222222220,  
311               parseInt("111111111122222222223") );
312
313 new TestCase( SECTION,
314               "parseInt( '11111111112222222222',10 )",   
315               11111111112222222222,  
316               parseInt("11111111112222222222",10) );
317
318 new TestCase( SECTION,
319               "parseInt( '111111111122222222223',10 )",   
320               111111111122222222220,  
321               parseInt("111111111122222222223",10) );
322
323 new TestCase( SECTION,
324               "parseInt( '01234567890', -1 )", 
325               Number.NaN,   
326               parseInt("01234567890",-1) );
327
328 new TestCase( SECTION,
329               "parseInt( '01234567890', 0 )", 
330               Number.NaN,    
331               parseInt("01234567890",1) );
332
333 new TestCase( SECTION,
334               "parseInt( '01234567890', 1 )", 
335               Number.NaN,    
336               parseInt("01234567890",1) );
337
338 new TestCase( SECTION,
339               "parseInt( '01234567890', 2 )", 
340               1,             
341               parseInt("01234567890",2) );
342
343 new TestCase( SECTION,
344               "parseInt( '01234567890', 3 )", 
345               5,             
346               parseInt("01234567890",3) );
347
348 new TestCase( SECTION,
349               "parseInt( '01234567890', 4 )", 
350               27,            
351               parseInt("01234567890",4) );
352
353 new TestCase( SECTION,
354               "parseInt( '01234567890', 5 )", 
355               194,           
356               parseInt("01234567890",5) );
357
358 new TestCase( SECTION,
359               "parseInt( '01234567890', 6 )", 
360               1865,          
361               parseInt("01234567890",6) );
362
363 new TestCase( SECTION,
364               "parseInt( '01234567890', 7 )", 
365               22875,         
366               parseInt("01234567890",7) );
367
368 new TestCase( SECTION,
369               "parseInt( '01234567890', 8 )", 
370               342391,        
371               parseInt("01234567890",8) );
372
373 new TestCase( SECTION,
374               "parseInt( '01234567890', 9 )", 
375               6053444,       
376               parseInt("01234567890",9) );
377
378 new TestCase( SECTION,
379               "parseInt( '01234567890', 10 )",
380               1234567890,    
381               parseInt("01234567890",10) );
382
383 // need more test cases with hex radix
384
385 new TestCase( SECTION,
386               "parseInt( '1234567890', '0xa')",
387               1234567890,
388               parseInt("1234567890","0xa") );
389
390 new TestCase( SECTION,
391               "parseInt( '012345', 11 )",     
392               17715,         
393               parseInt("012345",11) );
394
395 new TestCase( SECTION,
396               "parseInt( '012345', 35 )",     
397               1590195,       
398               parseInt("012345",35) );
399
400 new TestCase( SECTION,
401               "parseInt( '012345', 36 )",     
402               1776965,       
403               parseInt("012345",36) );
404
405 new TestCase( SECTION,
406               "parseInt( '012345', 37 )",     
407               Number.NaN,    
408               parseInt("012345",37) );
409
410 test();