blob: f9475d6fa56565a7c82c4fb1f9ab62ad45ffed92 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 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
Leon Clarkee46be812010-01-19 14:06:41 +000028// Flags: --allow-natives-syntax
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030// Test Math.min().
31
Leon Clarkee46be812010-01-19 14:06:41 +000032assertEquals(Infinity, Math.min());
Steve Blocka7e24c12009-10-30 11:49:00 +000033assertEquals(1, Math.min(1));
34assertEquals(1, Math.min(1, 2));
35assertEquals(1, Math.min(2, 1));
36assertEquals(1, Math.min(1, 2, 3));
37assertEquals(1, Math.min(3, 2, 1));
38assertEquals(1, Math.min(2, 3, 1));
Steve Blockd0582a62009-12-15 09:54:21 +000039assertEquals(1.1, Math.min(1.1, 2.2, 3.3));
40assertEquals(1.1, Math.min(3.3, 2.2, 1.1));
41assertEquals(1.1, Math.min(2.2, 3.3, 1.1));
Steve Blocka7e24c12009-10-30 11:49:00 +000042
Leon Clarkee46be812010-01-19 14:06:41 +000043// Prepare a non-Smi zero value.
44function returnsNonSmi(){ return 0.25; }
45var ZERO = returnsNonSmi() - returnsNonSmi();
46assertEquals(0, ZERO);
47assertEquals(Infinity, 1/ZERO);
48assertEquals(-Infinity, 1/-ZERO);
49assertFalse(%_IsSmi(ZERO));
50assertFalse(%_IsSmi(-ZERO));
51
Steve Blocka7e24c12009-10-30 11:49:00 +000052var o = {};
53o.valueOf = function() { return 1; };
54assertEquals(1, Math.min(2, 3, '1'));
55assertEquals(1, Math.min(3, o, 2));
Steve Blockd0582a62009-12-15 09:54:21 +000056assertEquals(1, Math.min(o, 2));
Leon Clarkee46be812010-01-19 14:06:41 +000057assertEquals(-Infinity, Infinity / Math.min(-0, +0));
58assertEquals(-Infinity, Infinity / Math.min(+0, -0));
59assertEquals(-Infinity, Infinity / Math.min(+0, -0, 1));
60assertEquals(-Infinity, Infinity / Math.min(-0, ZERO));
61assertEquals(-Infinity, Infinity / Math.min(ZERO, -0));
62assertEquals(-Infinity, Infinity / Math.min(ZERO, -0, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +000063assertEquals(-1, Math.min(+0, -0, -1));
64assertEquals(-1, Math.min(-1, +0, -0));
65assertEquals(-1, Math.min(+0, -1, -0));
66assertEquals(-1, Math.min(-0, -1, +0));
Steve Blockd0582a62009-12-15 09:54:21 +000067assertNaN(Math.min('oxen'));
68assertNaN(Math.min('oxen', 1));
69assertNaN(Math.min(1, 'oxen'));
Steve Blocka7e24c12009-10-30 11:49:00 +000070
71
72// Test Math.max().
73
74assertEquals(Number.NEGATIVE_INFINITY, Math.max());
75assertEquals(1, Math.max(1));
76assertEquals(2, Math.max(1, 2));
77assertEquals(2, Math.max(2, 1));
78assertEquals(3, Math.max(1, 2, 3));
79assertEquals(3, Math.max(3, 2, 1));
80assertEquals(3, Math.max(2, 3, 1));
Steve Blockd0582a62009-12-15 09:54:21 +000081assertEquals(3.3, Math.max(1.1, 2.2, 3.3));
82assertEquals(3.3, Math.max(3.3, 2.2, 1.1));
83assertEquals(3.3, Math.max(2.2, 3.3, 1.1));
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85var o = {};
86o.valueOf = function() { return 3; };
87assertEquals(3, Math.max(2, '3', 1));
88assertEquals(3, Math.max(1, o, 2));
Steve Blockd0582a62009-12-15 09:54:21 +000089assertEquals(3, Math.max(o, 1));
Leon Clarkee46be812010-01-19 14:06:41 +000090assertEquals(Infinity, Infinity / Math.max(-0, +0));
91assertEquals(Infinity, Infinity / Math.max(+0, -0));
92assertEquals(Infinity, Infinity / Math.max(+0, -0, -1));
93assertEquals(Infinity, Infinity / Math.max(-0, ZERO));
94assertEquals(Infinity, Infinity / Math.max(ZERO, -0));
95assertEquals(Infinity, Infinity / Math.max(ZERO, -0, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +000096assertEquals(1, Math.max(+0, -0, +1));
97assertEquals(1, Math.max(+1, +0, -0));
98assertEquals(1, Math.max(+0, +1, -0));
Steve Blockd0582a62009-12-15 09:54:21 +000099assertEquals(1, Math.max(-0, +1, +0));
100assertNaN(Math.max('oxen'));
101assertNaN(Math.max('oxen', 1));
102assertNaN(Math.max(1, 'oxen'));
Leon Clarkee46be812010-01-19 14:06:41 +0000103
104assertEquals(Infinity, 1/Math.max(ZERO, -0));
105assertEquals(Infinity, 1/Math.max(-0, ZERO));