From 51761d12d00fb1ebe7c63efce91b0e84b4afac5c Mon Sep 17 00:00:00 2001 From: halcanary Date: Thu, 8 Sep 2016 08:58:37 -0700 Subject: [PATCH] Tests: DiscardableMemory test no longer relies on global state BUG=skia: GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2323013002 Review-Url: https://codereview.chromium.org/2323013002 --- tests/DiscardableMemoryTest.cpp | 54 +++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/tests/DiscardableMemoryTest.cpp b/tests/DiscardableMemoryTest.cpp index 28dd6e9..5d2d6b9 100644 --- a/tests/DiscardableMemoryTest.cpp +++ b/tests/DiscardableMemoryTest.cpp @@ -5,28 +5,56 @@ * found in the LICENSE file. */ -#include "SkDiscardableMemory.h" +#include "SkDiscardableMemoryPool.h" #include "Test.h" -DEF_TEST(DiscardableMemory, reporter) { - const char testString[] = "HELLO, WORLD!"; - const size_t len = sizeof(testString); - SkAutoTDelete dm(SkDiscardableMemory::Create(len)); - REPORTER_ASSERT(reporter, dm.get() != nullptr); - if (nullptr == dm.get()) { +namespace { +constexpr char kTestString[] = "HELLO, WORLD!"; +constexpr size_t kTestStringLength = sizeof(kTestString); +} + +static void test_dm(skiatest::Reporter* reporter, + SkDiscardableMemory* dm, + bool assertRelock) { + REPORTER_ASSERT(reporter, dm); + if (!dm) { return; } void* ptr = dm->data(); - REPORTER_ASSERT(reporter, ptr != nullptr); - memcpy(ptr, testString, sizeof(testString)); + REPORTER_ASSERT(reporter, ptr); + if (!ptr) { + return; + } + memcpy(ptr, kTestString, sizeof(kTestString)); dm->unlock(); - bool success = dm->lock(); - REPORTER_ASSERT(reporter, success); - if (!success) { + bool relockSuccess = dm->lock(); + if (assertRelock) { + REPORTER_ASSERT(reporter, relockSuccess); + } + if (!relockSuccess) { return; } ptr = dm->data(); - REPORTER_ASSERT(reporter, 0 == memcmp(ptr, testString, len)); + REPORTER_ASSERT(reporter, ptr); + if (!ptr) { + return; + } + REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength)); dm->unlock(); } + +DEF_TEST(DiscardableMemory_global, reporter) { + std::unique_ptr dm(SkDiscardableMemory::Create(kTestStringLength)); + // lock() test is allowed to fail, since other threads could be + // using global pool. + test_dm(reporter, dm.get(), false); +} + +DEF_TEST(DiscardableMemory_nonglobal, reporter) { + std::unique_ptr pool( + SkDiscardableMemoryPool::Create(1024, /* mutex = */ nullptr)); + std::unique_ptr dm(pool->create(kTestStringLength)); + test_dm(reporter, dm.get(), true); +} + -- 2.7.4