marking-unittest.cc 6.34 KB
Newer Older
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
// Copyright 2016 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 <stdlib.h>

#include "src/globals.h"
#include "src/heap/marking.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace v8 {
namespace internal {

TEST(Marking, MarkWhiteBlackWhite) {
  Bitmap* bitmap = reinterpret_cast<Bitmap*>(
      calloc(Bitmap::kSize / kPointerSize, kPointerSize));
  const int kLocationsSize = 3;
  int position[kLocationsSize] = {
      Bitmap::kBitsPerCell - 2, Bitmap::kBitsPerCell - 1, Bitmap::kBitsPerCell};
  for (int i = 0; i < kLocationsSize; i++) {
    MarkBit mark_bit = bitmap->MarkBitFromIndex(position[i]);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::MarkBlack(mark_bit);
    CHECK(Marking::IsBlack(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::MarkWhite(mark_bit);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
  }
  free(bitmap);
}

TEST(Marking, TransitionWhiteBlackWhite) {
  Bitmap* bitmap = reinterpret_cast<Bitmap*>(
      calloc(Bitmap::kSize / kPointerSize, kPointerSize));
  const int kLocationsSize = 3;
  int position[kLocationsSize] = {
      Bitmap::kBitsPerCell - 2, Bitmap::kBitsPerCell - 1, Bitmap::kBitsPerCell};
  for (int i = 0; i < kLocationsSize; i++) {
    MarkBit mark_bit = bitmap->MarkBitFromIndex(position[i]);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::WhiteToBlack(mark_bit);
    CHECK(Marking::IsBlack(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::BlackToWhite(mark_bit);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
  }
  free(bitmap);
}

TEST(Marking, TransitionAnyToGrey) {
  Bitmap* bitmap = reinterpret_cast<Bitmap*>(
      calloc(Bitmap::kSize / kPointerSize, kPointerSize));
  const int kLocationsSize = 3;
  int position[kLocationsSize] = {
      Bitmap::kBitsPerCell - 2, Bitmap::kBitsPerCell - 1, Bitmap::kBitsPerCell};
  for (int i = 0; i < kLocationsSize; i++) {
    MarkBit mark_bit = bitmap->MarkBitFromIndex(position[i]);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::AnyToGrey(mark_bit);
    CHECK(Marking::IsGrey(mark_bit));
    CHECK(Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::MarkBlack(mark_bit);
    CHECK(Marking::IsBlack(mark_bit));
    CHECK(Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::AnyToGrey(mark_bit);
    CHECK(Marking::IsGrey(mark_bit));
    CHECK(Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::MarkWhite(mark_bit);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
  }
  free(bitmap);
}

TEST(Marking, TransitionWhiteGreyBlackGrey) {
  Bitmap* bitmap = reinterpret_cast<Bitmap*>(
      calloc(Bitmap::kSize / kPointerSize, kPointerSize));
  const int kLocationsSize = 3;
  int position[kLocationsSize] = {
      Bitmap::kBitsPerCell - 2, Bitmap::kBitsPerCell - 1, Bitmap::kBitsPerCell};
  for (int i = 0; i < kLocationsSize; i++) {
    MarkBit mark_bit = bitmap->MarkBitFromIndex(position[i]);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::WhiteToGrey(mark_bit);
    CHECK(Marking::IsGrey(mark_bit));
    CHECK(Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::GreyToBlack(mark_bit);
    CHECK(Marking::IsBlack(mark_bit));
    CHECK(Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::BlackToGrey(mark_bit);
    CHECK(Marking::IsGrey(mark_bit));
    CHECK(Marking::IsBlackOrGrey(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
    Marking::MarkWhite(mark_bit);
    CHECK(Marking::IsWhite(mark_bit));
    CHECK(!Marking::IsImpossible(mark_bit));
  }
  free(bitmap);
}

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
TEST(Marking, SetAndClearRange) {
  Bitmap* bitmap = reinterpret_cast<Bitmap*>(
      calloc(Bitmap::kSize / kPointerSize, kPointerSize));
  for (int i = 0; i < 3; i++) {
    bitmap->SetRange(i, Bitmap::kBitsPerCell + i);
    CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0xffffffff << i);
    CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], (1 << i) - 1);
    bitmap->ClearRange(i, Bitmap::kBitsPerCell + i);
    CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0x0);
    CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], 0x0);
  }
  free(bitmap);
}

TEST(Marking, ClearMultipleRanges) {
  Bitmap* bitmap = reinterpret_cast<Bitmap*>(
      calloc(Bitmap::kSize / kPointerSize, kPointerSize));
  CHECK(bitmap->AllBitsClearInRange(0, Bitmap::kBitsPerCell * 3));
  bitmap->SetRange(0, Bitmap::kBitsPerCell * 3);
  CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0xffffffff);
  CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], 0xffffffff);
  CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[2], 0xffffffff);
  CHECK(bitmap->AllBitsSetInRange(0, Bitmap::kBitsPerCell * 3));
  bitmap->ClearRange(Bitmap::kBitsPerCell / 2, Bitmap::kBitsPerCell);
  bitmap->ClearRange(Bitmap::kBitsPerCell,
                     Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2);
  bitmap->ClearRange(Bitmap::kBitsPerCell * 2 + 8,
                     Bitmap::kBitsPerCell * 2 + 16);
  bitmap->ClearRange(Bitmap::kBitsPerCell * 2 + 24, Bitmap::kBitsPerCell * 3);
  CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[0], 0xffff);
  CHECK(bitmap->AllBitsSetInRange(0, Bitmap::kBitsPerCell / 2));
  CHECK(bitmap->AllBitsClearInRange(Bitmap::kBitsPerCell / 2,
                                    Bitmap::kBitsPerCell));
  CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[1], 0xffff0000);
  CHECK(
      bitmap->AllBitsSetInRange(Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2,
                                2 * Bitmap::kBitsPerCell));
  CHECK(bitmap->AllBitsClearInRange(
      Bitmap::kBitsPerCell, Bitmap::kBitsPerCell + Bitmap::kBitsPerCell / 2));
  CHECK_EQ(reinterpret_cast<uint32_t*>(bitmap)[2], 0xff00ff);
  CHECK(bitmap->AllBitsSetInRange(2 * Bitmap::kBitsPerCell,
                                  2 * Bitmap::kBitsPerCell + 8));
  CHECK(bitmap->AllBitsClearInRange(2 * Bitmap::kBitsPerCell + 24,
                                    Bitmap::kBitsPerCell * 3));
  free(bitmap);
}
159 160
}  // namespace internal
}  // namespace v8