Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 1 | // Copyright 2010 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 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 29 | // Flags: --expose-natives-as natives --allow-natives-syntax |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | // Test the SameValue and SameValueZero internal methods. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 31 | |
| 32 | var obj1 = {x: 10, y: 11, z: "test"}; |
| 33 | var obj2 = {x: 10, y: 11, z: "test"}; |
| 34 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 35 | var sameValue = Object.is; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 36 | var sameValueZero = function(x, y) { return %SameValueZero(x, y); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | |
| 38 | // Calls SameValue and SameValueZero and checks that their results match. |
| 39 | function sameValueBoth(a, b) { |
| 40 | var result = sameValue(a, b); |
| 41 | assertTrue(result === sameValueZero(a, b)); |
| 42 | return result; |
| 43 | } |
| 44 | |
| 45 | // Calls SameValue and SameValueZero and checks that their results don't match. |
| 46 | function sameValueZeroOnly(a, b) { |
| 47 | var result = sameValueZero(a, b); |
| 48 | assertTrue(result && !sameValue(a, b)); |
| 49 | return result; |
| 50 | } |
| 51 | |
| 52 | assertTrue(sameValueBoth(0, 0)); |
| 53 | assertTrue(sameValueBoth(+0, +0)); |
| 54 | assertTrue(sameValueBoth(-0, -0)); |
| 55 | assertTrue(sameValueBoth(1, 1)); |
| 56 | assertTrue(sameValueBoth(2, 2)); |
| 57 | assertTrue(sameValueBoth(-1, -1)); |
| 58 | assertTrue(sameValueBoth(0.5, 0.5)); |
| 59 | assertTrue(sameValueBoth(true, true)); |
| 60 | assertTrue(sameValueBoth(false, false)); |
| 61 | assertTrue(sameValueBoth(NaN, NaN)); |
| 62 | assertTrue(sameValueBoth(null, null)); |
| 63 | assertTrue(sameValueBoth("foo", "foo")); |
| 64 | assertTrue(sameValueBoth(obj1, obj1)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 65 | // Undefined values. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | assertTrue(sameValueBoth()); |
| 67 | assertTrue(sameValueBoth(undefined, undefined)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 68 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | assertFalse(sameValueBoth(0,1)); |
| 70 | assertFalse(sameValueBoth("foo", "bar")); |
| 71 | assertFalse(sameValueBoth(obj1, obj2)); |
| 72 | assertFalse(sameValueBoth(true, false)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 73 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | assertFalse(sameValueBoth(obj1, true)); |
| 75 | assertFalse(sameValueBoth(obj1, "foo")); |
| 76 | assertFalse(sameValueBoth(obj1, 1)); |
| 77 | assertFalse(sameValueBoth(obj1, undefined)); |
| 78 | assertFalse(sameValueBoth(obj1, NaN)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 79 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | assertFalse(sameValueBoth(undefined, true)); |
| 81 | assertFalse(sameValueBoth(undefined, "foo")); |
| 82 | assertFalse(sameValueBoth(undefined, 1)); |
| 83 | assertFalse(sameValueBoth(undefined, obj1)); |
| 84 | assertFalse(sameValueBoth(undefined, NaN)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 85 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 86 | assertFalse(sameValueBoth(NaN, true)); |
| 87 | assertFalse(sameValueBoth(NaN, "foo")); |
| 88 | assertFalse(sameValueBoth(NaN, 1)); |
| 89 | assertFalse(sameValueBoth(NaN, obj1)); |
| 90 | assertFalse(sameValueBoth(NaN, undefined)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 91 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | assertFalse(sameValueBoth("foo", true)); |
| 93 | assertFalse(sameValueBoth("foo", 1)); |
| 94 | assertFalse(sameValueBoth("foo", obj1)); |
| 95 | assertFalse(sameValueBoth("foo", undefined)); |
| 96 | assertFalse(sameValueBoth("foo", NaN)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 97 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 98 | assertFalse(sameValueBoth(true, 1)); |
| 99 | assertFalse(sameValueBoth(true, obj1)); |
| 100 | assertFalse(sameValueBoth(true, undefined)); |
| 101 | assertFalse(sameValueBoth(true, NaN)); |
| 102 | assertFalse(sameValueBoth(true, "foo")); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 104 | assertFalse(sameValueBoth(1, true)); |
| 105 | assertFalse(sameValueBoth(1, obj1)); |
| 106 | assertFalse(sameValueBoth(1, undefined)); |
| 107 | assertFalse(sameValueBoth(1, NaN)); |
| 108 | assertFalse(sameValueBoth(1, "foo")); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 109 | |
| 110 | // Special string cases. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 111 | assertFalse(sameValueBoth("1", 1)); |
| 112 | assertFalse(sameValueBoth("true", true)); |
| 113 | assertFalse(sameValueBoth("false", false)); |
| 114 | assertFalse(sameValueBoth("undefined", undefined)); |
| 115 | assertFalse(sameValueBoth("NaN", NaN)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 116 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 117 | // SameValue considers -0 and +0 to be different; SameValueZero considers |
| 118 | // -0 and +0 to be the same. |
| 119 | assertTrue(sameValueZeroOnly(+0, -0)); |
| 120 | assertTrue(sameValueZeroOnly(-0, +0)); |