OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <stdlib.h> |
| 6 #include <cmath> |
| 7 #include <limits> |
| 8 |
| 9 #include "src/base/platform/platform.h" |
| 10 #include "src/base/utils/random-number-generator.h" |
| 11 #include "src/handles-inl.h" |
| 12 #include "src/objects-inl.h" |
| 13 #include "src/zone.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace v8 { |
| 17 namespace internal { |
| 18 |
| 19 void generate_data(byte* data, const uint32_t length) { |
| 20 for (uint32_t i = 0; i < length; i++) { |
| 21 data[i] = (i % 100) & 0xFF; |
| 22 } |
| 23 } |
| 24 |
| 25 bool validate_data(const byte* data, const uint32_t length) { |
| 26 for (uint32_t i = 0; i < length; i++) { |
| 27 if (data[i] != ((i % 100) & 0xFF)) return false; |
| 28 } |
| 29 return true; |
| 30 } |
| 31 |
| 32 TEST(Zone, EmptyAlloc) { |
| 33 base::AccountingAllocator allocator; |
| 34 Zone zone(&allocator); |
| 35 |
| 36 CHECK_EQ(zone.allocation_size(), 0); |
| 37 |
| 38 void* ptr = zone.New(0); |
| 39 |
| 40 CHECK_EQ(zone.allocation_size(), 0); |
| 41 CHECK_EQ(Zone::GetZoneFromPointer(ptr), &zone); |
| 42 } |
| 43 |
| 44 TEST(Zone, SimpleAlloc) { |
| 45 const size_t kNumberOfChunks = 10000; |
| 46 const size_t kMaxChunkSize = 1000; |
| 47 |
| 48 base::RandomNumberGenerator rnd(::v8::internal::FLAG_random_seed); |
| 49 base::AccountingAllocator allocator; |
| 50 Zone zone(&allocator); |
| 51 |
| 52 CHECK_EQ(zone.allocation_size(), 0); |
| 53 |
| 54 byte* chunks[kNumberOfChunks]; |
| 55 uint16_t sizes[kNumberOfChunks]; |
| 56 |
| 57 for (int i = 0; i < kNumberOfChunks; i++) { |
| 58 sizes[i] = 1 + (rnd.NextInt() % kMaxChunkSize); |
| 59 chunks[i] = reinterpret_cast<byte*>(zone.New(sizes[i])); |
| 60 |
| 61 generate_data(chunks[i], sizes[i]); |
| 62 } |
| 63 |
| 64 CHECK_GE(zone.allocation_size(), kNumberOfChunks); |
| 65 |
| 66 for (int i = 0; i < kNumberOfChunks; i++) { |
| 67 CHECK(validate_data(chunks[i], sizes[i])); |
| 68 } |
| 69 } |
| 70 |
| 71 TEST(Zone, LargeAlloc) { |
| 72 const size_t kNumberOfChunks = 50; |
| 73 const size_t kMaxChunkSize = 5000000; |
| 74 |
| 75 base::RandomNumberGenerator rnd(::v8::internal::FLAG_random_seed); |
| 76 base::AccountingAllocator allocator; |
| 77 Zone zone(&allocator); |
| 78 |
| 79 CHECK_EQ(zone.allocation_size(), 0); |
| 80 |
| 81 byte* chunks[kNumberOfChunks]; |
| 82 uint32_t sizes[kNumberOfChunks]; |
| 83 |
| 84 for (int i = 0; i < kNumberOfChunks; i++) { |
| 85 sizes[i] = 1 + (rnd.NextInt() % kMaxChunkSize); |
| 86 chunks[i] = reinterpret_cast<byte*>(zone.New(sizes[i])); |
| 87 |
| 88 generate_data(chunks[i], sizes[i]); |
| 89 } |
| 90 |
| 91 CHECK_GE(zone.allocation_size(), kNumberOfChunks); |
| 92 |
| 93 for (int i = 0; i < kNumberOfChunks; i++) { |
| 94 CHECK(validate_data(chunks[i], sizes[i])); |
| 95 } |
| 96 } |
| 97 |
| 98 TEST(Zone, ZoneRealloc) { |
| 99 const size_t kDataAmount = 5000000; |
| 100 |
| 101 base::AccountingAllocator allocator; |
| 102 Zone zone(&allocator); |
| 103 |
| 104 CHECK_EQ(zone.allocation_size(), 0); |
| 105 |
| 106 ZoneList<byte>* list = new (&zone) ZoneList<byte>(0, &zone); |
| 107 |
| 108 for (uint32_t i = 0; i < kDataAmount; i++) { |
| 109 list->Add(i % 100, &zone); |
| 110 } |
| 111 |
| 112 CHECK_EQ(static_cast<size_t>(list->length()), kDataAmount); |
| 113 CHECK_GE(static_cast<size_t>(list->capacity()), kDataAmount); |
| 114 CHECK_GE(zone.allocation_size(), kDataAmount); |
| 115 |
| 116 for (uint32_t i = 0; i < kDataAmount; i++) { |
| 117 CHECK_EQ((*list)[i], i % 100); |
| 118 } |
| 119 } |
| 120 |
| 121 } // namespace internal |
| 122 } // namespace v8 |
OLD | NEW |