skia2/tests/DataRefTest.cpp

153 lines
4.6 KiB
C++
Raw Normal View History

Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:26:00 +00:00
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "Test.h"
#include "SkData.h"
#include "SkDataSet.h"
#include "SkStream.h"
template <typename T> class SkTUnref {
public:
SkTUnref(T* ref) : fRef(ref) {}
~SkTUnref() { fRef->unref(); }
operator T*() { return fRef; }
operator const T*() { return fRef; }
private:
T* fRef;
};
static void unrefAll(const SkDataSet::Pair pairs[], int count) {
for (int i = 0; i < count; ++i) {
pairs[i].fValue->unref();
}
}
// asserts that inner is a subset of outer
static void test_dataset_subset(skiatest::Reporter* reporter,
const SkDataSet& outer, const SkDataSet& inner) {
SkDataSet::Iter iter(inner);
for (; !iter.done(); iter.next()) {
SkData* outerData = outer.find(iter.key());
REPORTER_ASSERT(reporter, outerData);
REPORTER_ASSERT(reporter, outerData->equals(iter.value()));
}
}
static void test_datasets_equal(skiatest::Reporter* reporter,
const SkDataSet& ds0, const SkDataSet& ds1) {
REPORTER_ASSERT(reporter, ds0.count() == ds1.count());
test_dataset_subset(reporter, ds0, ds1);
test_dataset_subset(reporter, ds1, ds0);
}
static void test_dataset(skiatest::Reporter* reporter, const SkDataSet& ds,
int count) {
REPORTER_ASSERT(reporter, ds.count() == count);
SkDataSet::Iter iter(ds);
int index = 0;
for (; !iter.done(); iter.next()) {
const char* name = iter.key();
SkData* data = iter.value();
// SkDebugf("[%d] %s:%s\n", index, name, (const char*)data->bytes());
index += 1;
}
REPORTER_ASSERT(reporter, index == count);
SkDynamicMemoryWStream ostream;
ds.writeToStream(&ostream);
SkMemoryStream istream;
istream.setData(ostream.copyToData())->unref();
SkDataSet copy(&istream);
test_datasets_equal(reporter, ds, copy);
}
static void test_dataset(skiatest::Reporter* reporter) {
SkDataSet set0(NULL, 0);
SkDataSet set1("hello", SkTUnref<SkData>(SkData::NewWithCString("world")));
const SkDataSet::Pair pairs[] = {
{ "one", SkData::NewWithCString("1") },
{ "two", SkData::NewWithCString("2") },
{ "three", SkData::NewWithCString("3") },
};
SkDataSet set3(pairs, 3);
unrefAll(pairs, 3);
test_dataset(reporter, set0, 0);
test_dataset(reporter, set1, 1);
test_dataset(reporter, set3, 3);
}
static void* gGlobal;
static void delete_int_proc(const void* ptr, size_t len, void* context) {
int* data = (int*)ptr;
SkASSERT(context == gGlobal);
delete[] data;
}
static void assert_len(skiatest::Reporter* reporter, SkData* ref, size_t len) {
REPORTER_ASSERT(reporter, ref->size() == len);
}
static void assert_data(skiatest::Reporter* reporter, SkData* ref,
const void* data, size_t len) {
REPORTER_ASSERT(reporter, ref->size() == len);
REPORTER_ASSERT(reporter, !memcmp(ref->data(), data, len));
}
static void test_cstring(skiatest::Reporter* reporter) {
const char str[] = "Hello world";
size_t len = strlen(str);
SkAutoTUnref<SkData> r0(SkData::NewWithCopy(str, len + 1));
SkAutoTUnref<SkData> r1(SkData::NewWithCString(str));
REPORTER_ASSERT(reporter, r0->equals(r1));
SkAutoTUnref<SkData> r2(SkData::NewWithCString(NULL));
REPORTER_ASSERT(reporter, 1 == r2->size());
REPORTER_ASSERT(reporter, 0 == *r2->bytes());
}
static void TestData(skiatest::Reporter* reporter) {
const char* str = "We the people, in order to form a more perfect union.";
const int N = 10;
SkAutoTUnref<SkData> r0(SkData::NewEmpty());
SkAutoTUnref<SkData> r1(SkData::NewWithCopy(str, strlen(str)));
SkAutoTUnref<SkData> r2(SkData::NewWithProc(new int[N], N*sizeof(int),
delete_int_proc, gGlobal));
SkAutoTUnref<SkData> r3(SkData::NewSubset(r1, 7, 6));
assert_len(reporter, r0, 0);
assert_len(reporter, r1, strlen(str));
assert_len(reporter, r2, N * sizeof(int));
assert_len(reporter, r3, 6);
assert_data(reporter, r1, str, strlen(str));
assert_data(reporter, r3, "people", 6);
SkData* tmp = SkData::NewSubset(r1, strlen(str), 10);
assert_len(reporter, tmp, 0);
tmp->unref();
tmp = SkData::NewSubset(r1, 0, 0);
assert_len(reporter, tmp, 0);
tmp->unref();
test_cstring(reporter);
test_dataset(reporter);
}
#include "TestClassDef.h"
DEFINE_TESTCLASS("Data", DataTestClass, TestData)