blob: 356e888016955507fbcd3bbc2cd9336592d1a1b4 [file] [log] [blame]
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01001// 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 Murdoch097c5b22016-05-18 11:27:45 +010029// Flags: --expose-natives-as natives --allow-natives-syntax
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030// Test the SameValue and SameValueZero internal methods.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010031
32var obj1 = {x: 10, y: 11, z: "test"};
33var obj2 = {x: 10, y: 11, z: "test"};
34
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035var sameValue = Object.is;
Ben Murdoch097c5b22016-05-18 11:27:45 +010036var sameValueZero = function(x, y) { return %SameValueZero(x, y); }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037
38// Calls SameValue and SameValueZero and checks that their results match.
39function 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.
46function sameValueZeroOnly(a, b) {
47 var result = sameValueZero(a, b);
48 assertTrue(result && !sameValue(a, b));
49 return result;
50}
51
52assertTrue(sameValueBoth(0, 0));
53assertTrue(sameValueBoth(+0, +0));
54assertTrue(sameValueBoth(-0, -0));
55assertTrue(sameValueBoth(1, 1));
56assertTrue(sameValueBoth(2, 2));
57assertTrue(sameValueBoth(-1, -1));
58assertTrue(sameValueBoth(0.5, 0.5));
59assertTrue(sameValueBoth(true, true));
60assertTrue(sameValueBoth(false, false));
61assertTrue(sameValueBoth(NaN, NaN));
62assertTrue(sameValueBoth(null, null));
63assertTrue(sameValueBoth("foo", "foo"));
64assertTrue(sameValueBoth(obj1, obj1));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010065// Undefined values.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066assertTrue(sameValueBoth());
67assertTrue(sameValueBoth(undefined, undefined));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010068
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000069assertFalse(sameValueBoth(0,1));
70assertFalse(sameValueBoth("foo", "bar"));
71assertFalse(sameValueBoth(obj1, obj2));
72assertFalse(sameValueBoth(true, false));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010073
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074assertFalse(sameValueBoth(obj1, true));
75assertFalse(sameValueBoth(obj1, "foo"));
76assertFalse(sameValueBoth(obj1, 1));
77assertFalse(sameValueBoth(obj1, undefined));
78assertFalse(sameValueBoth(obj1, NaN));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080assertFalse(sameValueBoth(undefined, true));
81assertFalse(sameValueBoth(undefined, "foo"));
82assertFalse(sameValueBoth(undefined, 1));
83assertFalse(sameValueBoth(undefined, obj1));
84assertFalse(sameValueBoth(undefined, NaN));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010085
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000086assertFalse(sameValueBoth(NaN, true));
87assertFalse(sameValueBoth(NaN, "foo"));
88assertFalse(sameValueBoth(NaN, 1));
89assertFalse(sameValueBoth(NaN, obj1));
90assertFalse(sameValueBoth(NaN, undefined));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010091
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092assertFalse(sameValueBoth("foo", true));
93assertFalse(sameValueBoth("foo", 1));
94assertFalse(sameValueBoth("foo", obj1));
95assertFalse(sameValueBoth("foo", undefined));
96assertFalse(sameValueBoth("foo", NaN));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010097
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098assertFalse(sameValueBoth(true, 1));
99assertFalse(sameValueBoth(true, obj1));
100assertFalse(sameValueBoth(true, undefined));
101assertFalse(sameValueBoth(true, NaN));
102assertFalse(sameValueBoth(true, "foo"));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000104assertFalse(sameValueBoth(1, true));
105assertFalse(sameValueBoth(1, obj1));
106assertFalse(sameValueBoth(1, undefined));
107assertFalse(sameValueBoth(1, NaN));
108assertFalse(sameValueBoth(1, "foo"));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100109
110// Special string cases.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111assertFalse(sameValueBoth("1", 1));
112assertFalse(sameValueBoth("true", true));
113assertFalse(sameValueBoth("false", false));
114assertFalse(sameValueBoth("undefined", undefined));
115assertFalse(sameValueBoth("NaN", NaN));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100116
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117// SameValue considers -0 and +0 to be different; SameValueZero considers
118// -0 and +0 to be the same.
119assertTrue(sameValueZeroOnly(+0, -0));
120assertTrue(sameValueZeroOnly(-0, +0));