OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkDiscardableMemory.h" | 8 #include "SkDiscardableMemoryPool.h" |
9 | 9 |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
12 DEF_TEST(DiscardableMemory, reporter) { | 12 namespace { |
13 const char testString[] = "HELLO, WORLD!"; | 13 constexpr char kTestString[] = "HELLO, WORLD!"; |
14 const size_t len = sizeof(testString); | 14 constexpr size_t kTestStringLength = sizeof(kTestString); |
15 SkAutoTDelete<SkDiscardableMemory> dm(SkDiscardableMemory::Create(len)); | 15 } |
16 REPORTER_ASSERT(reporter, dm.get() != nullptr); | 16 |
17 if (nullptr == dm.get()) { | 17 static void test_dm(skiatest::Reporter* reporter, |
| 18 SkDiscardableMemory* dm, |
| 19 bool assertRelock) { |
| 20 REPORTER_ASSERT(reporter, dm); |
| 21 if (!dm) { |
18 return; | 22 return; |
19 } | 23 } |
20 void* ptr = dm->data(); | 24 void* ptr = dm->data(); |
21 REPORTER_ASSERT(reporter, ptr != nullptr); | 25 REPORTER_ASSERT(reporter, ptr); |
22 memcpy(ptr, testString, sizeof(testString)); | 26 if (!ptr) { |
| 27 return; |
| 28 } |
| 29 memcpy(ptr, kTestString, sizeof(kTestString)); |
23 dm->unlock(); | 30 dm->unlock(); |
24 bool success = dm->lock(); | 31 bool relockSuccess = dm->lock(); |
25 REPORTER_ASSERT(reporter, success); | 32 if (assertRelock) { |
26 if (!success) { | 33 REPORTER_ASSERT(reporter, relockSuccess); |
| 34 } |
| 35 if (!relockSuccess) { |
27 return; | 36 return; |
28 } | 37 } |
29 ptr = dm->data(); | 38 ptr = dm->data(); |
30 REPORTER_ASSERT(reporter, 0 == memcmp(ptr, testString, len)); | 39 REPORTER_ASSERT(reporter, ptr); |
| 40 if (!ptr) { |
| 41 return; |
| 42 } |
| 43 REPORTER_ASSERT(reporter, 0 == memcmp(ptr, kTestString, kTestStringLength)); |
31 dm->unlock(); | 44 dm->unlock(); |
32 } | 45 } |
| 46 |
| 47 DEF_TEST(DiscardableMemory_global, reporter) { |
| 48 std::unique_ptr<SkDiscardableMemory> dm(SkDiscardableMemory::Create(kTestStr
ingLength)); |
| 49 // lock() test is allowed to fail, since other threads could be |
| 50 // using global pool. |
| 51 test_dm(reporter, dm.get(), false); |
| 52 } |
| 53 |
| 54 DEF_TEST(DiscardableMemory_nonglobal, reporter) { |
| 55 std::unique_ptr<SkDiscardableMemoryPool> pool( |
| 56 SkDiscardableMemoryPool::Create(1024, /* mutex = */ nullptr)); |
| 57 std::unique_ptr<SkDiscardableMemory> dm(pool->create(kTestStringLength)); |
| 58 test_dm(reporter, dm.get(), true); |
| 59 } |
| 60 |
OLD | NEW |