The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / mjsunit / number-tostring-func.js @ f230a1cf

History | View | Annotate | Download (15.4 KB)

1
// Copyright 2013 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27

    
28
// ----------------------------------------------------------------------
29
// toString
30
function testToString(a, b) {
31
  assertEquals(a, b.toString());
32
}
33

    
34
function testToStringP(a, b, c) {
35
  assertEquals(a, b.toString(c));
36
}
37

    
38
testToString("NaN", (NaN));
39
testToString("Infinity", (1/0));
40
testToString("-Infinity", (-1/0));
41
testToString("0", (0));
42
testToString("9", (9));
43
testToString("90", (90));
44
testToString("90.12", (90.12));
45
testToString("0.1", (0.1));
46
testToString("0.01", (0.01));
47
testToString("0.0123", (0.0123));
48
testToString("111111111111111110000", (111111111111111111111));
49
testToString("1.1111111111111111e+21", (1111111111111111111111));
50
testToString("1.1111111111111111e+22", (11111111111111111111111));
51
testToString("0.00001", (0.00001));
52
testToString("0.000001", (0.000001));
53
testToString("1e-7", (0.0000001));
54
testToString("1.2e-7", (0.00000012));
55
testToString("1.23e-7", (0.000000123));
56
testToString("1e-8", (0.00000001));
57
testToString("1.2e-8", (0.000000012));
58
testToString("1.23e-8", (0.0000000123));
59

    
60
testToString("0", (-0));
61
testToString("-9", (-9));
62
testToString("-90", (-90));
63
testToString("-90.12", (-90.12));
64
testToString("-0.1", (-0.1));
65
testToString("-0.01", (-0.01));
66
testToString("-0.0123", (-0.0123));
67
testToString("-111111111111111110000", (-111111111111111111111));
68
testToString("-1.1111111111111111e+21", (-1111111111111111111111));
69
testToString("-1.1111111111111111e+22", (-11111111111111111111111));
70
testToString("-0.00001", (-0.00001));
71
testToString("-0.000001", (-0.000001));
72
testToString("-1e-7", (-0.0000001));
73
testToString("-1.2e-7", (-0.00000012));
74
testToString("-1.23e-7", (-0.000000123));
75
testToString("-1e-8", (-0.00000001));
76
testToString("-1.2e-8", (-0.000000012));
77
testToString("-1.23e-8", (-0.0000000123));
78

    
79
testToString("1000", (1000));
80
testToString("0.00001", (0.00001));
81
testToString("1000000000000000100", (1000000000000000128));
82
testToString("1e+21", (1000000000000000012800));
83
testToString("-1e+21", (-1000000000000000012800));
84
testToString("1e-7", (0.0000001));
85
testToString("-1e-7", (-0.0000001));
86
testToString("1.0000000000000001e+21", (1000000000000000128000));
87
testToString("0.000001", (0.000001));
88
testToString("1e-7", (0.0000001));
89

    
90
testToStringP("NaN", (NaN), 16);
91
testToStringP("Infinity", (1/0), 16);
92
testToStringP("-Infinity", (-1/0), 16);
93
testToStringP("0", (0), 16);
94
testToStringP("9", (9), 16);
95
testToStringP("5a", (90), 16);
96
testToStringP("5a.1eb851eb852", (90.12), 16);
97
testToStringP("0.1999999999999a", (0.1), 16);
98
testToStringP("0.028f5c28f5c28f6", (0.01), 16);
99
testToStringP("0.032617c1bda511a", (0.0123), 16);
100
testToStringP("605f9f6dd18bc8000", (111111111111111111111), 16);
101
testToStringP("3c3bc3a4a2f75c0000", (1111111111111111111111), 16);
102
testToStringP("25a55a46e5da9a00000", (11111111111111111111111), 16);
103
testToStringP("0.0000a7c5ac471b4788", (0.00001), 16);
104
testToStringP("0.000010c6f7a0b5ed8d", (0.000001), 16);
105
testToStringP("0.000001ad7f29abcaf48", (0.0000001), 16);
106
testToStringP("0.000002036565348d256", (0.00000012), 16);
107
testToStringP("0.0000021047ee22aa466", (0.000000123), 16);
108
testToStringP("0.0000002af31dc4611874", (0.00000001), 16);
109
testToStringP("0.000000338a23b87483be", (0.000000012), 16);
110
testToStringP("0.00000034d3fe36aaa0a2", (0.0000000123), 16);
111

    
112
testToStringP("0", (-0), 16);
113
testToStringP("-9", (-9), 16);
114
testToStringP("-5a", (-90), 16);
115
testToStringP("-5a.1eb851eb852", (-90.12), 16);
116
testToStringP("-0.1999999999999a", (-0.1), 16);
117
testToStringP("-0.028f5c28f5c28f6", (-0.01), 16);
118
testToStringP("-0.032617c1bda511a", (-0.0123), 16);
119
testToStringP("-605f9f6dd18bc8000", (-111111111111111111111), 16);
120
testToStringP("-3c3bc3a4a2f75c0000", (-1111111111111111111111), 16);
121
testToStringP("-25a55a46e5da9a00000", (-11111111111111111111111), 16);
122
testToStringP("-0.0000a7c5ac471b4788", (-0.00001), 16);
123
testToStringP("-0.000010c6f7a0b5ed8d", (-0.000001), 16);
124
testToStringP("-0.000001ad7f29abcaf48", (-0.0000001), 16);
125
testToStringP("-0.000002036565348d256", (-0.00000012), 16);
126
testToStringP("-0.0000021047ee22aa466", (-0.000000123), 16);
127
testToStringP("-0.0000002af31dc4611874", (-0.00000001), 16);
128
testToStringP("-0.000000338a23b87483be", (-0.000000012), 16);
129
testToStringP("-0.00000034d3fe36aaa0a2", (-0.0000000123), 16);
130

    
131
testToString("4294967296", Math.pow(2,32));
132
testToStringP("ffffffff", (Math.pow(2,32)-1), 16);
133
testToStringP("11111111111111111111111111111111", (Math.pow(2,32)-1), 2);
134
testToStringP("5yc1z", (10000007), 36);
135
testToStringP("0", (0), 36);
136
testToStringP("0", (0), 16);
137
testToStringP("0", (0), 10);
138
testToStringP("0", (0), 8);
139
testToStringP("0", (0), 2);
140
testToStringP("100000000000000000000000000000000", Math.pow(2,32), 2);
141
testToStringP("100000000000000000000000000000001", (Math.pow(2,32) + 1), 2);
142
testToStringP("100000000000080", (0x100000000000081), 16);
143
testToStringP("1000000000000100", (-(-'0x1000000000000081')), 16);
144
testToStringP("1000000000000000", (-(-'0x1000000000000080')), 16);
145
testToStringP("1000000000000000", (-(-'0x100000000000007F')), 16);
146
testToStringP("100000000000000000000000000000000000000000000000010000000", (0x100000000000081), 2);
147
testToStringP("-11111111111111111111111111111111", (-(Math.pow(2,32)-1)), 2);
148
testToStringP("-5yc1z", (-10000007), 36);
149
testToStringP("-100000000000000000000000000000000", (-Math.pow(2,32)), 2);
150
testToStringP("-100000000000000000000000000000001", (-(Math.pow(2,32) + 1)), 2);
151
testToStringP("-100000000000080", (-0x100000000000081), 16);
152
testToStringP("-100000000000000000000000000000000000000000000000010000000", (-0x100000000000081), 2);
153
testToStringP("8.8", (8.5), 16);
154
testToStringP("-8.8", (-8.5), 16);
155

    
156
// ----------------------------------------------------------------------
157
// toFixed
158
function testToFixed(a, b, c) {
159
  assertEquals(a, b.toFixed(c));
160
}
161

    
162
testToFixed("NaN", (NaN), (2));
163
testToFixed("Infinity", (1/0), (2));
164
testToFixed("-Infinity", (-1/0), (2));
165

    
166
testToFixed("1.1111111111111111e+21", (1111111111111111111111), (8));
167
testToFixed("0.1", (0.1), (1));
168
testToFixed("0.10", (0.1), (2));
169
testToFixed("0.100", (0.1), (3));
170
testToFixed("0.01", (0.01), (2));
171
testToFixed("0.010", (0.01), (3));
172
testToFixed("0.0100", (0.01), (4));
173
testToFixed("0.00", (0.001), (2));
174
testToFixed("0.001", (0.001), (3));
175
testToFixed("0.0010", (0.001), (4));
176
testToFixed("1.0000", (1), (4));
177
testToFixed("1.0", (1), (1));
178
testToFixed("1", (1), (0));
179
testToFixed("12", (12), (0));
180
testToFixed("1", (1.1), (0));
181
testToFixed("12", (12.1), (0));
182
testToFixed("1", (1.12), (0));
183
testToFixed("12", (12.12), (0));
184
testToFixed("0.0000006", (0.0000006), (7));
185
testToFixed("0.00000006", (0.00000006), (8));
186
testToFixed("0.000000060", (0.00000006), (9));
187
testToFixed("0.0000000600", (0.00000006), (10));
188
testToFixed("0", (0), (0));
189
testToFixed("0.0", (0), (1));
190
testToFixed("0.00", (0), (2));
191

    
192
testToFixed("-1.1111111111111111e+21", (-1111111111111111111111), (8));
193
testToFixed("-0.1", (-0.1), (1));
194
testToFixed("-0.10", (-0.1), (2));
195
testToFixed("-0.100", (-0.1), (3));
196
testToFixed("-0.01", (-0.01), (2));
197
testToFixed("-0.010", (-0.01), (3));
198
testToFixed("-0.0100", (-0.01), (4));
199
testToFixed("-0.00", (-0.001), (2));
200
testToFixed("-0.001", (-0.001), (3));
201
testToFixed("-0.0010", (-0.001), (4));
202
testToFixed("-1.0000", (-1), (4));
203
testToFixed("-1.0", (-1), (1));
204
testToFixed("-1", (-1), (0));
205
testToFixed("-1", (-1.1), (0));
206
testToFixed("-12", (-12.1), (0));
207
testToFixed("-1", (-1.12), (0));
208
testToFixed("-12", (-12.12), (0));
209
testToFixed("-0.0000006", (-0.0000006), (7));
210
testToFixed("-0.00000006", (-0.00000006), (8));
211
testToFixed("-0.000000060", (-0.00000006), (9));
212
testToFixed("-0.0000000600", (-0.00000006), (10));
213
testToFixed("0", (-0), (0));
214
testToFixed("0.0", (-0), (1));
215
testToFixed("0.00", (-0), (2));
216

    
217
testToFixed("0.00001", (0.00001), (5));
218
testToFixed("0.00000000000000000010", (0.0000000000000000001), (20));
219
testToFixed("0.00001000000000000", (0.00001), (17));
220
testToFixed("1.00000000000000000", (1), (17));
221
testToFixed("100000000000000128.0", (100000000000000128), (1));
222
testToFixed("10000000000000128.00", (10000000000000128), (2));
223
testToFixed("10000000000000128.00000000000000000000", (10000000000000128), (20));
224
testToFixed("-42.000", (-42), (3));
225
testToFixed("-0.00000000000000000010", (-0.0000000000000000001), (20));
226
testToFixed("0.12312312312312299889", (0.123123123123123), (20));
227

    
228
assertEquals("-1000000000000000128", (-1000000000000000128).toFixed());
229
assertEquals("0", (0).toFixed());
230
assertEquals("1000000000000000128", (1000000000000000128).toFixed());
231
assertEquals("1000", (1000).toFixed());
232
assertEquals("0", (0.00001).toFixed());
233
// Test that we round up even when the last digit generated is even.
234
// dtoa does not do this in its original form.
235
assertEquals("1", 0.5.toFixed(0), "0.5.toFixed(0)");
236
assertEquals("-1", (-0.5).toFixed(0), "(-0.5).toFixed(0)");
237
assertEquals("1.3", 1.25.toFixed(1), "1.25.toFixed(1)");
238
// This is bizare, but Spidermonkey and KJS behave the same.
239
assertEquals("234.2040", (234.20405).toFixed(4), "234.2040.toFixed(4)");
240
assertEquals("234.2041", (234.2040506).toFixed(4));
241

    
242
// ----------------------------------------------------------------------
243
// toExponential
244
function testToExponential(a, b) {
245
  assertEquals(a, b.toExponential());
246
}
247

    
248
function testToExponentialP(a, b, c) {
249
  assertEquals(a, b.toExponential(c));
250
}
251

    
252
testToExponential("1e+0", (1));
253
testToExponential("1.1e+1", (11));
254
testToExponential("1.12e+2", (112));
255
testToExponential("1e-1", (0.1));
256
testToExponential("1.1e-1", (0.11));
257
testToExponential("1.12e-1", (0.112));
258
testToExponential("-1e+0", (-1));
259
testToExponential("-1.1e+1", (-11));
260
testToExponential("-1.12e+2", (-112));
261
testToExponential("-1e-1", (-0.1));
262
testToExponential("-1.1e-1", (-0.11));
263
testToExponential("-1.12e-1", (-0.112));
264
testToExponential("0e+0", (0));
265
testToExponential("1.12356e-4", (0.000112356));
266
testToExponential("-1.12356e-4", (-0.000112356));
267

    
268
testToExponentialP("1e+0", (1), (0));
269
testToExponentialP("1e+1", (11), (0));
270
testToExponentialP("1e+2", (112), (0));
271
testToExponentialP("1.0e+0", (1), (1));
272
testToExponentialP("1.1e+1", (11), (1));
273
testToExponentialP("1.1e+2", (112), (1));
274
testToExponentialP("1.00e+0", (1), (2));
275
testToExponentialP("1.10e+1", (11), (2));
276
testToExponentialP("1.12e+2", (112), (2));
277
testToExponentialP("1.000e+0", (1), (3));
278
testToExponentialP("1.100e+1", (11), (3));
279
testToExponentialP("1.120e+2", (112), (3));
280
testToExponentialP("1e-1", (0.1), (0));
281
testToExponentialP("1e-1", (0.11), (0));
282
testToExponentialP("1e-1", (0.112), (0));
283
testToExponentialP("1.0e-1", (0.1), (1));
284
testToExponentialP("1.1e-1", (0.11), (1));
285
testToExponentialP("1.1e-1", (0.112), (1));
286
testToExponentialP("1.00e-1", (0.1), (2));
287
testToExponentialP("1.10e-1", (0.11), (2));
288
testToExponentialP("1.12e-1", (0.112), (2));
289
testToExponentialP("1.000e-1", (0.1), (3));
290
testToExponentialP("1.100e-1", (0.11), (3));
291
testToExponentialP("1.120e-1", (0.112), (3));
292

    
293
testToExponentialP("-1e+0", (-1), (0));
294
testToExponentialP("-1e+1", (-11), (0));
295
testToExponentialP("-1e+2", (-112), (0));
296
testToExponentialP("-1.0e+0", (-1), (1));
297
testToExponentialP("-1.1e+1", (-11), (1));
298
testToExponentialP("-1.1e+2", (-112), (1));
299
testToExponentialP("-1.00e+0", (-1), (2));
300
testToExponentialP("-1.10e+1", (-11), (2));
301
testToExponentialP("-1.12e+2", (-112), (2));
302
testToExponentialP("-1.000e+0", (-1), (3));
303
testToExponentialP("-1.100e+1", (-11), (3));
304
testToExponentialP("-1.120e+2", (-112), (3));
305
testToExponentialP("-1e-1", (-0.1), (0));
306
testToExponentialP("-1e-1", (-0.11), (0));
307
testToExponentialP("-1e-1", (-0.112), (0));
308
testToExponentialP("-1.0e-1", (-0.1), (1));
309
testToExponentialP("-1.1e-1", (-0.11), (1));
310
testToExponentialP("-1.1e-1", (-0.112), (1));
311
testToExponentialP("-1.00e-1", (-0.1), (2));
312
testToExponentialP("-1.10e-1", (-0.11), (2));
313
testToExponentialP("-1.12e-1", (-0.112), (2));
314
testToExponentialP("-1.000e-1", (-0.1), (3));
315
testToExponentialP("-1.100e-1", (-0.11), (3));
316
testToExponentialP("-1.120e-1", (-0.112), (3));
317

    
318
testToExponentialP("NaN", (NaN), (2));
319
testToExponentialP("Infinity", (Infinity), (2));
320
testToExponentialP("-Infinity", (-Infinity), (2));
321
testToExponentialP("1e+0", (1), (0));
322
testToExponentialP("0.00e+0", (0), (2));
323
testToExponentialP("1e+1", (11.2356), (0));
324
testToExponentialP("1.1236e+1", (11.2356), (4));
325
testToExponentialP("1.1236e-4", (0.000112356), (4));
326
testToExponentialP("-1.1236e-4", (-0.000112356), (4));
327

    
328
// ----------------------------------------------------------------------
329
// toPrecision
330
function testToPrecision(a, b, c) {
331
  assertEquals(a, b.toPrecision(c));
332
}
333

    
334
testToPrecision("NaN", (NaN), (1));
335
testToPrecision("Infinity", (Infinity), (2));
336
testToPrecision("-Infinity", (-Infinity), (2));
337
testToPrecision("0.000555000000000000", (0.000555), (15));
338
testToPrecision("5.55000000000000e-7", (0.000000555), (15));
339
testToPrecision("-5.55000000000000e-7", (-0.000000555), (15));
340
testToPrecision("1e+8", (123456789), (1));
341
testToPrecision("123456789", (123456789), (9));
342
testToPrecision("1.2345679e+8", (123456789), (8));
343
testToPrecision("1.234568e+8", (123456789), (7));
344
testToPrecision("-1.234568e+8", (-123456789), (7));
345
testToPrecision("-1.2e-9", Number(-.0000000012345), (2));
346
testToPrecision("-1.2e-8", Number(-.000000012345), (2));
347
testToPrecision("-1.2e-7", Number(-.00000012345), (2));
348
testToPrecision("-0.0000012", Number(-.0000012345), (2));
349
testToPrecision("-0.000012", Number(-.000012345), (2));
350
testToPrecision("-0.00012", Number(-.00012345), (2));
351
testToPrecision("-0.0012", Number(-.0012345), (2));
352
testToPrecision("-0.012", Number(-.012345), (2));
353
testToPrecision("-0.12", Number(-.12345), (2));
354
testToPrecision("-1.2", Number(-1.2345), (2));
355
testToPrecision("-12", Number(-12.345), (2));
356
testToPrecision("-1.2e+2", Number(-123.45), (2));
357
testToPrecision("-1.2e+3", Number(-1234.5), (2));
358
testToPrecision("-1.2e+4", Number(-12345), (2));
359
testToPrecision("-1.235e+4", Number(-12345.67), (4));
360
testToPrecision("-1.234e+4", Number(-12344.67), (4));
361
// Test that we round up even when the last digit generated is even.
362
// dtoa does not do this in its original form.
363
assertEquals("1.3", 1.25.toPrecision(2), "1.25.toPrecision(2)");
364
assertEquals("1.4", 1.35.toPrecision(2), "1.35.toPrecision(2)");
365

    
366

    
367