2005-08-16 Marek Safar <marek.safar@seznam.cz>
[mono.git] / mcs / class / Microsoft.JScript / Test / Mozilla / ecma / TypeConversion / 9.8.1.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */\r
2 /* ***** BEGIN LICENSE BLOCK *****\r
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1\r
4  *\r
5  * The contents of this file are subject to the Mozilla Public License Version\r
6  * 1.1 (the "License"); you may not use this file except in compliance with\r
7  * the License. You may obtain a copy of the License at\r
8  * http://www.mozilla.org/MPL/\r
9  *\r
10  * Software distributed under the License is distributed on an "AS IS" basis,\r
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r
12  * for the specific language governing rights and limitations under the\r
13  * License.\r
14  *\r
15  * The Original Code is Mozilla Communicator client code, released\r
16  * March 31, 1998.\r
17  *\r
18  * The Initial Developer of the Original Code is\r
19  * Netscape Communications Corporation.\r
20  * Portions created by the Initial Developer are Copyright (C) 1998\r
21  * the Initial Developer. All Rights Reserved.\r
22  *\r
23  * Contributor(s):\r
24  *\r
25  * Alternatively, the contents of this file may be used under the terms of\r
26  * either the GNU General Public License Version 2 or later (the "GPL"), or\r
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),\r
28  * in which case the provisions of the GPL or the LGPL are applicable instead\r
29  * of those above. If you wish to allow use of your version of this file only\r
30  * under the terms of either the GPL or the LGPL, and not to allow others to\r
31  * use your version of this file under the terms of the MPL, indicate your\r
32  * decision by deleting the provisions above and replace them with the notice\r
33  * and other provisions required by the GPL or the LGPL. If you do not delete\r
34  * the provisions above, a recipient may use your version of this file under\r
35  * the terms of any one of the MPL, the GPL or the LGPL.\r
36  *\r
37  * ***** END LICENSE BLOCK ***** */\r
38 \r
39 /**\r
40    File Name:          9.8.1.js\r
41    ECMA Section:       9.8.1 ToString Applied to the Number Type\r
42    Description:        The operator ToString convers a number m to string\r
43    as follows:\r
44 \r
45    1.  if m is NaN, return the string "NaN"\r
46    2.  if m is +0 or -0, return the string "0"\r
47    3.  if m is less than zero, return the string\r
48    concatenation of the string "-" and ToString(-m).\r
49    4.  If m is Infinity, return the string "Infinity".\r
50    5.  Otherwise, let n, k, and s be integers such that\r
51    k >= 1, 10k1 <= s < 10k, the number value for s10nk\r
52    is m, and k is as small as possible. Note that k is\r
53    the number of digits in the decimal representation\r
54    of s, that s is not divisible by 10, and that the\r
55    least significant digit of s is not necessarily\r
56    uniquely determined by these criteria.\r
57    6.  If k <= n <= 21, return the string consisting of the\r
58    k digits of the decimal representation of s (in order,\r
59    with no leading zeroes), followed by n-k occurences\r
60    of the character '0'.\r
61    7.  If 0 < n <= 21, return the string consisting of the\r
62    most significant n digits of the decimal\r
63    representation of s, followed by a decimal point\r
64    '.', followed by the remaining kn digits of the\r
65    decimal representation of s.\r
66    8.  If 6 < n <= 0, return the string consisting of the\r
67    character '0', followed by a decimal point '.',\r
68    followed by n occurences of the character '0',\r
69    followed by the k digits of the decimal\r
70    representation of s.\r
71    9.  Otherwise, if k = 1, return the string consisting\r
72    of the single digit of s, followed by lowercase\r
73    character 'e', followed by a plus sign '+' or minus\r
74    sign '' according to whether n1 is positive or\r
75    negative, followed by the decimal representation\r
76    of the integer abs(n1) (with no leading zeros).\r
77    10.  Return the string consisting of the most significant\r
78    digit of the decimal representation of s, followed\r
79    by a decimal point '.', followed by the remaining k1\r
80    digits of the decimal representation of s, followed\r
81    by the lowercase character 'e', followed by a plus\r
82    sign '+' or minus sign '' according to whether n1 is\r
83    positive or negative, followed by the decimal\r
84    representation of the integer abs(n1) (with no\r
85    leading zeros).\r
86 \r
87    Note that if x is any number value other than 0, then\r
88    ToNumber(ToString(x)) is exactly the same number value as x.\r
89 \r
90    As noted, the least significant digit of s is not always\r
91    uniquely determined by the requirements listed in step 5.\r
92    The following specification for step 5 was considered, but\r
93    not adopted:\r
94 \r
95    Author:         christine@netscape.com\r
96    Date:           10 july 1997\r
97 */\r
98 \r
99 var SECTION = "9.8.1";\r
100 var VERSION = "ECMA_1";\r
101 startTest();\r
102 \r
103 writeHeaderToLog( SECTION + " ToString applied to the Number type");\r
104 \r
105 new TestCase( SECTION,    "Number.NaN",       "NaN",                  Number.NaN + "" );\r
106 new TestCase( SECTION,    "0",                "0",                    0 + "" );\r
107 new TestCase( SECTION,    "-0",               "0",                   -0 + "" );\r
108 new TestCase( SECTION,    "Number.POSITIVE_INFINITY", "Infinity",     Number.POSITIVE_INFINITY + "" );\r
109 new TestCase( SECTION,    "Number.NEGATIVE_INFINITY", "-Infinity",    Number.NEGATIVE_INFINITY + "" );\r
110 new TestCase( SECTION,    "-1",               "-1",                   -1 + "" );\r
111 \r
112 // cases in step 6:  integers  1e21 > x >= 1 or -1 >= x > -1e21\r
113 \r
114 new TestCase( SECTION,    "1",                    "1",                    1 + "" );\r
115 new TestCase( SECTION,    "10",                   "10",                   10 + "" );\r
116 new TestCase( SECTION,    "100",                  "100",                  100 + "" );\r
117 new TestCase( SECTION,    "1000",                 "1000",                 1000 + "" );\r
118 new TestCase( SECTION,    "10000",                "10000",                10000 + "" );\r
119 new TestCase( SECTION,    "10000000000",          "10000000000",          10000000000 + "" );\r
120 new TestCase( SECTION,    "10000000000000000000", "10000000000000000000", 10000000000000000000 + "" );\r
121 new TestCase( SECTION,    "100000000000000000000","100000000000000000000",100000000000000000000 + "" );\r
122 \r
123 new TestCase( SECTION,    "12345",                    "12345",                    12345 + "" );\r
124 new TestCase( SECTION,    "1234567890",               "1234567890",               1234567890 + "" );\r
125 \r
126 new TestCase( SECTION,    "-1",                       "-1",                       -1 + "" );\r
127 new TestCase( SECTION,    "-10",                      "-10",                      -10 + "" );\r
128 new TestCase( SECTION,    "-100",                     "-100",                     -100 + "" );\r
129 new TestCase( SECTION,    "-1000",                    "-1000",                    -1000 + "" );\r
130 new TestCase( SECTION,    "-1000000000",              "-1000000000",              -1000000000 + "" );\r
131 new TestCase( SECTION,    "-1000000000000000",        "-1000000000000000",        -1000000000000000 + "" );\r
132 new TestCase( SECTION,    "-100000000000000000000",   "-100000000000000000000",   -100000000000000000000 + "" );\r
133 new TestCase( SECTION,    "-1000000000000000000000",  "-1e+21",                   -1000000000000000000000 + "" );\r
134 \r
135 new TestCase( SECTION,    "-12345",                    "-12345",                  -12345 + "" );\r
136 new TestCase( SECTION,    "-1234567890",               "-1234567890",             -1234567890 + "" );\r
137 \r
138 // cases in step 7: numbers with a fractional component, 1e21> x >1 or  -1 > x > -1e21,\r
139 new TestCase( SECTION,    "1.0000001",                "1.0000001",                1.0000001 + "" );\r
140 \r
141 // cases in step 8:  fractions between 1 > x > -1, exclusive of 0 and -0\r
142 \r
143 // cases in step 9:  numbers with 1 significant digit >= 1e+21 or <= 1e-6\r
144 \r
145 new TestCase( SECTION,    "1000000000000000000000",   "1e+21",             1000000000000000000000 + "" );\r
146 new TestCase( SECTION,    "10000000000000000000000",   "1e+22",            10000000000000000000000 + "" );\r
147 \r
148 //  cases in step 10:  numbers with more than 1 significant digit >= 1e+21 or <= 1e-6\r
149 \r
150 new TestCase( SECTION,    "1.2345",                    "1.2345",                  String( 1.2345));\r
151 new TestCase( SECTION,    "1.234567890",               "1.23456789",             String( 1.234567890 ));\r
152 \r
153 \r
154 new TestCase( SECTION,    ".12345",                   "0.12345",                String(.12345 )     );\r
155 new TestCase( SECTION,    ".012345",                  "0.012345",               String(.012345)     );\r
156 new TestCase( SECTION,    ".0012345",                 "0.0012345",              String(.0012345)    );\r
157 new TestCase( SECTION,    ".00012345",                "0.00012345",             String(.00012345)   );\r
158 new TestCase( SECTION,    ".000012345",               "0.000012345",            String(.000012345)  );\r
159 new TestCase( SECTION,    ".0000012345",              "0.0000012345",           String(.0000012345) );\r
160 new TestCase( SECTION,    ".00000012345",             "1.2345e-7",              String(.00000012345));\r
161 \r
162 new TestCase( SECTION,    "-1e21",                    "-1e+21",                 String(-1e21) );\r
163 \r
164 test();\r
165 \r