Welcome to mirror list, hosted at ThFree Co, Russian Federation.

object-start-bitmap-unittest.cc « cppgc « heap « unittests « test « v8 « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2425889c1c381b18d3f0f0739f8eaaa7549c4733 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/heap/cppgc/object-start-bitmap.h"

#include "include/cppgc/allocation.h"
#include "src/base/macros.h"
#include "src/heap/cppgc/globals.h"
#include "src/heap/cppgc/heap-object-header-inl.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/object-start-bitmap-inl.h"
#include "src/heap/cppgc/page-memory-inl.h"
#include "src/heap/cppgc/raw-heap.h"
#include "test/unittests/heap/cppgc/tests.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cppgc {
namespace internal {

namespace {

bool IsEmpty(const ObjectStartBitmap& bitmap) {
  size_t count = 0;
  bitmap.Iterate([&count](Address) { count++; });
  return count == 0;
}

// Abstraction for objects that hides ObjectStartBitmap::kGranularity and
// the base address as getting either of it wrong will result in failed DCHECKs.
class Object {
 public:
  static Address kBaseOffset;

  explicit Object(size_t number) : number_(number) {
    const size_t max_entries = ObjectStartBitmap::MaxEntries();
    EXPECT_GE(max_entries, number_);
  }

  Address address() const {
    return kBaseOffset + ObjectStartBitmap::Granularity() * number_;
  }

  HeapObjectHeader* header() const {
    return reinterpret_cast<HeapObjectHeader*>(address());
  }

  // Allow implicitly converting Object to Address.
  operator Address() const { return address(); }  // NOLINT

 private:
  const size_t number_;
};

Address Object::kBaseOffset = reinterpret_cast<Address>(0x4000);

}  // namespace

TEST(ObjectStartBitmapTest, MoreThanZeroEntriesPossible) {
  const size_t max_entries = ObjectStartBitmap::MaxEntries();
  EXPECT_LT(0u, max_entries);
}

TEST(ObjectStartBitmapTest, InitialEmpty) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  EXPECT_TRUE(IsEmpty(bitmap));
}

TEST(ObjectStartBitmapTest, SetBitImpliesNonEmpty) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  bitmap.SetBit(Object(0));
  EXPECT_FALSE(IsEmpty(bitmap));
}

TEST(ObjectStartBitmapTest, SetBitCheckBit) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object(7);
  bitmap.SetBit(object);
  EXPECT_TRUE(bitmap.CheckBit(object));
}

TEST(ObjectStartBitmapTest, SetBitClearbitCheckBit) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object(77);
  bitmap.SetBit(object);
  bitmap.ClearBit(object);
  EXPECT_FALSE(bitmap.CheckBit(object));
}

TEST(ObjectStartBitmapTest, SetBitClearBitImpliesEmpty) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object(123);
  bitmap.SetBit(object);
  bitmap.ClearBit(object);
  EXPECT_TRUE(IsEmpty(bitmap));
}

TEST(ObjectStartBitmapTest, AdjacentObjectsAtBegin) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object0(0);
  Object object1(1);
  bitmap.SetBit(object0);
  bitmap.SetBit(object1);
  EXPECT_FALSE(bitmap.CheckBit(Object(3)));
  size_t count = 0;
  bitmap.Iterate([&count, object0, object1](Address current) {
    if (count == 0) {
      EXPECT_EQ(object0.address(), current);
    } else if (count == 1) {
      EXPECT_EQ(object1.address(), current);
    }
    count++;
  });
  EXPECT_EQ(2u, count);
}

#if defined(V8_CC_MSVC)
#define MAYBE_AdjacentObjectsAtEnd DISABLED_AdjacentObjectsAtEnd
#else  // !defined(V8_CC_MSVC)
#define MAYBE_AdjacentObjectsAtEnd AdjacentObjectsAtEnd
#endif  // !defined(V8_CC_MSVC)
TEST(ObjectStartBitmapTest, MAYBE_AdjacentObjectsAtEnd) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  const size_t last_entry_index = ObjectStartBitmap::MaxEntries() - 1;
  Object object0(last_entry_index - 1);
  Object object1(last_entry_index);
  bitmap.SetBit(object0);
  bitmap.SetBit(object1);
  EXPECT_FALSE(bitmap.CheckBit(Object(last_entry_index - 2)));
  size_t count = 0;
  bitmap.Iterate([&count, object0, object1](Address current) {
    if (count == 0) {
      EXPECT_EQ(object0.address(), current);
    } else if (count == 1) {
      EXPECT_EQ(object1.address(), current);
    }
    count++;
  });
  EXPECT_EQ(2u, count);
}

TEST(ObjectStartBitmapTest, FindHeaderExact) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object(654);
  bitmap.SetBit(object);
  EXPECT_EQ(object.header(), bitmap.FindHeader(object.address()));
}

TEST(ObjectStartBitmapTest, FindHeaderApproximate) {
  static const size_t kInternalDelta = 37;
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object(654);
  bitmap.SetBit(object);
  EXPECT_EQ(object.header(),
            bitmap.FindHeader(object.address() + kInternalDelta));
}

TEST(ObjectStartBitmapTest, FindHeaderIteratingWholeBitmap) {
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object_to_find(Object(0));
  Address hint_index = Object(ObjectStartBitmap::MaxEntries() - 1);
  bitmap.SetBit(object_to_find);
  EXPECT_EQ(object_to_find.header(), bitmap.FindHeader(hint_index));
}

TEST(ObjectStartBitmapTest, FindHeaderNextCell) {
  // This white box test makes use of the fact that cells are of type uint8_t.
  const size_t kCellSize = sizeof(uint8_t);
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object_to_find(Object(kCellSize - 1));
  Address hint = Object(kCellSize);
  bitmap.SetBit(Object(0));
  bitmap.SetBit(object_to_find);
  EXPECT_EQ(object_to_find.header(), bitmap.FindHeader(hint));
}

TEST(ObjectStartBitmapTest, FindHeaderSameCell) {
  // This white box test makes use of the fact that cells are of type uint8_t.
  const size_t kCellSize = sizeof(uint8_t);
  ObjectStartBitmap bitmap(Object::kBaseOffset);
  Object object_to_find(Object(kCellSize - 1));
  bitmap.SetBit(Object(0));
  bitmap.SetBit(object_to_find);
  EXPECT_EQ(object_to_find.header(),
            bitmap.FindHeader(object_to_find.address()));
}

}  // namespace internal
}  // namespace cppgc