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 --harmony-simd |
30 // Test the SameValue internal method. | 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; | |
Dan Ehrenberg
2015/07/10 20:59:22
SUggestion: Make a version of sameValueZero which
bbudge
2015/07/10 21:40:48
Good idea. Done.
| |
36 | 37 |
37 assertTrue(sameValue(0, 0)); | 38 // Calls SameValue and SameValueZero and verifies 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 assertTrue(sameValueBoth(0, 0)); |
45 assertTrue(sameValue(false, false)); | 46 assertTrue(sameValueBoth(+0, +0)); |
46 assertTrue(sameValue(NaN, NaN)); | 47 assertTrue(sameValueBoth(-0, -0)); |
47 assertTrue(sameValue(null, null)); | 48 assertTrue(sameValueBoth(1, 1)); |
48 assertTrue(sameValue("foo", "foo")); | 49 assertTrue(sameValueBoth(2, 2)); |
49 assertTrue(sameValue(obj1, obj1)); | 50 assertTrue(sameValueBoth(-1, -1)); |
51 assertTrue(sameValueBoth(0.5, 0.5)); | |
52 assertTrue(sameValueBoth(true, true)); | |
53 assertTrue(sameValueBoth(false, false)); | |
54 assertTrue(sameValueBoth(NaN, NaN)); | |
55 assertTrue(sameValueBoth(null, null)); | |
56 assertTrue(sameValueBoth("foo", "foo")); | |
57 assertTrue(sameValueBoth(obj1, obj1)); | |
50 // Undefined values. | 58 // Undefined values. |
51 assertTrue(sameValue()); | 59 assertTrue(sameValueBoth()); |
52 assertTrue(sameValue(undefined, undefined)); | 60 assertTrue(sameValueBoth(undefined, undefined)); |
53 | 61 |
54 assertFalse(sameValue(0,1)); | 62 assertFalse(sameValueBoth(0,1)); |
55 assertFalse(sameValue("foo", "bar")); | 63 assertFalse(sameValueBoth("foo", "bar")); |
56 assertFalse(sameValue(obj1, obj2)); | 64 assertFalse(sameValueBoth(obj1, obj2)); |
57 assertFalse(sameValue(true, false)); | 65 assertFalse(sameValueBoth(true, false)); |
58 | 66 |
59 assertFalse(sameValue(obj1, true)); | 67 assertFalse(sameValueBoth(obj1, true)); |
60 assertFalse(sameValue(obj1, "foo")); | 68 assertFalse(sameValueBoth(obj1, "foo")); |
61 assertFalse(sameValue(obj1, 1)); | 69 assertFalse(sameValueBoth(obj1, 1)); |
62 assertFalse(sameValue(obj1, undefined)); | 70 assertFalse(sameValueBoth(obj1, undefined)); |
63 assertFalse(sameValue(obj1, NaN)); | 71 assertFalse(sameValueBoth(obj1, NaN)); |
64 | 72 |
65 assertFalse(sameValue(undefined, true)); | 73 assertFalse(sameValueBoth(undefined, true)); |
66 assertFalse(sameValue(undefined, "foo")); | 74 assertFalse(sameValueBoth(undefined, "foo")); |
67 assertFalse(sameValue(undefined, 1)); | 75 assertFalse(sameValueBoth(undefined, 1)); |
68 assertFalse(sameValue(undefined, obj1)); | 76 assertFalse(sameValueBoth(undefined, obj1)); |
69 assertFalse(sameValue(undefined, NaN)); | 77 assertFalse(sameValueBoth(undefined, NaN)); |
70 | 78 |
71 assertFalse(sameValue(NaN, true)); | 79 assertFalse(sameValueBoth(NaN, true)); |
72 assertFalse(sameValue(NaN, "foo")); | 80 assertFalse(sameValueBoth(NaN, "foo")); |
73 assertFalse(sameValue(NaN, 1)); | 81 assertFalse(sameValueBoth(NaN, 1)); |
74 assertFalse(sameValue(NaN, obj1)); | 82 assertFalse(sameValueBoth(NaN, obj1)); |
75 assertFalse(sameValue(NaN, undefined)); | 83 assertFalse(sameValueBoth(NaN, undefined)); |
76 | 84 |
77 assertFalse(sameValue("foo", true)); | 85 assertFalse(sameValueBoth("foo", true)); |
78 assertFalse(sameValue("foo", 1)); | 86 assertFalse(sameValueBoth("foo", 1)); |
79 assertFalse(sameValue("foo", obj1)); | 87 assertFalse(sameValueBoth("foo", obj1)); |
80 assertFalse(sameValue("foo", undefined)); | 88 assertFalse(sameValueBoth("foo", undefined)); |
81 assertFalse(sameValue("foo", NaN)); | 89 assertFalse(sameValueBoth("foo", NaN)); |
82 | 90 |
83 assertFalse(sameValue(true, 1)); | 91 assertFalse(sameValueBoth(true, 1)); |
84 assertFalse(sameValue(true, obj1)); | 92 assertFalse(sameValueBoth(true, obj1)); |
85 assertFalse(sameValue(true, undefined)); | 93 assertFalse(sameValueBoth(true, undefined)); |
86 assertFalse(sameValue(true, NaN)); | 94 assertFalse(sameValueBoth(true, NaN)); |
87 assertFalse(sameValue(true, "foo")); | 95 assertFalse(sameValueBoth(true, "foo")); |
88 | 96 |
89 assertFalse(sameValue(1, true)); | 97 assertFalse(sameValueBoth(1, true)); |
90 assertFalse(sameValue(1, obj1)); | 98 assertFalse(sameValueBoth(1, obj1)); |
91 assertFalse(sameValue(1, undefined)); | 99 assertFalse(sameValueBoth(1, undefined)); |
92 assertFalse(sameValue(1, NaN)); | 100 assertFalse(sameValueBoth(1, NaN)); |
93 assertFalse(sameValue(1, "foo")); | 101 assertFalse(sameValueBoth(1, "foo")); |
94 | 102 |
95 // Special string cases. | 103 // Special string cases. |
96 assertFalse(sameValue("1", 1)); | 104 assertFalse(sameValueBoth("1", 1)); |
97 assertFalse(sameValue("true", true)); | 105 assertFalse(sameValueBoth("true", true)); |
98 assertFalse(sameValue("false", false)); | 106 assertFalse(sameValueBoth("false", false)); |
99 assertFalse(sameValue("undefined", undefined)); | 107 assertFalse(sameValueBoth("undefined", undefined)); |
100 assertFalse(sameValue("NaN", NaN)); | 108 assertFalse(sameValueBoth("NaN", NaN)); |
101 | 109 |
102 // -0 and +0 are should be different | 110 // SameValue considers -0 and +0 to be different. |
103 assertFalse(sameValue(+0, -0)); | 111 assertFalse(sameValue(+0, -0)); |
104 assertFalse(sameValue(-0, +0)); | 112 assertFalse(sameValue(-0, +0)); |
113 | |
114 // SameValueZero considers -0 and +0 to be the same. | |
115 assertTrue(sameValueZero(+0, -0)); | |
116 assertTrue(sameValueZero(-0, +0)); | |
117 | |
118 // SIMD value types. | |
119 // All lanes checked. | |
120 assertTrue(sameValueBoth(SIMD.float32x4(1, 2, 3, 4), | |
121 SIMD.float32x4(1, 2, 3, 4))); | |
122 assertFalse(sameValueBoth(SIMD.float32x4(1, 2, 3, 4), | |
123 SIMD.float32x4(NaN, 2, 3, 4))); | |
124 assertFalse(sameValueBoth(SIMD.float32x4(1, 2, 3, 4), | |
125 SIMD.float32x4(1, NaN, 3, 4))); | |
126 assertFalse(sameValueBoth(SIMD.float32x4(1, 2, 3, 4), | |
127 SIMD.float32x4(1, 2, NaN, 4))); | |
128 assertFalse(sameValueBoth(SIMD.float32x4(1, 2, 3, 4), | |
129 SIMD.float32x4(1, 2, 3, NaN))); | |
130 // Special values. | |
131 assertTrue(sameValueBoth(SIMD.float32x4(+0, -0, NaN, 1), | |
132 SIMD.float32x4(+0, -0, NaN, 1))); | |
133 assertFalse(sameValue(SIMD.float32x4(+0, -0, NaN, 1), | |
134 SIMD.float32x4(-0, -0, NaN, 1))); | |
135 assertFalse(sameValue(SIMD.float32x4(+0, -0, NaN, 1), | |
136 SIMD.float32x4(+0, +0, NaN, 1))); | |
137 assertTrue(sameValueZero(SIMD.float32x4(+0, -0, NaN, 1), | |
138 SIMD.float32x4(-0, -0, NaN, 1))); | |
139 assertTrue(sameValueZero(SIMD.float32x4(+0, -0, NaN, 1), | |
140 SIMD.float32x4(+0, +0, NaN, 1))); | |
OLD | NEW |