OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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/memory/discardable_memory.h" | 5 #include "base/memory/discardable_memory.h" |
6 | 6 |
7 #include <limits> | 7 #include <algorithm> |
8 | 8 |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
| 11 #if defined(OS_ANDROID) |
| 12 #include <limits> |
| 13 #endif |
| 14 |
11 namespace base { | 15 namespace base { |
| 16 namespace { |
| 17 |
| 18 class DiscardableMemoryTest |
| 19 : public testing::TestWithParam<DiscardableMemoryType> { |
| 20 protected: |
| 21 scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) { |
| 22 return DiscardableMemory::CreateLockedMemory(GetParam(), size).Pass(); |
| 23 } |
| 24 }; |
12 | 25 |
13 const size_t kSize = 1024; | 26 const size_t kSize = 1024; |
14 | 27 |
| 28 TEST_P(DiscardableMemoryTest, IsNamed) { |
| 29 std::string type_name(DiscardableMemory::GetTypeName(GetParam())); |
| 30 EXPECT_NE("unknown", type_name); |
| 31 EXPECT_EQ(GetParam(), DiscardableMemory::GetNamedType(type_name)); |
| 32 } |
| 33 |
15 #if defined(OS_ANDROID) | 34 #if defined(OS_ANDROID) |
16 TEST(DiscardableMemoryTest, TooLargeAllocationFails) { | 35 TEST_P(DiscardableMemoryTest, TooLargeAllocationFails) { |
| 36 if (GetParam() != DISCARDABLE_MEMORY_TYPE_ANDROID) |
| 37 return; |
17 const size_t kPageSize = 4096; | 38 const size_t kPageSize = 4096; |
18 const size_t max_allowed_allocation_size = | 39 const size_t max_allowed_allocation_size = |
19 std::numeric_limits<size_t>::max() - kPageSize + 1; | 40 std::numeric_limits<size_t>::max() - kPageSize + 1; |
20 scoped_ptr<DiscardableMemory> memory( | 41 scoped_ptr<DiscardableMemory> memory( |
21 DiscardableMemory::CreateLockedMemory(max_allowed_allocation_size + 1)); | 42 CreateLockedMemory(max_allowed_allocation_size + 1)); |
22 // On certain platforms (e.g. Android), page-alignment would have caused an | 43 // On certain platforms (e.g. Android), page-alignment would have caused an |
23 // overflow resulting in a small allocation if the input size wasn't checked | 44 // overflow resulting in a small allocation if the input size wasn't checked |
24 // correctly. | 45 // correctly. |
25 ASSERT_FALSE(memory); | 46 ASSERT_FALSE(memory); |
26 } | 47 } |
27 #endif | 48 #endif |
28 | 49 |
29 TEST(DiscardableMemoryTest, SupportedNatively) { | 50 bool IsNativeType(DiscardableMemoryType type) { |
| 51 return |
| 52 type == DISCARDABLE_MEMORY_TYPE_ANDROID || |
| 53 type == DISCARDABLE_MEMORY_TYPE_MAC; |
| 54 } |
| 55 |
| 56 TEST_P(DiscardableMemoryTest, SupportedNatively) { |
| 57 std::vector<DiscardableMemoryType> supported_types; |
| 58 DiscardableMemory::GetSupportedTypes(&supported_types); |
30 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) | 59 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) |
31 ASSERT_TRUE(DiscardableMemory::SupportedNatively()); | 60 EXPECT_NE(0u, std::count_if(supported_types.begin(), |
| 61 supported_types.end(), |
| 62 IsNativeType)); |
32 #else | 63 #else |
33 // If we ever have a platform that decides at runtime if it can support | 64 // If we ever have a platform that decides at runtime if it can support |
34 // discardable memory natively, then we'll have to add a 'never supported | 65 // discardable memory natively, then we'll have to add a 'never supported |
35 // natively' define for this case. At present, if it's not always supported | 66 // natively' define for this case. At present, if it's not always supported |
36 // natively, it's never supported. | 67 // natively, it's never supported. |
37 ASSERT_FALSE(DiscardableMemory::SupportedNatively()); | 68 EXPECT_EQ(0u, std::count_if(supported_types.begin(), |
| 69 supported_types.end(), |
| 70 IsNativeType)); |
38 #endif | 71 #endif |
39 } | 72 } |
40 | 73 |
41 // Test Lock() and Unlock() functionalities. | 74 // Test Lock() and Unlock() functionalities. |
42 TEST(DiscardableMemoryTest, LockAndUnLock) { | 75 TEST_P(DiscardableMemoryTest, LockAndUnLock) { |
43 const scoped_ptr<DiscardableMemory> memory( | 76 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
44 DiscardableMemory::CreateLockedMemory(kSize)); | |
45 ASSERT_TRUE(memory); | 77 ASSERT_TRUE(memory); |
46 void* addr = memory->Memory(); | 78 void* addr = memory->Memory(); |
47 ASSERT_NE(static_cast<void*>(NULL), addr); | 79 ASSERT_NE(static_cast<void*>(NULL), addr); |
48 | 80 |
49 memory->Unlock(); | 81 memory->Unlock(); |
50 // The system should have no reason to purge discardable blocks in this brief | 82 // The system should have no reason to purge discardable blocks in this brief |
51 // interval, though technically speaking this might flake. | 83 // interval, though technically speaking this might flake. |
52 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); | 84 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, memory->Lock()); |
53 addr = memory->Memory(); | 85 addr = memory->Memory(); |
54 ASSERT_NE(static_cast<void*>(NULL), addr); | 86 ASSERT_NE(static_cast<void*>(NULL), addr); |
55 | 87 |
56 memory->Unlock(); | 88 memory->Unlock(); |
57 } | 89 } |
58 | 90 |
59 // Test delete a discardable memory while it is locked. | 91 // Test delete a discardable memory while it is locked. |
60 TEST(DiscardableMemoryTest, DeleteWhileLocked) { | 92 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) { |
61 const scoped_ptr<DiscardableMemory> memory( | 93 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
62 DiscardableMemory::CreateLockedMemory(kSize)); | |
63 ASSERT_TRUE(memory); | 94 ASSERT_TRUE(memory); |
64 } | 95 } |
65 | 96 |
66 #if !defined(OS_ANDROID) | 97 #if !defined(OS_ANDROID) |
67 // Test forced purging. | 98 // Test forced purging. |
68 TEST(DiscardableMemoryTest, Purge) { | 99 TEST_P(DiscardableMemoryTest, Purge) { |
69 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); | 100 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported()); |
70 | 101 |
71 const scoped_ptr<DiscardableMemory> memory( | 102 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
72 DiscardableMemory::CreateLockedMemory(kSize)); | |
73 ASSERT_TRUE(memory); | 103 ASSERT_TRUE(memory); |
74 memory->Unlock(); | 104 memory->Unlock(); |
75 | 105 |
76 DiscardableMemory::PurgeForTesting(); | 106 DiscardableMemory::PurgeForTesting(); |
77 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); | 107 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock()); |
78 } | 108 } |
79 #endif // !OS_ANDROID | 109 #endif // !OS_ANDROID |
80 | 110 |
81 #if !defined(NDEBUG) && !defined(OS_ANDROID) | 111 #if !defined(NDEBUG) && !defined(OS_ANDROID) |
82 // Death tests are not supported with Android APKs. | 112 // Death tests are not supported with Android APKs. |
83 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { | 113 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { |
84 const scoped_ptr<DiscardableMemory> memory( | 114 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize)); |
85 DiscardableMemory::CreateLockedMemory(kSize)); | |
86 ASSERT_TRUE(memory); | 115 ASSERT_TRUE(memory); |
87 memory->Unlock(); | 116 memory->Unlock(); |
88 ASSERT_DEATH_IF_SUPPORTED( | 117 ASSERT_DEATH_IF_SUPPORTED( |
89 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); | 118 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); |
90 } | 119 } |
91 #endif | 120 #endif |
92 | 121 |
| 122 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() { |
| 123 std::vector<DiscardableMemoryType> supported_types; |
| 124 DiscardableMemory::GetSupportedTypes(&supported_types); |
| 125 return supported_types; |
93 } | 126 } |
| 127 |
| 128 INSTANTIATE_TEST_CASE_P( |
| 129 DiscardableMemoryTests, |
| 130 DiscardableMemoryTest, |
| 131 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes())); |
| 132 |
| 133 } // namespace |
| 134 } // namespace base |
OLD | NEW |