zone-chunk-list-unittest.cc 4.53 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
// 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 "src/zone/zone-chunk-list.h"

#include "src/zone/accounting-allocator.h"
#include "src/zone/zone.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace v8 {
namespace internal {

const size_t kItemCount = size_t(1) << 10;

TEST(ZoneChunkList, ForwardIterationTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uintptr_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back(static_cast<uintptr_t>(i));
  }

  size_t count = 0;

  for (uintptr_t item : zone_chunk_list) {
    EXPECT_EQ(static_cast<size_t>(item), count);
    count++;
  }

  EXPECT_EQ(count, kItemCount);
}

TEST(ZoneChunkList, ReverseIterationTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uintptr_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back(static_cast<uintptr_t>(i));
  }

  size_t count = 0;

  for (auto it = zone_chunk_list.rbegin(); it != zone_chunk_list.rend(); ++it) {
    EXPECT_EQ(static_cast<size_t>(*it), kItemCount - count - 1);
    count++;
  }

  EXPECT_EQ(count, kItemCount);
}

TEST(ZoneChunkList, PushFrontTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uintptr_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_front(static_cast<uintptr_t>(i));
  }

  size_t count = 0;

  for (uintptr_t item : zone_chunk_list) {
    EXPECT_EQ(static_cast<size_t>(item), kItemCount - count - 1);
    count++;
  }

  EXPECT_EQ(count, kItemCount);
}

TEST(ZoneChunkList, RewindTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uintptr_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back(static_cast<uintptr_t>(i));
  }

  zone_chunk_list.Rewind(42);

  size_t count = 0;

  for (uintptr_t item : zone_chunk_list) {
    EXPECT_EQ(static_cast<size_t>(item), count);
    count++;
  }

95
  EXPECT_EQ(count, 42u);
96 97 98 99 100 101 102 103 104 105 106
  EXPECT_EQ(count, zone_chunk_list.size());

  zone_chunk_list.Rewind(0);

  count = 0;

  for (uintptr_t item : zone_chunk_list) {
    USE(item);
    count++;
  }

107
  EXPECT_EQ(count, 0u);
108 109 110 111 112 113 114 115 116 117 118
  EXPECT_EQ(count, zone_chunk_list.size());

  zone_chunk_list.Rewind(100);

  count = 0;

  for (uintptr_t item : zone_chunk_list) {
    EXPECT_EQ(static_cast<size_t>(item), count);
    count++;
  }

119
  EXPECT_EQ(count, 0u);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  EXPECT_EQ(count, zone_chunk_list.size());
}

TEST(ZoneChunkList, FindTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uintptr_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back(static_cast<uintptr_t>(i));
  }

  const size_t index = kItemCount / 2 + 42;

  EXPECT_EQ(*zone_chunk_list.Find(index), static_cast<uintptr_t>(index));

  *zone_chunk_list.Find(index) = 42;

139
  EXPECT_EQ(*zone_chunk_list.Find(index), 42u);
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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
TEST(ZoneChunkList, CopyToTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uintptr_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back(static_cast<uintptr_t>(i));
  }

  uintptr_t* array = zone.NewArray<uintptr_t>(kItemCount);

  zone_chunk_list.CopyTo(array);

  for (size_t i = 0; i < kItemCount; ++i) {
    EXPECT_EQ(array[i], static_cast<uintptr_t>(i));
  }
}

TEST(ZoneChunkList, SmallCopyToTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<uint8_t> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back(static_cast<uint8_t>(i & 0xFF));
  }

  uint8_t* array = zone.NewArray<uint8_t>(kItemCount);

  zone_chunk_list.CopyTo(array);

  for (size_t i = 0; i < kItemCount; ++i) {
    EXPECT_EQ(array[i], static_cast<uint8_t>(i & 0xFF));
  }
}

struct Fubar {
  size_t a_;
  size_t b_;
};

TEST(ZoneChunkList, BigCopyToTest) {
  AccountingAllocator allocator;
  Zone zone(&allocator, ZONE_NAME);

  ZoneChunkList<Fubar> zone_chunk_list(&zone);

  for (size_t i = 0; i < kItemCount; ++i) {
    zone_chunk_list.push_back({i, i + 5});
  }

  Fubar* array = zone.NewArray<Fubar>(kItemCount);

  zone_chunk_list.CopyTo(array);

  for (size_t i = 0; i < kItemCount; ++i) {
    EXPECT_EQ(array[i].a_, i);
    EXPECT_EQ(array[i].b_, i + 5);
  }
}

205 206
}  // namespace internal
}  // namespace v8