| Index: test/unittests/zone-unittest.cc
|
| diff --git a/test/unittests/zone-unittest.cc b/test/unittests/zone-unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..99dd55abbd31aba128442a5cdc8f4acc288a353a
|
| --- /dev/null
|
| +++ b/test/unittests/zone-unittest.cc
|
| @@ -0,0 +1,122 @@
|
| +// 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 <cmath>
|
| +#include <limits>
|
| +
|
| +#include "src/base/platform/platform.h"
|
| +#include "src/base/utils/random-number-generator.h"
|
| +#include "src/handles-inl.h"
|
| +#include "src/objects-inl.h"
|
| +#include "src/zone.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +
|
| +namespace v8 {
|
| +namespace internal {
|
| +
|
| +void generate_data(byte* data, const uint32_t length) {
|
| + for (uint32_t i = 0; i < length; i++) {
|
| + data[i] = (i % 100) & 0xFF;
|
| + }
|
| +}
|
| +
|
| +bool validate_data(const byte* data, const uint32_t length) {
|
| + for (uint32_t i = 0; i < length; i++) {
|
| + if (data[i] != ((i % 100) & 0xFF)) return false;
|
| + }
|
| + return true;
|
| +}
|
| +
|
| +TEST(Zone, EmptyAlloc) {
|
| + base::AccountingAllocator allocator;
|
| + Zone zone(&allocator);
|
| +
|
| + CHECK_EQ(zone.allocation_size(), 0);
|
| +
|
| + void* ptr = zone.New(0);
|
| +
|
| + CHECK_EQ(zone.allocation_size(), 0);
|
| + CHECK_EQ(Zone::GetZoneFromPointer(ptr), &zone);
|
| +}
|
| +
|
| +TEST(Zone, SimpleAlloc) {
|
| + const size_t kNumberOfChunks = 10000;
|
| + const size_t kMaxChunkSize = 1000;
|
| +
|
| + base::RandomNumberGenerator rnd(::v8::internal::FLAG_random_seed);
|
| + base::AccountingAllocator allocator;
|
| + Zone zone(&allocator);
|
| +
|
| + CHECK_EQ(zone.allocation_size(), 0);
|
| +
|
| + byte* chunks[kNumberOfChunks];
|
| + uint16_t sizes[kNumberOfChunks];
|
| +
|
| + for (int i = 0; i < kNumberOfChunks; i++) {
|
| + sizes[i] = 1 + (rnd.NextInt() % kMaxChunkSize);
|
| + chunks[i] = reinterpret_cast<byte*>(zone.New(sizes[i]));
|
| +
|
| + generate_data(chunks[i], sizes[i]);
|
| + }
|
| +
|
| + CHECK_GE(zone.allocation_size(), kNumberOfChunks);
|
| +
|
| + for (int i = 0; i < kNumberOfChunks; i++) {
|
| + CHECK(validate_data(chunks[i], sizes[i]));
|
| + }
|
| +}
|
| +
|
| +TEST(Zone, LargeAlloc) {
|
| + const size_t kNumberOfChunks = 50;
|
| + const size_t kMaxChunkSize = 5000000;
|
| +
|
| + base::RandomNumberGenerator rnd(::v8::internal::FLAG_random_seed);
|
| + base::AccountingAllocator allocator;
|
| + Zone zone(&allocator);
|
| +
|
| + CHECK_EQ(zone.allocation_size(), 0);
|
| +
|
| + byte* chunks[kNumberOfChunks];
|
| + uint32_t sizes[kNumberOfChunks];
|
| +
|
| + for (int i = 0; i < kNumberOfChunks; i++) {
|
| + sizes[i] = 1 + (rnd.NextInt() % kMaxChunkSize);
|
| + chunks[i] = reinterpret_cast<byte*>(zone.New(sizes[i]));
|
| +
|
| + generate_data(chunks[i], sizes[i]);
|
| + }
|
| +
|
| + CHECK_GE(zone.allocation_size(), kNumberOfChunks);
|
| +
|
| + for (int i = 0; i < kNumberOfChunks; i++) {
|
| + CHECK(validate_data(chunks[i], sizes[i]));
|
| + }
|
| +}
|
| +
|
| +TEST(Zone, ZoneRealloc) {
|
| + const size_t kDataAmount = 5000000;
|
| +
|
| + base::AccountingAllocator allocator;
|
| + Zone zone(&allocator);
|
| +
|
| + CHECK_EQ(zone.allocation_size(), 0);
|
| +
|
| + ZoneList<byte>* list = new (&zone) ZoneList<byte>(0, &zone);
|
| +
|
| + for (uint32_t i = 0; i < kDataAmount; i++) {
|
| + list->Add(i % 100, &zone);
|
| + }
|
| +
|
| + CHECK_EQ(static_cast<size_t>(list->length()), kDataAmount);
|
| + CHECK_GE(static_cast<size_t>(list->capacity()), kDataAmount);
|
| + CHECK_GE(zone.allocation_size(), kDataAmount);
|
| +
|
| + for (uint32_t i = 0; i < kDataAmount; i++) {
|
| + CHECK_EQ((*list)[i], i % 100);
|
| + }
|
| +}
|
| +
|
| +} // namespace internal
|
| +} // namespace v8
|
|
|