1// Copyright 2012 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// Number.prototype methods on non-Numbers.
29
30assertThrows(function() { Number.prototype.toExponential.call({}) },
31             TypeError);
32
33assertThrows(function() { Number.prototype.toPrecision.call({}) },
34             TypeError);
35
36assertThrows(function() { Number.prototype.toFixed.call({}) },
37             TypeError);
38
39assertThrows(function() { Number.prototype.toString.call({}) },
40             TypeError);
41
42assertThrows(function() { Number.prototype.toLocaleString.call({}) },
43             TypeError);
44
45assertThrows(function() { Number.prototype.ValueOf.call({}) },
46             TypeError);
47
48
49// Call on Number objects with custom valueOf method.
50
51var x_obj = new Number(1);
52x_obj.valueOf = function() { assertUnreachable(); };
53
54assertEquals("1.00e+0",
55             Number.prototype.toExponential.call(x_obj, 2));
56
57assertEquals("1.0",
58             Number.prototype.toPrecision.call(x_obj, 2));
59
60assertEquals("1.00",
61             Number.prototype.toFixed.call(x_obj, 2));
62
63// Call on primitive numbers.
64assertEquals("1.00e+0",
65             Number.prototype.toExponential.call(1, 2));
66
67assertEquals("1.0",
68             Number.prototype.toPrecision.call(1, 2));
69
70assertEquals("1.00",
71             Number.prototype.toFixed.call(1, 2));
72
73
74// toExponential and toPrecision does following steps in order
75// 1) convert the argument using ToInteger
76// 2) check for non-finite receiver, on which it returns,
77// 3) check argument range and throw exception if out of range.
78// Note that the the last two steps are reversed for toFixed.
79// Luckily, the receiver is expected to be a number or number
80// wrapper, so that getting its value is not observable.
81
82var f_flag = false;
83var f_obj = { valueOf: function() { f_flag = true; return 1000; } };
84
85assertEquals("NaN",
86             Number.prototype.toExponential.call(NaN, f_obj));
87assertTrue(f_flag);
88
89f_flag = false;
90assertEquals("Infinity",
91             Number.prototype.toExponential.call(1/0, f_obj));
92assertTrue(f_flag);
93
94f_flag = false;
95assertEquals("-Infinity",
96             Number.prototype.toExponential.call(-1/0, f_obj));
97assertTrue(f_flag);
98
99f_flag = false;
100assertEquals("NaN",
101             Number.prototype.toPrecision.call(NaN, f_obj));
102assertTrue(f_flag);
103
104f_flag = false;
105assertEquals("Infinity",
106             Number.prototype.toPrecision.call(1/0, f_obj));
107assertTrue(f_flag);
108
109f_flag = false;
110assertEquals("-Infinity",
111             Number.prototype.toPrecision.call(-1/0, f_obj));
112assertTrue(f_flag);
113
114// The odd man out: toFixed.
115
116f_flag = false;
117assertThrows(function() { Number.prototype.toFixed.call(NaN, f_obj) },
118             RangeError);
119assertTrue(f_flag);
120
121f_flag = false;
122assertThrows(function() { Number.prototype.toFixed.call(1/0, f_obj) },
123             RangeError);
124assertTrue(f_flag);
125
126f_flag = false;
127assertThrows(function() { Number.prototype.toFixed.call(-1/0, f_obj) },
128             RangeError);
129assertTrue(f_flag);
130