1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --allow-natives-syntax
6
7assertEquals(1, %ToNumber(1));
8assertEquals(1, %_ToNumber(1));
9
10assertEquals(.5, %ToNumber(.5));
11assertEquals(.5, %_ToNumber(.5));
12
13assertEquals(0, %ToNumber(null));
14assertEquals(0, %_ToNumber(null));
15
16assertEquals(1, %ToNumber(true));
17assertEquals(1, %_ToNumber(true));
18
19assertEquals(0, %ToNumber(false));
20assertEquals(0, %_ToNumber(false));
21
22assertEquals(NaN, %ToNumber(undefined));
23assertEquals(NaN, %_ToNumber(undefined));
24
25assertEquals(-1, %ToNumber("-1"));
26assertEquals(-1, %_ToNumber("-1"));
27assertEquals(123, %ToNumber("123"));
28assertEquals(123, %_ToNumber("123"));
29assertEquals(NaN, %ToNumber("random text"));
30assertEquals(NaN, %_ToNumber("random text"));
31
32assertThrows(function() { %ToNumber(Symbol.toPrimitive) }, TypeError);
33assertThrows(function() { %_ToNumber(Symbol.toPrimitive) }, TypeError);
34
35var a = { toString: function() { return 54321 }};
36assertEquals(54321, %ToNumber(a));
37assertEquals(54321, %_ToNumber(a));
38
39var b = { valueOf: function() { return 42 }};
40assertEquals(42, %ToNumber(b));
41assertEquals(42, %_ToNumber(b));
42
43var c = {
44  toString: function() { return "x"},
45  valueOf: function() { return 123 }
46};
47assertEquals(123, %ToNumber(c));
48assertEquals(123, %_ToNumber(c));
49
50var d = {
51  [Symbol.toPrimitive]: function(hint) {
52    assertEquals("number", hint);
53    return 987654321;
54  }
55};
56assertEquals(987654321, %ToNumber(d));
57assertEquals(987654321, %_ToNumber(d));
58
59var e = new Date(0);
60assertEquals(0, %ToNumber(e));
61assertEquals(0, %_ToNumber(e));
62