Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / base / memory / discardable_memory_unittest.cc
blob516a96b5a4fa2576a6cb031877ddf7d7bff0cfac
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.
5 #include "base/memory/discardable_memory.h"
7 #include <algorithm>
9 #include "testing/gtest/include/gtest/gtest.h"
11 #if defined(OS_ANDROID)
12 #include <limits>
13 #endif
15 namespace base {
16 namespace {
18 class DiscardableMemoryTest
19 : public testing::TestWithParam<DiscardableMemoryType> {
20 public:
21 DiscardableMemoryTest() {}
22 virtual ~DiscardableMemoryTest() {
25 protected:
26 scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size) {
27 return DiscardableMemory::CreateLockedMemoryWithType(
28 GetParam(), size).Pass();
32 const size_t kSize = 1024;
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));
40 bool IsNativeType(DiscardableMemoryType type) {
41 return
42 type == DISCARDABLE_MEMORY_TYPE_ASHMEM ||
43 type == DISCARDABLE_MEMORY_TYPE_MAC;
46 TEST_P(DiscardableMemoryTest, SupportedNatively) {
47 std::vector<DiscardableMemoryType> supported_types;
48 DiscardableMemory::GetSupportedTypes(&supported_types);
49 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
50 EXPECT_NE(0, std::count_if(supported_types.begin(),
51 supported_types.end(),
52 IsNativeType));
53 #else
54 // If we ever have a platform that decides at runtime if it can support
55 // discardable memory natively, then we'll have to add a 'never supported
56 // natively' define for this case. At present, if it's not always supported
57 // natively, it's never supported.
58 EXPECT_EQ(0, std::count_if(supported_types.begin(),
59 supported_types.end(),
60 IsNativeType));
61 #endif
64 // Test Lock() and Unlock() functionalities.
65 TEST_P(DiscardableMemoryTest, LockAndUnLock) {
66 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
67 ASSERT_TRUE(memory);
68 void* addr = memory->Memory();
69 ASSERT_NE(static_cast<void*>(NULL), addr);
71 memory->Unlock();
73 EXPECT_NE(DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, memory->Lock());
74 addr = memory->Memory();
75 ASSERT_NE(static_cast<void*>(NULL), addr);
77 memory->Unlock();
80 // Test delete a discardable memory while it is locked.
81 TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
82 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
83 ASSERT_TRUE(memory);
86 // Test forced purging.
87 TEST_P(DiscardableMemoryTest, Purge) {
88 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
89 ASSERT_TRUE(memory);
90 memory->Unlock();
92 DiscardableMemory::PurgeForTesting();
93 EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock());
96 #if !defined(NDEBUG) && !defined(OS_ANDROID)
97 // Death tests are not supported with Android APKs.
98 TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
99 const scoped_ptr<DiscardableMemory> memory(CreateLockedMemory(kSize));
100 ASSERT_TRUE(memory);
101 memory->Unlock();
102 ASSERT_DEATH_IF_SUPPORTED(
103 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
105 #endif
107 std::vector<DiscardableMemoryType> GetSupportedDiscardableMemoryTypes() {
108 std::vector<DiscardableMemoryType> supported_types;
109 DiscardableMemory::GetSupportedTypes(&supported_types);
110 return supported_types;
113 INSTANTIATE_TEST_CASE_P(
114 DiscardableMemoryTests,
115 DiscardableMemoryTest,
116 ::testing::ValuesIn(GetSupportedDiscardableMemoryTypes()));
118 } // namespace
119 } // namespace base