| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/unit_test.h" | 6 #include "vm/unit_test.h" |
| 7 #include "vm/hash_map.h" | 7 #include "vm/hash_map.h" |
| 8 | 8 |
| 9 namespace dart { | 9 namespace dart { |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 EXPECT(map.IsEmpty()); | 24 EXPECT(map.IsEmpty()); |
| 25 TestValue v1(0); | 25 TestValue v1(0); |
| 26 TestValue v2(1); | 26 TestValue v2(1); |
| 27 TestValue v3(0); | 27 TestValue v3(0); |
| 28 map.Insert(&v1); | 28 map.Insert(&v1); |
| 29 EXPECT(map.LookupValue(&v1) == &v1); | 29 EXPECT(map.LookupValue(&v1) == &v1); |
| 30 map.Insert(&v2); | 30 map.Insert(&v2); |
| 31 EXPECT(map.LookupValue(&v1) == &v1); | 31 EXPECT(map.LookupValue(&v1) == &v1); |
| 32 EXPECT(map.LookupValue(&v2) == &v2); | 32 EXPECT(map.LookupValue(&v2) == &v2); |
| 33 EXPECT(map.LookupValue(&v3) == &v1); | 33 EXPECT(map.LookupValue(&v3) == &v1); |
| 34 EXPECT(map.Remove(&v1)); |
| 35 EXPECT(map.Lookup(&v1) == NULL); |
| 36 map.Insert(&v1); |
| 34 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map); | 37 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map); |
| 35 EXPECT(map2.LookupValue(&v1) == &v1); | 38 EXPECT(map2.LookupValue(&v1) == &v1); |
| 36 EXPECT(map2.LookupValue(&v2) == &v2); | 39 EXPECT(map2.LookupValue(&v2) == &v2); |
| 37 EXPECT(map2.LookupValue(&v3) == &v1); | 40 EXPECT(map2.LookupValue(&v3) == &v1); |
| 38 } | 41 } |
| 39 | 42 |
| 40 | 43 |
| 44 TEST_CASE(DirectChainedHashMapInsertRemove) { |
| 45 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map; |
| 46 EXPECT(map.IsEmpty()); |
| 47 TestValue v1(1); |
| 48 TestValue v2(3); // Note: v1, v2, v3 should have the same hash. |
| 49 TestValue v3(5); |
| 50 |
| 51 // Start with adding and removing the same element. |
| 52 map.Insert(&v1); |
| 53 EXPECT(map.LookupValue(&v1) == &v1); |
| 54 EXPECT(map.Remove(&v1)); |
| 55 EXPECT(map.Lookup(&v1) == NULL); |
| 56 |
| 57 // Inserting v2 first should put it at the head of the list. |
| 58 map.Insert(&v2); |
| 59 map.Insert(&v1); |
| 60 EXPECT(map.LookupValue(&v2) == &v2); |
| 61 EXPECT(map.LookupValue(&v1) == &v1); |
| 62 |
| 63 // Check to see if removing the head of the list causes issues. |
| 64 EXPECT(map.Remove(&v2)); |
| 65 EXPECT(map.Lookup(&v2) == NULL); |
| 66 EXPECT(map.LookupValue(&v1) == &v1); |
| 67 |
| 68 // Reinsert v2, which will place it at the back of the hash map list. |
| 69 map.Insert(&v2); |
| 70 EXPECT(map.LookupValue(&v2) == &v2); |
| 71 |
| 72 // Remove from the back of the hash map list. |
| 73 EXPECT(map.Remove(&v2)); |
| 74 EXPECT(map.Lookup(&v2) == NULL); |
| 75 EXPECT(map.Remove(&v1)); |
| 76 EXPECT(map.Lookup(&v1) == NULL); |
| 77 |
| 78 // Check to see that removing an invalid element returns false. |
| 79 EXPECT(!map.Remove(&v1)); |
| 80 |
| 81 // One last case: remove from the middle of a hash map list. |
| 82 map.Insert(&v1); |
| 83 map.Insert(&v2); |
| 84 map.Insert(&v3); |
| 85 |
| 86 EXPECT(map.LookupValue(&v1) == &v1); |
| 87 EXPECT(map.LookupValue(&v2) == &v2); |
| 88 EXPECT(map.LookupValue(&v3) == &v3); |
| 89 |
| 90 EXPECT(map.Remove(&v2)); |
| 91 EXPECT(map.LookupValue(&v1) == &v1); |
| 92 EXPECT(map.Lookup(&v2) == NULL); |
| 93 EXPECT(map.LookupValue(&v3) == &v3); |
| 94 |
| 95 EXPECT(map.Remove(&v1)); |
| 96 EXPECT(map.Remove(&v3)); |
| 97 |
| 98 EXPECT(map.IsEmpty()); |
| 99 } |
| 100 |
| 101 |
| 41 TEST_CASE(MallocDirectChainedHashMap) { | 102 TEST_CASE(MallocDirectChainedHashMap) { |
| 42 MallocDirectChainedHashMap<PointerKeyValueTrait<TestValue> > map; | 103 MallocDirectChainedHashMap<PointerKeyValueTrait<TestValue> > map; |
| 43 EXPECT(map.IsEmpty()); | 104 EXPECT(map.IsEmpty()); |
| 44 TestValue v1(0); | 105 TestValue v1(0); |
| 45 TestValue v2(1); | 106 TestValue v2(1); |
| 46 TestValue v3(0); | 107 TestValue v3(0); |
| 47 map.Insert(&v1); | 108 map.Insert(&v1); |
| 48 EXPECT(map.LookupValue(&v1) == &v1); | 109 EXPECT(map.LookupValue(&v1) == &v1); |
| 49 map.Insert(&v2); | 110 map.Insert(&v2); |
| 50 EXPECT(map.LookupValue(&v1) == &v1); | 111 EXPECT(map.LookupValue(&v1) == &v1); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 171 } |
| 111 count++; | 172 count++; |
| 112 sum += p->second(); | 173 sum += p->second(); |
| 113 } | 174 } |
| 114 | 175 |
| 115 EXPECT(count == 5); | 176 EXPECT(count == 5); |
| 116 EXPECT(sum == 15); | 177 EXPECT(sum == 15); |
| 117 } | 178 } |
| 118 | 179 |
| 119 } // namespace dart | 180 } // namespace dart |
| OLD | NEW |