Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2834)

Unified Diff: base/memory/shared_memory_allocator_unittest.cc

Issue 1410213004: Create "persistent memory allocator" for persisting and sharing objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed compile problems; added paging test Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/shared_memory_allocator_unittest.cc
diff --git a/base/memory/shared_memory_allocator_unittest.cc b/base/memory/shared_memory_allocator_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f2ac2174dbc45c5f46c664cc5facb382fdff667c
--- /dev/null
+++ b/base/memory/shared_memory_allocator_unittest.cc
@@ -0,0 +1,106 @@
+// Copyright 2015 The Chromium 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 "base/memory/shared_memory_allocator.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "testing/gmock/include/gmock/gmock.h"
+
+namespace {
+
+const int32 TEST_MEMORY_SIZE = 1 << 20; // 1 MiB
+const int32 TEST_MEMORY_PAGE = 64 << 10; // 64 KiB
+
+} // namespace
+
+namespace base {
+
+class SharedMemoryAllocatorTest : public testing::Test {
+ public:
+ struct TestObject1 {
+ int32 onething;
+ char oranother;
+ };
+
+ struct TestObject2 {
+ int thiis;
+ long that;
+ float andthe;
+ char other;
+ double thing;
+ };
+
+ void SetUp() override {
+ allocator_.reset();
+ mem_segment_.reset(new char[TEST_MEMORY_SIZE]);
+ memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE);
+ allocator_.reset(new SharedMemoryAllocator(mem_segment_.get(),
+ TEST_MEMORY_SIZE,
+ TEST_MEMORY_PAGE));
+ }
+
+ void TearDown() override {
+ allocator_.reset();
+ mem_segment_.reset();
+ }
+
+ scoped_ptr<char[]> mem_segment_;
+ scoped_ptr<SharedMemoryAllocator> allocator_;
+};
+
+TEST_F(SharedMemoryAllocatorTest, AllocateAndIterate) {
+ SharedMemoryAllocator::MemoryInfo meminfo0;
+ allocator_->GetMemoryInfo(&meminfo0);
+ EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total);
+ EXPECT_GT(meminfo0.total, meminfo0.free);
+
+ int32 block1 = allocator_->Allocate(sizeof(TestObject1), 1);
+ EXPECT_NE(0, block1);
+ EXPECT_NE(nullptr, allocator_->GetObject(block1, 1, (TestObject1*)0));
+ EXPECT_EQ(nullptr, allocator_->GetObject(block1, 1, (TestObject2*)0));
+ SharedMemoryAllocator::MemoryInfo meminfo1;
+ allocator_->GetMemoryInfo(&meminfo1);
+ EXPECT_EQ(meminfo0.total, meminfo1.total);
+ EXPECT_GT(meminfo0.free, meminfo1.free);
+
+ SharedMemoryAllocator::Iterator iter;
+ int32 type;
+ EXPECT_EQ(0, allocator_->GetFirstIterable(&iter, &type));
+ EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
+ allocator_->MakeIterable(block1);
+ EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type));
+ EXPECT_EQ(1, type);
+ EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
+
+ int32 block2 = allocator_->Allocate(sizeof(TestObject2), 2);
+ EXPECT_NE(0, block2);
+ EXPECT_NE(nullptr, allocator_->GetObject(block2, 2, (TestObject2*)0));
+ EXPECT_EQ(nullptr, allocator_->GetObject(block2, 1, (TestObject2*)0));
+ SharedMemoryAllocator::MemoryInfo meminfo2;
+ allocator_->GetMemoryInfo(&meminfo2);
+ EXPECT_EQ(meminfo1.total, meminfo2.total);
+ EXPECT_GT(meminfo1.free, meminfo2.free);
+
+ allocator_->MakeIterable(block2);
+ EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type));
+ EXPECT_EQ(2, type);
+ EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
+
+ EXPECT_FALSE(allocator_->IsFull());
+ EXPECT_FALSE(allocator_->IsCorrupted());
+}
+
+TEST_F(SharedMemoryAllocatorTest, PageTest) {
+ int32 block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1);
+ EXPECT_LT(0, block1);
+ EXPECT_GT(TEST_MEMORY_PAGE, block1);
+
+ int32 block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2);
+ EXPECT_EQ(TEST_MEMORY_PAGE, block2);
+
+ int32 block3 = allocator_->Allocate(99, 3);
+ EXPECT_EQ(2 * TEST_MEMORY_PAGE, block3);
+}
+
+} // namespace base
« base/memory/shared_memory_allocator.cc ('K') | « base/memory/shared_memory_allocator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698