Add discardable memory emulation for non-android/mac platforms
[chromium-blink-merge.git] / base / memory / discardable_memory_unittest.cc
blobeb730f19f18e8482fde972598c99378f2c30447a
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"
8 namespace base {
10 const size_t kSize = 1024;
12 TEST(DiscardableMemoryTest, SupportedNatively) {
13 #if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
14 ASSERT_TRUE(DiscardableMemory::SupportedNatively());
15 #else
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());
21 #endif
24 // Test Lock() and Unlock() functionalities.
25 TEST(DiscardableMemoryTest, LockAndUnLock) {
26 const scoped_ptr<DiscardableMemory> memory(
27 DiscardableMemory::CreateLockedMemory(kSize));
28 ASSERT_TRUE(memory);
29 void* addr = memory->Memory();
30 ASSERT_NE(static_cast<void*>(NULL), addr);
32 memory->Unlock();
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);
39 memory->Unlock();
42 // Test delete a discardable memory while it is locked.
43 TEST(DiscardableMemoryTest, DeleteWhileLocked) {
44 const scoped_ptr<DiscardableMemory> memory(
45 DiscardableMemory::CreateLockedMemory(kSize));
46 ASSERT_TRUE(memory);
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));
56 ASSERT_TRUE(memory);
57 memory->Unlock();
59 DiscardableMemory::PurgeForTesting();
60 EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
62 #endif // !OS_ANDROID
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));
69 ASSERT_TRUE(memory);
70 memory->Unlock();
71 ASSERT_DEATH_IF_SUPPORTED(
72 { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*");
74 #endif