unsafe_shared_memory_pool_unittest.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2021 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. #include "base/memory/unsafe_shared_memory_pool.h"
  5. #include "testing/gtest/include/gtest/gtest.h"
  6. namespace base {
  7. TEST(UnsafeSharedMemoryPoolTest, CreatesRegion) {
  8. scoped_refptr<UnsafeSharedMemoryPool> pool(
  9. base::MakeRefCounted<UnsafeSharedMemoryPool>());
  10. auto handle = pool->MaybeAllocateBuffer(1000);
  11. ASSERT_TRUE(handle);
  12. EXPECT_TRUE(handle->GetRegion().IsValid());
  13. EXPECT_TRUE(handle->GetMapping().IsValid());
  14. }
  15. TEST(UnsafeSharedMemoryPoolTest, ReusesRegions) {
  16. scoped_refptr<UnsafeSharedMemoryPool> pool(
  17. base::MakeRefCounted<UnsafeSharedMemoryPool>());
  18. auto handle = pool->MaybeAllocateBuffer(1000u);
  19. ASSERT_TRUE(handle);
  20. auto id1 = handle->GetRegion().GetGUID();
  21. // Return memory to the pool.
  22. handle.reset();
  23. handle = pool->MaybeAllocateBuffer(1000u);
  24. // Should reuse the freed region.
  25. EXPECT_EQ(id1, handle->GetRegion().GetGUID());
  26. }
  27. TEST(UnsafeSharedMemoryPoolTest, RespectsSize) {
  28. scoped_refptr<UnsafeSharedMemoryPool> pool(
  29. base::MakeRefCounted<UnsafeSharedMemoryPool>());
  30. auto handle = pool->MaybeAllocateBuffer(1000u);
  31. ASSERT_TRUE(handle);
  32. EXPECT_GE(handle->GetRegion().GetSize(), 1000u);
  33. handle = pool->MaybeAllocateBuffer(100u);
  34. ASSERT_TRUE(handle);
  35. EXPECT_GE(handle->GetRegion().GetSize(), 100u);
  36. handle = pool->MaybeAllocateBuffer(1100u);
  37. ASSERT_TRUE(handle);
  38. EXPECT_GE(handle->GetRegion().GetSize(), 1100u);
  39. }
  40. } // namespace base