Debian Codegraph @

rhino/testsrc/tests/ecma/TypeConversion/9.8.1.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.8.1.js';
7
8 /**
9 File Name: 9.8.1.js
10 ECMA Section: 9.8.1 ToString Applied to the Number Type
11 Description: The operator ToString convers a number m to string
12 as follows:
13
14 1. if m is NaN, return the string "NaN"
15 2. if m is +0 or -0, return the string "0"
16 3. if m is less than zero, return the string
17 concatenation of the string "-" and ToString(-m).
18 4. If m is Infinity, return the string "Infinity".
19 5. Otherwise, let n, k, and s be integers such that
20 k >= 1, 10k1 <= s < 10k, the number value for s10nk
21 is m, and k is as small as possible. Note that k is
22 the number of digits in the decimal representation
23 of s, that s is not divisible by 10, and that the
24 least significant digit of s is not necessarily
25 uniquely determined by these criteria.
26 6. If k <= n <= 21, return the string consisting of the
27 k digits of the decimal representation of s (in order,
28 with no leading zeroes), followed by n-k occurences
29 of the character '0'.
30 7. If 0 < n <= 21, return the string consisting of the
31 most significant n digits of the decimal
32 representation of s, followed by a decimal point
33 '.', followed by the remaining kn digits of the
34 decimal representation of s.
35 8. If 6 < n <= 0, return the string consisting of the
36 character '0', followed by a decimal point '.',
37 followed by n occurences of the character '0',
38 followed by the k digits of the decimal
39 representation of s.
40 9. Otherwise, if k = 1, return the string consisting
41 of the single digit of s, followed by lowercase
42 character 'e', followed by a plus sign '+' or minus
43 sign '' according to whether n1 is positive or
44 negative, followed by the decimal representation
45 of the integer abs(n1) (with no leading zeros).
46 10. Return the string consisting of the most significant
47 digit of the decimal representation of s, followed
48 by a decimal point '.', followed by the remaining k1
49 digits of the decimal representation of s, followed
50 by the lowercase character 'e', followed by a plus
51 sign '+' or minus sign '' according to whether n1 is
52 positive or negative, followed by the decimal
53 representation of the integer abs(n1) (with no
54 leading zeros).
55
56 Note that if x is any number value other than 0, then
57 ToNumber(ToString(x)) is exactly the same number value as x.
58
59 As noted, the least significant digit of s is not always
60 uniquely determined by the requirements listed in step 5.
61 The following specification for step 5 was considered, but
62 not adopted:
63
64 Author: [email protected]
65 Date: 10 july 1997
66 */
67
68 var SECTION = "9.8.1";
69 var VERSION = "ECMA_1";
70 startTest();
71
72 writeHeaderToLog( SECTION + " ToString applied to the Number type");
73
74 new TestCase( SECTION, "Number.NaN", "NaN", Number.NaN + "" );
75 new TestCase( SECTION, "0", "0", 0 + "" );
76 new TestCase( SECTION, "-0", "0", -0 + "" );
77 new TestCase( SECTION, "Number.POSITIVE_INFINITY", "Infinity", Number.POSITIVE_INFINITY + "" );
78 new TestCase( SECTION, "Number.NEGATIVE_INFINITY", "-Infinity", Number.NEGATIVE_INFINITY + "" );
79 new TestCase( SECTION, "-1", "-1", -1 + "" );
80
81 // cases in step 6: integers 1e21 > x >= 1 or -1 >= x > -1e21
82
83 new TestCase( SECTION, "1", "1", 1 + "" );
84 new TestCase( SECTION, "10", "10", 10 + "" );
85 new TestCase( SECTION, "100", "100", 100 + "" );
86 new TestCase( SECTION, "1000", "1000", 1000 + "" );
87 new TestCase( SECTION, "10000", "10000", 10000 + "" );
88 new TestCase( SECTION, "10000000000", "10000000000", 10000000000 + "" );
89 new TestCase( SECTION, "10000000000000000000", "10000000000000000000", 10000000000000000000 + "" );
90 new TestCase( SECTION, "100000000000000000000","100000000000000000000",100000000000000000000 + "" );
91
92 new TestCase( SECTION, "12345", "12345", 12345 + "" );
93 new TestCase( SECTION, "1234567890", "1234567890", 1234567890 + "" );
94
95 new TestCase( SECTION, "-1", "-1", -1 + "" );
96 new TestCase( SECTION, "-10", "-10", -10 + "" );
97 new TestCase( SECTION, "-100", "-100", -100 + "" );
98 new TestCase( SECTION, "-1000", "-1000", -1000 + "" );
99 new TestCase( SECTION, "-1000000000", "-1000000000", -1000000000 + "" );
100 new TestCase( SECTION, "-1000000000000000", "-1000000000000000", -1000000000000000 + "" );
101 new TestCase( SECTION, "-100000000000000000000", "-100000000000000000000", -100000000000000000000 + "" );
102 new TestCase( SECTION, "-1000000000000000000000", "-1e+21", -1000000000000000000000 + "" );
103
104 new TestCase( SECTION, "-12345", "-12345", -12345 + "" );
105 new TestCase( SECTION, "-1234567890", "-1234567890", -1234567890 + "" );
106
107 // cases in step 7: numbers with a fractional component, 1e21> x >1 or -1 > x > -1e21,
108 new TestCase( SECTION, "1.0000001", "1.0000001", 1.0000001 + "" );
109
110 // cases in step 8: fractions between 1 > x > -1, exclusive of 0 and -0
111
112 // cases in step 9: numbers with 1 significant digit >= 1e+21 or <= 1e-6
113
114 new TestCase( SECTION, "1000000000000000000000", "1e+21", 1000000000000000000000 + "" );
115 new TestCase( SECTION, "10000000000000000000000", "1e+22", 10000000000000000000000 + "" );
116
117 // cases in step 10: numbers with more than 1 significant digit >= 1e+21 or <= 1e-6
118
119 new TestCase( SECTION, "1.2345", "1.2345", String( 1.2345));
120 new TestCase( SECTION, "1.234567890", "1.23456789", String( 1.234567890 ));
121
122
123 new TestCase( SECTION, ".12345", "0.12345", String(.12345 ) );
124 new TestCase( SECTION, ".012345", "0.012345", String(.012345) );
125 new TestCase( SECTION, ".0012345", "0.0012345", String(.0012345) );
126 new TestCase( SECTION, ".00012345", "0.00012345", String(.00012345) );
127 new TestCase( SECTION, ".000012345", "0.000012345", String(.000012345) );
128 new TestCase( SECTION, ".0000012345", "0.0000012345", String(.0000012345) );
129 new TestCase( SECTION, ".00000012345", "1.2345e-7", String(.00000012345));
130
131 new TestCase( SECTION, "-1e21", "-1e+21", String(-1e21) );
132
133 test();
134