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"
6 #include "testing/gtest/include/gtest/gtest.h"
10 const size_t kSize = 1024;
12 TEST(DiscardableMemoryTest, SupportedNatively) {
13 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
14 ASSERT_TRUE(DiscardableMemory::SupportedNatively());
16 // If we ever have a platform that decides at runtime if it can support
17 // discardable memory natively, then we'll have to add a 'never supported
18 // natively' define for this case. At present, if it's not always supported
19 // natively, it's never supported.
20 ASSERT_FALSE(DiscardableMemory::SupportedNatively());
24 // Test Lock() and Unlock() functionalities.
25 TEST(DiscardableMemoryTest, LockAndUnLock) {
26 const scoped_ptr<DiscardableMemory> memory(
27 DiscardableMemory::CreateLockedMemory(kSize));
29 void* addr = memory->Memory();
30 ASSERT_NE(static_cast<void*>(NULL), addr);
33 // The system should have no reason to purge discardable blocks in this brief
34 // interval, though technically speaking this might flake.
35 EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
36 addr = memory->Memory();
37 ASSERT_NE(static_cast<void*>(NULL), addr);
42 // Test delete a discardable memory while it is locked.
43 TEST(DiscardableMemoryTest, DeleteWhileLocked) {
44 const scoped_ptr<DiscardableMemory> memory(
45 DiscardableMemory::CreateLockedMemory(kSize));
49 #if !defined(OS_ANDROID)
50 // Test forced purging.
51 TEST(DiscardableMemoryTest, Purge) {
52 ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());
54 const scoped_ptr<DiscardableMemory> memory(
55 DiscardableMemory::CreateLockedMemory(kSize));
59 DiscardableMemory::PurgeForTesting();
60 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
64 #if !defined(NDEBUG) && !defined(OS_ANDROID)
65 // Death tests are not supported with Android APKs.
66 TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
67 const scoped_ptr<DiscardableMemory> memory(
68 DiscardableMemory::CreateLockedMemory(kSize));
71 ASSERT_DEATH_IF_SUPPORTED(
72 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");