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 | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 10 | #include "SkDataSet.h" |
| 11 | #include "SkStream.h" |
| 12 | |
| 13 | template <typename T> class SkTUnref { |
| 14 | public: |
| 15 | SkTUnref(T* ref) : fRef(ref) {} |
| 16 | ~SkTUnref() { fRef->unref(); } |
| 17 | |
| 18 | operator T*() { return fRef; } |
| 19 | operator const T*() { return fRef; } |
| 20 | |
| 21 | private: |
| 22 | T* fRef; |
| 23 | }; |
| 24 | |
| 25 | static void unrefAll(const SkDataSet::Pair pairs[], int count) { |
| 26 | for (int i = 0; i < count; ++i) { |
| 27 | pairs[i].fValue->unref(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // asserts that inner is a subset of outer |
| 32 | static void test_dataset_subset(skiatest::Reporter* reporter, |
| 33 | const SkDataSet& outer, const SkDataSet& inner) { |
| 34 | SkDataSet::Iter iter(inner); |
| 35 | for (; !iter.done(); iter.next()) { |
| 36 | SkData* outerData = outer.find(iter.key()); |
| 37 | REPORTER_ASSERT(reporter, outerData); |
| 38 | REPORTER_ASSERT(reporter, outerData->equals(iter.value())); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static void test_datasets_equal(skiatest::Reporter* reporter, |
| 43 | const SkDataSet& ds0, const SkDataSet& ds1) { |
| 44 | REPORTER_ASSERT(reporter, ds0.count() == ds1.count()); |
| 45 | |
| 46 | test_dataset_subset(reporter, ds0, ds1); |
| 47 | test_dataset_subset(reporter, ds1, ds0); |
| 48 | } |
| 49 | |
| 50 | static void test_dataset(skiatest::Reporter* reporter, const SkDataSet& ds, |
| 51 | int count) { |
| 52 | REPORTER_ASSERT(reporter, ds.count() == count); |
| 53 | |
| 54 | SkDataSet::Iter iter(ds); |
| 55 | int index = 0; |
| 56 | for (; !iter.done(); iter.next()) { |
| 57 | const char* name = iter.key(); |
| 58 | SkData* data = iter.value(); |
reed@google.com | 79382af | 2012-07-11 18:55:23 +0000 | [diff] [blame] | 59 | // SkDebugf("[%d] %s:%s\n", index, name, (const char*)data->bytes()); |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 60 | index += 1; |
| 61 | } |
| 62 | REPORTER_ASSERT(reporter, index == count); |
| 63 | |
| 64 | SkDynamicMemoryWStream ostream; |
| 65 | ds.writeToStream(&ostream); |
| 66 | SkMemoryStream istream; |
| 67 | istream.setData(ostream.copyToData())->unref(); |
| 68 | SkDataSet copy(&istream); |
| 69 | |
| 70 | test_datasets_equal(reporter, ds, copy); |
| 71 | } |
| 72 | |
| 73 | static void test_dataset(skiatest::Reporter* reporter) { |
| 74 | SkDataSet set0(NULL, 0); |
| 75 | SkDataSet set1("hello", SkTUnref<SkData>(SkData::NewWithCString("world"))); |
| 76 | |
| 77 | const SkDataSet::Pair pairs[] = { |
| 78 | { "one", SkData::NewWithCString("1") }, |
| 79 | { "two", SkData::NewWithCString("2") }, |
| 80 | { "three", SkData::NewWithCString("3") }, |
| 81 | }; |
| 82 | SkDataSet set3(pairs, 3); |
| 83 | unrefAll(pairs, 3); |
| 84 | |
| 85 | test_dataset(reporter, set0, 0); |
| 86 | test_dataset(reporter, set1, 1); |
| 87 | test_dataset(reporter, set3, 3); |
| 88 | } |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 89 | |
| 90 | static void* gGlobal; |
| 91 | |
| 92 | static void delete_int_proc(const void* ptr, size_t len, void* context) { |
| 93 | int* data = (int*)ptr; |
| 94 | SkASSERT(context == gGlobal); |
| 95 | delete[] data; |
| 96 | } |
| 97 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 98 | 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] | 99 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 100 | } |
| 101 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 102 | static void assert_data(skiatest::Reporter* reporter, SkData* ref, |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 103 | const void* data, size_t len) { |
| 104 | REPORTER_ASSERT(reporter, ref->size() == len); |
| 105 | REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len)); |
| 106 | } |
| 107 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 108 | static void test_cstring(skiatest::Reporter* reporter) { |
| 109 | const char str[] = "Hello world"; |
| 110 | size_t len = strlen(str); |
| 111 | |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 112 | SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1)); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 113 | SkAutoTUnref<SkData> r1(SkData::NewWithCString(str)); |
| 114 | |
| 115 | REPORTER_ASSERT(reporter, r0->equals(r1)); |
| 116 | |
| 117 | SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL)); |
reed@google.com | a63a851 | 2012-07-02 20:29:00 +0000 | [diff] [blame] | 118 | REPORTER_ASSERT(reporter, 1 == r2->size()); |
| 119 | REPORTER_ASSERT(reporter, 0 == *r2->bytes()); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 120 | } |
| 121 | |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 122 | static void TestData(skiatest::Reporter* reporter) { |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 123 | const char* str = "We the people, in order to form a more perfect union."; |
| 124 | const int N = 10; |
| 125 | |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 126 | SkAutoTUnref<SkData> r0(SkData::NewEmpty()); |
| 127 | SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str))); |
| 128 | SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int), |
| 129 | delete_int_proc, gGlobal)); |
| 130 | SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6)); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 131 | |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 132 | assert_len(reporter, r0, 0); |
| 133 | assert_len(reporter, r1, strlen(str)); |
| 134 | assert_len(reporter, r2, N * sizeof(int)); |
| 135 | assert_len(reporter, r3, 6); |
| 136 | |
| 137 | assert_data(reporter, r1, str, strlen(str)); |
| 138 | assert_data(reporter, r3, "people", 6); |
| 139 | |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 140 | SkData* tmp = SkData::NewSubset(r1, strlen(str), 10); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 141 | assert_len(reporter, tmp, 0); |
| 142 | tmp->unref(); |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 143 | tmp = SkData::NewSubset(r1, 0, 0); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 144 | assert_len(reporter, tmp, 0); |
| 145 | tmp->unref(); |
reed@google.com | dbc936d | 2012-06-28 15:40:09 +0000 | [diff] [blame] | 146 | |
| 147 | test_cstring(reporter); |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 148 | test_dataset(reporter); |
reed@google.com | 1622c99 | 2011-06-14 19:22:21 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | #include "TestClassDef.h" |
reed@google.com | 91bd459 | 2012-07-11 17:24:49 +0000 | [diff] [blame] | 152 | DEFINE_TESTCLASS("Data", DataTestClass, TestData) |