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
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.
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.
29 // Flags: --expose-natives-as natives
30 // Test the SameValue and SameValueZero internal methods.
32 var obj1 = {x: 10, y: 11, z: "test"};
33 var obj2 = {x: 10, y: 11, z: "test"};
35 var sameValue = natives.$sameValue;
36 var sameValueZero = natives.$sameValueZero;
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));
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));
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));
66 assertTrue(sameValueBoth());
67 assertTrue(sameValueBoth(undefined, undefined));
69 assertFalse(sameValueBoth(0,1));
70 assertFalse(sameValueBoth("foo", "bar"));
71 assertFalse(sameValueBoth(obj1, obj2));
72 assertFalse(sameValueBoth(true, false));
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));
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));
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));
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));
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"));
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"));
110 // Special string cases.
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));
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));