Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | BitmapTest() : memory_(new uint8_t[Bitmap::kSize]) { |
| 20 | memset(memory_, 0, Bitmap::kSize); |
| 21 | } |
| 22 | |
| 23 | virtual ~BitmapTest() { 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 | TEST_F(BitmapTest, IsZeroInitialized) { |
| 40 | // We require all tests to start from a zero-initialized bitmap. Manually |
| 41 | // verify this invariant here. |
| 42 | for (size_t i = 0; i < Bitmap::kSize; i++) { |
| 43 | EXPECT_EQ(raw_bitmap()[i], kWhiteByte); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | TEST_F(BitmapTest, Cells) { |
| 49 | Bitmap* bm = bitmap(); |
| 50 | bm->cells()[1] = kBlackCell; |
| 51 | uint8_t* raw = raw_bitmap(); |
| 52 | int second_cell_base = Bitmap::kBytesPerCell; |
| 53 | for (size_t i = 0; i < Bitmap::kBytesPerCell; i++) { |
| 54 | EXPECT_EQ(raw[second_cell_base + i], kBlackByte); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | TEST_F(BitmapTest, CellsCount) { |
| 60 | int last_cell_index = bitmap()->CellsCount() - 1; |
| 61 | bitmap()->cells()[last_cell_index] = kBlackCell; |
| 62 | // Manually verify on raw memory. |
| 63 | uint8_t* raw = raw_bitmap(); |
| 64 | for (size_t i = 0; i < Bitmap::kSize; i++) { |
| 65 | // Last cell should be set. |
| 66 | if (i >= (Bitmap::kSize - Bitmap::kBytesPerCell)) { |
| 67 | EXPECT_EQ(raw[i], kBlackByte); |
| 68 | } else { |
| 69 | EXPECT_EQ(raw[i], kWhiteByte); |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | TEST_F(BitmapTest, IsClean) { |
| 76 | Bitmap* bm = bitmap(); |
| 77 | EXPECT_TRUE(bm->IsClean()); |
| 78 | bm->cells()[0] = kBlackCell; |
| 79 | EXPECT_FALSE(bm->IsClean()); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | TEST_F(BitmapTest, ClearRange1) { |
| 84 | Bitmap* bm = bitmap(); |
| 85 | bm->cells()[0] = kBlackCell; |
| 86 | bm->cells()[1] = kBlackCell; |
| 87 | bm->cells()[2] = kBlackCell; |
| 88 | bm->ClearRange(0, Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2); |
| 89 | EXPECT_EQ(bm->cells()[0], kWhiteCell); |
| 90 | EXPECT_EQ(bm->cells()[1], 0xAAAA0000); |
| 91 | EXPECT_EQ(bm->cells()[2], kBlackCell); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | TEST_F(BitmapTest, ClearRange2) { |
| 96 | Bitmap* bm = bitmap(); |
| 97 | bm->cells()[0] = kBlackCell; |
| 98 | bm->cells()[1] = kBlackCell; |
| 99 | bm->cells()[2] = kBlackCell; |
| 100 | bm->ClearRange(Bitmap::kBitsPerCell, |
| 101 | Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2); |
| 102 | EXPECT_EQ(bm->cells()[0], kBlackCell); |
| 103 | EXPECT_EQ(bm->cells()[1], 0xAAAA0000); |
| 104 | EXPECT_EQ(bm->cells()[2], kBlackCell); |
| 105 | } |
| 106 | |
| 107 | } // namespace |