blob: 1a98d440a357a39b573fe80e5f9de863a269a417 [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
28// Test Math.min().
29
30assertEquals(Number.POSITIVE_INFINITY, Math.min());
31assertEquals(1, Math.min(1));
32assertEquals(1, Math.min(1, 2));
33assertEquals(1, Math.min(2, 1));
34assertEquals(1, Math.min(1, 2, 3));
35assertEquals(1, Math.min(3, 2, 1));
36assertEquals(1, Math.min(2, 3, 1));
Steve Blockd0582a62009-12-15 09:54:21 +000037assertEquals(1.1, Math.min(1.1, 2.2, 3.3));
38assertEquals(1.1, Math.min(3.3, 2.2, 1.1));
39assertEquals(1.1, Math.min(2.2, 3.3, 1.1));
Steve Blocka7e24c12009-10-30 11:49:00 +000040
41var o = {};
42o.valueOf = function() { return 1; };
43assertEquals(1, Math.min(2, 3, '1'));
44assertEquals(1, Math.min(3, o, 2));
Steve Blockd0582a62009-12-15 09:54:21 +000045assertEquals(1, Math.min(o, 2));
Steve Blocka7e24c12009-10-30 11:49:00 +000046assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(-0, +0));
47assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(+0, -0));
48assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(+0, -0, 1));
49assertEquals(-1, Math.min(+0, -0, -1));
50assertEquals(-1, Math.min(-1, +0, -0));
51assertEquals(-1, Math.min(+0, -1, -0));
52assertEquals(-1, Math.min(-0, -1, +0));
Steve Blockd0582a62009-12-15 09:54:21 +000053assertNaN(Math.min('oxen'));
54assertNaN(Math.min('oxen', 1));
55assertNaN(Math.min(1, 'oxen'));
Steve Blocka7e24c12009-10-30 11:49:00 +000056
57
58// Test Math.max().
59
60assertEquals(Number.NEGATIVE_INFINITY, Math.max());
61assertEquals(1, Math.max(1));
62assertEquals(2, Math.max(1, 2));
63assertEquals(2, Math.max(2, 1));
64assertEquals(3, Math.max(1, 2, 3));
65assertEquals(3, Math.max(3, 2, 1));
66assertEquals(3, Math.max(2, 3, 1));
Steve Blockd0582a62009-12-15 09:54:21 +000067assertEquals(3.3, Math.max(1.1, 2.2, 3.3));
68assertEquals(3.3, Math.max(3.3, 2.2, 1.1));
69assertEquals(3.3, Math.max(2.2, 3.3, 1.1));
Steve Blocka7e24c12009-10-30 11:49:00 +000070
71var o = {};
72o.valueOf = function() { return 3; };
73assertEquals(3, Math.max(2, '3', 1));
74assertEquals(3, Math.max(1, o, 2));
Steve Blockd0582a62009-12-15 09:54:21 +000075assertEquals(3, Math.max(o, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +000076assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(-0, +0));
77assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(+0, -0));
78assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(+0, -0, -1));
79assertEquals(1, Math.max(+0, -0, +1));
80assertEquals(1, Math.max(+1, +0, -0));
81assertEquals(1, Math.max(+0, +1, -0));
Steve Blockd0582a62009-12-15 09:54:21 +000082assertEquals(1, Math.max(-0, +1, +0));
83assertNaN(Math.max('oxen'));
84assertNaN(Math.max('oxen', 1));
85assertNaN(Math.max(1, 'oxen'));