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 | |
| 28 | // Test Math.min(). |
| 29 | |
| 30 | assertEquals(Number.POSITIVE_INFINITY, Math.min()); |
| 31 | assertEquals(1, Math.min(1)); |
| 32 | assertEquals(1, Math.min(1, 2)); |
| 33 | assertEquals(1, Math.min(2, 1)); |
| 34 | assertEquals(1, Math.min(1, 2, 3)); |
| 35 | assertEquals(1, Math.min(3, 2, 1)); |
| 36 | assertEquals(1, Math.min(2, 3, 1)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 37 | assertEquals(1.1, Math.min(1.1, 2.2, 3.3)); |
| 38 | assertEquals(1.1, Math.min(3.3, 2.2, 1.1)); |
| 39 | assertEquals(1.1, Math.min(2.2, 3.3, 1.1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 40 | |
| 41 | var o = {}; |
| 42 | o.valueOf = function() { return 1; }; |
| 43 | assertEquals(1, Math.min(2, 3, '1')); |
| 44 | assertEquals(1, Math.min(3, o, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 45 | assertEquals(1, Math.min(o, 2)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 46 | assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(-0, +0)); |
| 47 | assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(+0, -0)); |
| 48 | assertEquals(Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY / Math.min(+0, -0, 1)); |
| 49 | assertEquals(-1, Math.min(+0, -0, -1)); |
| 50 | assertEquals(-1, Math.min(-1, +0, -0)); |
| 51 | assertEquals(-1, Math.min(+0, -1, -0)); |
| 52 | assertEquals(-1, Math.min(-0, -1, +0)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 53 | assertNaN(Math.min('oxen')); |
| 54 | assertNaN(Math.min('oxen', 1)); |
| 55 | assertNaN(Math.min(1, 'oxen')); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | |
| 57 | |
| 58 | // Test Math.max(). |
| 59 | |
| 60 | assertEquals(Number.NEGATIVE_INFINITY, Math.max()); |
| 61 | assertEquals(1, Math.max(1)); |
| 62 | assertEquals(2, Math.max(1, 2)); |
| 63 | assertEquals(2, Math.max(2, 1)); |
| 64 | assertEquals(3, Math.max(1, 2, 3)); |
| 65 | assertEquals(3, Math.max(3, 2, 1)); |
| 66 | assertEquals(3, Math.max(2, 3, 1)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 67 | assertEquals(3.3, Math.max(1.1, 2.2, 3.3)); |
| 68 | assertEquals(3.3, Math.max(3.3, 2.2, 1.1)); |
| 69 | assertEquals(3.3, Math.max(2.2, 3.3, 1.1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | |
| 71 | var o = {}; |
| 72 | o.valueOf = function() { return 3; }; |
| 73 | assertEquals(3, Math.max(2, '3', 1)); |
| 74 | assertEquals(3, Math.max(1, o, 2)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 75 | assertEquals(3, Math.max(o, 1)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 76 | assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(-0, +0)); |
| 77 | assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(+0, -0)); |
| 78 | assertEquals(Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY / Math.max(+0, -0, -1)); |
| 79 | assertEquals(1, Math.max(+0, -0, +1)); |
| 80 | assertEquals(1, Math.max(+1, +0, -0)); |
| 81 | assertEquals(1, Math.max(+0, +1, -0)); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame^] | 82 | assertEquals(1, Math.max(-0, +1, +0)); |
| 83 | assertNaN(Math.max('oxen')); |
| 84 | assertNaN(Math.max('oxen', 1)); |
| 85 | assertNaN(Math.max(1, 'oxen')); |