OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium 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 "base/memory/shared_memory_allocator.h" |
| 6 |
| 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 const int32_t TEST_MEMORY_SIZE = 1 << 20; // 1 MiB |
| 13 const int32_t TEST_MEMORY_PAGE = 64 << 10; // 64 KiB |
| 14 |
| 15 } // namespace |
| 16 |
| 17 namespace base { |
| 18 |
| 19 class SharedMemoryAllocatorTest : public testing::Test { |
| 20 public: |
| 21 struct TestObject1 { |
| 22 int32_t onething; |
| 23 char oranother; |
| 24 }; |
| 25 |
| 26 struct TestObject2 { |
| 27 int thiis; |
| 28 long that; |
| 29 float andthe; |
| 30 char other; |
| 31 double thing; |
| 32 }; |
| 33 |
| 34 SharedMemoryAllocatorTest() { |
| 35 mem_segment_.reset(new char[TEST_MEMORY_SIZE]); |
| 36 } |
| 37 |
| 38 void SetUp() override { |
| 39 allocator_.reset(); |
| 40 memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE); |
| 41 allocator_.reset(new SharedMemoryAllocator(mem_segment_.get(), |
| 42 TEST_MEMORY_SIZE, |
| 43 TEST_MEMORY_PAGE)); |
| 44 } |
| 45 |
| 46 void TearDown() override { |
| 47 allocator_.reset(); |
| 48 } |
| 49 |
| 50 scoped_ptr<char[]> mem_segment_; |
| 51 scoped_ptr<SharedMemoryAllocator> allocator_; |
| 52 }; |
| 53 |
| 54 TEST_F(SharedMemoryAllocatorTest, AllocateAndIterate) { |
| 55 SharedMemoryAllocator::MemoryInfo meminfo0; |
| 56 allocator_->GetMemoryInfo(&meminfo0); |
| 57 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total); |
| 58 EXPECT_GT(meminfo0.total, meminfo0.free); |
| 59 |
| 60 int32_t block1 = allocator_->Allocate(sizeof(TestObject1), 1); |
| 61 EXPECT_NE(0, block1); |
| 62 EXPECT_NE(nullptr, allocator_->GetObject<TestObject1>(block1, 1)); |
| 63 EXPECT_EQ(nullptr, allocator_->GetObject<TestObject2>(block1, 1)); |
| 64 SharedMemoryAllocator::MemoryInfo meminfo1; |
| 65 allocator_->GetMemoryInfo(&meminfo1); |
| 66 EXPECT_EQ(meminfo0.total, meminfo1.total); |
| 67 EXPECT_GT(meminfo0.free, meminfo1.free); |
| 68 |
| 69 SharedMemoryAllocator::Iterator iter; |
| 70 int32_t type; |
| 71 EXPECT_EQ(0, allocator_->GetFirstIterable(&iter, &type)); |
| 72 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| 73 allocator_->MakeIterable(block1); |
| 74 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); |
| 75 EXPECT_EQ(1, type); |
| 76 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| 77 |
| 78 int32_t block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
| 79 EXPECT_NE(0, block2); |
| 80 EXPECT_NE(nullptr, allocator_->GetObject<TestObject2>(block2, 2)); |
| 81 EXPECT_EQ(nullptr, allocator_->GetObject<TestObject2>(block2, 1)); |
| 82 SharedMemoryAllocator::MemoryInfo meminfo2; |
| 83 allocator_->GetMemoryInfo(&meminfo2); |
| 84 EXPECT_EQ(meminfo1.total, meminfo2.total); |
| 85 EXPECT_GT(meminfo1.free, meminfo2.free); |
| 86 |
| 87 allocator_->MakeIterable(block2); |
| 88 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); |
| 89 EXPECT_EQ(2, type); |
| 90 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type)); |
| 91 |
| 92 EXPECT_FALSE(allocator_->IsFull()); |
| 93 EXPECT_FALSE(allocator_->IsCorrupted()); |
| 94 } |
| 95 |
| 96 TEST_F(SharedMemoryAllocatorTest, PageTest) { |
| 97 int32_t block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
| 98 EXPECT_LT(0, block1); |
| 99 EXPECT_GT(TEST_MEMORY_PAGE, block1); |
| 100 |
| 101 int32_t block2 = allocator_->Allocate(TEST_MEMORY_PAGE - 16, 2); |
| 102 EXPECT_EQ(TEST_MEMORY_PAGE, block2); |
| 103 |
| 104 int32_t block3 = allocator_->Allocate(99, 3); |
| 105 EXPECT_EQ(2 * TEST_MEMORY_PAGE, block3); |
| 106 } |
| 107 |
| 108 } // namespace base |
OLD | NEW |