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 internal method. | 30 // Test the SameValue and SameValueZero internal methods. |
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; |
36 | 37 |
37 assertTrue(sameValue(0, 0)); | 38 // Calls SameValue and SameValueZero and checks that their results match. |
38 assertTrue(sameValue(+0, +0)); | 39 function sameValueBoth(a, b) { |
39 assertTrue(sameValue(-0, -0)); | 40 var result = sameValue(a, b); |
40 assertTrue(sameValue(1, 1)); | 41 assertTrue(result === sameValueZero(a, b)); |
41 assertTrue(sameValue(2, 2)); | 42 return result; |
42 assertTrue(sameValue(-1, -1)); | 43 } |
43 assertTrue(sameValue(0.5, 0.5)); | 44 |
44 assertTrue(sameValue(true, true)); | 45 // Calls SameValue and SameValueZero and checks that their results don't match. |
45 assertTrue(sameValue(false, false)); | 46 function sameValueZeroOnly(a, b) { |
46 assertTrue(sameValue(NaN, NaN)); | 47 var result = sameValueZero(a, b); |
47 assertTrue(sameValue(null, null)); | 48 assertTrue(result && !sameValue(a, b)); |
48 assertTrue(sameValue("foo", "foo")); | 49 return result; |
49 assertTrue(sameValue(obj1, obj1)); | 50 } |
| 51 |
| 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)); |
50 // Undefined values. | 65 // Undefined values. |
51 assertTrue(sameValue()); | 66 assertTrue(sameValueBoth()); |
52 assertTrue(sameValue(undefined, undefined)); | 67 assertTrue(sameValueBoth(undefined, undefined)); |
53 | 68 |
54 assertFalse(sameValue(0,1)); | 69 assertFalse(sameValueBoth(0,1)); |
55 assertFalse(sameValue("foo", "bar")); | 70 assertFalse(sameValueBoth("foo", "bar")); |
56 assertFalse(sameValue(obj1, obj2)); | 71 assertFalse(sameValueBoth(obj1, obj2)); |
57 assertFalse(sameValue(true, false)); | 72 assertFalse(sameValueBoth(true, false)); |
58 | 73 |
59 assertFalse(sameValue(obj1, true)); | 74 assertFalse(sameValueBoth(obj1, true)); |
60 assertFalse(sameValue(obj1, "foo")); | 75 assertFalse(sameValueBoth(obj1, "foo")); |
61 assertFalse(sameValue(obj1, 1)); | 76 assertFalse(sameValueBoth(obj1, 1)); |
62 assertFalse(sameValue(obj1, undefined)); | 77 assertFalse(sameValueBoth(obj1, undefined)); |
63 assertFalse(sameValue(obj1, NaN)); | 78 assertFalse(sameValueBoth(obj1, NaN)); |
64 | 79 |
65 assertFalse(sameValue(undefined, true)); | 80 assertFalse(sameValueBoth(undefined, true)); |
66 assertFalse(sameValue(undefined, "foo")); | 81 assertFalse(sameValueBoth(undefined, "foo")); |
67 assertFalse(sameValue(undefined, 1)); | 82 assertFalse(sameValueBoth(undefined, 1)); |
68 assertFalse(sameValue(undefined, obj1)); | 83 assertFalse(sameValueBoth(undefined, obj1)); |
69 assertFalse(sameValue(undefined, NaN)); | 84 assertFalse(sameValueBoth(undefined, NaN)); |
70 | 85 |
71 assertFalse(sameValue(NaN, true)); | 86 assertFalse(sameValueBoth(NaN, true)); |
72 assertFalse(sameValue(NaN, "foo")); | 87 assertFalse(sameValueBoth(NaN, "foo")); |
73 assertFalse(sameValue(NaN, 1)); | 88 assertFalse(sameValueBoth(NaN, 1)); |
74 assertFalse(sameValue(NaN, obj1)); | 89 assertFalse(sameValueBoth(NaN, obj1)); |
75 assertFalse(sameValue(NaN, undefined)); | 90 assertFalse(sameValueBoth(NaN, undefined)); |
76 | 91 |
77 assertFalse(sameValue("foo", true)); | 92 assertFalse(sameValueBoth("foo", true)); |
78 assertFalse(sameValue("foo", 1)); | 93 assertFalse(sameValueBoth("foo", 1)); |
79 assertFalse(sameValue("foo", obj1)); | 94 assertFalse(sameValueBoth("foo", obj1)); |
80 assertFalse(sameValue("foo", undefined)); | 95 assertFalse(sameValueBoth("foo", undefined)); |
81 assertFalse(sameValue("foo", NaN)); | 96 assertFalse(sameValueBoth("foo", NaN)); |
82 | 97 |
83 assertFalse(sameValue(true, 1)); | 98 assertFalse(sameValueBoth(true, 1)); |
84 assertFalse(sameValue(true, obj1)); | 99 assertFalse(sameValueBoth(true, obj1)); |
85 assertFalse(sameValue(true, undefined)); | 100 assertFalse(sameValueBoth(true, undefined)); |
86 assertFalse(sameValue(true, NaN)); | 101 assertFalse(sameValueBoth(true, NaN)); |
87 assertFalse(sameValue(true, "foo")); | 102 assertFalse(sameValueBoth(true, "foo")); |
88 | 103 |
89 assertFalse(sameValue(1, true)); | 104 assertFalse(sameValueBoth(1, true)); |
90 assertFalse(sameValue(1, obj1)); | 105 assertFalse(sameValueBoth(1, obj1)); |
91 assertFalse(sameValue(1, undefined)); | 106 assertFalse(sameValueBoth(1, undefined)); |
92 assertFalse(sameValue(1, NaN)); | 107 assertFalse(sameValueBoth(1, NaN)); |
93 assertFalse(sameValue(1, "foo")); | 108 assertFalse(sameValueBoth(1, "foo")); |
94 | 109 |
95 // Special string cases. | 110 // Special string cases. |
96 assertFalse(sameValue("1", 1)); | 111 assertFalse(sameValueBoth("1", 1)); |
97 assertFalse(sameValue("true", true)); | 112 assertFalse(sameValueBoth("true", true)); |
98 assertFalse(sameValue("false", false)); | 113 assertFalse(sameValueBoth("false", false)); |
99 assertFalse(sameValue("undefined", undefined)); | 114 assertFalse(sameValueBoth("undefined", undefined)); |
100 assertFalse(sameValue("NaN", NaN)); | 115 assertFalse(sameValueBoth("NaN", NaN)); |
101 | 116 |
102 // -0 and +0 are should be different | 117 // SameValue considers -0 and +0 to be different; SameValueZero considers |
103 assertFalse(sameValue(+0, -0)); | 118 // -0 and +0 to be the same. |
104 assertFalse(sameValue(-0, +0)); | 119 assertTrue(sameValueZeroOnly(+0, -0)); |
| 120 assertTrue(sameValueZeroOnly(-0, +0)); |
OLD | NEW |