| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | 5 #include <algorithm> |
| 6 #include <cstring> | 6 #include <cstring> |
| 7 #include <map> | 7 #include <map> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "platform/assert.h" | 13 #include "platform/assert.h" |
| 14 #include "vm/unit_test.h" | 14 #include "vm/unit_test.h" |
| 15 #include "vm/hash_table.h" | 15 #include "vm/hash_table.h" |
| 16 | 16 |
| 17 namespace dart { | 17 namespace dart { |
| 18 | 18 |
| 19 // Various ways to look up strings. Uses length as the hash code to make it | 19 // Various ways to look up strings. Uses length as the hash code to make it |
| 20 // easy to engineer collisions. | 20 // easy to engineer collisions. |
| 21 class TestTraits { | 21 class TestTraits { |
| 22 public: | 22 public: |
| 23 static const char* Name() { return "TestTraits"; } | 23 static const char* Name() { return "TestTraits"; } |
| 24 static bool ReportStats() { return false; } | 24 static bool ReportStats() { return false; } |
| 25 | 25 |
| 26 static bool IsMatch(const char* key, const Object& obj) { | 26 static bool IsMatch(const char* key, const Object& obj) { |
| 27 return String::Cast(obj).Equals(key); | 27 return String::Cast(obj).Equals(key); |
| 28 } | 28 } |
| 29 static uword Hash(const char* key) { | 29 static uword Hash(const char* key) { return static_cast<uword>(strlen(key)); } |
| 30 return static_cast<uword>(strlen(key)); | |
| 31 } | |
| 32 static bool IsMatch(const Object& a, const Object& b) { | 30 static bool IsMatch(const Object& a, const Object& b) { |
| 33 return a.IsString() && b.IsString() && | 31 return a.IsString() && b.IsString() && |
| 34 String::Cast(a).Equals(String::Cast(b)); | 32 String::Cast(a).Equals(String::Cast(b)); |
| 35 } | 33 } |
| 36 static uword Hash(const Object& obj) { | 34 static uword Hash(const Object& obj) { return String::Cast(obj).Length(); } |
| 37 return String::Cast(obj).Length(); | 35 static RawObject* NewKey(const char* key) { return String::New(key); } |
| 38 } | |
| 39 static RawObject* NewKey(const char* key) { | |
| 40 return String::New(key); | |
| 41 } | |
| 42 }; | 36 }; |
| 43 | 37 |
| 44 | 38 |
| 45 template<typename Table> | 39 template <typename Table> |
| 46 void Validate(const Table& table) { | 40 void Validate(const Table& table) { |
| 47 // Verify consistency of entry state tracking. | 41 // Verify consistency of entry state tracking. |
| 48 intptr_t num_entries = table.NumEntries(); | 42 intptr_t num_entries = table.NumEntries(); |
| 49 intptr_t num_unused = table.NumUnused(); | 43 intptr_t num_unused = table.NumUnused(); |
| 50 intptr_t num_occupied = table.NumOccupied(); | 44 intptr_t num_occupied = table.NumOccupied(); |
| 51 intptr_t num_deleted = table.NumDeleted(); | 45 intptr_t num_deleted = table.NumDeleted(); |
| 52 for (intptr_t i = 0; i < num_entries; ++i) { | 46 for (intptr_t i = 0; i < num_entries; ++i) { |
| 53 EXPECT_EQ(1, table.IsUnused(i) + table.IsOccupied(i) + table.IsDeleted(i)); | 47 EXPECT_EQ(1, table.IsUnused(i) + table.IsOccupied(i) + table.IsDeleted(i)); |
| 54 num_unused -= table.IsUnused(i); | 48 num_unused -= table.IsUnused(i); |
| 55 num_occupied -= table.IsOccupied(i); | 49 num_occupied -= table.IsOccupied(i); |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 for (intptr_t i = 0; i < str.Length(); ++i) { | 122 for (intptr_t i = 0; i < str.Length(); ++i) { |
| 129 result += static_cast<char>(str.CharAt(i)); | 123 result += static_cast<char>(str.CharAt(i)); |
| 130 } | 124 } |
| 131 return result; | 125 return result; |
| 132 } | 126 } |
| 133 | 127 |
| 134 | 128 |
| 135 // Checks that 'expected' and 'actual' are equal sets. If 'ordered' is true, | 129 // Checks that 'expected' and 'actual' are equal sets. If 'ordered' is true, |
| 136 // it also verifies that their iteration orders match, i.e., that actual's | 130 // it also verifies that their iteration orders match, i.e., that actual's |
| 137 // insertion order coincides with lexicographic order. | 131 // insertion order coincides with lexicographic order. |
| 138 template<typename Set> | 132 template <typename Set> |
| 139 void VerifyStringSetsEqual(const std::set<std::string>& expected, | 133 void VerifyStringSetsEqual(const std::set<std::string>& expected, |
| 140 const Set& actual, | 134 const Set& actual, |
| 141 bool ordered) { | 135 bool ordered) { |
| 142 // Get actual keys in iteration order. | 136 // Get actual keys in iteration order. |
| 143 Array& keys = Array::Handle(HashTables::ToArray(actual, true)); | 137 Array& keys = Array::Handle(HashTables::ToArray(actual, true)); |
| 144 // Cardinality must match. | 138 // Cardinality must match. |
| 145 EXPECT_EQ(static_cast<intptr_t>(expected.size()), keys.Length()); | 139 EXPECT_EQ(static_cast<intptr_t>(expected.size()), keys.Length()); |
| 146 std::vector<std::string> expected_vec(expected.begin(), expected.end()); | 140 std::vector<std::string> expected_vec(expected.begin(), expected.end()); |
| 147 // Check containment. | 141 // Check containment. |
| 148 for (uintptr_t i = 0; i < expected_vec.size(); ++i) { | 142 for (uintptr_t i = 0; i < expected_vec.size(); ++i) { |
| 149 EXPECT(actual.ContainsKey(expected_vec[i].c_str())); | 143 EXPECT(actual.ContainsKey(expected_vec[i].c_str())); |
| 150 } | 144 } |
| 151 // Equality, including order, if requested. | 145 // Equality, including order, if requested. |
| 152 std::vector<std::string> actual_vec; | 146 std::vector<std::string> actual_vec; |
| 153 String& key = String::Handle(); | 147 String& key = String::Handle(); |
| 154 for (int i = 0; i < keys.Length(); ++i) { | 148 for (int i = 0; i < keys.Length(); ++i) { |
| 155 key ^= keys.At(i); | 149 key ^= keys.At(i); |
| 156 actual_vec.push_back(ToStdString(key)); | 150 actual_vec.push_back(ToStdString(key)); |
| 157 } | 151 } |
| 158 if (!ordered) { | 152 if (!ordered) { |
| 159 std::sort(actual_vec.begin(), actual_vec.end()); | 153 std::sort(actual_vec.begin(), actual_vec.end()); |
| 160 } | 154 } |
| 161 EXPECT(std::equal(actual_vec.begin(), actual_vec.end(), | 155 EXPECT( |
| 162 expected_vec.begin())); | 156 std::equal(actual_vec.begin(), actual_vec.end(), expected_vec.begin())); |
| 163 } | 157 } |
| 164 | 158 |
| 165 | 159 |
| 166 // Checks that 'expected' and 'actual' are equal maps. If 'ordered' is true, | 160 // Checks that 'expected' and 'actual' are equal maps. If 'ordered' is true, |
| 167 // it also verifies that their iteration orders match, i.e., that actual's | 161 // it also verifies that their iteration orders match, i.e., that actual's |
| 168 // insertion order coincides with lexicographic order. | 162 // insertion order coincides with lexicographic order. |
| 169 template<typename Map> | 163 template <typename Map> |
| 170 void VerifyStringMapsEqual(const std::map<std::string, int>& expected, | 164 void VerifyStringMapsEqual(const std::map<std::string, int>& expected, |
| 171 const Map& actual, | 165 const Map& actual, |
| 172 bool ordered) { | 166 bool ordered) { |
| 173 intptr_t expected_size = expected.size(); | 167 intptr_t expected_size = expected.size(); |
| 174 // Get actual concatenated (key, value) pairs in iteration order. | 168 // Get actual concatenated (key, value) pairs in iteration order. |
| 175 Array& entries = Array::Handle(HashTables::ToArray(actual, true)); | 169 Array& entries = Array::Handle(HashTables::ToArray(actual, true)); |
| 176 // Cardinality must match. | 170 // Cardinality must match. |
| 177 EXPECT_EQ(expected_size * 2, entries.Length()); | 171 EXPECT_EQ(expected_size * 2, entries.Length()); |
| 178 std::vector<std::pair<std::string, int> > expected_vec(expected.begin(), | 172 std::vector<std::pair<std::string, int> > expected_vec(expected.begin(), |
| 179 expected.end()); | 173 expected.end()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 193 String& key = String::Handle(); | 187 String& key = String::Handle(); |
| 194 for (int i = 0; i < expected_size; ++i) { | 188 for (int i = 0; i < expected_size; ++i) { |
| 195 key ^= entries.At(2 * i); | 189 key ^= entries.At(2 * i); |
| 196 value ^= entries.At(2 * i + 1); | 190 value ^= entries.At(2 * i + 1); |
| 197 EXPECT(expected_vec[i].first == ToStdString(key)); | 191 EXPECT(expected_vec[i].first == ToStdString(key)); |
| 198 EXPECT_EQ(expected_vec[i].second, value.Value()); | 192 EXPECT_EQ(expected_vec[i].second, value.Value()); |
| 199 } | 193 } |
| 200 } | 194 } |
| 201 | 195 |
| 202 | 196 |
| 203 template<typename Set> | 197 template <typename Set> |
| 204 void TestSet(intptr_t initial_capacity, bool ordered) { | 198 void TestSet(intptr_t initial_capacity, bool ordered) { |
| 205 std::set<std::string> expected; | 199 std::set<std::string> expected; |
| 206 Set actual(HashTables::New<Set>(initial_capacity)); | 200 Set actual(HashTables::New<Set>(initial_capacity)); |
| 207 // Insert the following strings twice: | 201 // Insert the following strings twice: |
| 208 // aaa...aaa (length 26) | 202 // aaa...aaa (length 26) |
| 209 // bbb..bbb | 203 // bbb..bbb |
| 210 // ... | 204 // ... |
| 211 // yy | 205 // yy |
| 212 // z | 206 // z |
| 213 for (int i = 0; i < 2; ++i) { | 207 for (int i = 0; i < 2; ++i) { |
| 214 for (char ch = 'a'; ch <= 'z'; ++ch) { | 208 for (char ch = 'a'; ch <= 'z'; ++ch) { |
| 215 std::string key('z' - ch + 1, ch); | 209 std::string key('z' - ch + 1, ch); |
| 216 expected.insert(key); | 210 expected.insert(key); |
| 217 bool present = actual.Insert(String::Handle(String::New(key.c_str()))); | 211 bool present = actual.Insert(String::Handle(String::New(key.c_str()))); |
| 218 EXPECT_EQ((i != 0), present); | 212 EXPECT_EQ((i != 0), present); |
| 219 Validate(actual); | 213 Validate(actual); |
| 220 VerifyStringSetsEqual(expected, actual, ordered); | 214 VerifyStringSetsEqual(expected, actual, ordered); |
| 221 } | 215 } |
| 222 } | 216 } |
| 223 actual.Clear(); | 217 actual.Clear(); |
| 224 EXPECT_EQ(0, actual.NumOccupied()); | 218 EXPECT_EQ(0, actual.NumOccupied()); |
| 225 actual.Release(); | 219 actual.Release(); |
| 226 } | 220 } |
| 227 | 221 |
| 228 | 222 |
| 229 template<typename Map> | 223 template <typename Map> |
| 230 void TestMap(intptr_t initial_capacity, bool ordered) { | 224 void TestMap(intptr_t initial_capacity, bool ordered) { |
| 231 std::map<std::string, int> expected; | 225 std::map<std::string, int> expected; |
| 232 Map actual(HashTables::New<Map>(initial_capacity)); | 226 Map actual(HashTables::New<Map>(initial_capacity)); |
| 233 // Insert the following (strings, int) mapping: | 227 // Insert the following (strings, int) mapping: |
| 234 // aaa...aaa -> 26 | 228 // aaa...aaa -> 26 |
| 235 // bbb..bbb -> 25 | 229 // bbb..bbb -> 25 |
| 236 // ... | 230 // ... |
| 237 // yy -> 2 | 231 // yy -> 2 |
| 238 // z -> 1 | 232 // z -> 1 |
| 239 for (int i = 0; i < 2; ++i) { | 233 for (int i = 0; i < 2; ++i) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 251 VerifyStringMapsEqual(expected, actual, ordered); | 245 VerifyStringMapsEqual(expected, actual, ordered); |
| 252 } | 246 } |
| 253 } | 247 } |
| 254 actual.Clear(); | 248 actual.Clear(); |
| 255 EXPECT_EQ(0, actual.NumOccupied()); | 249 EXPECT_EQ(0, actual.NumOccupied()); |
| 256 actual.Release(); | 250 actual.Release(); |
| 257 } | 251 } |
| 258 | 252 |
| 259 | 253 |
| 260 TEST_CASE(Sets) { | 254 TEST_CASE(Sets) { |
| 261 for (intptr_t initial_capacity = 0; | 255 for (intptr_t initial_capacity = 0; initial_capacity < 32; |
| 262 initial_capacity < 32; | |
| 263 ++initial_capacity) { | 256 ++initial_capacity) { |
| 264 TestSet<UnorderedHashSet<TestTraits> >(initial_capacity, false); | 257 TestSet<UnorderedHashSet<TestTraits> >(initial_capacity, false); |
| 265 } | 258 } |
| 266 } | 259 } |
| 267 | 260 |
| 268 | 261 |
| 269 TEST_CASE(Maps) { | 262 TEST_CASE(Maps) { |
| 270 for (intptr_t initial_capacity = 0; | 263 for (intptr_t initial_capacity = 0; initial_capacity < 32; |
| 271 initial_capacity < 32; | |
| 272 ++initial_capacity) { | 264 ++initial_capacity) { |
| 273 TestMap<UnorderedHashMap<TestTraits> >(initial_capacity, false); | 265 TestMap<UnorderedHashMap<TestTraits> >(initial_capacity, false); |
| 274 } | 266 } |
| 275 } | 267 } |
| 276 | 268 |
| 277 } // namespace dart | 269 } // namespace dart |
| OLD | NEW |