Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/heap/spaces.h"
6 : #include "testing/gtest/include/gtest/gtest.h"
7 :
8 : namespace {
9 :
10 : using v8::internal::Bitmap;
11 :
12 : class BitmapTest : public ::testing::Test {
13 : public:
14 : static const uint32_t kBlackCell;
15 : static const uint32_t kWhiteCell;
16 : static const uint32_t kBlackByte;
17 : static const uint32_t kWhiteByte;
18 :
19 6 : BitmapTest() : memory_(new uint8_t[Bitmap::kSize]) {
20 : memset(memory_, 0, Bitmap::kSize);
21 6 : }
22 :
23 6 : ~BitmapTest() override { delete[] memory_; }
24 :
25 : Bitmap* bitmap() { return reinterpret_cast<Bitmap*>(memory_); }
26 : uint8_t* raw_bitmap() { return memory_; }
27 :
28 : private:
29 : uint8_t* memory_;
30 : };
31 :
32 :
33 : const uint32_t BitmapTest::kBlackCell = 0xAAAAAAAA;
34 : const uint32_t BitmapTest::kWhiteCell = 0x00000000;
35 : const uint32_t BitmapTest::kBlackByte = 0xAA;
36 : const uint32_t BitmapTest::kWhiteByte = 0x00;
37 :
38 :
39 15128 : TEST_F(BitmapTest, IsZeroInitialized) {
40 : // We require all tests to start from a zero-initialized bitmap. Manually
41 : // verify this invariant here.
42 8193 : for (size_t i = 0; i < Bitmap::kSize; i++) {
43 16384 : EXPECT_EQ(raw_bitmap()[i], kWhiteByte);
44 : }
45 1 : }
46 :
47 :
48 15128 : TEST_F(BitmapTest, Cells) {
49 2 : Bitmap* bm = bitmap();
50 1 : bm->cells()[1] = kBlackCell;
51 : uint8_t* raw = raw_bitmap();
52 : int second_cell_base = Bitmap::kBytesPerCell;
53 5 : for (size_t i = 0; i < Bitmap::kBytesPerCell; i++) {
54 8 : EXPECT_EQ(raw[second_cell_base + i], kBlackByte);
55 : }
56 1 : }
57 :
58 :
59 15128 : TEST_F(BitmapTest, CellsCount) {
60 2 : int last_cell_index = bitmap()->CellsCount() - 1;
61 1 : bitmap()->cells()[last_cell_index] = kBlackCell;
62 : // Manually verify on raw memory.
63 : uint8_t* raw = raw_bitmap();
64 8193 : for (size_t i = 0; i < Bitmap::kSize; i++) {
65 : // Last cell should be set.
66 8192 : if (i >= (Bitmap::kSize - Bitmap::kBytesPerCell)) {
67 8 : EXPECT_EQ(raw[i], kBlackByte);
68 : } else {
69 16376 : EXPECT_EQ(raw[i], kWhiteByte);
70 : }
71 : }
72 1 : }
73 :
74 :
75 15128 : TEST_F(BitmapTest, IsClean) {
76 1 : Bitmap* bm = bitmap();
77 2 : EXPECT_TRUE(bm->IsClean());
78 1 : bm->cells()[0] = kBlackCell;
79 2 : EXPECT_FALSE(bm->IsClean());
80 1 : }
81 :
82 :
83 15128 : TEST_F(BitmapTest, ClearRange1) {
84 1 : Bitmap* bm = bitmap();
85 1 : bm->cells()[0] = kBlackCell;
86 1 : bm->cells()[1] = kBlackCell;
87 1 : bm->cells()[2] = kBlackCell;
88 1 : bm->ClearRange(0, Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2);
89 1 : EXPECT_EQ(bm->cells()[0], kWhiteCell);
90 2 : EXPECT_EQ(bm->cells()[1], 0xAAAA0000);
91 2 : EXPECT_EQ(bm->cells()[2], kBlackCell);
92 1 : }
93 :
94 :
95 15128 : TEST_F(BitmapTest, ClearRange2) {
96 1 : Bitmap* bm = bitmap();
97 1 : bm->cells()[0] = kBlackCell;
98 1 : bm->cells()[1] = kBlackCell;
99 1 : bm->cells()[2] = kBlackCell;
100 : bm->ClearRange(Bitmap::kBitsPerCell,
101 1 : Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2);
102 1 : EXPECT_EQ(bm->cells()[0], kBlackCell);
103 2 : EXPECT_EQ(bm->cells()[1], 0xAAAA0000);
104 2 : EXPECT_EQ(bm->cells()[2], kBlackCell);
105 1 : }
106 :
107 9075 : } // namespace
|