OLD | NEW |
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 | 28 |
29 // Flags: --expose-natives-as natives | 29 // Flags: --expose-natives-as natives |
30 // Test the SameValue and SameValueZero internal methods. | 30 // Test the SameValue internal method. |
31 | 31 |
32 var obj1 = {x: 10, y: 11, z: "test"}; | 32 var obj1 = {x: 10, y: 11, z: "test"}; |
33 var obj2 = {x: 10, y: 11, z: "test"}; | 33 var obj2 = {x: 10, y: 11, z: "test"}; |
34 | 34 |
35 var sameValue = natives.$sameValue; | 35 var sameValue = natives.$sameValue; |
36 var sameValueZero = natives.$sameValueZero; | |
37 | 36 |
38 // Calls SameValue and SameValueZero and checks that their results match. | 37 assertTrue(sameValue(0, 0)); |
39 function sameValueBoth(a, b) { | 38 assertTrue(sameValue(+0, +0)); |
40 var result = sameValue(a, b); | 39 assertTrue(sameValue(-0, -0)); |
41 assertTrue(result === sameValueZero(a, b)); | 40 assertTrue(sameValue(1, 1)); |
42 return result; | 41 assertTrue(sameValue(2, 2)); |
43 } | 42 assertTrue(sameValue(-1, -1)); |
| 43 assertTrue(sameValue(0.5, 0.5)); |
| 44 assertTrue(sameValue(true, true)); |
| 45 assertTrue(sameValue(false, false)); |
| 46 assertTrue(sameValue(NaN, NaN)); |
| 47 assertTrue(sameValue(null, null)); |
| 48 assertTrue(sameValue("foo", "foo")); |
| 49 assertTrue(sameValue(obj1, obj1)); |
| 50 // Undefined values. |
| 51 assertTrue(sameValue()); |
| 52 assertTrue(sameValue(undefined, undefined)); |
44 | 53 |
45 // Calls SameValue and SameValueZero and checks that their results don't match. | 54 assertFalse(sameValue(0,1)); |
46 function sameValueZeroOnly(a, b) { | 55 assertFalse(sameValue("foo", "bar")); |
47 var result = sameValueZero(a, b); | 56 assertFalse(sameValue(obj1, obj2)); |
48 assertTrue(result && !sameValue(a, b)); | 57 assertFalse(sameValue(true, false)); |
49 return result; | |
50 } | |
51 | 58 |
52 assertTrue(sameValueBoth(0, 0)); | 59 assertFalse(sameValue(obj1, true)); |
53 assertTrue(sameValueBoth(+0, +0)); | 60 assertFalse(sameValue(obj1, "foo")); |
54 assertTrue(sameValueBoth(-0, -0)); | 61 assertFalse(sameValue(obj1, 1)); |
55 assertTrue(sameValueBoth(1, 1)); | 62 assertFalse(sameValue(obj1, undefined)); |
56 assertTrue(sameValueBoth(2, 2)); | 63 assertFalse(sameValue(obj1, NaN)); |
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)); | |
65 // Undefined values. | |
66 assertTrue(sameValueBoth()); | |
67 assertTrue(sameValueBoth(undefined, undefined)); | |
68 | 64 |
69 assertFalse(sameValueBoth(0,1)); | 65 assertFalse(sameValue(undefined, true)); |
70 assertFalse(sameValueBoth("foo", "bar")); | 66 assertFalse(sameValue(undefined, "foo")); |
71 assertFalse(sameValueBoth(obj1, obj2)); | 67 assertFalse(sameValue(undefined, 1)); |
72 assertFalse(sameValueBoth(true, false)); | 68 assertFalse(sameValue(undefined, obj1)); |
| 69 assertFalse(sameValue(undefined, NaN)); |
73 | 70 |
74 assertFalse(sameValueBoth(obj1, true)); | 71 assertFalse(sameValue(NaN, true)); |
75 assertFalse(sameValueBoth(obj1, "foo")); | 72 assertFalse(sameValue(NaN, "foo")); |
76 assertFalse(sameValueBoth(obj1, 1)); | 73 assertFalse(sameValue(NaN, 1)); |
77 assertFalse(sameValueBoth(obj1, undefined)); | 74 assertFalse(sameValue(NaN, obj1)); |
78 assertFalse(sameValueBoth(obj1, NaN)); | 75 assertFalse(sameValue(NaN, undefined)); |
79 | 76 |
80 assertFalse(sameValueBoth(undefined, true)); | 77 assertFalse(sameValue("foo", true)); |
81 assertFalse(sameValueBoth(undefined, "foo")); | 78 assertFalse(sameValue("foo", 1)); |
82 assertFalse(sameValueBoth(undefined, 1)); | 79 assertFalse(sameValue("foo", obj1)); |
83 assertFalse(sameValueBoth(undefined, obj1)); | 80 assertFalse(sameValue("foo", undefined)); |
84 assertFalse(sameValueBoth(undefined, NaN)); | 81 assertFalse(sameValue("foo", NaN)); |
85 | 82 |
86 assertFalse(sameValueBoth(NaN, true)); | 83 assertFalse(sameValue(true, 1)); |
87 assertFalse(sameValueBoth(NaN, "foo")); | 84 assertFalse(sameValue(true, obj1)); |
88 assertFalse(sameValueBoth(NaN, 1)); | 85 assertFalse(sameValue(true, undefined)); |
89 assertFalse(sameValueBoth(NaN, obj1)); | 86 assertFalse(sameValue(true, NaN)); |
90 assertFalse(sameValueBoth(NaN, undefined)); | 87 assertFalse(sameValue(true, "foo")); |
91 | 88 |
92 assertFalse(sameValueBoth("foo", true)); | 89 assertFalse(sameValue(1, true)); |
93 assertFalse(sameValueBoth("foo", 1)); | 90 assertFalse(sameValue(1, obj1)); |
94 assertFalse(sameValueBoth("foo", obj1)); | 91 assertFalse(sameValue(1, undefined)); |
95 assertFalse(sameValueBoth("foo", undefined)); | 92 assertFalse(sameValue(1, NaN)); |
96 assertFalse(sameValueBoth("foo", NaN)); | 93 assertFalse(sameValue(1, "foo")); |
97 | |
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")); | |
103 | |
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")); | |
109 | 94 |
110 // Special string cases. | 95 // Special string cases. |
111 assertFalse(sameValueBoth("1", 1)); | 96 assertFalse(sameValue("1", 1)); |
112 assertFalse(sameValueBoth("true", true)); | 97 assertFalse(sameValue("true", true)); |
113 assertFalse(sameValueBoth("false", false)); | 98 assertFalse(sameValue("false", false)); |
114 assertFalse(sameValueBoth("undefined", undefined)); | 99 assertFalse(sameValue("undefined", undefined)); |
115 assertFalse(sameValueBoth("NaN", NaN)); | 100 assertFalse(sameValue("NaN", NaN)); |
116 | 101 |
117 // SameValue considers -0 and +0 to be different; SameValueZero considers | 102 // -0 and +0 are should be different |
118 // -0 and +0 to be the same. | 103 assertFalse(sameValue(+0, -0)); |
119 assertTrue(sameValueZeroOnly(+0, -0)); | 104 assertFalse(sameValue(-0, +0)); |
120 assertTrue(sameValueZeroOnly(-0, +0)); | |
OLD | NEW |