OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/persistent_memory_allocator.h" | 5 #include "base/metrics/persistent_memory_allocator.h" |
6 | 6 |
7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/memory_mapped_file.h" | 9 #include "base/files/memory_mapped_file.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
61 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, | 61 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, |
62 TEST_ID, TEST_NAME, false)); | 62 TEST_ID, TEST_NAME, false)); |
63 allocator_->CreateTrackingHistograms(allocator_->Name()); | 63 allocator_->CreateTrackingHistograms(allocator_->Name()); |
64 } | 64 } |
65 | 65 |
66 void TearDown() override { | 66 void TearDown() override { |
67 allocator_.reset(); | 67 allocator_.reset(); |
68 } | 68 } |
69 | 69 |
70 unsigned CountIterables() { | 70 unsigned CountIterables() { |
71 PersistentMemoryAllocator::Iterator iter; | 71 PersistentMemoryAllocator::Iterator iter(allocator_.get()); |
72 uint32_t type; | 72 uint32_t type; |
73 unsigned count = 0; | 73 unsigned count = 0; |
74 for (allocator_->CreateIterator(&iter); | 74 while (iter.GetNext(&type) != 0) { |
75 allocator_->GetNextIterable(&iter, &type) != 0;) { | 75 ++count; |
76 count++; | |
77 } | 76 } |
78 return count; | 77 return count; |
79 } | 78 } |
80 | 79 |
81 protected: | 80 protected: |
82 scoped_ptr<char[]> mem_segment_; | 81 scoped_ptr<char[]> mem_segment_; |
83 scoped_ptr<PersistentMemoryAllocator> allocator_; | 82 scoped_ptr<PersistentMemoryAllocator> allocator_; |
84 }; | 83 }; |
85 | 84 |
86 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { | 85 TEST_F(PersistentMemoryAllocatorTest, AllocateAndIterate) { |
(...skipping 20 matching lines...) Expand all Loading... | |
107 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1)); | 106 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block1, 1)); |
108 EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1)); | 107 EXPECT_LE(sizeof(TestObject1), allocator_->GetAllocSize(block1)); |
109 EXPECT_GT(sizeof(TestObject1) + kAllocAlignment, | 108 EXPECT_GT(sizeof(TestObject1) + kAllocAlignment, |
110 allocator_->GetAllocSize(block1)); | 109 allocator_->GetAllocSize(block1)); |
111 PersistentMemoryAllocator::MemoryInfo meminfo1; | 110 PersistentMemoryAllocator::MemoryInfo meminfo1; |
112 allocator_->GetMemoryInfo(&meminfo1); | 111 allocator_->GetMemoryInfo(&meminfo1); |
113 EXPECT_EQ(meminfo0.total, meminfo1.total); | 112 EXPECT_EQ(meminfo0.total, meminfo1.total); |
114 EXPECT_GT(meminfo0.free, meminfo1.free); | 113 EXPECT_GT(meminfo0.free, meminfo1.free); |
115 | 114 |
116 // Ensure that the test-object can be made iterable. | 115 // Ensure that the test-object can be made iterable. |
117 PersistentMemoryAllocator::Iterator iter; | 116 PersistentMemoryAllocator::Iterator iter1a(allocator_.get()); |
118 uint32_t type; | 117 uint32_t type; |
119 allocator_->CreateIterator(&iter); | 118 EXPECT_EQ(0U, iter1a.GetNext(&type)); |
120 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | |
121 allocator_->MakeIterable(block1); | 119 allocator_->MakeIterable(block1); |
122 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type)); | 120 EXPECT_EQ(block1, iter1a.GetNext(&type)); |
123 EXPECT_EQ(1U, type); | 121 EXPECT_EQ(1U, type); |
124 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | 122 EXPECT_EQ(0U, iter1a.GetNext(&type)); |
125 | 123 |
126 // Create second test-object and ensure everything is good and it cannot | 124 // Create second test-object and ensure everything is good and it cannot |
127 // be confused with test-object of another type. | 125 // be confused with test-object of another type. |
128 Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); | 126 Reference block2 = allocator_->Allocate(sizeof(TestObject2), 2); |
129 EXPECT_NE(0U, block2); | 127 EXPECT_NE(0U, block2); |
130 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2)); | 128 EXPECT_NE(nullptr, allocator_->GetAsObject<TestObject2>(block2, 2)); |
131 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1)); | 129 EXPECT_EQ(nullptr, allocator_->GetAsObject<TestObject2>(block2, 1)); |
132 EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2)); | 130 EXPECT_LE(sizeof(TestObject2), allocator_->GetAllocSize(block2)); |
133 EXPECT_GT(sizeof(TestObject2) + kAllocAlignment, | 131 EXPECT_GT(sizeof(TestObject2) + kAllocAlignment, |
134 allocator_->GetAllocSize(block2)); | 132 allocator_->GetAllocSize(block2)); |
135 PersistentMemoryAllocator::MemoryInfo meminfo2; | 133 PersistentMemoryAllocator::MemoryInfo meminfo2; |
136 allocator_->GetMemoryInfo(&meminfo2); | 134 allocator_->GetMemoryInfo(&meminfo2); |
137 EXPECT_EQ(meminfo1.total, meminfo2.total); | 135 EXPECT_EQ(meminfo1.total, meminfo2.total); |
138 EXPECT_GT(meminfo1.free, meminfo2.free); | 136 EXPECT_GT(meminfo1.free, meminfo2.free); |
139 | 137 |
140 // Ensure that second test-object can also be made iterable. | 138 // Ensure that second test-object can also be made iterable. |
141 allocator_->MakeIterable(block2); | 139 allocator_->MakeIterable(block2); |
142 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); | 140 EXPECT_EQ(block2, iter1a.GetNext(&type)); |
143 EXPECT_EQ(2U, type); | 141 EXPECT_EQ(2U, type); |
144 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | 142 EXPECT_EQ(0U, iter1a.GetNext(&type)); |
145 | 143 |
146 // Check that iteration can begin after an arbitrary location. | 144 // Check that iteration can begin after an arbitrary location. |
147 allocator_->CreateIterator(&iter, block1); | 145 PersistentMemoryAllocator::Iterator iter1b(allocator_.get(), block1); |
148 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type)); | 146 EXPECT_EQ(block2, iter1b.GetNext(&type)); |
149 EXPECT_EQ(0U, allocator_->GetNextIterable(&iter, &type)); | 147 EXPECT_EQ(0U, iter1b.GetNext(&type)); |
150 | 148 |
151 // Ensure nothing has gone noticably wrong. | 149 // Ensure nothing has gone noticably wrong. |
152 EXPECT_FALSE(allocator_->IsFull()); | 150 EXPECT_FALSE(allocator_->IsFull()); |
153 EXPECT_FALSE(allocator_->IsCorrupt()); | 151 EXPECT_FALSE(allocator_->IsCorrupt()); |
154 | 152 |
155 // Check the internal histogram record of used memory. | 153 // Check the internal histogram record of used memory. |
156 allocator_->UpdateTrackingHistograms(); | 154 allocator_->UpdateTrackingHistograms(); |
157 scoped_ptr<HistogramSamples> used_samples( | 155 scoped_ptr<HistogramSamples> used_samples( |
158 allocator_->used_histogram_->SnapshotSamples()); | 156 allocator_->used_histogram_->SnapshotSamples()); |
159 EXPECT_TRUE(used_samples); | 157 EXPECT_TRUE(used_samples); |
(...skipping 25 matching lines...) Expand all Loading... | |
185 scoped_ptr<PersistentMemoryAllocator> allocator2( | 183 scoped_ptr<PersistentMemoryAllocator> allocator2( |
186 new PersistentMemoryAllocator( | 184 new PersistentMemoryAllocator( |
187 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", | 185 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", |
188 false)); | 186 false)); |
189 EXPECT_EQ(TEST_ID, allocator2->Id()); | 187 EXPECT_EQ(TEST_ID, allocator2->Id()); |
190 EXPECT_FALSE(allocator2->used_histogram_); | 188 EXPECT_FALSE(allocator2->used_histogram_); |
191 EXPECT_FALSE(allocator2->allocs_histogram_); | 189 EXPECT_FALSE(allocator2->allocs_histogram_); |
192 EXPECT_NE(allocator2->allocs_histogram_, allocator_->allocs_histogram_); | 190 EXPECT_NE(allocator2->allocs_histogram_, allocator_->allocs_histogram_); |
193 | 191 |
194 // Ensure that iteration and access through second allocator works. | 192 // Ensure that iteration and access through second allocator works. |
195 allocator2->CreateIterator(&iter); | 193 PersistentMemoryAllocator::Iterator iter2(allocator2.get()); |
196 EXPECT_EQ(block1, allocator2->GetNextIterable(&iter, &type)); | 194 EXPECT_EQ(block1, iter2.GetNext(&type)); |
197 EXPECT_EQ(block2, allocator2->GetNextIterable(&iter, &type)); | 195 EXPECT_EQ(block2, iter2.GetNext(&type)); |
198 EXPECT_EQ(0U, allocator2->GetNextIterable(&iter, &type)); | 196 EXPECT_EQ(0U, iter2.GetNext(&type)); |
199 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject1>(block1, 1)); | 197 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject1>(block1, 1)); |
200 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject2>(block2, 2)); | 198 EXPECT_NE(nullptr, allocator2->GetAsObject<TestObject2>(block2, 2)); |
201 | 199 |
202 // Create a third allocator (read-only) using the same memory segment. | 200 // Create a third allocator (read-only) using the same memory segment. |
203 scoped_ptr<const PersistentMemoryAllocator> allocator3( | 201 scoped_ptr<const PersistentMemoryAllocator> allocator3( |
204 new PersistentMemoryAllocator( | 202 new PersistentMemoryAllocator( |
205 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", true)); | 203 mem_segment_.get(), TEST_MEMORY_SIZE, TEST_MEMORY_PAGE, 0, "", true)); |
206 EXPECT_EQ(TEST_ID, allocator3->Id()); | 204 EXPECT_EQ(TEST_ID, allocator3->Id()); |
207 EXPECT_FALSE(allocator3->used_histogram_); | 205 EXPECT_FALSE(allocator3->used_histogram_); |
208 EXPECT_FALSE(allocator3->allocs_histogram_); | 206 EXPECT_FALSE(allocator3->allocs_histogram_); |
209 | 207 |
210 // Ensure that iteration and access through third allocator works. | 208 // Ensure that iteration and access through third allocator works. |
211 allocator3->CreateIterator(&iter); | 209 PersistentMemoryAllocator::Iterator iter3(allocator3.get()); |
212 EXPECT_EQ(block1, allocator3->GetNextIterable(&iter, &type)); | 210 EXPECT_EQ(block1, iter3.GetNext(&type)); |
213 EXPECT_EQ(block2, allocator3->GetNextIterable(&iter, &type)); | 211 EXPECT_EQ(block2, iter3.GetNext(&type)); |
214 EXPECT_EQ(0U, allocator3->GetNextIterable(&iter, &type)); | 212 EXPECT_EQ(0U, iter3.GetNext(&type)); |
215 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject1>(block1, 1)); | 213 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject1>(block1, 1)); |
216 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject2>(block2, 2)); | 214 EXPECT_NE(nullptr, allocator3->GetAsObject<TestObject2>(block2, 2)); |
215 | |
216 // Ensure that GetNextOfType works. | |
217 PersistentMemoryAllocator::Iterator iter1c(allocator_.get()); | |
218 EXPECT_EQ(block2, iter1c.GetNextOfType(2)); | |
219 EXPECT_EQ(0U, iter1c.GetNextOfType(2)); | |
217 } | 220 } |
218 | 221 |
219 TEST_F(PersistentMemoryAllocatorTest, PageTest) { | 222 TEST_F(PersistentMemoryAllocatorTest, PageTest) { |
220 // This allocation will go into the first memory page. | 223 // This allocation will go into the first memory page. |
221 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); | 224 Reference block1 = allocator_->Allocate(TEST_MEMORY_PAGE / 2, 1); |
222 EXPECT_LT(0U, block1); | 225 EXPECT_LT(0U, block1); |
223 EXPECT_GT(TEST_MEMORY_PAGE, block1); | 226 EXPECT_GT(TEST_MEMORY_PAGE, block1); |
224 | 227 |
225 // This allocation won't fit in same page as previous block. | 228 // This allocation won't fit in same page as previous block. |
226 Reference block2 = | 229 Reference block2 = |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 scoped_ptr<SharedMemory> shmem2(new SharedMemory(shared_handle, | 423 scoped_ptr<SharedMemory> shmem2(new SharedMemory(shared_handle, |
421 /*readonly=*/true)); | 424 /*readonly=*/true)); |
422 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); | 425 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); |
423 | 426 |
424 SharedPersistentMemoryAllocator shalloc2(std::move(shmem2), 0, "", true); | 427 SharedPersistentMemoryAllocator shalloc2(std::move(shmem2), 0, "", true); |
425 EXPECT_TRUE(shalloc2.IsReadonly()); | 428 EXPECT_TRUE(shalloc2.IsReadonly()); |
426 EXPECT_EQ(TEST_ID, shalloc2.Id()); | 429 EXPECT_EQ(TEST_ID, shalloc2.Id()); |
427 EXPECT_FALSE(shalloc2.IsFull()); | 430 EXPECT_FALSE(shalloc2.IsFull()); |
428 EXPECT_FALSE(shalloc2.IsCorrupt()); | 431 EXPECT_FALSE(shalloc2.IsCorrupt()); |
429 | 432 |
430 PersistentMemoryAllocator::Iterator iter2; | 433 PersistentMemoryAllocator::Iterator iter2(&shalloc2); |
431 uint32_t type; | 434 uint32_t type; |
432 shalloc2.CreateIterator(&iter2); | 435 EXPECT_EQ(r123, iter2.GetNext(&type)); |
433 EXPECT_EQ(r123, shalloc2.GetNextIterable(&iter2, &type)); | 436 EXPECT_EQ(r789, iter2.GetNext(&type)); |
434 EXPECT_EQ(r789, shalloc2.GetNextIterable(&iter2, &type)); | 437 EXPECT_EQ(0U, iter2.GetNext(&type)); |
435 EXPECT_EQ(0U, shalloc2.GetNextIterable(&iter2, &type)); | |
436 | 438 |
437 EXPECT_EQ(123U, shalloc2.GetType(r123)); | 439 EXPECT_EQ(123U, shalloc2.GetType(r123)); |
438 EXPECT_EQ(654U, shalloc2.GetType(r456)); | 440 EXPECT_EQ(654U, shalloc2.GetType(r456)); |
439 EXPECT_EQ(789U, shalloc2.GetType(r789)); | 441 EXPECT_EQ(789U, shalloc2.GetType(r789)); |
440 | 442 |
441 PersistentMemoryAllocator::MemoryInfo meminfo2; | 443 PersistentMemoryAllocator::MemoryInfo meminfo2; |
442 shalloc2.GetMemoryInfo(&meminfo2); | 444 shalloc2.GetMemoryInfo(&meminfo2); |
443 EXPECT_EQ(meminfo1.total, meminfo2.total); | 445 EXPECT_EQ(meminfo1.total, meminfo2.total); |
444 EXPECT_EQ(meminfo1.free, meminfo2.free); | 446 EXPECT_EQ(meminfo1.free, meminfo2.free); |
445 | 447 |
446 // Read/write test. | 448 // Read/write test. |
447 scoped_ptr<SharedMemory> shmem3(new SharedMemory(shared_handle, | 449 scoped_ptr<SharedMemory> shmem3(new SharedMemory(shared_handle, |
448 /*readonly=*/false)); | 450 /*readonly=*/false)); |
449 ASSERT_TRUE(shmem3->Map(TEST_MEMORY_SIZE)); | 451 ASSERT_TRUE(shmem3->Map(TEST_MEMORY_SIZE)); |
450 | 452 |
451 SharedPersistentMemoryAllocator shalloc3(std::move(shmem3), 0, "", false); | 453 SharedPersistentMemoryAllocator shalloc3(std::move(shmem3), 0, "", false); |
452 EXPECT_FALSE(shalloc3.IsReadonly()); | 454 EXPECT_FALSE(shalloc3.IsReadonly()); |
453 EXPECT_EQ(TEST_ID, shalloc3.Id()); | 455 EXPECT_EQ(TEST_ID, shalloc3.Id()); |
454 EXPECT_FALSE(shalloc3.IsFull()); | 456 EXPECT_FALSE(shalloc3.IsFull()); |
455 EXPECT_FALSE(shalloc3.IsCorrupt()); | 457 EXPECT_FALSE(shalloc3.IsCorrupt()); |
456 | 458 |
457 PersistentMemoryAllocator::Iterator iter3; | 459 PersistentMemoryAllocator::Iterator iter3(&shalloc3); |
458 shalloc3.CreateIterator(&iter3); | 460 EXPECT_EQ(r123, iter3.GetNext(&type)); |
459 EXPECT_EQ(r123, shalloc3.GetNextIterable(&iter3, &type)); | 461 EXPECT_EQ(r789, iter3.GetNext(&type)); |
460 EXPECT_EQ(r789, shalloc3.GetNextIterable(&iter3, &type)); | 462 EXPECT_EQ(0U, iter3.GetNext(&type)); |
461 EXPECT_EQ(0U, shalloc3.GetNextIterable(&iter3, &type)); | |
462 | 463 |
463 EXPECT_EQ(123U, shalloc3.GetType(r123)); | 464 EXPECT_EQ(123U, shalloc3.GetType(r123)); |
464 EXPECT_EQ(654U, shalloc3.GetType(r456)); | 465 EXPECT_EQ(654U, shalloc3.GetType(r456)); |
465 EXPECT_EQ(789U, shalloc3.GetType(r789)); | 466 EXPECT_EQ(789U, shalloc3.GetType(r789)); |
466 | 467 |
467 PersistentMemoryAllocator::MemoryInfo meminfo3; | 468 PersistentMemoryAllocator::MemoryInfo meminfo3; |
468 shalloc3.GetMemoryInfo(&meminfo3); | 469 shalloc3.GetMemoryInfo(&meminfo3); |
469 EXPECT_EQ(meminfo1.total, meminfo3.total); | 470 EXPECT_EQ(meminfo1.total, meminfo3.total); |
470 EXPECT_EQ(meminfo1.free, meminfo3.free); | 471 EXPECT_EQ(meminfo1.free, meminfo3.free); |
471 | 472 |
472 // Interconnectivity test. | 473 // Interconnectivity test. |
473 Reference obj = shalloc3.Allocate(42, 42); | 474 Reference obj = shalloc3.Allocate(42, 42); |
474 ASSERT_TRUE(obj); | 475 ASSERT_TRUE(obj); |
475 shalloc3.MakeIterable(obj); | 476 shalloc3.MakeIterable(obj); |
476 EXPECT_EQ(obj, shalloc2.GetNextIterable(&iter2, &type)); | 477 EXPECT_EQ(obj, iter2.GetNext(&type)); |
477 EXPECT_EQ(42U, type); | 478 EXPECT_EQ(42U, type); |
478 } | 479 } |
479 | 480 |
480 | 481 |
481 //----- FilePersistentMemoryAllocator ------------------------------------------ | 482 //----- FilePersistentMemoryAllocator ------------------------------------------ |
482 | 483 |
483 TEST(FilePersistentMemoryAllocatorTest, CreationTest) { | 484 TEST(FilePersistentMemoryAllocatorTest, CreationTest) { |
484 ScopedTempDir temp_dir; | 485 ScopedTempDir temp_dir; |
485 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 486 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
486 FilePath file_path = temp_dir.path().AppendASCII("persistent_memory"); | 487 FilePath file_path = temp_dir.path().AppendASCII("persistent_memory"); |
(...skipping 23 matching lines...) Expand all Loading... | |
510 EXPECT_TRUE(mmfile->IsValid()); | 511 EXPECT_TRUE(mmfile->IsValid()); |
511 const size_t mmlength = mmfile->length(); | 512 const size_t mmlength = mmfile->length(); |
512 EXPECT_GE(meminfo1.total, mmlength); | 513 EXPECT_GE(meminfo1.total, mmlength); |
513 | 514 |
514 FilePersistentMemoryAllocator file(std::move(mmfile), 0, ""); | 515 FilePersistentMemoryAllocator file(std::move(mmfile), 0, ""); |
515 EXPECT_TRUE(file.IsReadonly()); | 516 EXPECT_TRUE(file.IsReadonly()); |
516 EXPECT_EQ(TEST_ID, file.Id()); | 517 EXPECT_EQ(TEST_ID, file.Id()); |
517 EXPECT_FALSE(file.IsFull()); | 518 EXPECT_FALSE(file.IsFull()); |
518 EXPECT_FALSE(file.IsCorrupt()); | 519 EXPECT_FALSE(file.IsCorrupt()); |
519 | 520 |
520 PersistentMemoryAllocator::Iterator iter; | 521 PersistentMemoryAllocator::Iterator iter(&file); |
521 uint32_t type; | 522 uint32_t type; |
522 file.CreateIterator(&iter); | 523 EXPECT_EQ(r123, iter.GetNext(&type)); |
523 EXPECT_EQ(r123, file.GetNextIterable(&iter, &type)); | 524 EXPECT_EQ(r789, iter.GetNext(&type)); |
524 EXPECT_EQ(r789, file.GetNextIterable(&iter, &type)); | 525 EXPECT_EQ(0U, iter.GetNext(&type)); |
525 EXPECT_EQ(0U, file.GetNextIterable(&iter, &type)); | |
526 | 526 |
527 EXPECT_EQ(123U, file.GetType(r123)); | 527 EXPECT_EQ(123U, file.GetType(r123)); |
528 EXPECT_EQ(654U, file.GetType(r456)); | 528 EXPECT_EQ(654U, file.GetType(r456)); |
529 EXPECT_EQ(789U, file.GetType(r789)); | 529 EXPECT_EQ(789U, file.GetType(r789)); |
530 | 530 |
531 PersistentMemoryAllocator::MemoryInfo meminfo2; | 531 PersistentMemoryAllocator::MemoryInfo meminfo2; |
532 file.GetMemoryInfo(&meminfo2); | 532 file.GetMemoryInfo(&meminfo2); |
533 EXPECT_GE(meminfo1.total, meminfo2.total); | 533 EXPECT_GE(meminfo1.total, meminfo2.total); |
534 EXPECT_GE(meminfo1.free, meminfo2.free); | 534 EXPECT_GE(meminfo1.free, meminfo2.free); |
535 EXPECT_EQ(mmlength, meminfo2.total); | 535 EXPECT_EQ(mmlength, meminfo2.total); |
536 EXPECT_EQ(0U, meminfo2.free); | 536 EXPECT_EQ(0U, meminfo2.free); |
537 } | 537 } |
538 | 538 |
539 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { | 539 TEST(FilePersistentMemoryAllocatorTest, AcceptableTest) { |
540 ScopedTempDir temp_dir; | 540 ScopedTempDir temp_dir; |
541 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 541 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
542 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); | 542 FilePath file_path_base = temp_dir.path().AppendASCII("persistent_memory_"); |
543 | 543 |
544 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); | 544 LocalPersistentMemoryAllocator local(TEST_MEMORY_SIZE, TEST_ID, ""); |
545 local.Allocate(1, 1); | 545 local.MakeIterable(local.Allocate(1, 1)); |
546 local.Allocate(11, 11); | 546 local.MakeIterable(local.Allocate(11, 11)); |
547 const size_t minsize = local.used(); | 547 const size_t minsize = local.used(); |
548 scoped_ptr<char[]> garbage(new char[minsize]); | 548 scoped_ptr<char[]> garbage(new char[minsize]); |
549 RandBytes(garbage.get(), minsize); | 549 RandBytes(garbage.get(), minsize); |
550 | 550 |
551 scoped_ptr<MemoryMappedFile> mmfile; | 551 scoped_ptr<MemoryMappedFile> mmfile; |
552 char filename[100]; | 552 char filename[100]; |
553 for (size_t filesize = minsize; filesize > 0; --filesize) { | 553 for (size_t filesize = minsize; filesize > 0; --filesize) { |
554 strings::SafeSPrintf(filename, "memory_%d_A", filesize); | 554 strings::SafeSPrintf(filename, "memory_%d_A", filesize); |
555 FilePath file_path = temp_dir.path().AppendASCII(filename); | 555 FilePath file_path = temp_dir.path().AppendASCII(filename); |
556 ASSERT_FALSE(PathExists(file_path)); | 556 ASSERT_FALSE(PathExists(file_path)); |
557 { | 557 { |
558 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); | 558 File writer(file_path, File::FLAG_CREATE | File::FLAG_WRITE); |
559 ASSERT_TRUE(writer.IsValid()); | 559 ASSERT_TRUE(writer.IsValid()); |
560 writer.Write(0, (const char*)local.data(), filesize); | 560 writer.Write(0, (const char*)local.data(), filesize); |
561 } | 561 } |
562 ASSERT_TRUE(PathExists(file_path)); | 562 ASSERT_TRUE(PathExists(file_path)); |
563 | 563 |
564 mmfile.reset(new MemoryMappedFile()); | 564 mmfile.reset(new MemoryMappedFile()); |
565 mmfile->Initialize(file_path); | 565 mmfile->Initialize(file_path); |
566 EXPECT_EQ(filesize, mmfile->length()); | 566 EXPECT_EQ(filesize, mmfile->length()); |
567 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { | 567 if (FilePersistentMemoryAllocator::IsFileAcceptable(*mmfile)) { |
568 // Make sure construction doesn't crash. | 568 // Make sure construction doesn't crash. |
569 FilePersistentMemoryAllocator allocator(std::move(mmfile), 0, ""); | 569 FilePersistentMemoryAllocator allocator(std::move(mmfile), 0, ""); |
570 // Also make sure that iteration doesn't crash. | 570 // Also make sure that iteration doesn't crash. |
571 PersistentMemoryAllocator::Iterator iter; | 571 PersistentMemoryAllocator::Iterator iter(&allocator); |
572 allocator.CreateIterator(&iter); | 572 uint32_t type_id; |
573 for (;;) { | 573 Reference ref; |
574 Reference ref = allocator.GetNextIterable(&iter, 0); | 574 while ((ref = iter.GetNext(&type_id)) != 0) { |
575 if (!ref) | |
576 break; | |
577 const char* data = allocator.GetAsObject<char>(ref, 0); | 575 const char* data = allocator.GetAsObject<char>(ref, 0); |
578 uint32_t type = allocator.GetType(ref); | 576 uint32_t type = allocator.GetType(ref); |
579 size_t size = allocator.GetAllocSize(ref); | 577 size_t size = allocator.GetAllocSize(ref); |
580 // Ensure compiler can't optimize-out above variables. | 578 // Ensure compiler can't optimize-out above variables. |
581 (void)data; | 579 (void)data; |
582 (void)type; | 580 (void)type; |
583 (void)size; | 581 (void)size; |
584 // Ensure that corruption-detected flag gets properly set. | |
585 EXPECT_EQ(filesize != minsize, allocator.IsCorrupt()); | |
586 } | 582 } |
583 // Ensure that short files are detected as corrupt and full files are not. | |
584 DCHECK_EQ(filesize != minsize, allocator.IsCorrupt()); | |
Ilya Sherman
2016/03/24 02:03:58
Did you mean EXPECT_EQ?
bcwhite
2016/03/24 14:22:34
Done. (Was changed to make it easier to find a pr
| |
587 } else { | 585 } else { |
588 // For filesize >= minsize, the file must be acceptable. This | 586 // For filesize >= minsize, the file must be acceptable. This |
589 // else clause (file-not-acceptable) should be reached only if | 587 // else clause (file-not-acceptable) should be reached only if |
590 // filesize < minsize. | 588 // filesize < minsize. |
591 EXPECT_LT(filesize, minsize); | 589 EXPECT_LT(filesize, minsize); |
592 } | 590 } |
593 | 591 |
594 strings::SafeSPrintf(filename, "memory_%d_B", filesize); | 592 strings::SafeSPrintf(filename, "memory_%d_B", filesize); |
595 file_path = temp_dir.path().AppendASCII(filename); | 593 file_path = temp_dir.path().AppendASCII(filename); |
596 ASSERT_FALSE(PathExists(file_path)); | 594 ASSERT_FALSE(PathExists(file_path)); |
(...skipping 14 matching lines...) Expand all Loading... | |
611 } else { | 609 } else { |
612 // For filesize >= minsize, the file must be acceptable. This | 610 // For filesize >= minsize, the file must be acceptable. This |
613 // else clause (file-not-acceptable) should be reached only if | 611 // else clause (file-not-acceptable) should be reached only if |
614 // filesize < minsize. | 612 // filesize < minsize. |
615 EXPECT_GT(minsize, filesize); | 613 EXPECT_GT(minsize, filesize); |
616 } | 614 } |
617 } | 615 } |
618 } | 616 } |
619 | 617 |
620 } // namespace base | 618 } // namespace base |
OLD | NEW |