Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2785)

Unified Diff: base/memory/discardable_memory_unittest.cc

Issue 114923005: base: Discardable memory types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add missing include Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/discardable_memory_unittest.cc
diff --git a/base/memory/discardable_memory_unittest.cc b/base/memory/discardable_memory_unittest.cc
index c9f67b2556f17a24a1e7f65d9533403106e6c94e..19d694142fe88388e6167e8f8c608a3343bef481 100644
--- a/base/memory/discardable_memory_unittest.cc
+++ b/base/memory/discardable_memory_unittest.cc
@@ -2,18 +2,45 @@
// 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>
+#if defined(OS_ANDROID)
willchan no longer on Chromium 2013/12/24 01:21:06 Move platform specific includes to the end, as per
reveman 2013/12/26 21:53:35 Done.
#include <limits>
+#endif
+#include "base/memory/discardable_memory.h"
willchan no longer on Chromium 2013/12/24 01:21:06 This should be first.
reveman 2013/12/26 11:46:08 Done.
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
+namespace {
+
+class DiscardableMemoryTest
+ : public testing::TestWithParam<DiscardableMemoryType> {
+ public:
+ // Overridden from testing::Test:
+ virtual void SetUp() OVERRIDE {
+ old_type_ = DiscardableMemory::GetType();
+ DiscardableMemory::SetType(GetParam());
+ }
+ virtual void TearDown() OVERRIDE {
+ DiscardableMemory::SetType(old_type_);
+ }
+
+ private:
+ DiscardableMemoryType old_type_;
+};
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));
+}
+
#if defined(OS_ANDROID)
-TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
+TEST_P(DiscardableMemoryTest, TooLargeAllocationFails) {
+ if (GetParam() != DISCARDABLE_MEMORY_TYPE_ANDROID)
+ return;
const size_t kPageSize = 4096;
const size_t max_allowed_allocation_size =
std::numeric_limits<size_t>::max() - kPageSize + 1;
@@ -26,20 +53,32 @@ TEST(DiscardableMemoryTest, TooLargeAllocationFails) {
}
#endif
-TEST(DiscardableMemoryTest, SupportedNatively) {
+bool IsNativeType(DiscardableMemoryType type) {
+ return
+ type == DISCARDABLE_MEMORY_TYPE_ANDROID ||
+ type == DISCARDABLE_MEMORY_TYPE_MAC;
+}
+
+TEST_P(DiscardableMemoryTest, SupportedNatively) {
+ std::vector<DiscardableMemoryType> supported_types;
+ DiscardableMemory::GetSupportedTypes(&supported_types);
#if defined(DISCARDABLE_MEMORY_ALWAYS_SUPPORTED_NATIVELY)
- ASSERT_TRUE(DiscardableMemory::SupportedNatively());
+ EXPECT_NE(0u, 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.
- ASSERT_FALSE(DiscardableMemory::SupportedNatively());
+ EXPECT_EQ(0u, std::count_if(supported_types.begin(),
+ supported_types.end(),
+ IsNativeType));
#endif
}
// Test Lock() and Unlock() functionalities.
-TEST(DiscardableMemoryTest, LockAndUnLock) {
+TEST_P(DiscardableMemoryTest, LockAndUnLock) {
const scoped_ptr<DiscardableMemory> memory(
DiscardableMemory::CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
@@ -49,7 +88,7 @@ TEST(DiscardableMemoryTest, LockAndUnLock) {
memory->Unlock();
// The system should have no reason to purge discardable blocks in this brief
// interval, though technically speaking this might flake.
- EXPECT_EQ(DISCARDABLE_MEMORY_SUCCESS, memory->Lock());
+ EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS, memory->Lock());
addr = memory->Memory();
ASSERT_NE(static_cast<void*>(NULL), addr);
@@ -57,7 +96,7 @@ TEST(DiscardableMemoryTest, LockAndUnLock) {
}
// Test delete a discardable memory while it is locked.
-TEST(DiscardableMemoryTest, DeleteWhileLocked) {
+TEST_P(DiscardableMemoryTest, DeleteWhileLocked) {
const scoped_ptr<DiscardableMemory> memory(
DiscardableMemory::CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
@@ -65,7 +104,7 @@ TEST(DiscardableMemoryTest, DeleteWhileLocked) {
#if !defined(OS_ANDROID)
// Test forced purging.
-TEST(DiscardableMemoryTest, Purge) {
+TEST_P(DiscardableMemoryTest, Purge) {
ASSERT_TRUE(DiscardableMemory::PurgeForTestingSupported());
const scoped_ptr<DiscardableMemory> memory(
@@ -74,13 +113,13 @@ TEST(DiscardableMemoryTest, Purge) {
memory->Unlock();
DiscardableMemory::PurgeForTesting();
- EXPECT_EQ(DISCARDABLE_MEMORY_PURGED, memory->Lock());
+ EXPECT_EQ(DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, memory->Lock());
}
#endif // !OS_ANDROID
#if !defined(NDEBUG) && !defined(OS_ANDROID)
// Death tests are not supported with Android APKs.
-TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
+TEST_P(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
const scoped_ptr<DiscardableMemory> memory(
DiscardableMemory::CreateLockedMemory(kSize));
ASSERT_TRUE(memory);
@@ -90,4 +129,16 @@ TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) {
}
#endif
+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

Powered by Google App Engine
This is Rietveld 408576698