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
29// Flags: --expose-natives-as natives
30// Test the SameValue and SameValueZero internal methods.
31
32var obj1 = {x: 10, y: 11, z: "test"};
33var obj2 = {x: 10, y: 11, z: "test"};
34
35var sameValue = Object.is;
36var sameValueZero = natives.ImportNow("SameValueZero");
37
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));
65// Undefined values.
66assertTrue(sameValueBoth());
67assertTrue(sameValueBoth(undefined, undefined));
68
69assertFalse(sameValueBoth(0,1));
70assertFalse(sameValueBoth("foo", "bar"));
71assertFalse(sameValueBoth(obj1, obj2));
72assertFalse(sameValueBoth(true, false));
73
74assertFalse(sameValueBoth(obj1, true));
75assertFalse(sameValueBoth(obj1, "foo"));
76assertFalse(sameValueBoth(obj1, 1));
77assertFalse(sameValueBoth(obj1, undefined));
78assertFalse(sameValueBoth(obj1, NaN));
79
80assertFalse(sameValueBoth(undefined, true));
81assertFalse(sameValueBoth(undefined, "foo"));
82assertFalse(sameValueBoth(undefined, 1));
83assertFalse(sameValueBoth(undefined, obj1));
84assertFalse(sameValueBoth(undefined, NaN));
85
86assertFalse(sameValueBoth(NaN, true));
87assertFalse(sameValueBoth(NaN, "foo"));
88assertFalse(sameValueBoth(NaN, 1));
89assertFalse(sameValueBoth(NaN, obj1));
90assertFalse(sameValueBoth(NaN, undefined));
91
92assertFalse(sameValueBoth("foo", true));
93assertFalse(sameValueBoth("foo", 1));
94assertFalse(sameValueBoth("foo", obj1));
95assertFalse(sameValueBoth("foo", undefined));
96assertFalse(sameValueBoth("foo", NaN));
97
98assertFalse(sameValueBoth(true, 1));
99assertFalse(sameValueBoth(true, obj1));
100assertFalse(sameValueBoth(true, undefined));
101assertFalse(sameValueBoth(true, NaN));
102assertFalse(sameValueBoth(true, "foo"));
103
104assertFalse(sameValueBoth(1, true));
105assertFalse(sameValueBoth(1, obj1));
106assertFalse(sameValueBoth(1, undefined));
107assertFalse(sameValueBoth(1, NaN));
108assertFalse(sameValueBoth(1, "foo"));
109
110// Special string cases.
111assertFalse(sameValueBoth("1", 1));
112assertFalse(sameValueBoth("true", true));
113assertFalse(sameValueBoth("false", false));
114assertFalse(sameValueBoth("undefined", undefined));
115assertFalse(sameValueBoth("NaN", NaN));
116
117// 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));
121