epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 8 | #include "Test.h" |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 9 | #include "SkData.h" |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 10 | |
| 11 | static void* gGlobal; |
| 12 | |
| 13 | static void delete_int_proc(const void* ptr, size_t len, void* context) { |
| 14 | int* data = (int*)ptr; |
| 15 | SkASSERT(context == gGlobal); |
| 16 | delete[] data; |
| 17 | } |
| 18 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 19 | static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 20 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 21 | } |
| 22 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 23 | static void assert_data(skiatest::Reporter* reporter, SkData* ref, |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 24 | const void* data, size_t len) { |
| 25 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 26 | REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len)); |
| 27 | } |
| 28 | |
| 29 | void TestDataRef(skiatest::Reporter* reporter) { |
| 30 | const char* str = "We the people, in order to form a more perfect union."; |
| 31 | const int N = 10; |
| 32 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 33 | SkData* r0 = SkData::NewEmpty(); |
| 34 | SkData* r1 = SkData::NewWithCopy(str, strlen(str)); |
| 35 | SkData* r2 = SkData::NewWithProc(new int[N], N*sizeof(int), |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 36 | delete_int_proc, gGlobal); |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 37 | SkData* r3 = SkData::NewSubset(r1, 7, 6); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 38 | |
| 39 | SkAutoUnref aur0(r0); |
| 40 | SkAutoUnref aur1(r1); |
| 41 | SkAutoUnref aur2(r2); |
| 42 | SkAutoUnref aur3(r3); |
| 43 | |
| 44 | assert_len(reporter, r0, 0); |
| 45 | assert_len(reporter, r1, strlen(str)); |
| 46 | assert_len(reporter, r2, N * sizeof(int)); |
| 47 | assert_len(reporter, r3, 6); |
| 48 | |
| 49 | assert_data(reporter, r1, str, strlen(str)); |
| 50 | assert_data(reporter, r3, "people", 6); |
| 51 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 52 | SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 53 | assert_len(reporter, tmp, 0); |
| 54 | tmp->unref(); |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 55 | tmp = SkData::NewSubset(r1, 0, 0); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 56 | assert_len(reporter, tmp, 0); |
| 57 | tmp->unref(); |
| 58 | } |
| 59 | |
| 60 | #include "TestClassDef.h" |
| 61 | DEFINE_TESTCLASS("DataRef", DataRefTestClass, TestDataRef) |