OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/base/functional.h" | 5 #include "src/base/functional.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "test/unittests/test-utils.h" | 10 #include "test/unittests/test-utils.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 90 } |
91 std::set<size_t> hs; | 91 std::set<size_t> hs; |
92 for (const auto& v : vs) { | 92 for (const auto& v : vs) { |
93 hash<TypeParam> h; | 93 hash<TypeParam> h; |
94 hs.insert(h(v)); | 94 hs.insert(h(v)); |
95 } | 95 } |
96 EXPECT_LE(vs.size() / 4u, hs.size()); | 96 EXPECT_LE(vs.size() / 4u, hs.size()); |
97 } | 97 } |
98 | 98 |
99 | 99 |
| 100 TYPED_TEST(FunctionalTest, HashValueArrayUsesHashRange) { |
| 101 TypeParam values[128]; |
| 102 this->rng()->NextBytes(&values, sizeof(values)); |
| 103 EXPECT_EQ(hash_range(values, values + arraysize(values)), hash_value(values)); |
| 104 } |
| 105 |
| 106 |
| 107 TYPED_TEST(FunctionalTest, BitEqualTo) { |
| 108 bit_equal_to<TypeParam> pred; |
| 109 for (size_t i = 0; i < 128; ++i) { |
| 110 TypeParam v1, v2; |
| 111 this->rng()->NextBytes(&v1, sizeof(v1)); |
| 112 this->rng()->NextBytes(&v2, sizeof(v2)); |
| 113 EXPECT_PRED2(pred, v1, v1); |
| 114 EXPECT_PRED2(pred, v2, v2); |
| 115 EXPECT_EQ(memcmp(&v1, &v2, sizeof(TypeParam)) == 0, pred(v1, v2)); |
| 116 } |
| 117 } |
| 118 |
| 119 |
| 120 TYPED_TEST(FunctionalTest, BitEqualToImpliesSameBitHash) { |
| 121 bit_hash<TypeParam> h; |
| 122 bit_equal_to<TypeParam> e; |
| 123 TypeParam values[32]; |
| 124 this->rng()->NextBytes(&values, sizeof(values)); |
| 125 TRACED_FOREACH(TypeParam, v1, values) { |
| 126 TRACED_FOREACH(TypeParam, v2, values) { |
| 127 if (e(v1, v2)) EXPECT_EQ(h(v1), h(v2)); |
| 128 } |
| 129 } |
| 130 } |
| 131 |
| 132 |
100 namespace { | 133 namespace { |
101 | 134 |
102 struct Foo { | 135 struct Foo { |
103 int x; | 136 int x; |
104 double y; | 137 double y; |
105 }; | 138 }; |
106 | 139 |
107 | 140 |
108 size_t hash_value(Foo const& v) { return hash_combine(v.x, v.y); } | 141 size_t hash_value(Foo const& v) { return hash_combine(v.x, v.y); } |
109 | 142 |
110 } // namespace | 143 } // namespace |
111 | 144 |
112 | 145 |
113 TEST(FunctionalTest, HashUsesArgumentDependentLookup) { | 146 TEST(FunctionalTest, HashUsesArgumentDependentLookup) { |
114 const int kIntValues[] = {std::numeric_limits<int>::min(), -1, 0, 1, 42, | 147 const int kIntValues[] = {std::numeric_limits<int>::min(), -1, 0, 1, 42, |
115 std::numeric_limits<int>::max()}; | 148 std::numeric_limits<int>::max()}; |
116 const double kDoubleValues[] = { | 149 const double kDoubleValues[] = { |
117 std::numeric_limits<double>::min(), -1, -0, 0, 1, | 150 std::numeric_limits<double>::min(), -1, -0, 0, 1, |
118 std::numeric_limits<double>::max()}; | 151 std::numeric_limits<double>::max()}; |
119 TRACED_FOREACH(int, x, kIntValues) { | 152 TRACED_FOREACH(int, x, kIntValues) { |
120 TRACED_FOREACH(double, y, kDoubleValues) { | 153 TRACED_FOREACH(double, y, kDoubleValues) { |
121 hash<Foo> h; | 154 hash<Foo> h; |
122 Foo foo = {x, y}; | 155 Foo foo = {x, y}; |
123 EXPECT_EQ(hash_combine(x, y), h(foo)); | 156 EXPECT_EQ(hash_combine(x, y), h(foo)); |
124 } | 157 } |
125 } | 158 } |
126 } | 159 } |
127 | 160 |
| 161 |
| 162 TEST(FunctionalTest, BitEqualToStructWithoutComparisonOperator) { |
| 163 Foo const foo1 = {0, 0.0}; |
| 164 Foo const foo2 = {0, 0.0}; |
| 165 Foo const foo3 = {1, 0.0}; |
| 166 Foo const foo4 = {0, -0.0}; |
| 167 bit_equal_to<Foo> pred; |
| 168 EXPECT_TRUE(pred(foo1, foo2)); |
| 169 EXPECT_FALSE(pred(foo1, foo3)); |
| 170 EXPECT_FALSE(pred(foo2, foo3)); |
| 171 EXPECT_FALSE(pred(foo3, foo4)); |
| 172 } |
| 173 |
| 174 |
| 175 TEST(FunctionalTest, BitEqualToFloat) { |
| 176 bit_equal_to<float> pred; |
| 177 EXPECT_FALSE(pred(0.0f, -0.0f)); |
| 178 EXPECT_FALSE(pred(-0.0f, 0.0f)); |
| 179 float const qNaN = std::numeric_limits<float>::quiet_NaN(); |
| 180 float const sNaN = std::numeric_limits<float>::signaling_NaN(); |
| 181 EXPECT_PRED2(pred, qNaN, qNaN); |
| 182 EXPECT_PRED2(pred, sNaN, sNaN); |
| 183 } |
| 184 |
| 185 |
| 186 TEST(FunctionalTest, BitEqualToDouble) { |
| 187 bit_equal_to<double> pred; |
| 188 EXPECT_FALSE(pred(0.0, -0.0)); |
| 189 EXPECT_FALSE(pred(-0.0, 0.0)); |
| 190 double const qNaN = std::numeric_limits<double>::quiet_NaN(); |
| 191 double const sNaN = std::numeric_limits<double>::signaling_NaN(); |
| 192 EXPECT_PRED2(pred, qNaN, qNaN); |
| 193 EXPECT_PRED2(pred, sNaN, sNaN); |
| 194 } |
| 195 |
128 } // namespace base | 196 } // namespace base |
129 } // namespace v8 | 197 } // namespace v8 |
OLD | NEW |