blob: 72d8ba30d9b9e3cb3b90160cb642b2d8352bfe7e [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; }
Leon Clarkeac952652010-07-15 11:15:24 +010045var ZERO = (function() {
46 var z;
47 // We have to have a loop here because the first time we get a Smi from the
48 // runtime system. After a while the binary op IC settles down and we get
49 // a non-Smi from the generated code.
50 for (var i = 0; i < 10; i++) {
51 z = returnsNonSmi() - returnsNonSmi();
52 }
53 return z;
54})();
Leon Clarkee46be812010-01-19 14:06:41 +000055assertEquals(0, ZERO);
56assertEquals(Infinity, 1/ZERO);
57assertEquals(-Infinity, 1/-ZERO);
58assertFalse(%_IsSmi(ZERO));
59assertFalse(%_IsSmi(-ZERO));
60
Steve Blocka7e24c12009-10-30 11:49:00 +000061var o = {};
62o.valueOf = function() { return 1; };
63assertEquals(1, Math.min(2, 3, '1'));
64assertEquals(1, Math.min(3, o, 2));
Steve Blockd0582a62009-12-15 09:54:21 +000065assertEquals(1, Math.min(o, 2));
Leon Clarkee46be812010-01-19 14:06:41 +000066assertEquals(-Infinity, Infinity / Math.min(-0, +0));
67assertEquals(-Infinity, Infinity / Math.min(+0, -0));
68assertEquals(-Infinity, Infinity / Math.min(+0, -0, 1));
69assertEquals(-Infinity, Infinity / Math.min(-0, ZERO));
70assertEquals(-Infinity, Infinity / Math.min(ZERO, -0));
71assertEquals(-Infinity, Infinity / Math.min(ZERO, -0, 1));
Steve Blocka7e24c12009-10-30 11:49:00 +000072assertEquals(-1, Math.min(+0, -0, -1));
73assertEquals(-1, Math.min(-1, +0, -0));
74assertEquals(-1, Math.min(+0, -1, -0));
75assertEquals(-1, Math.min(-0, -1, +0));
Steve Blockd0582a62009-12-15 09:54:21 +000076assertNaN(Math.min('oxen'));
77assertNaN(Math.min('oxen', 1));
78assertNaN(Math.min(1, 'oxen'));
Steve Blocka7e24c12009-10-30 11:49:00 +000079
80
81// Test Math.max().
82
83assertEquals(Number.NEGATIVE_INFINITY, Math.max());
84assertEquals(1, Math.max(1));
85assertEquals(2, Math.max(1, 2));
86assertEquals(2, Math.max(2, 1));
87assertEquals(3, Math.max(1, 2, 3));
88assertEquals(3, Math.max(3, 2, 1));
89assertEquals(3, Math.max(2, 3, 1));
Steve Blockd0582a62009-12-15 09:54:21 +000090assertEquals(3.3, Math.max(1.1, 2.2, 3.3));
91assertEquals(3.3, Math.max(3.3, 2.2, 1.1));
92assertEquals(3.3, Math.max(2.2, 3.3, 1.1));
Steve Blocka7e24c12009-10-30 11:49:00 +000093
94var o = {};
95o.valueOf = function() { return 3; };
96assertEquals(3, Math.max(2, '3', 1));
97assertEquals(3, Math.max(1, o, 2));
Steve Blockd0582a62009-12-15 09:54:21 +000098assertEquals(3, Math.max(o, 1));
Leon Clarkee46be812010-01-19 14:06:41 +000099assertEquals(Infinity, Infinity / Math.max(-0, +0));
100assertEquals(Infinity, Infinity / Math.max(+0, -0));
101assertEquals(Infinity, Infinity / Math.max(+0, -0, -1));
102assertEquals(Infinity, Infinity / Math.max(-0, ZERO));
103assertEquals(Infinity, Infinity / Math.max(ZERO, -0));
104assertEquals(Infinity, Infinity / Math.max(ZERO, -0, -1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000105assertEquals(1, Math.max(+0, -0, +1));
106assertEquals(1, Math.max(+1, +0, -0));
107assertEquals(1, Math.max(+0, +1, -0));
Steve Blockd0582a62009-12-15 09:54:21 +0000108assertEquals(1, Math.max(-0, +1, +0));
109assertNaN(Math.max('oxen'));
110assertNaN(Math.max('oxen', 1));
111assertNaN(Math.max(1, 'oxen'));
Leon Clarkee46be812010-01-19 14:06:41 +0000112
113assertEquals(Infinity, 1/Math.max(ZERO, -0));
114assertEquals(Infinity, 1/Math.max(-0, ZERO));