forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscardable_memory_unittest.cc
134 lines (110 loc) · 4.04 KB
/
discardable_memory_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/memory/discardable_memory.h"
#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> {
public:
DiscardableMemoryTest() {}
virtual ~DiscardableMemoryTest() {
}
protected:
scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) {
return DiscardableMemory::CreateLockedMemoryWithType(
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));
}
bool IsNativeType(DiscardableMemoryType type) {
return type == DISCARDABLE_MEMORY_TYPE_ASHMEM ||
type == DISCARDABLE_MEMORY_TYPE_MACH;
}
TEST_P(DiscardableMemoryTest, SupportedNatively) {
std::vector<DiscardableMemoryType> supported_types;
DiscardableMemory::GetSupportedTypes(&supported_types);
#if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
EXPECT_NE(0, 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.
EXPECT_EQ(0, std::count_if(supported_types.begin(),
supported_types.end(),
IsNativeType));
#endif
}
// Test Lock() and Unlock() functionalities.
TEST_P(DiscardableMemoryTest, LockAndUnLock) {
const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
void* addr = memory->Memory();
ASSERT_NE(nullptr, addr);
memory->Unlock();
EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, memory->Lock());
addr = memory->Memory();
ASSERT_NE(nullptr, addr);
memory->Unlock();
}
// Test delete a discardable memory while it is locked.
TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
}
// Test forced purging.
TEST_P(DiscardableMemoryTest, Purge) {
const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
memory->Unlock();
DiscardableMemory::PurgeForTesting();
EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock());
}
#if !defined(NDEBUG) && !defined(OS_ANDROID)
// Death tests are not supported with Android APKs.
TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
memory->Unlock();
ASSERT_DEATH_IF_SUPPORTED(
{ *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
}
#endif
// Test behavior when creating enough instances that could use up a 32-bit
// address space.
TEST_P(DiscardableMemoryTest, AddressSpace) {
const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB.
const size_t kNumberOfInstances = 1024 + 1; // >4GiB total.
scoped_ptr<DiscardableMemory> instances[kNumberOfInstances];
for (auto& memory : instances) {
memory = CreateLockedMemory(kLargeSize);
ASSERT_TRUE(memory);
void* addr = memory->Memory();
ASSERT_NE(nullptr, addr);
memory->Unlock();
}
}
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