| 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/hash_table.h" |
| 14 #include "vm/unit_test.h" | 15 #include "vm/unit_test.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) { return static_cast<uword>(strlen(key)); } | 29 static uword Hash(const char* key) { return static_cast<uword>(strlen(key)); } |
| 30 static bool IsMatch(const Object& a, const Object& b) { | 30 static bool IsMatch(const Object& a, const Object& b) { |
| 31 return a.IsString() && b.IsString() && | 31 return a.IsString() && b.IsString() && |
| 32 String::Cast(a).Equals(String::Cast(b)); | 32 String::Cast(a).Equals(String::Cast(b)); |
| 33 } | 33 } |
| 34 static uword Hash(const Object& obj) { return String::Cast(obj).Length(); } | 34 static uword Hash(const Object& obj) { return String::Cast(obj).Length(); } |
| 35 static RawObject* NewKey(const char* key) { return String::New(key); } | 35 static RawObject* NewKey(const char* key) { return String::New(key); } |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 | |
| 39 template <typename Table> | 38 template <typename Table> |
| 40 void Validate(const Table& table) { | 39 void Validate(const Table& table) { |
| 41 // Verify consistency of entry state tracking. | 40 // Verify consistency of entry state tracking. |
| 42 intptr_t num_entries = table.NumEntries(); | 41 intptr_t num_entries = table.NumEntries(); |
| 43 intptr_t num_unused = table.NumUnused(); | 42 intptr_t num_unused = table.NumUnused(); |
| 44 intptr_t num_occupied = table.NumOccupied(); | 43 intptr_t num_occupied = table.NumOccupied(); |
| 45 intptr_t num_deleted = table.NumDeleted(); | 44 intptr_t num_deleted = table.NumDeleted(); |
| 46 for (intptr_t i = 0; i < num_entries; ++i) { | 45 for (intptr_t i = 0; i < num_entries; ++i) { |
| 47 EXPECT_EQ(1, table.IsUnused(i) + table.IsOccupied(i) + table.IsDeleted(i)); | 46 EXPECT_EQ(1, table.IsUnused(i) + table.IsOccupied(i) + table.IsDeleted(i)); |
| 48 num_unused -= table.IsUnused(i); | 47 num_unused -= table.IsUnused(i); |
| 49 num_occupied -= table.IsOccupied(i); | 48 num_occupied -= table.IsOccupied(i); |
| 50 num_deleted -= table.IsDeleted(i); | 49 num_deleted -= table.IsDeleted(i); |
| 51 } | 50 } |
| 52 EXPECT_EQ(0, num_unused); | 51 EXPECT_EQ(0, num_unused); |
| 53 EXPECT_EQ(0, num_occupied); | 52 EXPECT_EQ(0, num_occupied); |
| 54 EXPECT_EQ(0, num_deleted); | 53 EXPECT_EQ(0, num_deleted); |
| 55 } | 54 } |
| 56 | 55 |
| 57 | |
| 58 TEST_CASE(HashTable) { | 56 TEST_CASE(HashTable) { |
| 59 typedef HashTable<TestTraits, 2, 1> Table; | 57 typedef HashTable<TestTraits, 2, 1> Table; |
| 60 Table table(Thread::Current()->zone(), HashTables::New<Table>(5)); | 58 Table table(Thread::Current()->zone(), HashTables::New<Table>(5)); |
| 61 // Ensure that we did get at least 5 entries. | 59 // Ensure that we did get at least 5 entries. |
| 62 EXPECT_LE(5, table.NumEntries()); | 60 EXPECT_LE(5, table.NumEntries()); |
| 63 EXPECT_EQ(0, table.NumOccupied()); | 61 EXPECT_EQ(0, table.NumOccupied()); |
| 64 Validate(table); | 62 Validate(table); |
| 65 EXPECT_EQ(-1, table.FindKey("a")); | 63 EXPECT_EQ(-1, table.FindKey("a")); |
| 66 | 64 |
| 67 // Insertion and lookup. | 65 // Insertion and lookup. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 k = String::New("e"); | 106 k = String::New("e"); |
| 109 table.InsertKey(entry, k); | 107 table.InsertKey(entry, k); |
| 110 EXPECT(!table.FindKeyOrDeletedOrUnused("f", &entry)); | 108 EXPECT(!table.FindKeyOrDeletedOrUnused("f", &entry)); |
| 111 k = String::New("f"); | 109 k = String::New("f"); |
| 112 table.InsertKey(entry, k); | 110 table.InsertKey(entry, k); |
| 113 EXPECT_EQ(5, table.NumOccupied()); | 111 EXPECT_EQ(5, table.NumOccupied()); |
| 114 } | 112 } |
| 115 table.Release(); | 113 table.Release(); |
| 116 } | 114 } |
| 117 | 115 |
| 118 | |
| 119 std::string ToStdString(const String& str) { | 116 std::string ToStdString(const String& str) { |
| 120 EXPECT(str.IsOneByteString()); | 117 EXPECT(str.IsOneByteString()); |
| 121 std::string result; | 118 std::string result; |
| 122 for (intptr_t i = 0; i < str.Length(); ++i) { | 119 for (intptr_t i = 0; i < str.Length(); ++i) { |
| 123 result += static_cast<char>(str.CharAt(i)); | 120 result += static_cast<char>(str.CharAt(i)); |
| 124 } | 121 } |
| 125 return result; | 122 return result; |
| 126 } | 123 } |
| 127 | 124 |
| 128 | |
| 129 // Checks that 'expected' and 'actual' are equal sets. If 'ordered' is true, | 125 // Checks that 'expected' and 'actual' are equal sets. If 'ordered' is true, |
| 130 // it also verifies that their iteration orders match, i.e., that actual's | 126 // it also verifies that their iteration orders match, i.e., that actual's |
| 131 // insertion order coincides with lexicographic order. | 127 // insertion order coincides with lexicographic order. |
| 132 template <typename Set> | 128 template <typename Set> |
| 133 void VerifyStringSetsEqual(const std::set<std::string>& expected, | 129 void VerifyStringSetsEqual(const std::set<std::string>& expected, |
| 134 const Set& actual, | 130 const Set& actual, |
| 135 bool ordered) { | 131 bool ordered) { |
| 136 // Get actual keys in iteration order. | 132 // Get actual keys in iteration order. |
| 137 Array& keys = Array::Handle(HashTables::ToArray(actual, true)); | 133 Array& keys = Array::Handle(HashTables::ToArray(actual, true)); |
| 138 // Cardinality must match. | 134 // Cardinality must match. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 149 key ^= keys.At(i); | 145 key ^= keys.At(i); |
| 150 actual_vec.push_back(ToStdString(key)); | 146 actual_vec.push_back(ToStdString(key)); |
| 151 } | 147 } |
| 152 if (!ordered) { | 148 if (!ordered) { |
| 153 std::sort(actual_vec.begin(), actual_vec.end()); | 149 std::sort(actual_vec.begin(), actual_vec.end()); |
| 154 } | 150 } |
| 155 EXPECT( | 151 EXPECT( |
| 156 std::equal(actual_vec.begin(), actual_vec.end(), expected_vec.begin())); | 152 std::equal(actual_vec.begin(), actual_vec.end(), expected_vec.begin())); |
| 157 } | 153 } |
| 158 | 154 |
| 159 | |
| 160 // Checks that 'expected' and 'actual' are equal maps. If 'ordered' is true, | 155 // Checks that 'expected' and 'actual' are equal maps. If 'ordered' is true, |
| 161 // it also verifies that their iteration orders match, i.e., that actual's | 156 // it also verifies that their iteration orders match, i.e., that actual's |
| 162 // insertion order coincides with lexicographic order. | 157 // insertion order coincides with lexicographic order. |
| 163 template <typename Map> | 158 template <typename Map> |
| 164 void VerifyStringMapsEqual(const std::map<std::string, int>& expected, | 159 void VerifyStringMapsEqual(const std::map<std::string, int>& expected, |
| 165 const Map& actual, | 160 const Map& actual, |
| 166 bool ordered) { | 161 bool ordered) { |
| 167 intptr_t expected_size = expected.size(); | 162 intptr_t expected_size = expected.size(); |
| 168 // Get actual concatenated (key, value) pairs in iteration order. | 163 // Get actual concatenated (key, value) pairs in iteration order. |
| 169 Array& entries = Array::Handle(HashTables::ToArray(actual, true)); | 164 Array& entries = Array::Handle(HashTables::ToArray(actual, true)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 186 std::vector<std::string> actual_vec; | 181 std::vector<std::string> actual_vec; |
| 187 String& key = String::Handle(); | 182 String& key = String::Handle(); |
| 188 for (int i = 0; i < expected_size; ++i) { | 183 for (int i = 0; i < expected_size; ++i) { |
| 189 key ^= entries.At(2 * i); | 184 key ^= entries.At(2 * i); |
| 190 value ^= entries.At(2 * i + 1); | 185 value ^= entries.At(2 * i + 1); |
| 191 EXPECT(expected_vec[i].first == ToStdString(key)); | 186 EXPECT(expected_vec[i].first == ToStdString(key)); |
| 192 EXPECT_EQ(expected_vec[i].second, value.Value()); | 187 EXPECT_EQ(expected_vec[i].second, value.Value()); |
| 193 } | 188 } |
| 194 } | 189 } |
| 195 | 190 |
| 196 | |
| 197 template <typename Set> | 191 template <typename Set> |
| 198 void TestSet(intptr_t initial_capacity, bool ordered) { | 192 void TestSet(intptr_t initial_capacity, bool ordered) { |
| 199 std::set<std::string> expected; | 193 std::set<std::string> expected; |
| 200 Set actual(HashTables::New<Set>(initial_capacity)); | 194 Set actual(HashTables::New<Set>(initial_capacity)); |
| 201 // Insert the following strings twice: | 195 // Insert the following strings twice: |
| 202 // aaa...aaa (length 26) | 196 // aaa...aaa (length 26) |
| 203 // bbb..bbb | 197 // bbb..bbb |
| 204 // ... | 198 // ... |
| 205 // yy | 199 // yy |
| 206 // z | 200 // z |
| 207 for (int i = 0; i < 2; ++i) { | 201 for (int i = 0; i < 2; ++i) { |
| 208 for (char ch = 'a'; ch <= 'z'; ++ch) { | 202 for (char ch = 'a'; ch <= 'z'; ++ch) { |
| 209 std::string key('z' - ch + 1, ch); | 203 std::string key('z' - ch + 1, ch); |
| 210 expected.insert(key); | 204 expected.insert(key); |
| 211 bool present = actual.Insert(String::Handle(String::New(key.c_str()))); | 205 bool present = actual.Insert(String::Handle(String::New(key.c_str()))); |
| 212 EXPECT_EQ((i != 0), present); | 206 EXPECT_EQ((i != 0), present); |
| 213 Validate(actual); | 207 Validate(actual); |
| 214 VerifyStringSetsEqual(expected, actual, ordered); | 208 VerifyStringSetsEqual(expected, actual, ordered); |
| 215 } | 209 } |
| 216 } | 210 } |
| 217 actual.Clear(); | 211 actual.Clear(); |
| 218 EXPECT_EQ(0, actual.NumOccupied()); | 212 EXPECT_EQ(0, actual.NumOccupied()); |
| 219 actual.Release(); | 213 actual.Release(); |
| 220 } | 214 } |
| 221 | 215 |
| 222 | |
| 223 template <typename Map> | 216 template <typename Map> |
| 224 void TestMap(intptr_t initial_capacity, bool ordered) { | 217 void TestMap(intptr_t initial_capacity, bool ordered) { |
| 225 std::map<std::string, int> expected; | 218 std::map<std::string, int> expected; |
| 226 Map actual(HashTables::New<Map>(initial_capacity)); | 219 Map actual(HashTables::New<Map>(initial_capacity)); |
| 227 // Insert the following (strings, int) mapping: | 220 // Insert the following (strings, int) mapping: |
| 228 // aaa...aaa -> 26 | 221 // aaa...aaa -> 26 |
| 229 // bbb..bbb -> 25 | 222 // bbb..bbb -> 25 |
| 230 // ... | 223 // ... |
| 231 // yy -> 2 | 224 // yy -> 2 |
| 232 // z -> 1 | 225 // z -> 1 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 243 EXPECT_EQ((i != 0), present); | 236 EXPECT_EQ((i != 0), present); |
| 244 Validate(actual); | 237 Validate(actual); |
| 245 VerifyStringMapsEqual(expected, actual, ordered); | 238 VerifyStringMapsEqual(expected, actual, ordered); |
| 246 } | 239 } |
| 247 } | 240 } |
| 248 actual.Clear(); | 241 actual.Clear(); |
| 249 EXPECT_EQ(0, actual.NumOccupied()); | 242 EXPECT_EQ(0, actual.NumOccupied()); |
| 250 actual.Release(); | 243 actual.Release(); |
| 251 } | 244 } |
| 252 | 245 |
| 253 | |
| 254 TEST_CASE(Sets) { | 246 TEST_CASE(Sets) { |
| 255 for (intptr_t initial_capacity = 0; initial_capacity < 32; | 247 for (intptr_t initial_capacity = 0; initial_capacity < 32; |
| 256 ++initial_capacity) { | 248 ++initial_capacity) { |
| 257 TestSet<UnorderedHashSet<TestTraits> >(initial_capacity, false); | 249 TestSet<UnorderedHashSet<TestTraits> >(initial_capacity, false); |
| 258 } | 250 } |
| 259 } | 251 } |
| 260 | 252 |
| 261 | |
| 262 TEST_CASE(Maps) { | 253 TEST_CASE(Maps) { |
| 263 for (intptr_t initial_capacity = 0; initial_capacity < 32; | 254 for (intptr_t initial_capacity = 0; initial_capacity < 32; |
| 264 ++initial_capacity) { | 255 ++initial_capacity) { |
| 265 TestMap<UnorderedHashMap<TestTraits> >(initial_capacity, false); | 256 TestMap<UnorderedHashMap<TestTraits> >(initial_capacity, false); |
| 266 } | 257 } |
| 267 } | 258 } |
| 268 | 259 |
| 269 } // namespace dart | 260 } // namespace dart |
| OLD | NEW |