ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 1 | // Copyright 2008 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 28 | // Flags: --allow-natives-syntax |
| 29 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 30 | // Test Math.min(). |
| 31 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 32 | assertEquals(Infinity, Math.min()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +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)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +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)); |
| 42 | |
| 43 | // Prepare a non-Smi zero value. |
| 44 | function returnsNonSmi(){ return 0.25; } |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 45 | var 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 | })(); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 55 | assertEquals(0, ZERO); |
| 56 | assertEquals(Infinity, 1/ZERO); |
| 57 | assertEquals(-Infinity, 1/-ZERO); |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 58 | // Here we would like to have assertFalse(%_IsSmi(ZERO)); This is, however, |
| 59 | // unreliable, since a new space exhaustion at a critical moment could send |
| 60 | // us into the runtime system, which would quite legitimately put a Smi zero |
| 61 | // here. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 62 | assertFalse(%_IsSmi(-ZERO)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 63 | |
| 64 | var o = {}; |
| 65 | o.valueOf = function() { return 1; }; |
| 66 | assertEquals(1, Math.min(2, 3, '1')); |
| 67 | assertEquals(1, Math.min(3, o, 2)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 68 | assertEquals(1, Math.min(o, 2)); |
| 69 | assertEquals(-Infinity, Infinity / Math.min(-0, +0)); |
| 70 | assertEquals(-Infinity, Infinity / Math.min(+0, -0)); |
| 71 | assertEquals(-Infinity, Infinity / Math.min(+0, -0, 1)); |
| 72 | assertEquals(-Infinity, Infinity / Math.min(-0, ZERO)); |
| 73 | assertEquals(-Infinity, Infinity / Math.min(ZERO, -0)); |
| 74 | assertEquals(-Infinity, Infinity / Math.min(ZERO, -0, 1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 75 | assertEquals(-1, Math.min(+0, -0, -1)); |
| 76 | assertEquals(-1, Math.min(-1, +0, -0)); |
| 77 | assertEquals(-1, Math.min(+0, -1, -0)); |
| 78 | assertEquals(-1, Math.min(-0, -1, +0)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 79 | assertEquals(NaN, Math.min('oxen')); |
| 80 | assertEquals(NaN, Math.min('oxen', 1)); |
| 81 | assertEquals(NaN, Math.min(1, 'oxen')); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | // Test Math.max(). |
| 85 | |
| 86 | assertEquals(Number.NEGATIVE_INFINITY, Math.max()); |
| 87 | assertEquals(1, Math.max(1)); |
| 88 | assertEquals(2, Math.max(1, 2)); |
| 89 | assertEquals(2, Math.max(2, 1)); |
| 90 | assertEquals(3, Math.max(1, 2, 3)); |
| 91 | assertEquals(3, Math.max(3, 2, 1)); |
| 92 | assertEquals(3, Math.max(2, 3, 1)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 93 | assertEquals(3.3, Math.max(1.1, 2.2, 3.3)); |
| 94 | assertEquals(3.3, Math.max(3.3, 2.2, 1.1)); |
| 95 | assertEquals(3.3, Math.max(2.2, 3.3, 1.1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 96 | |
| 97 | var o = {}; |
| 98 | o.valueOf = function() { return 3; }; |
| 99 | assertEquals(3, Math.max(2, '3', 1)); |
| 100 | assertEquals(3, Math.max(1, o, 2)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 101 | assertEquals(3, Math.max(o, 1)); |
| 102 | assertEquals(Infinity, Infinity / Math.max(-0, +0)); |
| 103 | assertEquals(Infinity, Infinity / Math.max(+0, -0)); |
| 104 | assertEquals(Infinity, Infinity / Math.max(+0, -0, -1)); |
| 105 | assertEquals(Infinity, Infinity / Math.max(-0, ZERO)); |
| 106 | assertEquals(Infinity, Infinity / Math.max(ZERO, -0)); |
| 107 | assertEquals(Infinity, Infinity / Math.max(ZERO, -0, -1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 108 | assertEquals(1, Math.max(+0, -0, +1)); |
| 109 | assertEquals(1, Math.max(+1, +0, -0)); |
| 110 | assertEquals(1, Math.max(+0, +1, -0)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 111 | assertEquals(1, Math.max(-0, +1, +0)); |
danno@chromium.org | 160a7b0 | 2011-04-18 15:51:38 +0000 | [diff] [blame] | 112 | assertEquals(NaN, Math.max('oxen')); |
| 113 | assertEquals(NaN, Math.max('oxen', 1)); |
| 114 | assertEquals(NaN, Math.max(1, 'oxen')); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 115 | |
| 116 | assertEquals(Infinity, 1/Math.max(ZERO, -0)); |
| 117 | assertEquals(Infinity, 1/Math.max(-0, ZERO)); |