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
28assertTrue(isNaN(Math.hypot({})));
29assertTrue(isNaN(Math.hypot(undefined, 1)));
30assertTrue(isNaN(Math.hypot(1, undefined)));
31assertTrue(isNaN(Math.hypot(Math.hypot, 1)));
32assertEquals(1, Math.hypot(1));
33assertEquals(Math.PI, Math.hypot(Math.PI));
34assertEquals(5, Math.hypot(3, 4));
35assertEquals(13, Math.hypot(3, 4, 12));
36assertEquals(15, Math.hypot(" 2 ",
37                            "0x5",
38                            { valueOf: function() { return "0xe"; } }));
39assertEquals(17, Math.hypot({ valueOf: function() { return 1; } },
40                            { toString: function() { return 12; } },
41                            { toString: function() { return "12"; } }));
42
43// Check function length.
44assertEquals(2, Math.hypot.length);
45
46// Check that 0 is returned for no arguments.
47assertEquals(0, Math.hypot());
48
49// Check that Infinity is returned if any of the arguments is +/-Infinity.
50assertEquals("Infinity", String(Math.hypot(NaN, Infinity)));
51assertEquals("Infinity", String(Math.hypot(1, -Infinity, 2)));
52
53// Check that NaN is returned if any argument is NaN and none is +/-Infinity/
54assertTrue(isNaN(Math.hypot(1, 2, NaN)));
55assertTrue(isNaN(Math.hypot(NaN, NaN, 4)));
56
57// Check that +0 is returned if all arguments are +/-0.
58assertEquals("Infinity", String(1/Math.hypot(-0)));
59assertEquals("Infinity", String(1/Math.hypot(0)));
60assertEquals("Infinity", String(1/Math.hypot(-0, -0)));
61assertEquals("Infinity", String(1/Math.hypot(-0, 0)));
62
63// Check that we avoid overflows and underflows.
64assertEqualsDelta(5E300, Math.hypot(3E300, 4E300), 1E285);
65assertEqualsDelta(17E-300, Math.hypot(8E-300, 15E-300), 1E-315);
66assertEqualsDelta(19E300, Math.hypot(6E300, 6E300, 17E300), 1E285);
67
68// Check that we sufficiently account for rounding errors when summing up.
69// For this, we calculate a simple fractal square that recurses in the
70// fourth quarter.
71var fractals = [];
72var edge_length = Math.E * 1E20;
73
74var fractal_length = edge_length;
75while(fractal_length >= 1) {
76  fractal_length *= 0.5;
77  fractals.push(fractal_length);
78  fractals.push(fractal_length);
79  fractals.push(fractal_length);
80}
81
82fractals.push(fractal_length);
83assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
84fractals.reverse();
85assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
86// Also shuffle the array.
87var c = 0;
88function random_sort(a, b) { c++; return (c & 3) - 1.5; }
89fractals.sort(random_sort);
90assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
91fractals.sort(random_sort);
92assertEqualsDelta(edge_length, Math.hypot.apply(Math, fractals), 1E-15);
93