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 |
11 class TestValue { | 11 class TestValue { |
12 public: | 12 public: |
13 explicit TestValue(intptr_t x) : x_(x) { } | 13 explicit TestValue(intptr_t x) : x_(x) { } |
14 intptr_t Hashcode() const { return x_ & 1; } | 14 intptr_t Hashcode() const { return x_ & 1; } |
15 bool Equals(TestValue* other) { return x_ == other->x_; } | 15 bool Equals(TestValue* other) { return x_ == other->x_; } |
16 private: | 16 private: |
17 intptr_t x_; | 17 intptr_t x_; |
18 }; | 18 }; |
19 | 19 |
20 | 20 |
21 TEST_CASE(DirectChainedHashMap) { | 21 TEST_CASE(DirectChainedHashMap) { |
22 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map; | 22 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map; |
23 EXPECT(map.IsEmpty()); | 23 EXPECT(map.IsEmpty()); |
24 TestValue v1(0); | 24 TestValue v1(0); |
25 TestValue v2(1); | 25 TestValue v2(1); |
26 TestValue v3(0); | 26 TestValue v3(0); |
27 map.Insert(&v1); | 27 map.Insert(&v1); |
28 EXPECT(map.Lookup(&v1) == &v1); | 28 EXPECT(map.LookupValue(&v1) == &v1); |
29 map.Insert(&v2); | 29 map.Insert(&v2); |
30 EXPECT(map.Lookup(&v1) == &v1); | 30 EXPECT(map.LookupValue(&v1) == &v1); |
31 EXPECT(map.Lookup(&v2) == &v2); | 31 EXPECT(map.LookupValue(&v2) == &v2); |
32 EXPECT(map.Lookup(&v3) == &v1); | 32 EXPECT(map.LookupValue(&v3) == &v1); |
33 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map); | 33 DirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map); |
34 EXPECT(map2.Lookup(&v1) == &v1); | 34 EXPECT(map2.LookupValue(&v1) == &v1); |
35 EXPECT(map2.Lookup(&v2) == &v2); | 35 EXPECT(map2.LookupValue(&v2) == &v2); |
36 EXPECT(map2.Lookup(&v3) == &v1); | 36 EXPECT(map2.LookupValue(&v3) == &v1); |
| 37 } |
| 38 |
| 39 |
| 40 TEST_CASE(MallocDirectChainedHashMap) { |
| 41 MallocDirectChainedHashMap<PointerKeyValueTrait<TestValue> > map; |
| 42 EXPECT(map.IsEmpty()); |
| 43 TestValue v1(0); |
| 44 TestValue v2(1); |
| 45 TestValue v3(0); |
| 46 map.Insert(&v1); |
| 47 EXPECT(map.LookupValue(&v1) == &v1); |
| 48 map.Insert(&v2); |
| 49 EXPECT(map.LookupValue(&v1) == &v1); |
| 50 EXPECT(map.LookupValue(&v2) == &v2); |
| 51 EXPECT(map.LookupValue(&v3) == &v1); |
| 52 MallocDirectChainedHashMap<PointerKeyValueTrait<TestValue> > map2(map); |
| 53 EXPECT(map2.LookupValue(&v1) == &v1); |
| 54 EXPECT(map2.LookupValue(&v2) == &v2); |
| 55 EXPECT(map2.LookupValue(&v3) == &v1); |
| 56 } |
| 57 |
| 58 |
| 59 class IntptrPair { |
| 60 public: |
| 61 IntptrPair() : first_(-1), second_(-1) {} |
| 62 IntptrPair(intptr_t first, intptr_t second) |
| 63 : first_(first), second_(second) {} |
| 64 |
| 65 intptr_t first() const { return first_; } |
| 66 intptr_t second() const { return second_; } |
| 67 |
| 68 bool operator==(const IntptrPair& other) { |
| 69 return (first_ == other.first_) && (second_ == other.second_); |
| 70 } |
| 71 |
| 72 bool operator!=(const IntptrPair& other) { |
| 73 return (first_ != other.first_) || (second_ != other.second_); |
| 74 } |
| 75 |
| 76 private: |
| 77 intptr_t first_; |
| 78 intptr_t second_; |
| 79 }; |
| 80 |
| 81 |
| 82 TEST_CASE(DirectChainedHashMapIterator) { |
| 83 IntptrPair p1(1, 1); |
| 84 IntptrPair p2(2, 2); |
| 85 IntptrPair p3(3, 3); |
| 86 IntptrPair p4(4, 4); |
| 87 IntptrPair p5(5, 5); |
| 88 DirectChainedHashMap<NumbersKeyValueTrait<IntptrPair> > map; |
| 89 EXPECT(map.IsEmpty()); |
| 90 DirectChainedHashMap<NumbersKeyValueTrait<IntptrPair> >::Iterator it = |
| 91 map.GetIterator(); |
| 92 EXPECT(it.Next() == NULL); |
| 93 it.Reset(); |
| 94 |
| 95 map.Insert(p1); |
| 96 EXPECT(*it.Next() == p1); |
| 97 it.Reset(); |
| 98 |
| 99 map.Insert(p2); |
| 100 map.Insert(p3); |
| 101 map.Insert(p4); |
| 102 map.Insert(p5); |
| 103 intptr_t count = 0; |
| 104 intptr_t sum = 0; |
| 105 while (true) { |
| 106 IntptrPair* p = it.Next(); |
| 107 if (p == NULL) { |
| 108 break; |
| 109 } |
| 110 count++; |
| 111 sum += p->second(); |
| 112 } |
| 113 |
| 114 EXPECT(count == 5); |
| 115 EXPECT(sum == 15); |
37 } | 116 } |
38 | 117 |
39 } // namespace dart | 118 } // namespace dart |
OLD | NEW |