From 8c5c7a905b708f7c0a991ca7c872af645544afef Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Fri, 19 Apr 2013 20:16:01 +0000 Subject: add SkDataTable, to efficiently store an immutable array. Includes a builder helper class. Review URL: https://codereview.chromium.org/14188049 git-svn-id: http://skia.googlecode.com/svn/trunk@8779 2bbb7eff-a529-9590-31e7-b0007b416f81 --- tests/DataRefTest.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'tests/DataRefTest.cpp') diff --git a/tests/DataRefTest.cpp b/tests/DataRefTest.cpp index 8c002c80c0..449149af95 100644 --- a/tests/DataRefTest.cpp +++ b/tests/DataRefTest.cpp @@ -8,6 +8,7 @@ #include "Test.h" #include "SkData.h" #include "SkDataSet.h" +#include "SkDataTable.h" #include "SkStream.h" template class SkTUnref { @@ -22,6 +23,79 @@ private: T* fRef; }; +static void test_simpletable(skiatest::Reporter* reporter) { + const int idata[] = { 1, 4, 9, 16, 25, 63 }; + int icount = SK_ARRAY_COUNT(idata); + SkAutoTUnref itable(SkDataTable::NewCopyArray(idata, + sizeof(idata[0]), + icount)); + REPORTER_ASSERT(reporter, itable->count() == icount); + for (int i = 0; i < icount; ++i) { + size_t size; + REPORTER_ASSERT(reporter, sizeof(int) == itable->atSize(i)); + REPORTER_ASSERT(reporter, *itable->atDataT(i, &size) == idata[i]); + REPORTER_ASSERT(reporter, sizeof(int) == size); + } +} + +static void test_vartable(skiatest::Reporter* reporter) { + const char* str[] = { + "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg" + }; + int count = SK_ARRAY_COUNT(str); + size_t sizes[SK_ARRAY_COUNT(str)]; + for (int i = 0; i < count; ++i) { + sizes[i] = strlen(str[i]) + 1; + } + + SkAutoTUnref table(SkDataTable::NewCopyArrays( + (const void*const*)str, sizes, count)); + + REPORTER_ASSERT(reporter, table->count() == count); + for (int i = 0; i < count; ++i) { + size_t size; + REPORTER_ASSERT(reporter, table->atSize(i) == sizes[i]); + REPORTER_ASSERT(reporter, !strcmp(table->atDataT(i, &size), + str[i])); + REPORTER_ASSERT(reporter, size == sizes[i]); + + const char* s = table->atStr(i); + REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); + } +} + +static void test_tablebuilder(skiatest::Reporter* reporter) { + const char* str[] = { + "", "a", "be", "see", "deigh", "ef", "ggggggggggggggggggggggggggg" + }; + int count = SK_ARRAY_COUNT(str); + + SkDataTableBuilder builder(16); + + for (int i = 0; i < count; ++i) { + builder.append(str[i], strlen(str[i]) + 1); + } + SkAutoTUnref table(builder.createDataTable()); + + REPORTER_ASSERT(reporter, table->count() == count); + for (int i = 0; i < count; ++i) { + size_t size; + REPORTER_ASSERT(reporter, table->atSize(i) == strlen(str[i]) + 1); + REPORTER_ASSERT(reporter, !strcmp(table->atDataT(i, &size), + str[i])); + REPORTER_ASSERT(reporter, size == strlen(str[i]) + 1); + + const char* s = table->atStr(i); + REPORTER_ASSERT(reporter, strlen(s) == strlen(str[i])); + } +} + +static void test_datatable(skiatest::Reporter* reporter) { + test_simpletable(reporter); + test_vartable(reporter); + test_tablebuilder(reporter); +} + static void unrefAll(const SkDataSet::Pair pairs[], int count) { for (int i = 0; i < count; ++i) { pairs[i].fValue->unref(); @@ -146,6 +220,7 @@ static void TestData(skiatest::Reporter* reporter) { test_cstring(reporter); test_dataset(reporter); + test_datatable(reporter); } #include "TestClassDef.h" -- cgit v1.2.3