Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame^] | 28 | // Flags: --allow-natives-syntax |
| 29 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 30 | // Test Math.min(). |
| 31 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame^] | 32 | assertEquals(Infinity, Math.min()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 33 | assertEquals(1, Math.min(1)); |
| 34 | assertEquals(1, Math.min(1, 2)); |
| 35 | assertEquals(1, Math.min(2, 1)); |
| 36 | assertEquals(1, Math.min(1, 2, 3)); |
| 37 | assertEquals(1, Math.min(3, 2, 1)); |
| 38 | assertEquals(1, Math.min(2, 3, 1)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 39 | assertEquals(1.1, Math.min(1.1, 2.2, 3.3)); |
| 40 | assertEquals(1.1, Math.min(3.3, 2.2, 1.1)); |
| 41 | assertEquals(1.1, Math.min(2.2, 3.3, 1.1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 42 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame^] | 43 | // Prepare a non-Smi zero value. |
| 44 | function returnsNonSmi(){ return 0.25; } |
| 45 | var ZERO = returnsNonSmi() - returnsNonSmi(); |
| 46 | assertEquals(0, ZERO); |
| 47 | assertEquals(Infinity, 1/ZERO); |
| 48 | assertEquals(-Infinity, 1/-ZERO); |
| 49 | assertFalse(%_IsSmi(ZERO)); |
| 50 | assertFalse(%_IsSmi(-ZERO)); |
| 51 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | var o = {}; |
| 53 | o.valueOf = function() { return 1; }; |
| 54 | assertEquals(1, Math.min(2, 3, '1')); |
| 55 | assertEquals(1, Math.min(3, o, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 56 | assertEquals(1, Math.min(o, 2)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame^] | 57 | assertEquals(-Infinity, Infinity / Math.min(-0, +0)); |
| 58 | assertEquals(-Infinity, Infinity / Math.min(+0, -0)); |
| 59 | assertEquals(-Infinity, Infinity / Math.min(+0, -0, 1)); |
| 60 | assertEquals(-Infinity, Infinity / Math.min(-0, ZERO)); |
| 61 | assertEquals(-Infinity, Infinity / Math.min(ZERO, -0)); |
| 62 | assertEquals(-Infinity, Infinity / Math.min(ZERO, -0, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 63 | assertEquals(-1, Math.min(+0, -0, -1)); |
| 64 | assertEquals(-1, Math.min(-1, +0, -0)); |
| 65 | assertEquals(-1, Math.min(+0, -1, -0)); |
| 66 | assertEquals(-1, Math.min(-0, -1, +0)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 67 | assertNaN(Math.min('oxen')); |
| 68 | assertNaN(Math.min('oxen', 1)); |
| 69 | assertNaN(Math.min(1, 'oxen')); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | |
| 71 | |
| 72 | // Test Math.max(). |
| 73 | |
| 74 | assertEquals(Number.NEGATIVE_INFINITY, Math.max()); |
| 75 | assertEquals(1, Math.max(1)); |
| 76 | assertEquals(2, Math.max(1, 2)); |
| 77 | assertEquals(2, Math.max(2, 1)); |
| 78 | assertEquals(3, Math.max(1, 2, 3)); |
| 79 | assertEquals(3, Math.max(3, 2, 1)); |
| 80 | assertEquals(3, Math.max(2, 3, 1)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 81 | assertEquals(3.3, Math.max(1.1, 2.2, 3.3)); |
| 82 | assertEquals(3.3, Math.max(3.3, 2.2, 1.1)); |
| 83 | assertEquals(3.3, Math.max(2.2, 3.3, 1.1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 84 | |
| 85 | var o = {}; |
| 86 | o.valueOf = function() { return 3; }; |
| 87 | assertEquals(3, Math.max(2, '3', 1)); |
| 88 | assertEquals(3, Math.max(1, o, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 89 | assertEquals(3, Math.max(o, 1)); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame^] | 90 | assertEquals(Infinity, Infinity / Math.max(-0, +0)); |
| 91 | assertEquals(Infinity, Infinity / Math.max(+0, -0)); |
| 92 | assertEquals(Infinity, Infinity / Math.max(+0, -0, -1)); |
| 93 | assertEquals(Infinity, Infinity / Math.max(-0, ZERO)); |
| 94 | assertEquals(Infinity, Infinity / Math.max(ZERO, -0)); |
| 95 | assertEquals(Infinity, Infinity / Math.max(ZERO, -0, -1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | assertEquals(1, Math.max(+0, -0, +1)); |
| 97 | assertEquals(1, Math.max(+1, +0, -0)); |
| 98 | assertEquals(1, Math.max(+0, +1, -0)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 99 | assertEquals(1, Math.max(-0, +1, +0)); |
| 100 | assertNaN(Math.max('oxen')); |
| 101 | assertNaN(Math.max('oxen', 1)); |
| 102 | assertNaN(Math.max(1, 'oxen')); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame^] | 103 | |
| 104 | assertEquals(Infinity, 1/Math.max(ZERO, -0)); |
| 105 | assertEquals(Infinity, 1/Math.max(-0, ZERO)); |