| Index: base/memory/discardable_memory_unittest.cc
|
| diff --git a/base/memory/discardable_memory_unittest.cc b/base/memory/discardable_memory_unittest.cc
|
| index c9f67b2556f17a24a1e7f65d9533403106e6c94e..65c5ab64cd8ba10607e6afe841276ffabe20c181 100644
|
| --- a/base/memory/discardable_memory_unittest.cc
|
| +++ b/base/memory/discardable_memory_unittest.cc
|
| @@ -4,21 +4,42 @@
|
|
|
| #include "base/memory/discardable_memory.h"
|
|
|
| -#include <limits>
|
| +#include <algorithm>
|
|
|
| #include "testing/gtest/include/gtest/gtest.h"
|
|
|
| +#if defined(OS_ANDROID)
|
| +#include <limits>
|
| +#endif
|
| +
|
| namespace base {
|
| +namespace {
|
| +
|
| +class DiscardableMemoryTest
|
| + : public testing::TestWithParam<DiscardableMemoryType> {
|
| + protected:
|
| + scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) {
|
| + return DiscardableMemory::CreateLockedMemory(GetParam(), size).Pass();
|
| + }
|
| +};
|
|
|
| const size_t kSize = 1024;
|
|
|
| +TEST_P(DiscardableMemoryTest, IsNamed) {
|
| + std::string type_name(DiscardableMemory::GetTypeName(GetParam()));
|
| + EXPECT_NE("unknown", type_name);
|
| + EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name));
|
| +}
|
| +
|
| #if defined(OS_ANDROID)
|
| -TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
|
| +TEST_P(DiscardableMemoryTest, TooLargeAllocationFails) {
|
| + if (GetParam() != DISCARDABLE_MEMORY_TYPE_ANDROID)
|
| + return;
|
| const size_t kPageSize = 4096;
|
| const size_t max_allowed_allocation_size =
|
| std::numeric_limits<size_t>::max() - kPageSize + 1;
|
| scoped_ptr<DiscardableMemory> memory(
|
| - DiscardableMemory::CreateLockedMemory(max_allowed_allocation_size + 1));
|
| + CreateLockedMemory(max_allowed_allocation_size + 1));
|
| // On certain platforms (e.g. Android), page-alignment would have caused an
|
| // overflow resulting in a small allocation if the input size wasn't checked
|
| // correctly.
|
| @@ -26,22 +47,33 @@ TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
|
| }
|
| #endif
|
|
|
| -TEST(DiscardableMemoryTest, SupportedNatively) {
|
| +bool IsNativeType(DiscardableMemoryType type) {
|
| + return
|
| + type == DISCARDABLE_MEMORY_TYPE_ANDROID ||
|
| + type == DISCARDABLE_MEMORY_TYPE_MAC;
|
| +}
|
| +
|
| +TEST_P(DiscardableMemoryTest, SupportedNatively) {
|
| + std::vector<DiscardableMemoryType> supported_types;
|
| + DiscardableMemory::GetSupportedTypes(&supported_types);
|
| #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
|
| - ASSERT_TRUE(DiscardableMemory::SupportedNatively());
|
| + EXPECT_NE(0u, std::count_if(supported_types.begin(),
|
| + supported_types.end(),
|
| + IsNativeType));
|
| #else
|
| // If we ever have a platform that decides at runtime if it can support
|
| // discardable memory natively, then we'll have to add a 'never supported
|
| // natively' define for this case. At present, if it's not always supported
|
| // natively, it's never supported.
|
| - ASSERT_FALSE(DiscardableMemory::SupportedNatively());
|
| + EXPECT_EQ(0u, std::count_if(supported_types.begin(),
|
| + supported_types.end(),
|
| + IsNativeType));
|
| #endif
|
| }
|
|
|
| // Test Lock() and Unlock() functionalities.
|
| -TEST(DiscardableMemoryTest, LockAndUnLock) {
|
| - const scoped_ptr<DiscardableMemory> memory(
|
| - DiscardableMemory::CreateLockedMemory(kSize));
|
| +TEST_P(DiscardableMemoryTest, LockAndUnLock) {
|
| + const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| void* addr = memory->Memory();
|
| ASSERT_NE(static_cast<void*>(NULL), addr);
|
| @@ -49,7 +81,7 @@ TEST(DiscardableMemoryTest, LockAndUnLock) {
|
| memory->Unlock();
|
| // The system should have no reason to purge discardable blocks in this brief
|
| // interval, though technically speaking this might flake.
|
| - EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
|
| + EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, memory->Lock());
|
| addr = memory->Memory();
|
| ASSERT_NE(static_cast<void*>(NULL), addr);
|
|
|
| @@ -57,32 +89,29 @@ TEST(DiscardableMemoryTest, LockAndUnLock) {
|
| }
|
|
|
| // Test delete a discardable memory while it is locked.
|
| -TEST(DiscardableMemoryTest, DeleteWhileLocked) {
|
| - const scoped_ptr<DiscardableMemory> memory(
|
| - DiscardableMemory::CreateLockedMemory(kSize));
|
| +TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
|
| + const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| }
|
|
|
| #if !defined(OS_ANDROID)
|
| // Test forced purging.
|
| -TEST(DiscardableMemoryTest, Purge) {
|
| +TEST_P(DiscardableMemoryTest, Purge) {
|
| ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());
|
|
|
| - const scoped_ptr<DiscardableMemory> memory(
|
| - DiscardableMemory::CreateLockedMemory(kSize));
|
| + const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| memory->Unlock();
|
|
|
| DiscardableMemory::PurgeForTesting();
|
| - EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
|
| + EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock());
|
| }
|
| #endif // !OS_ANDROID
|
|
|
| #if !defined(NDEBUG) && !defined(OS_ANDROID)
|
| // Death tests are not supported with Android APKs.
|
| -TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
|
| - const scoped_ptr<DiscardableMemory> memory(
|
| - DiscardableMemory::CreateLockedMemory(kSize));
|
| +TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
|
| + const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
|
| ASSERT_TRUE(memory);
|
| memory->Unlock();
|
| ASSERT_DEATH_IF_SUPPORTED(
|
| @@ -90,4 +119,16 @@ TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
|
| }
|
| #endif
|
|
|
| +std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() {
|
| + std::vector<DiscardableMemoryType> supported_types;
|
| + DiscardableMemory::GetSupportedTypes(&supported_types);
|
| + return supported_types;
|
| }
|
| +
|
| +INSTANTIATE_TEST_CASE_P(
|
| + DiscardableMemoryTests,
|
| + DiscardableMemoryTest,
|
| + ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes()));
|
| +
|
| +} // namespace
|
| +} // namespace base
|
|
|