Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: base/memory/discardable_memory_unittest.cc

Issue 114923005: base: Discardable memory types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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)
7 #include <limits>
8 #endif
9
5 #include "base/memory/discardable_memory.h" 10 #include "base/memory/discardable_memory.h"
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
15 #if defined(OS_ANDROID) 34 #if defined(OS_ANDROID)
16 TEST(DiscardableMemoryTest, TooLargeAllocationFails) { 35 TEST_P(DiscardableMemoryTest, TooLargeAllocationFails) {
reveman 2013/12/13 18:21:53 Follow up patch will move this to _ashmem_unittest
Philippe 2013/12/17 14:28:21 SGTM
36 if (GetParam() != DISCARDABLE_MEMORY_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 DiscardableMemory::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 type == DISCARDABLE_MEMORY_ANDROID || type == DISCARDABLE_MEMORY_MAC;
52 }
53
54 TEST_P(DiscardableMemoryTest, SupportedNatively) {
30 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY) 55 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
31 ASSERT_TRUE(DiscardableMemory::SupportedNatively()); 56 ASSERT_TRUE(std::find_if(DiscardableMemory::GetSupportedTypes().begin(),
Philippe 2013/12/17 14:28:21 JFYI, std::count_if() would be slightly more compa
reveman 2013/12/18 08:12:38 Good idea. Done.
57 DiscardableMemory::GetSupportedTypes().end(),
58 IsNativeType) !=
59 DiscardableMemory::GetSupportedTypes().end());
32 #else 60 #else
33 // If we ever have a platform that decides at runtime if it can support 61 // 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 62 // 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 63 // natively' define for this case. At present, if it's not always supported
36 // natively, it's never supported. 64 // natively, it's never supported.
37 ASSERT_FALSE(DiscardableMemory::SupportedNatively()); 65 ASSERT_FALSE(std::find_if(DiscardableMemory::GetSupportedTypes().begin(),
66 DiscardableMemory::GetSupportedTypes().end(),
67 IsNativeType) !=
68 DiscardableMemory::GetSupportedTypes().end());
38 #endif 69 #endif
39 } 70 }
40 71
41 // Test Lock() and Unlock() functionalities. 72 // Test Lock() and Unlock() functionalities.
42 TEST(DiscardableMemoryTest, LockAndUnLock) { 73 TEST_P(DiscardableMemoryTest, LockAndUnLock) {
43 const scoped_ptr<DiscardableMemory> memory( 74 const scoped_ptr<DiscardableMemory> memory(
44 DiscardableMemory::CreateLockedMemory(kSize)); 75 DiscardableMemory::CreateLockedMemory(kSize));
45 ASSERT_TRUE(memory); 76 ASSERT_TRUE(memory);
46 void* addr = memory->Memory(); 77 void* addr = memory->Memory();
47 ASSERT_NE(static_cast<void*>(NULL), addr); 78 ASSERT_NE(static_cast<void*>(NULL), addr);
48 79
49 memory->Unlock(); 80 memory->Unlock();
50 // The system should have no reason to purge discardable blocks in this brief 81 // The system should have no reason to purge discardable blocks in this brief
51 // interval, though technically speaking this might flake. 82 // interval, though technically speaking this might flake.
52 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock()); 83 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
53 addr = memory->Memory(); 84 addr = memory->Memory();
54 ASSERT_NE(static_cast<void*>(NULL), addr); 85 ASSERT_NE(static_cast<void*>(NULL), addr);
55 86
56 memory->Unlock(); 87 memory->Unlock();
57 } 88 }
58 89
59 // Test delete a discardable memory while it is locked. 90 // Test delete a discardable memory while it is locked.
60 TEST(DiscardableMemoryTest, DeleteWhileLocked) { 91 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
61 const scoped_ptr<DiscardableMemory> memory( 92 const scoped_ptr<DiscardableMemory> memory(
62 DiscardableMemory::CreateLockedMemory(kSize)); 93 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(
72 DiscardableMemory::CreateLockedMemory(kSize)); 103 DiscardableMemory::CreateLockedMemory(kSize));
73 ASSERT_TRUE(memory); 104 ASSERT_TRUE(memory);
74 memory->Unlock(); 105 memory->Unlock();
75 106
76 DiscardableMemory::PurgeForTesting(); 107 DiscardableMemory::PurgeForTesting();
77 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock()); 108 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
78 } 109 }
79 #endif // !OS_ANDROID 110 #endif // !OS_ANDROID
80 111
81 #if !defined(NDEBUG) && !defined(OS_ANDROID) 112 #if !defined(NDEBUG) && !defined(OS_ANDROID)
82 // Death tests are not supported with Android APKs. 113 // Death tests are not supported with Android APKs.
83 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { 114 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
84 const scoped_ptr<DiscardableMemory> memory( 115 const scoped_ptr<DiscardableMemory> memory(
85 DiscardableMemory::CreateLockedMemory(kSize)); 116 DiscardableMemory::CreateLockedMemory(kSize));
86 ASSERT_TRUE(memory); 117 ASSERT_TRUE(memory);
87 memory->Unlock(); 118 memory->Unlock();
88 ASSERT_DEATH_IF_SUPPORTED( 119 ASSERT_DEATH_IF_SUPPORTED(
89 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); 120 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
90 } 121 }
91 #endif 122 #endif
92 123
93 } 124 INSTANTIATE_TEST_CASE_P(
125 DiscardableMemoryTests,
126 DiscardableMemoryTest,
127 ::testing::ValuesIn(DiscardableMemory::GetSupportedTypes()));
reveman 2013/12/16 21:05:51 Runs all tests for all supported discardable memor
Philippe 2013/12/17 14:28:21 SGTM
128
129 } // namespace
130 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698