The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / cctest / test-hashmap.cc @ 40c0f755

History | View | Annotate | Download (3.53 KB)

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
class IntSet {
42
 public:
43
  IntSet() : map_(DefaultMatchFun)  {}
44

    
45
  void Insert(int x) {
46
    ASSERT(x != 0);  // 0 corresponds to (void*)NULL - illegal key value
47
    HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), Hash(x), true);
48
    CHECK(p != NULL);  // insert is set!
49
    CHECK_EQ(reinterpret_cast<void*>(x), p->key);
50
    // we don't care about p->value
51
  }
52

    
53
  bool Present(int x) {
54
    HashMap::Entry* p = map_.Lookup(reinterpret_cast<void*>(x), Hash(x), false);
55
    if (p != NULL) {
56
      CHECK_EQ(reinterpret_cast<void*>(x), p->key);
57
    }
58
    return p != NULL;
59
  }
60

    
61
  void Clear() {
62
    map_.Clear();
63
  }
64

    
65
  uint32_t occupancy() const {
66
    uint32_t count = 0;
67
    for (HashMap::Entry* p = map_.Start(); p != NULL; p = map_.Next(p)) {
68
      count++;
69
    }
70
    CHECK_EQ(map_.occupancy(), static_cast<double>(count));
71
    return count;
72
  }
73

    
74
 private:
75
  HashMap map_;
76
  static uint32_t Hash(uint32_t key)  { return key * 23; }
77
};
78

    
79

    
80
TEST(Set) {
81
  IntSet set;
82
  CHECK_EQ(0, set.occupancy());
83

    
84
  set.Insert(1);
85
  set.Insert(2);
86
  set.Insert(3);
87
  CHECK_EQ(3, set.occupancy());
88

    
89
  set.Insert(2);
90
  set.Insert(3);
91
  CHECK_EQ(3, set.occupancy());
92

    
93
  CHECK(set.Present(1));
94
  CHECK(set.Present(2));
95
  CHECK(set.Present(3));
96
  CHECK(!set.Present(4));
97
  CHECK_EQ(3, set.occupancy());
98

    
99
  set.Clear();
100
  CHECK_EQ(0, set.occupancy());
101

    
102
  // Insert a long series of values.
103
  const int start = 453;
104
  const int factor = 13;
105
  const int offset = 7;
106
  const uint32_t n = 1000;
107

    
108
  int x = start;
109
  for (uint32_t i = 0; i < n; i++) {
110
    CHECK_EQ(i, static_cast<double>(set.occupancy()));
111
    set.Insert(x);
112
    x = x*factor + offset;
113
  }
114

    
115
  // Verify the same sequence of values.
116
  x = start;
117
  for (uint32_t i = 0; i < n; i++) {
118
    CHECK(set.Present(x));
119
    x = x*factor + offset;
120
  }
121

    
122
  CHECK_EQ(n, static_cast<double>(set.occupancy()));
123
}