aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/DiscardableMemoryTest.cpp
diff options
context:
space:
mode:
authorGravatar halcanary <halcanary@google.com>2016-09-08 08:58:37 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2016-09-08 08:58:37 -0700
commit51761d12d00fb1ebe7c63efce91b0e84b4afac5c (patch)
tree932c686c31bffb3ec933605d66e84f38d33e5bce /tests/DiscardableMemoryTest.cpp
parentf53fcc8ccaaa5e3cd24846d9e3ac38ad311f3711 (diff)
Tests: DiscardableMemory test no longer relies on global state
Diffstat (limited to 'tests/DiscardableMemoryTest.cpp')
-rw-r--r--tests/DiscardableMemoryTest.cpp54
1 files changed, 41 insertions, 13 deletions
diff --git a/tests/DiscardableMemoryTest.cpp b/tests/DiscardableMemoryTest.cpp
index 28dd6e98a5..5d2d6b9e1e 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<SkDiscardableMemory> 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<SkDiscardableMemory> 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<SkDiscardableMemoryPool> pool(
+ SkDiscardableMemoryPool::Create(1024, /* mutex = */ nullptr));
+ std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength));
+ test_dm(reporter, dm.get(), true);
+}
+