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