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/ 5 * 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. 10 * 11 * The Original Code is Mozilla Communicator client code, released March 12 * 31, 1998. 13 * 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 17 * Rights Reserved. 18 * 19 * Contributor(s): 20 * 21 */ 22/** 23 File Name: 9.8.1.js 24 ECMA Section: 9.8.1 ToString Applied to the Number Type 25 Description: The operator ToString convers a number m to string 26 as follows: 27 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 43 of the character '0'. 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 53 representation of s. 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 68 leading zeros). 69 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. 72 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 76 not adopted: 77 78 Author: christine@netscape.com 79 Date: 10 july 1997 80*/ 81 82 var SECTION = "9.8.1"; 83 var VERSION = "ECMA_1"; 84 startTest(); 85 var testcases = getTestCases(); 86 87 writeHeaderToLog( SECTION + " ToString applied to the Number type"); 88 test(); 89 90function test() { 91 for ( tc=0; tc < testcases.length; tc++ ) { 92 testcases[tc].passed = writeTestCaseResult( 93 testcases[tc].expect, 94 testcases[tc].actual, 95 testcases[tc].description +" = "+ 96 testcases[tc].actual ); 97 98 testcases[tc].reason += ( testcases[tc].passed ) ? "" : "wrong value "; 99 } 100 stopTest(); 101 return ( testcases ); 102} 103function getTestCases() { 104 var array = new Array(); 105 var item = 0; 106 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 + "" ); 113 114 // cases in step 6: integers 1e21 > x >= 1 or -1 >= x > -1e21 115 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 + "" ); 124 125 array[item++] = new TestCase( SECTION, "12345", "12345", 12345 + "" ); 126 array[item++] = new TestCase( SECTION, "1234567890", "1234567890", 1234567890 + "" ); 127 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 + "" ); 136 137 array[item++] = new TestCase( SECTION, "-12345", "-12345", -12345 + "" ); 138 array[item++] = new TestCase( SECTION, "-1234567890", "-1234567890", -1234567890 + "" ); 139 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 + "" ); 142 143 // cases in step 8: fractions between 1 > x > -1, exclusive of 0 and -0 144 145 // cases in step 9: numbers with 1 significant digit >= 1e+21 or <= 1e-6 146 147 array[item++] = new TestCase( SECTION, "1000000000000000000000", "1e+21", 1000000000000000000000 + "" ); 148 array[item++] = new TestCase( SECTION, "10000000000000000000000", "1e+22", 10000000000000000000000 + "" ); 149 150 // cases in step 10: numbers with more than 1 significant digit >= 1e+21 or <= 1e-6 151 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 )); 154 155 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)); 163 164 array[item++] = new TestCase( SECTION, "-1e21", "-1e+21", String(-1e21) ); 165 return ( array ); 166} 167