v8  3.14.5(node0.10.28)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-hashmap.cc
Go to the documentation of this file.
1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #include <stdlib.h>
29 
30 #include "v8.h"
31 #include "hashmap.h"
32 #include "cctest.h"
33 
34 using namespace v8::internal;
35 
36 static bool DefaultMatchFun(void* a, void* b) {
37  return a == b;
38 }
39 
40 
41 typedef uint32_t (*IntKeyHash)(uint32_t key);
42 
43 
44 class IntSet {
45  public:
46  explicit IntSet(IntKeyHash hash) : hash_(hash), map_(DefaultMatchFun) {}
47 
48  void Insert(int x) {
49  CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value
50  HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), hash_(x), true);
51  CHECK(p != NULL); // insert is set!
52  CHECK_EQ(reinterpret_cast<void*>(x), p->key);
53  // we don't care about p->value
54  }
55 
56  void Remove(int x) {
57  CHECK_NE(0, x); // 0 corresponds to (void*)NULL - illegal key value
58  map_.Remove(reinterpret_cast<void*>(x), hash_(x));
59  }
60 
61  bool Present(int x) {
62  HashMap::Entry* p =
63  map_.Lookup(reinterpret_cast<void*>(x), hash_(x), false);
64  if (p != NULL) {
65  CHECK_EQ(reinterpret_cast<void*>(x), p->key);
66  }
67  return p != NULL;
68  }
69 
70  void Clear() {
71  map_.Clear();
72  }
73 
74  uint32_t occupancy() const {
75  uint32_t count = 0;
76  for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) {
77  count++;
78  }
79  CHECK_EQ(map_.occupancy(), static_cast<double>(count));
80  return count;
81  }
82 
83  private:
84  IntKeyHash hash_;
85  HashMap map_;
86 };
87 
88 
89 static uint32_t Hash(uint32_t key) { return 23; }
90 static uint32_t CollisionHash(uint32_t key) { return key & 0x3; }
91 
92 
93 void TestSet(IntKeyHash hash, int size) {
94  IntSet set(hash);
95  CHECK_EQ(0, set.occupancy());
96 
97  set.Insert(1);
98  set.Insert(2);
99  set.Insert(3);
100  CHECK_EQ(3, set.occupancy());
101 
102  set.Insert(2);
103  set.Insert(3);
104  CHECK_EQ(3, set.occupancy());
105 
106  CHECK(set.Present(1));
107  CHECK(set.Present(2));
108  CHECK(set.Present(3));
109  CHECK(!set.Present(4));
110  CHECK_EQ(3, set.occupancy());
111 
112  set.Remove(1);
113  CHECK(!set.Present(1));
114  CHECK(set.Present(2));
115  CHECK(set.Present(3));
116  CHECK_EQ(2, set.occupancy());
117 
118  set.Remove(3);
119  CHECK(!set.Present(1));
120  CHECK(set.Present(2));
121  CHECK(!set.Present(3));
122  CHECK_EQ(1, set.occupancy());
123 
124  set.Clear();
125  CHECK_EQ(0, set.occupancy());
126 
127  // Insert a long series of values.
128  const int start = 453;
129  const int factor = 13;
130  const int offset = 7;
131  const uint32_t n = size;
132 
133  int x = start;
134  for (uint32_t i = 0; i < n; i++) {
135  CHECK_EQ(i, static_cast<double>(set.occupancy()));
136  set.Insert(x);
137  x = x * factor + offset;
138  }
139  CHECK_EQ(n, static_cast<double>(set.occupancy()));
140 
141  // Verify the same sequence of values.
142  x = start;
143  for (uint32_t i = 0; i < n; i++) {
144  CHECK(set.Present(x));
145  x = x * factor + offset;
146  }
147  CHECK_EQ(n, static_cast<double>(set.occupancy()));
148 
149  // Remove all these values.
150  x = start;
151  for (uint32_t i = 0; i < n; i++) {
152  CHECK_EQ(n - i, static_cast<double>(set.occupancy()));
153  CHECK(set.Present(x));
154  set.Remove(x);
155  CHECK(!set.Present(x));
156  x = x * factor + offset;
157 
158  // Verify the the expected values are still there.
159  int y = start;
160  for (uint32_t j = 0; j < n; j++) {
161  if (j <= i) {
162  CHECK(!set.Present(y));
163  } else {
164  CHECK(set.Present(y));
165  }
166  y = y * factor + offset;
167  }
168  }
169  CHECK_EQ(0, set.occupancy());
170 }
171 
172 
173 TEST(Set) {
174  TestSet(Hash, 100);
175  TestSet(CollisionHash, 50);
176 }
#define CHECK_EQ(expected, value)
Definition: checks.h:219
IntSet(IntKeyHash hash)
Definition: test-hashmap.cc:46
#define CHECK(condition)
Definition: checks.h:56
void Remove(int x)
Definition: test-hashmap.cc:56
void Insert(int x)
Definition: test-hashmap.cc:48
#define CHECK_NE(unexpected, value)
Definition: checks.h:223
void Clear()
Definition: test-hashmap.cc:70
void TestSet(IntKeyHash hash, int size)
Definition: test-hashmap.cc:93
uint32_t occupancy() const
Definition: test-hashmap.cc:74
bool Present(int x)
Definition: test-hashmap.cc:61
activate correct semantics for inheriting readonliness enable harmony semantics for typeof enable harmony enable harmony proxies enable all harmony harmony_scoping harmony_proxies harmony_scoping tracks arrays with only smi values automatically unbox arrays of doubles use crankshaft use hydrogen range analysis use hydrogen global value numbering use function inlining maximum number of AST nodes considered for a single inlining loop invariant code motion print statistics for hydrogen trace generated IR for specified phases trace register allocator trace range analysis trace representation types environment for every instruction put a break point before deoptimizing polymorphic inlining perform array bounds checks elimination use dead code elimination trace on stack replacement optimize closures cache optimized code for closures functions with arguments object loop weight for representation inference allow uint32 values on optimize frames if they are used only in safe operations track parallel recompilation enable all profiler experiments number of stack frames inspected by the profiler call recompile stub directly when self optimizing trigger profiler ticks based on counting instead of timing weight back edges by jump distance for interrupt triggering percentage of ICs that must have type info to allow optimization watch_ic_patching retry_self_opt interrupt_at_exit extra verbose compilation tracing generate extra emit comments in code disassembly enable use of SSE3 instructions if available enable use of CMOV instruction if available enable use of SAHF instruction if enable use of VFP3 instructions if available this implies enabling ARMv7 and VFP2 enable use of VFP2 instructions if available enable use of SDIV and UDIV instructions if enable loading bit constant by means of movw movt instruction enable unaligned accesses for enable use of MIPS FPU instructions if NULL
Definition: flags.cc:301
uint32_t(* IntKeyHash)(uint32_t key)
Definition: test-hashmap.cc:41