1 /* The contents of this file are subject to the Netscape Public
2 * License Version 1.1 (the "License"); you may not use this file
3 * except in compliance with the License. You may obtain a copy of
4 * the License at http://www.mozilla.org/NPL/
6 * Software distributed under the License is distributed on an "AS
7 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
8 * implied. See the License for the specific language governing
9 * rights and limitations under the License.
11 * The Original Code is Mozilla Communicator client code, released March
14 * The Initial Developer of the Original Code is Netscape Communications
15 * Corporation. Portions created by Netscape are
16 * Copyright (C) 1998 Netscape Communications Corporation. All
24 ECMA Section: 9.8.1 ToString Applied to the Number Type
25 Description: The operator ToString convers a number m to string
28 1. if m is NaN, return the string "NaN"
29 2. if m is +0 or -0, return the string "0"
30 3. if m is less than zero, return the string
31 concatenation of the string "-" and ToString(-m).
32 4. If m is Infinity, return the string "Infinity".
33 5. Otherwise, let n, k, and s be integers such that
34 k >= 1, 10k1 <= s < 10k, the number value for s10nk
35 is m, and k is as small as possible. Note that k is
36 the number of digits in the decimal representation
37 of s, that s is not divisible by 10, and that the
38 least significant digit of s is not necessarily
39 uniquely determined by these criteria.
40 6. If k <= n <= 21, return the string consisting of the
41 k digits of the decimal representation of s (in order,
42 with no leading zeroes), followed by n-k occurences
44 7. If 0 < n <= 21, return the string consisting of the
45 most significant n digits of the decimal
46 representation of s, followed by a decimal point
47 '.', followed by the remaining kn digits of the
48 decimal representation of s.
49 8. If 6 < n <= 0, return the string consisting of the
50 character '0', followed by a decimal point '.',
51 followed by n occurences of the character '0',
52 followed by the k digits of the decimal
54 9. Otherwise, if k = 1, return the string consisting
55 of the single digit of s, followed by lowercase
56 character 'e', followed by a plus sign '+' or minus
57 sign '' according to whether n1 is positive or
58 negative, followed by the decimal representation
59 of the integer abs(n1) (with no leading zeros).
60 10. Return the string consisting of the most significant
61 digit of the decimal representation of s, followed
62 by a decimal point '.', followed by the remaining k1
63 digits of the decimal representation of s, followed
64 by the lowercase character 'e', followed by a plus
65 sign '+' or minus sign '' according to whether n1 is
66 positive or negative, followed by the decimal
67 representation of the integer abs(n1) (with no
70 Note that if x is any number value other than 0, then
71 ToNumber(ToString(x)) is exactly the same number value as x.
73 As noted, the least significant digit of s is not always
74 uniquely determined by the requirements listed in step 5.
75 The following specification for step 5 was considered, but
78 Author: christine@netscape.com
82 var SECTION = "9.8.1";
83 var VERSION = "ECMA_1";
85 var testcases = getTestCases();
87 writeHeaderToLog( SECTION + " ToString applied to the Number type");
91 for ( tc=0; tc < testcases.length; tc++ ) {
92 testcases[tc].passed = writeTestCaseResult(
95 testcases[tc].description +" = "+
96 testcases[tc].actual );
98 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value ";
101 return ( testcases );
103 function getTestCases() {
104 var array = new Array();
107 array[item++] = new TestCase( SECTION, "Number.NaN", "NaN", Number.NaN + "" );
108 array[item++] = new TestCase( SECTION, "0", "0", 0 + "" );
109 array[item++] = new TestCase( SECTION, "-0", "0", -0 + "" );
110 array[item++] = new TestCase( SECTION, "Number.POSITIVE_INFINITY", "Infinity", Number.POSITIVE_INFINITY + "" );
111 array[item++] = new TestCase( SECTION, "Number.NEGATIVE_INFINITY", "-Infinity", Number.NEGATIVE_INFINITY + "" );
112 array[item++] = new TestCase( SECTION, "-1", "-1", -1 + "" );
114 // cases in step 6: integers 1e21 > x >= 1 or -1 >= x > -1e21
116 array[item++] = new TestCase( SECTION, "1", "1", 1 + "" );
117 array[item++] = new TestCase( SECTION, "10", "10", 10 + "" );
118 array[item++] = new TestCase( SECTION, "100", "100", 100 + "" );
119 array[item++] = new TestCase( SECTION, "1000", "1000", 1000 + "" );
120 array[item++] = new TestCase( SECTION, "10000", "10000", 10000 + "" );
121 array[item++] = new TestCase( SECTION, "10000000000", "10000000000", 10000000000 + "" );
122 array[item++] = new TestCase( SECTION, "10000000000000000000", "10000000000000000000", 10000000000000000000 + "" );
123 array[item++] = new TestCase( SECTION, "100000000000000000000","100000000000000000000",100000000000000000000 + "" );
125 array[item++] = new TestCase( SECTION, "12345", "12345", 12345 + "" );
126 array[item++] = new TestCase( SECTION, "1234567890", "1234567890", 1234567890 + "" );
128 array[item++] = new TestCase( SECTION, "-1", "-1", -1 + "" );
129 array[item++] = new TestCase( SECTION, "-10", "-10", -10 + "" );
130 array[item++] = new TestCase( SECTION, "-100", "-100", -100 + "" );
131 array[item++] = new TestCase( SECTION, "-1000", "-1000", -1000 + "" );
132 array[item++] = new TestCase( SECTION, "-1000000000", "-1000000000", -1000000000 + "" );
133 array[item++] = new TestCase( SECTION, "-1000000000000000", "-1000000000000000", -1000000000000000 + "" );
134 array[item++] = new TestCase( SECTION, "-100000000000000000000", "-100000000000000000000", -100000000000000000000 + "" );
135 array[item++] = new TestCase( SECTION, "-1000000000000000000000", "-1e+21", -1000000000000000000000 + "" );
137 array[item++] = new TestCase( SECTION, "-12345", "-12345", -12345 + "" );
138 array[item++] = new TestCase( SECTION, "-1234567890", "-1234567890", -1234567890 + "" );
140 // cases in step 7: numbers with a fractional component, 1e21> x >1 or -1 > x > -1e21,
141 array[item++] = new TestCase( SECTION, "1.0000001", "1.0000001", 1.0000001 + "" );
143 // cases in step 8: fractions between 1 > x > -1, exclusive of 0 and -0
145 // cases in step 9: numbers with 1 significant digit >= 1e+21 or <= 1e-6
147 array[item++] = new TestCase( SECTION, "1000000000000000000000", "1e+21", 1000000000000000000000 + "" );
148 array[item++] = new TestCase( SECTION, "10000000000000000000000", "1e+22", 10000000000000000000000 + "" );
150 // cases in step 10: numbers with more than 1 significant digit >= 1e+21 or <= 1e-6
152 array[item++] = new TestCase( SECTION, "1.2345", "1.2345", String( 1.2345));
153 array[item++] = new TestCase( SECTION, "1.234567890", "1.23456789", String( 1.234567890 ));
156 array[item++] = new TestCase( SECTION, ".12345", "0.12345", String(.12345 ) );
157 array[item++] = new TestCase( SECTION, ".012345", "0.012345", String(.012345) );
158 array[item++] = new TestCase( SECTION, ".0012345", "0.0012345", String(.0012345) );
159 array[item++] = new TestCase( SECTION, ".00012345", "0.00012345", String(.00012345) );
160 array[item++] = new TestCase( SECTION, ".000012345", "0.000012345", String(.000012345) );
161 array[item++] = new TestCase( SECTION, ".0000012345", "0.0000012345", String(.0000012345) );
162 array[item++] = new TestCase( SECTION, ".00000012345", "1.2345e-7", String(.00000012345));
164 array[item++] = new TestCase( SECTION, "-1e21", "-1e+21", String(-1e21) );