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"
9 #include "testing/gtest/include/gtest/gtest.h"
13 const size_t kSize
= 1024;
15 #if defined(OS_ANDROID)
16 TEST(DiscardableMemoryTest
, TooLargeAllocationFails
) {
17 const size_t kPageSize
= 4096;
18 const size_t max_allowed_allocation_size
=
19 std::numeric_limits
<size_t>::max() - kPageSize
+ 1;
20 scoped_ptr
<DiscardableMemory
> memory(
21 DiscardableMemory::CreateLockedMemory(max_allowed_allocation_size
+ 1));
22 // 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
29 TEST(DiscardableMemoryTest
, SupportedNatively
) {
30 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
31 ASSERT_TRUE(DiscardableMemory::SupportedNatively());
33 // 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
35 // natively' define for this case. At present, if it's not always supported
36 // natively, it's never supported.
37 ASSERT_FALSE(DiscardableMemory::SupportedNatively());
41 // Test Lock() and Unlock() functionalities.
42 TEST(DiscardableMemoryTest
, LockAndUnLock
) {
43 const scoped_ptr
<DiscardableMemory
> memory(
44 DiscardableMemory::CreateLockedMemory(kSize
));
46 void* addr
= memory
->Memory();
47 ASSERT_NE(static_cast<void*>(NULL
), addr
);
50 // The system should have no reason to purge discardable blocks in this brief
51 // interval, though technically speaking this might flake.
52 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS
, memory
->Lock());
53 addr
= memory
->Memory();
54 ASSERT_NE(static_cast<void*>(NULL
), addr
);
59 // Test delete a discardable memory while it is locked.
60 TEST(DiscardableMemoryTest
, DeleteWhileLocked
) {
61 const scoped_ptr
<DiscardableMemory
> memory(
62 DiscardableMemory::CreateLockedMemory(kSize
));
66 #if !defined(OS_ANDROID)
67 // Test forced purging.
68 TEST(DiscardableMemoryTest
, Purge
) {
69 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());
71 const scoped_ptr
<DiscardableMemory
> memory(
72 DiscardableMemory::CreateLockedMemory(kSize
));
76 DiscardableMemory::PurgeForTesting();
77 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED
, memory
->Lock());
81 #if !defined(NDEBUG) && !defined(OS_ANDROID)
82 // Death tests are not supported with Android APKs.
83 TEST(DiscardableMemoryTest
, UnlockedMemoryAccessCrashesInDebugMode
) {
84 const scoped_ptr
<DiscardableMemory
> memory(
85 DiscardableMemory::CreateLockedMemory(kSize
));
88 ASSERT_DEATH_IF_SUPPORTED(
89 { *static_cast<int*>(memory
->Memory()) = 0xdeadbeef; }, ".*");