blob: 40d9a76b48ca4c0701418b03cc986f2d8dacf3ec [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028// Flags: --harmony-simd
29
Steve Blocka7e24c12009-10-30 11:49:00 +000030/**
31 * This test uses assert{True,False}(... == ...) instead of
32 * assertEquals(..., ...) to not rely on the details of the
33 * implementation of assertEquals.
34 */
35
Ben Murdoch257744e2011-11-30 15:57:28 +000036function testEqual(a, b) {
37 assertTrue(a == b);
38 assertTrue(b == a);
39 assertFalse(a != b);
40 assertFalse(b != a);
41}
Steve Blocka7e24c12009-10-30 11:49:00 +000042
Ben Murdoch257744e2011-11-30 15:57:28 +000043function testNotEqual(a, b) {
44 assertFalse(a == b);
45 assertFalse(b == a);
46 assertTrue(a != b);
47 assertTrue(b != a);
48}
Steve Blocka7e24c12009-10-30 11:49:00 +000049
Ben Murdoch257744e2011-11-30 15:57:28 +000050// Object where ToPrimitive returns value.
51function Wrapper(value) {
52 this.value = value;
53 this.valueOf = function () { return this.value; };
54}
Steve Blocka7e24c12009-10-30 11:49:00 +000055
Ben Murdoch257744e2011-11-30 15:57:28 +000056// Object where ToPrimitive returns value by failover to toString when
57// valueOf isn't a function.
58function Wrapper2(value) {
59 this.value = value;
60 this.valueOf = null;
61 this.toString = function () { return this.value; };
62}
Steve Blocka7e24c12009-10-30 11:49:00 +000063
Steve Blocka7e24c12009-10-30 11:49:00 +000064
Ben Murdoch257744e2011-11-30 15:57:28 +000065// Compare values of same type.
Steve Blocka7e24c12009-10-30 11:49:00 +000066
Ben Murdoch257744e2011-11-30 15:57:28 +000067// Numbers are equal if same, unless NaN, which isn't equal to anything, and
68// +/-0 being equal.
69
70testNotEqual(NaN, NaN);
71testNotEqual(NaN, 0);
72testNotEqual(NaN, Infinity);
73
74testEqual(Number.MAX_VALUE, Number.MAX_VALUE);
75testEqual(Number.MIN_VALUE, Number.MIN_VALUE);
76testEqual(Infinity, Infinity);
77testEqual(-Infinity, -Infinity);
78
79testEqual(0, 0);
80testEqual(0, -0);
81testEqual(-0, -0);
82
83testNotEqual(0.9, 1);
84testNotEqual(0.999999, 1);
85testNotEqual(0.9999999999, 1);
86testNotEqual(0.9999999999999, 1);
87
88// Strings are equal if containing the same code points.
89
90testEqual('hello', 'hello');
91testEqual('hello', 'hel' + 'lo');
92testEqual('', '');
93testEqual('\u0020\x20', ' '); // Escapes are not part of the value.
94
95// Booleans are equal if they are the same.
96
97testEqual(true, true);
98testEqual(false, false);
99testNotEqual(true, false);
100
101// Null and undefined are equal to themselves.
102
103testEqual(null, null);
104testEqual(undefined, undefined);
105
106// Objects are equal if they are the same object only.
107
108testEqual(Math, Math);
109testEqual(Object.prototype, Object.prototype);
110
Steve Blocka7e24c12009-10-30 11:49:00 +0000111
112(function () {
113 var x = new Wrapper(null);
114 var y = x, z = x;
Ben Murdoch257744e2011-11-30 15:57:28 +0000115 testEqual(y, x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000116})();
117
118(function () {
119 var x = new Boolean(true);
120 var y = x, z = x;
Ben Murdoch257744e2011-11-30 15:57:28 +0000121 testEqual(y, x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000122})();
123
124(function () {
125 var x = new Boolean(false);
126 var y = x, z = x;
Ben Murdoch257744e2011-11-30 15:57:28 +0000127 testEqual(y, x);
Steve Blocka7e24c12009-10-30 11:49:00 +0000128})();
129
Ben Murdoch257744e2011-11-30 15:57:28 +0000130// Test comparing values of different types.
Steve Blocka7e24c12009-10-30 11:49:00 +0000131
Ben Murdoch257744e2011-11-30 15:57:28 +0000132// Null and undefined are equal to each-other, and to nothing else.
133testEqual(null, undefined);
134testEqual(undefined, null);
Steve Blocka7e24c12009-10-30 11:49:00 +0000135
Ben Murdoch257744e2011-11-30 15:57:28 +0000136testNotEqual(null, new Wrapper(null));
137testNotEqual(null, 0);
138testNotEqual(null, false);
139testNotEqual(null, "");
140testNotEqual(null, new Object());
141testNotEqual(undefined, new Wrapper(undefined));
142testNotEqual(undefined, 0);
143testNotEqual(undefined, false);
144testNotEqual(undefined, "");
145testNotEqual(undefined, new Object());
Steve Blocka7e24c12009-10-30 11:49:00 +0000146
Ben Murdoch257744e2011-11-30 15:57:28 +0000147// Numbers compared to Strings will convert the string to a number using
148// the internal ToNumber conversion.
Steve Blocka7e24c12009-10-30 11:49:00 +0000149
Ben Murdoch257744e2011-11-30 15:57:28 +0000150testEqual(1, '1');
151testEqual(255, '0xff');
152testEqual(0, '\r'); // ToNumber ignores tailing and trailing whitespace.
153testEqual(1e19, '1e19');
154testEqual(Infinity, "Infinity");
Steve Blocka7e24c12009-10-30 11:49:00 +0000155
Ben Murdoch257744e2011-11-30 15:57:28 +0000156// Booleans compared to anything else will be converted to numbers.
157testEqual(false, 0);
158testEqual(true, 1);
159testEqual(false, "0"); // String also converted to number.
160testEqual(true, "1");
161
162// Objects compared to Number or String (or Boolean, since that's converted
163// to Number too) is converted to primitive using ToPrimitive with NO HINT.
164// Having no hint means Date gets a string hint, and everything else gets
165// a number hint.
166
167testEqual(new Boolean(true), true);
168testEqual(new Boolean(true), 1); // First to primtive boolean, then to number.
169testEqual(new Boolean(false), false);
170testEqual(new Boolean(false), 0);
171
172testEqual(new Wrapper(true), true);
173testEqual(new Wrapper(true), 1);
174testEqual(new Wrapper(false), false);
175testEqual(new Wrapper(false), 0);
176
177testEqual(new Wrapper2(true), true);
178testEqual(new Wrapper2(true), 1);
179testEqual(new Wrapper2(false), false);
180testEqual(new Wrapper2(false), 0);
181
182testEqual(new Number(1), true);
183testEqual(new Number(1), 1);
184testEqual(new Number(0), false);
185testEqual(new Number(0), 0);
186
187// Date objects convert to string, not number (and the string does not
188// convert to the number).
189testEqual(new Date(42), String(new Date(42)));
190testNotEqual(new Date(42), Number(new Date(42)));
191var dnow = new Date();
192testEqual(dnow, dnow);
193testEqual(dnow, String(dnow));
194testNotEqual(dnow, Number(dnow));
195
196// Doesn't just call toString, but uses ToPrimitive which tries toString first
197// and valueOf second.
198dnow.toString = null;
199testEqual(dnow, Number(dnow));
200dnow.valueOf = function () { return "42"; };
201testEqual(dnow, 42);
202dnow.toString = function () { return "1"; };
203testEqual(dnow, true);
204
205
206// Objects compared to other objects, or to null and undefined, are not
207// converted to primitive.
208testNotEqual(new Wrapper(null), new Wrapper(null));
209testNotEqual(new Boolean(true), new Boolean(true));
210testNotEqual(new Boolean(false), new Boolean(false));
211testNotEqual(new String("a"), new String("a"));
212testNotEqual(new Number(42), new Number(42));
213testNotEqual(new Date(42), new Date(42));
214testNotEqual(new Array(42), new Array(42));
215testNotEqual(new Object(), new Object());
216
217// Object that can't be converted to primitive.
218var badObject = {
219 valueOf: null,
220 toString: function() {
221 return this; // Not primitive.
222 }
223};
224
225testEqual(badObject, badObject);
226testNotEqual(badObject, {});
227testNotEqual(badObject, null);
228testNotEqual(badObject, undefined);
229// Forcing conversion will throw.
230function testBadConversion(value) {
231 assertThrows(function() { return badObject == value; });
232 assertThrows(function() { return badObject != value; });
233 assertThrows(function() { return value == badObject; });
234 assertThrows(function() { return value != badObject; });
Steve Blocka7e24c12009-10-30 11:49:00 +0000235}
Ben Murdoch257744e2011-11-30 15:57:28 +0000236testBadConversion(0);
237testBadConversion("string");
238testBadConversion(true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000239
240var s = Symbol();
241testEqual(s, s);
242testEqual(Object(s), s);
243testEqual(new Wrapper(s), s);
244testNotEqual(Object(s), Object(s));
245
246var simd = SIMD.Float32x4(1, 2, 3, 4);
247testEqual(simd, simd);
248testEqual(Object(simd), simd);
249testEqual(new Wrapper(simd), simd);
250testNotEqual(Object(simd), Object(simd));