Debian Codegraph @

rhino/testsrc/tests/ecma/TypeConversion/9.3.1-3.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.3.1-3.js';
7
8 /**
9 File Name: 9.3.1-3.js
10 ECMA Section: 9.3 Type Conversion: ToNumber
11 Description: rules for converting an argument to a number.
12 see 9.3.1 for cases for converting strings to numbers.
13 special cases:
14 undefined NaN
15 Null NaN
16 Boolean 1 if true; +0 if false
17 Number the argument ( no conversion )
18 String see test 9.3.1
19 Object see test 9.3-1
20
21
22 Test cases provided by waldemar.
23
24
25 Author: [email protected]
26 Date: 10 june 1998
27
28 */
29
30 var SECTION = "9.3.1-3";
31 var VERSION = "ECMA_1";
32 var BUGNUMBER="129087";
33
34 var TITLE = "Number To String, String To Number";
35
36 startTest();
37
38 writeHeaderToLog( SECTION + " "+ TITLE);
39
40 // test case from http://scopus.mcom.com/bugsplat/show_bug.cgi?id=312954
41 var z = 0;
42
43 new TestCase(
44 SECTION,
45 "var z = 0; print(1/-z)",
46 -Infinity,
47 1/-z );
48
49
50
51
52
53 // test cases from bug http://scopus.mcom.com/bugsplat/show_bug.cgi?id=122882
54
55
56
57 new TestCase( SECTION,
58 '- -"0x80000000"',
59 2147483648,
60 - -"0x80000000" );
61
62 new TestCase( SECTION,
63 '- -"0x100000000"',
64 4294967296,
65 - -"0x100000000" );
66
67 new TestCase( SECTION,
68 '- "-0x123456789abcde8"',
69 81985529216486880,
70 - "-0x123456789abcde8" );
71
72 // Convert some large numbers to string
73
74
75 new TestCase( SECTION,
76 "1e2000 +''",
77 "Infinity",
78 1e2000 +"" );
79
80 new TestCase( SECTION,
81 "1e2000",
82 Infinity,
83 1e2000 );
84
85 new TestCase( SECTION,
86 "-1e2000 +''",
87 "-Infinity",
88 -1e2000 +"" );
89
90 new TestCase( SECTION,
91 "-\"1e2000\"",
92 -Infinity,
93 -"1e2000" );
94
95 new TestCase( SECTION,
96 "-\"-1e2000\" +''",
97 "Infinity",
98 -"-1e2000" +"" );
99
100 new TestCase( SECTION,
101 "1e-2000",
102 0,
103 1e-2000 );
104
105 new TestCase( SECTION,
106 "1/1e-2000",
107 Infinity,
108 1/1e-2000 );
109
110 // convert some strings to large numbers
111
112 new TestCase( SECTION,
113 "1/-1e-2000",
114 -Infinity,
115 1/-1e-2000 );
116
117 new TestCase( SECTION,
118 "1/\"1e-2000\"",
119 Infinity,
120 1/"1e-2000" );
121
122 new TestCase( SECTION,
123 "1/\"-1e-2000\"",
124 -Infinity,
125 1/"-1e-2000" );
126
127 new TestCase( SECTION,
128 "parseFloat(\"1e2000\")",
129 Infinity,
130 parseFloat("1e2000") );
131
132 new TestCase( SECTION,
133 "parseFloat(\"1e-2000\")",
134 0,
135 parseFloat("1e-2000") );
136
137 new TestCase( SECTION,
138 "1.7976931348623157E+308",
139 1.7976931348623157e+308,
140 1.7976931348623157E+308 );
141
142 new TestCase( SECTION,
143 "1.7976931348623158e+308",
144 1.7976931348623157e+308,
145 1.7976931348623158e+308 );
146
147 new TestCase( SECTION,
148 "1.7976931348623159e+308",
149 Infinity,
150 1.7976931348623159e+308 );
151
152 s =
153 "17976931348623158079372897140530341507993413271003782693617377898044496829276475094664901797758720709633028641669288791094655554785194040263065748867150582068";
154
155 print("s = " + s);
156 print("-s = " + (-s));
157
158 new TestCase( SECTION,
159 "s = " + s +"; s +="+
160 "\"190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779\""+
161
162 +"; s",
163 "17976931348623158079372897140530341507993413271003782693617377898044496829276475094664901797758720709633028641669288791094655554785194040263065748867150582068190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779",
164 s +=
165 "190890200070838367627385484581771153176447573027006985557136695962284291481986083493647529271907416844436551070434271155969950809304288017790417449779"
166 );
167
168 s1 = s+1;
169
170 print("s1 = " + s1);
171 print("-s1 = " + (-s1));
172
173 new TestCase( SECTION,
174 "s1 = s+1; s1",
175 "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497791",
176 s1 );
177
178 /***** This answer is preferred but -Infinity is also acceptable here *****/
179
180 new TestCase( SECTION,
181 "-s1 == Infinity || s1 == 1.7976931348623157e+308",
182 true,
183 -s1 == Infinity || s1 == 1.7976931348623157e+308 );
184
185 s2 = s + 2;
186
187 print("s2 = " + s2);
188 print("-s2 = " + (-s2));
189
190 new TestCase( SECTION,
191 "s2 = s+2; s2",
192 "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497792",
193 s2 );
194
195 // ***** This answer is preferred but -1.7976931348623157e+308 is also acceptable here *****
196 new TestCase( SECTION,
197 "-s2 == -Infinity || -s2 == -1.7976931348623157e+308 ",
198 true,
199 -s2 == -Infinity || -s2 == -1.7976931348623157e+308 );
200
201 s3 = s+3;
202
203 print("s3 = " + s3);
204 print("-s3 = " + (-s3));
205
206 new TestCase( SECTION,
207 "s3 = s+3; s3",
208 "179769313486231580793728971405303415079934132710037826936173778980444968292764750946649017977587207096330286416692887910946555547851940402630657488671505820681908902000708383676273854845817711531764475730270069855571366959622842914819860834936475292719074168444365510704342711559699508093042880177904174497793",
209 s3 );
210
211 //***** This answer is preferred but -1.7976931348623157e+308 is also acceptable here *****
212
213 new TestCase( SECTION,
214 "-s3 == -Infinity || -s3 == -1.7976931348623157e+308",
215 true,
216 -s3 == -Infinity || -s3 == -1.7976931348623157e+308 );
217
218
219 //***** This answer is preferred but Infinity is also acceptable here *****
220
221 new TestCase( SECTION,
222 "parseInt(s1,10) == 1.7976931348623157e+308 || parseInt(s1,10) == Infinity",
223 true,
224 parseInt(s1,10) == 1.7976931348623157e+308 || parseInt(s1,10) == Infinity );
225
226 //***** This answer is preferred but 1.7976931348623157e+308 is also acceptable here *****
227 new TestCase( SECTION,
228 "parseInt(s2,10) == Infinity || parseInt(s2,10) == 1.7976931348623157e+308",
229 true ,
230 parseInt(s2,10) == Infinity || parseInt(s2,10) == 1.7976931348623157e+308 );
231
232 //***** This answer is preferred but Infinity is also acceptable here *****
233
234 new TestCase( SECTION,
235 "parseInt(s1) == 1.7976931348623157e+308 || parseInt(s1) == Infinity",
236 true,
237 parseInt(s1) == 1.7976931348623157e+308 || parseInt(s1) == Infinity);
238
239 //***** This answer is preferred but 1.7976931348623157e+308 is also acceptable here *****
240 new TestCase( SECTION,
241 "parseInt(s2) == Infinity || parseInt(s2) == 1.7976931348623157e+308",
242 true,
243 parseInt(s2) == Infinity || parseInt(s2) == 1.7976931348623157e+308 );
244
245 new TestCase( SECTION,
246 "0x12345678",
247 305419896,
248 0x12345678 );
249
250 new TestCase( SECTION,
251 "0x80000000",
252 2147483648,
253 0x80000000 );
254
255 new TestCase( SECTION,
256 "0xffffffff",
257 4294967295,
258 0xffffffff );
259
260 new TestCase( SECTION,
261 "0x100000000",
262 4294967296,
263 0x100000000 );
264
265 new TestCase( SECTION,
266 "077777777777777777",
267 2251799813685247,
268 077777777777777777 );
269
270 new TestCase( SECTION,
271 "077777777777777776",
272 2251799813685246,
273 077777777777777776 );
274
275 new TestCase( SECTION,
276 "0x1fffffffffffff",
277 9007199254740991,
278 0x1fffffffffffff );
279
280 new TestCase( SECTION,
281 "0x20000000000000",
282 9007199254740992,
283 0x20000000000000 );
284
285 new TestCase( SECTION,
286 "0x20123456789abc",
287 9027215253084860,
288 0x20123456789abc );
289
290 new TestCase( SECTION,
291 "0x20123456789abd",
292 9027215253084860,
293 0x20123456789abd );
294
295 new TestCase( SECTION,
296 "0x20123456789abe",
297 9027215253084862,
298 0x20123456789abe );
299
300 new TestCase( SECTION,
301 "0x20123456789abf",
302 9027215253084864,
303 0x20123456789abf );
304
305 /***** These test the round-to-nearest-or-even-if-equally-close rule *****/
306
307 new TestCase( SECTION,
308 "0x1000000000000080",
309 1152921504606847000,
310 0x1000000000000080 );
311
312 new TestCase( SECTION,
313 "0x1000000000000081",
314 1152921504606847200,
315 0x1000000000000081 );
316
317 new TestCase( SECTION,
318 "0x1000000000000100",
319 1152921504606847200,
320 0x1000000000000100 );
321 new TestCase( SECTION,
322 "0x100000000000017f",
323 1152921504606847200,
324 0x100000000000017f );
325
326 new TestCase( SECTION,
327 "0x1000000000000180",
328 1152921504606847500,
329 0x1000000000000180 );
330
331 new TestCase( SECTION,
332 "0x1000000000000181",
333 1152921504606847500,
334 0x1000000000000181 );
335
336 new TestCase( SECTION,
337 "0x10000000000001f0",
338 1152921504606847500,
339 0x10000000000001f0 );
340
341 new TestCase( SECTION,
342 "0x1000000000000200",
343 1152921504606847500,
344 0x1000000000000200 );
345
346 new TestCase( SECTION,
347 "0x100000000000027f",
348 1152921504606847500,
349 0x100000000000027f );
350
351 new TestCase( SECTION,
352 "0x1000000000000280",
353 1152921504606847500,
354 0x1000000000000280 );
355
356 new TestCase( SECTION,
357 "0x1000000000000281",
358 1152921504606847700,
359 0x1000000000000281 );
360
361 new TestCase( SECTION,
362 "0x10000000000002ff",
363 1152921504606847700,
364 0x10000000000002ff );
365
366 new TestCase( SECTION,
367 "0x1000000000000300",
368 1152921504606847700,
369 0x1000000000000300 );
370
371 new TestCase( SECTION,
372 "0x10000000000000000",
373 18446744073709552000,
374 0x10000000000000000 );
375
376 new TestCase( SECTION,
377 "parseInt(\"000000100000000100100011010001010110011110001001101010111100\",2)",
378 9027215253084860,
379 parseInt("000000100000000100100011010001010110011110001001101010111100",2) );
380
381 new TestCase( SECTION,
382 "parseInt(\"000000100000000100100011010001010110011110001001101010111101\",2)",
383 9027215253084860,
384 parseInt("000000100000000100100011010001010110011110001001101010111101",2) );
385
386 new TestCase( SECTION,
387 "parseInt(\"000000100000000100100011010001010110011110001001101010111111\",2)",
388 9027215253084864,
389 parseInt("000000100000000100100011010001010110011110001001101010111111",2) );
390
391 new TestCase( SECTION,
392 "parseInt(\"0000001000000001001000110100010101100111100010011010101111010\",2)",
393 18054430506169720,
394 parseInt("0000001000000001001000110100010101100111100010011010101111010",2));
395
396 new TestCase( SECTION,
397 "parseInt(\"0000001000000001001000110100010101100111100010011010101111011\",2)",
398 18054430506169724,
399 parseInt("0000001000000001001000110100010101100111100010011010101111011",2) );
400
401 new TestCase( SECTION,
402 "parseInt(\"0000001000000001001000110100010101100111100010011010101111100\",2)",
403 18054430506169724,
404 parseInt("0000001000000001001000110100010101100111100010011010101111100",2));
405
406 new TestCase( SECTION,
407 "parseInt(\"0000001000000001001000110100010101100111100010011010101111110\",2)",
408 18054430506169728,
409 parseInt("0000001000000001001000110100010101100111100010011010101111110",2));
410
411 new TestCase( SECTION,
412 "parseInt(\"yz\",35)",
413 34,
414 parseInt("yz",35) );
415
416 new TestCase( SECTION,
417 "parseInt(\"yz\",36)",
418 1259,
419 parseInt("yz",36) );
420
421 new TestCase( SECTION,
422 "parseInt(\"yz\",37)",
423 NaN,
424 parseInt("yz",37) );
425
426 new TestCase( SECTION,
427 "parseInt(\"+77\")",
428 77,
429 parseInt("+77") );
430
431 new TestCase( SECTION,
432 "parseInt(\"-77\",9)",
433 -70,
434 parseInt("-77",9) );
435
436 new TestCase( SECTION,
437 "parseInt(\"\\u20001234\\u2000\")",
438 1234,
439 parseInt("\u20001234\u2000") );
440
441 new TestCase( SECTION,
442 "parseInt(\"123456789012345678\")",
443 123456789012345680,
444 parseInt("123456789012345678") );
445
446 new TestCase( SECTION,
447 "parseInt(\"9\",8)",
448 NaN,
449 parseInt("9",8) );
450
451 new TestCase( SECTION,
452 "parseInt(\"1e2\")",
453 1,
454 parseInt("1e2") );
455
456 new TestCase( SECTION,
457 "parseInt(\"1.9999999999999999999\")",
458 1,
459 parseInt("1.9999999999999999999") );
460
461 new TestCase( SECTION,
462 "parseInt(\"0x10\")",
463 16,
464 parseInt("0x10") );
465
466 new TestCase( SECTION,
467 "parseInt(\"0x10\",10)",
468 0,
469 parseInt("0x10",10) );
470
471 new TestCase( SECTION,
472 "parseInt(\"0022\")",
473 18,
474 parseInt("0022") );
475
476 new TestCase( SECTION,
477 "parseInt(\"0022\",10)",
478 22,
479 parseInt("0022",10) );
480
481 new TestCase( SECTION,
482 "parseInt(\"0x1000000000000080\")",
483 1152921504606847000,
484 parseInt("0x1000000000000080") );
485
486 new TestCase( SECTION,
487 "parseInt(\"0x1000000000000081\")",
488 1152921504606847200,
489 parseInt("0x1000000000000081") );
490
491 s =
492 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
493
494 new TestCase( SECTION, "s = "+
495 "\"0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";"+
496 "s",
497 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
498 s );
499
500
501 new TestCase( SECTION, "s +="+
502 "\"0000000000000000000000000000000000000\"; s",
503 "0xFFFFFFFFFFFFF800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
504 s += "0000000000000000000000000000000000000" );
505
506 new TestCase( SECTION, "-s",
507 -1.7976931348623157e+308,
508 -s );
509
510 s =
511 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
512
513 new TestCase( SECTION, "s ="+
514 "\"0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";"+
515 "s",
516 "0xFFFFFFFFFFFFF80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
517 s );
518
519 new TestCase( SECTION,
520 "s += \"0000000000000000000000000000000000001\"",
521 "0xFFFFFFFFFFFFF800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
522 s += "0000000000000000000000000000000000001" );
523
524 new TestCase( SECTION,
525 "-s",
526 -1.7976931348623157e+308,
527 -s );
528
529 s =
530 "0xFFFFFFFFFFFFFC0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
531
532 new TestCase( SECTION,
533 "s ="+
534 "\"0xFFFFFFFFFFFFFC0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";"+
535 "s",
536 "0xFFFFFFFFFFFFFC0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
537 s );
538
539
540 new TestCase( SECTION,
541 "s += \"0000000000000000000000000000000000000\"",
542 "0xFFFFFFFFFFFFFC00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
543 s += "0000000000000000000000000000000000000");
544
545
546 new TestCase( SECTION,
547 "-s",
548 -Infinity,
549 -s );
550
551 s =
552 "0xFFFFFFFFFFFFFB0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
553
554 new TestCase( SECTION,
555 "s = "+
556 "\"0xFFFFFFFFFFFFFB0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\";s",
557 "0xFFFFFFFFFFFFFB0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
558 s);
559
560 new TestCase( SECTION,
561 "s += \"0000000000000000000000000000000000001\"",
562 "0xFFFFFFFFFFFFFB00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
563 s += "0000000000000000000000000000000000001" );
564
565 new TestCase( SECTION,
566 "-s",
567 -1.7976931348623157e+308,
568 -s );
569
570 new TestCase( SECTION,
571 "s += \"0\"",
572 "0xFFFFFFFFFFFFFB000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010",
573 s += "0" );
574
575 new TestCase( SECTION,
576 "-s",
577 -Infinity,
578 -s );
579
580 new TestCase( SECTION,
581 "parseInt(s)",
582 Infinity,
583 parseInt(s) );
584
585 new TestCase( SECTION,
586 "parseInt(s,32)",
587 0,
588 parseInt(s,32) );
589
590 new TestCase( SECTION,
591 "parseInt(s,36)",
592 Infinity,
593 parseInt(s,36) );
594
595 new TestCase( SECTION,
596 "-\"\"",
597 0,
598 -"" );
599
600 new TestCase( SECTION,
601 "-\" \"",
602 0,
603 -" " );
604
605 new TestCase( SECTION,
606 "-\"999\"",
607 -999,
608 -"999" );
609
610 new TestCase( SECTION,
611 "-\" 999\"",
612 -999,
613 -" 999" );
614
615 new TestCase( SECTION,
616 "-\"\\t999\"",
617 -999,
618 -"\t999" );
619
620 new TestCase( SECTION,
621 "-\"013 \"",
622 -13,
623 -"013 " );
624
625 new TestCase( SECTION,
626 "-\"999\\t\"",
627 -999,
628 -"999\t" );
629
630 new TestCase( SECTION,
631 "-\"-Infinity\"",
632 Infinity,
633 -"-Infinity" );
634
635 new TestCase( SECTION,
636 "-\"+Infinity\"",
637 -Infinity,
638 -"+Infinity" );
639
640 new TestCase( SECTION,
641 "-\"+Infiniti\"",
642 NaN,
643 -"+Infiniti" );
644
645 new TestCase( SECTION,
646 "- -\"0x80000000\"",
647 2147483648,
648 - -"0x80000000" );
649
650 new TestCase( SECTION,
651 "- -\"0x100000000\"",
652 4294967296,
653 - -"0x100000000" );
654
655 new TestCase( SECTION,
656 "- \"-0x123456789abcde8\"",
657 81985529216486880,
658 - "-0x123456789abcde8" );
659
660 // the following two tests are not strictly ECMA 1.0
661
662 new TestCase( SECTION,
663 "-\"\\u20001234\\u2001\"",
664 -1234,
665 -"\u20001234\u2001" );
666
667 new TestCase( SECTION,
668 "-\"\\u20001234\\0\"",
669 NaN,
670 -"\u20001234\0" );
671
672 new TestCase( SECTION,
673 "-\"0x10\"",
674 -16,
675 -"0x10" );
676
677 new TestCase( SECTION,
678 "-\"+\"",
679 NaN,
680 -"+" );
681
682 new TestCase( SECTION,
683 "-\"-\"",
684 NaN,
685 -"-" );
686
687 new TestCase( SECTION,
688 "-\"-0-\"",
689 NaN,
690 -"-0-" );
691
692 new TestCase( SECTION,
693 "-\"1e-\"",
694 NaN,
695 -"1e-" );
696
697 new TestCase( SECTION,
698 "-\"1e-1\"",
699 -0.1,
700 -"1e-1" );
701
702 test();
703
704