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..97b3237846cf086a36b6c171f52cbada8dc5a427 |
--- /dev/null |
+++ b/test/unittests/zone-unittest.cc |
@@ -0,0 +1,132 @@ |
+// Copyright 2014 the V8 project authors. All rights reserved. |
Jakob Kummerow
2016/09/07 12:49:03
nit: 2016
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <cmath> |
+#include <limits> |
+ |
+#include "src/objects-inl.h" |
+#include "src/objects.h" |
Jakob Kummerow
2016/09/07 12:49:03
nit: when you #include foo-inl.h, you don't need t
|
+ |
Jakob Kummerow
2016/09/07 12:49:02
nit: no empty line necessary here. Instead, sort #
|
+#include "src/handles-inl.h" |
+#include "src/handles.h" |
Jakob Kummerow
2016/09/07 12:49:03
again
|
+ |
+#include "src/base/utils/random-number-generator.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] = static_cast<byte>(i % 100); |
Jakob Kummerow
2016/09/07 12:49:02
how about s/i % 100/i & 0xFF/ ?
|
+ } |
+} |
+ |
+bool validate_data(const byte* data, const uint32_t length) { |
+ for (uint32_t i = 0; i < length; i++) { |
+ if (data[i] != static_cast<byte>(i % 100)) return false; |
+ } |
+ return true; |
+} |
+ |
+void empty_alloc() { |
Jakob Kummerow
2016/09/07 12:49:03
Any reason this is a separate function? I'd prefer
|
+ 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, EmptyAlloc) { empty_alloc(); } |
+ |
+void simple_alloc() { |
+ const size_t kNumberOfChunks = 10000; |
Jakob Kummerow
2016/09/07 12:49:03
How long does it take to run these tests?
|
+ 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, SimpleAlloc) { simple_alloc(); } |
+ |
+void large_alloc() { |
+ 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, LargeAlloc) { large_alloc(); } |
+ |
+void zone_realloc() { |
+ const size_t kDataAmount = 5000000; |
+ |
+ base::AccountingAllocator allocator; |
+ Zone zone(&allocator); |
+ |
+ CHECK_EQ(zone.allocation_size(), 0); |
+ |
+ auto list = new (&zone) ZoneList<byte>(0, &zone); |
Jakob Kummerow
2016/09/07 12:49:03
s/auto/ZoneList<byte>/
|
+ |
+ 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); |
+ } |
+} |
+ |
+TEST(Zone, ZoneRealloc) { zone_realloc(); } |
+ |
+} // namespace internal |
+} // namespace v8 |