Chromium Code Reviews| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 479 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 EXPECT_EQ(42U, allocator.Id()); | 490 EXPECT_EQ(42U, allocator.Id()); |
| 491 EXPECT_NE(0U, allocator.Allocate(24, 1)); | 491 EXPECT_NE(0U, allocator.Allocate(24, 1)); |
| 492 EXPECT_FALSE(allocator.IsFull()); | 492 EXPECT_FALSE(allocator.IsFull()); |
| 493 EXPECT_FALSE(allocator.IsCorrupt()); | 493 EXPECT_FALSE(allocator.IsCorrupt()); |
| 494 } | 494 } |
| 495 | 495 |
| 496 | 496 |
| 497 //----- SharedPersistentMemoryAllocator ---------------------------------------- | 497 //----- SharedPersistentMemoryAllocator ---------------------------------------- |
| 498 | 498 |
| 499 TEST(SharedPersistentMemoryAllocatorTest, CreationTest) { | 499 TEST(SharedPersistentMemoryAllocatorTest, CreationTest) { |
| 500 SharedMemoryHandle shared_handle; | 500 SharedMemoryHandle shared_handle_1; |
| 501 SharedMemoryHandle shared_handle_2; | |
| 501 | 502 |
| 502 PersistentMemoryAllocator::MemoryInfo meminfo1; | 503 PersistentMemoryAllocator::MemoryInfo meminfo1; |
| 503 Reference r123, r456, r789; | 504 Reference r123, r456, r789; |
| 504 { | 505 { |
| 505 std::unique_ptr<SharedMemory> shmem1(new SharedMemory()); | 506 std::unique_ptr<SharedMemory> shmem1(new SharedMemory()); |
| 506 ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); | 507 ASSERT_TRUE(shmem1->CreateAndMapAnonymous(TEST_MEMORY_SIZE)); |
| 507 SharedPersistentMemoryAllocator local(std::move(shmem1), TEST_ID, "", | 508 SharedPersistentMemoryAllocator local(std::move(shmem1), TEST_ID, "", |
| 508 false); | 509 false); |
| 509 EXPECT_FALSE(local.IsReadonly()); | 510 EXPECT_FALSE(local.IsReadonly()); |
| 510 r123 = local.Allocate(123, 123); | 511 r123 = local.Allocate(123, 123); |
| 511 r456 = local.Allocate(456, 456); | 512 r456 = local.Allocate(456, 456); |
| 512 r789 = local.Allocate(789, 789); | 513 r789 = local.Allocate(789, 789); |
| 513 local.MakeIterable(r123); | 514 local.MakeIterable(r123); |
| 514 local.ChangeType(r456, 654, 456); | 515 local.ChangeType(r456, 654, 456); |
| 515 local.MakeIterable(r789); | 516 local.MakeIterable(r789); |
| 516 local.GetMemoryInfo(&meminfo1); | 517 local.GetMemoryInfo(&meminfo1); |
| 517 EXPECT_FALSE(local.IsFull()); | 518 EXPECT_FALSE(local.IsFull()); |
| 518 EXPECT_FALSE(local.IsCorrupt()); | 519 EXPECT_FALSE(local.IsCorrupt()); |
| 519 | 520 |
| 520 ASSERT_TRUE(local.shared_memory()->ShareToProcess( | 521 ASSERT_TRUE(local.shared_memory()->ShareToProcess( |
| 521 GetCurrentProcessHandle(), | 522 GetCurrentProcessHandle(), |
| 522 &shared_handle)); | 523 &shared_handle_1)); |
| 524 ASSERT_TRUE(local.shared_memory()->ShareToProcess( | |
| 525 GetCurrentProcessHandle(), | |
| 526 &shared_handle_2)); | |
| 523 } | 527 } |
| 524 | 528 |
| 525 // Read-only test. | 529 // Read-only test. |
| 526 std::unique_ptr<SharedMemory> shmem2(new SharedMemory(shared_handle, | 530 std::unique_ptr<SharedMemory> shmem2(new SharedMemory(shared_handle_1, |
| 527 /*readonly=*/true)); | 531 /*readonly=*/true)); |
| 528 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); | 532 ASSERT_TRUE(shmem2->Map(TEST_MEMORY_SIZE)); |
| 529 | 533 |
| 530 SharedPersistentMemoryAllocator shalloc2(std::move(shmem2), 0, "", true); | 534 SharedPersistentMemoryAllocator shalloc2(std::move(shmem2), 0, "", true); |
| 531 EXPECT_TRUE(shalloc2.IsReadonly()); | 535 EXPECT_TRUE(shalloc2.IsReadonly()); |
| 532 EXPECT_EQ(TEST_ID, shalloc2.Id()); | 536 EXPECT_EQ(TEST_ID, shalloc2.Id()); |
| 533 EXPECT_FALSE(shalloc2.IsFull()); | 537 EXPECT_FALSE(shalloc2.IsFull()); |
| 534 EXPECT_FALSE(shalloc2.IsCorrupt()); | 538 EXPECT_FALSE(shalloc2.IsCorrupt()); |
| 535 | 539 |
| 536 PersistentMemoryAllocator::Iterator iter2(&shalloc2); | 540 PersistentMemoryAllocator::Iterator iter2(&shalloc2); |
| 537 uint32_t type; | 541 uint32_t type; |
| 538 EXPECT_EQ(r123, iter2.GetNext(&type)); | 542 EXPECT_EQ(r123, iter2.GetNext(&type)); |
| 539 EXPECT_EQ(r789, iter2.GetNext(&type)); | 543 EXPECT_EQ(r789, iter2.GetNext(&type)); |
| 540 EXPECT_EQ(0U, iter2.GetNext(&type)); | 544 EXPECT_EQ(0U, iter2.GetNext(&type)); |
| 541 | 545 |
| 542 EXPECT_EQ(123U, shalloc2.GetType(r123)); | 546 EXPECT_EQ(123U, shalloc2.GetType(r123)); |
| 543 EXPECT_EQ(654U, shalloc2.GetType(r456)); | 547 EXPECT_EQ(654U, shalloc2.GetType(r456)); |
| 544 EXPECT_EQ(789U, shalloc2.GetType(r789)); | 548 EXPECT_EQ(789U, shalloc2.GetType(r789)); |
| 545 | 549 |
| 546 PersistentMemoryAllocator::MemoryInfo meminfo2; | 550 PersistentMemoryAllocator::MemoryInfo meminfo2; |
| 547 shalloc2.GetMemoryInfo(&meminfo2); | 551 shalloc2.GetMemoryInfo(&meminfo2); |
| 548 EXPECT_EQ(meminfo1.total, meminfo2.total); | 552 EXPECT_EQ(meminfo1.total, meminfo2.total); |
| 549 EXPECT_EQ(meminfo1.free, meminfo2.free); | 553 EXPECT_EQ(meminfo1.free, meminfo2.free); |
| 550 | 554 |
| 551 // Read/write test. | 555 // Read/write test. |
| 552 std::unique_ptr<SharedMemory> shmem3(new SharedMemory(shared_handle, | 556 std::unique_ptr<SharedMemory> shmem3(new SharedMemory(shared_handle_2, |
|
stanisc
2016/06/29 18:36:18
Changed this to avoid double tracking of the same
| |
| 553 /*readonly=*/false)); | 557 /*readonly=*/false)); |
| 554 ASSERT_TRUE(shmem3->Map(TEST_MEMORY_SIZE)); | 558 ASSERT_TRUE(shmem3->Map(TEST_MEMORY_SIZE)); |
| 555 | 559 |
| 556 SharedPersistentMemoryAllocator shalloc3(std::move(shmem3), 0, "", false); | 560 SharedPersistentMemoryAllocator shalloc3(std::move(shmem3), 0, "", false); |
| 557 EXPECT_FALSE(shalloc3.IsReadonly()); | 561 EXPECT_FALSE(shalloc3.IsReadonly()); |
| 558 EXPECT_EQ(TEST_ID, shalloc3.Id()); | 562 EXPECT_EQ(TEST_ID, shalloc3.Id()); |
| 559 EXPECT_FALSE(shalloc3.IsFull()); | 563 EXPECT_FALSE(shalloc3.IsFull()); |
| 560 EXPECT_FALSE(shalloc3.IsCorrupt()); | 564 EXPECT_FALSE(shalloc3.IsCorrupt()); |
| 561 | 565 |
| 562 PersistentMemoryAllocator::Iterator iter3(&shalloc3); | 566 PersistentMemoryAllocator::Iterator iter3(&shalloc3); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 781 // For filesize >= minsize, the file must be acceptable. This | 785 // For filesize >= minsize, the file must be acceptable. This |
| 782 // else clause (file-not-acceptable) should be reached only if | 786 // else clause (file-not-acceptable) should be reached only if |
| 783 // filesize < minsize. | 787 // filesize < minsize. |
| 784 EXPECT_GT(minsize, filesize); | 788 EXPECT_GT(minsize, filesize); |
| 785 } | 789 } |
| 786 } | 790 } |
| 787 } | 791 } |
| 788 #endif // !defined(OS_NACL) | 792 #endif // !defined(OS_NACL) |
| 789 | 793 |
| 790 } // namespace base | 794 } // namespace base |
| OLD | NEW |