| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2013 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/discardable_memory.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 #if defined(OS_ANDROID) | |
| 12 #include <limits> | |
| 13 #endif | |
| 14 | |
| 15 namespace base { | |
| 16 namespace { | |
| 17 | |
| 18 class DiscardableMemoryTest | |
| 19 : public testing::TestWithParam<DiscardableMemoryType> { | |
| 20 public: | |
| 21 DiscardableMemoryTest() {} | |
| 22 virtual ~DiscardableMemoryTest() { | |
| 23 } | |
| 24 | |
| 25 protected: | |
| 26 scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) { | |
| 27 return DiscardableMemory::CreateLockedMemoryWithType( | |
| 28 GetParam(), size).Pass(); | |
| 29 } | |
| 30 }; | |
| 31 | |
| 32 const size_t kSize = 1024; | |
| 33 | |
| 34 TEST_P(DiscardableMemoryTest, IsNamed) { | |
| 35 std::string type_name(DiscardableMemory::GetTypeName(GetParam())); | |
| 36 EXPECT_NE("unknown", type_name); | |
| 37 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name)); | |
| 38 } | |
| 39 | |
| 40 bool IsNativeType(DiscardableMemoryType type) { | |
| 41 #if defined(OS_ANDROID) | |
| 42 // SHMEM is backed by native discardable memory on Android. | |
| 43 return type == DISCARDABLE_MEMORY_TYPE_SHMEM; | |
| 44 #else | |
| 45 return false; | |
| 46 #endif | |
| 47 } | |
| 48 | |
| 49 TEST_P(DiscardableMemoryTest, SupportedNatively) { | |
| 50 std::vector<DiscardableMemoryType> supported_types; | |
| 51 DiscardableMemory::GetSupportedTypes(&supported_types); | |
| 52 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) | |
| 53 EXPECT_NE(0, std::count_if(supported_types.begin(), | |
| 54 supported_types.end(), | |
| 55 IsNativeType)); | |
| 56 #else | |
| 57 // If we ever have a platform that decides at runtime if it can support | |
| 58 // discardable memory natively, then we'll have to add a 'never supported | |
| 59 // natively' define for this case. At present, if it's not always supported | |
| 60 // natively, it's never supported. | |
| 61 EXPECT_EQ(0, std::count_if(supported_types.begin(), | |
| 62 supported_types.end(), | |
| 63 IsNativeType)); | |
| 64 #endif | |
| 65 } | |
| 66 | |
| 67 // Test Lock() and Unlock() functionalities. | |
| 68 TEST_P(DiscardableMemoryTest, LockAndUnLock) { | |
| 69 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | |
| 70 ASSERT_TRUE(memory); | |
| 71 void* addr = memory->Memory(); | |
| 72 EXPECT_NE(nullptr, addr); | |
| 73 memory->Unlock(); | |
| 74 } | |
| 75 | |
| 76 // Test delete a discardable memory while it is locked. | |
| 77 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) { | |
| 78 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | |
| 79 ASSERT_TRUE(memory); | |
| 80 } | |
| 81 | |
| 82 #if !defined(NDEBUG) && !defined(OS_ANDROID) | |
| 83 // Death tests are not supported with Android APKs. | |
| 84 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | |
| 85 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); | |
| 86 ASSERT_TRUE(memory); | |
| 87 memory->Unlock(); | |
| 88 ASSERT_DEATH_IF_SUPPORTED( | |
| 89 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | |
| 90 } | |
| 91 #endif | |
| 92 | |
| 93 // Test behavior when creating enough instances that could use up a 32-bit | |
| 94 // address space. | |
| 95 // This is disabled under AddressSanitizer on Windows as it crashes (by design) | |
| 96 // on OOM. See http://llvm.org/PR22026 for the details. | |
| 97 #if !defined(ADDRESS_SANITIZER) || !defined(OS_WIN) | |
| 98 TEST_P(DiscardableMemoryTest, AddressSpace) { | |
| 99 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. | |
| 100 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. | |
| 101 | |
| 102 scoped_ptr<DiscardableMemory> instances[kNumberOfInstances]; | |
| 103 for (auto& memory : instances) { | |
| 104 memory = CreateLockedMemory(kLargeSize); | |
| 105 ASSERT_TRUE(memory); | |
| 106 void* addr = memory->Memory(); | |
| 107 EXPECT_NE(nullptr, addr); | |
| 108 memory->Unlock(); | |
| 109 } | |
| 110 } | |
| 111 #endif | |
| 112 | |
| 113 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { | |
| 114 std::vector<DiscardableMemoryType> supported_types; | |
| 115 DiscardableMemory::GetSupportedTypes(&supported_types); | |
| 116 return supported_types; | |
| 117 } | |
| 118 | |
| 119 INSTANTIATE_TEST_CASE_P( | |
| 120 DiscardableMemoryTests, | |
| 121 DiscardableMemoryTest, | |
| 122 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); | |
| 123 | |
| 124 } // namespace | |
| 125 } // namespace base | |
| OLD | NEW |