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.
|
|
|
|
*/
|
2013-12-12 21:11:12 +00:00
|
|
|
|
2015-04-02 20:19:51 +00:00
|
|
|
#include "Resources.h"
|
2017-01-11 17:44:43 +00:00
|
|
|
#include "SkAutoMalloc.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkData.h"
|
2015-04-02 20:19:51 +00:00
|
|
|
#include "SkFrontBufferedStream.h"
|
2013-05-29 13:43:31 +00:00
|
|
|
#include "SkOSFile.h"
|
2016-11-07 23:05:29 +00:00
|
|
|
#include "SkOSPath.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "SkRandom.h"
|
2009-03-07 03:39:23 +00:00
|
|
|
#include "SkStream.h"
|
2015-08-18 20:30:25 +00:00
|
|
|
#include "SkStreamPriv.h"
|
2014-01-24 20:56:26 +00:00
|
|
|
#include "Test.h"
|
2009-03-07 03:39:23 +00:00
|
|
|
|
2013-02-25 20:24:24 +00:00
|
|
|
#ifndef SK_BUILD_FOR_WIN
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2009-03-07 03:39:23 +00:00
|
|
|
#define MAX_SIZE (256 * 1024)
|
|
|
|
|
2013-02-25 20:24:24 +00:00
|
|
|
static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream,
|
|
|
|
const void* src, size_t len, int repeat) {
|
|
|
|
SkAutoSMalloc<256> storage(len);
|
|
|
|
void* tmp = storage.get();
|
|
|
|
|
|
|
|
for (int i = 0; i < repeat; ++i) {
|
|
|
|
size_t bytes = stream->read(tmp, len);
|
|
|
|
REPORTER_ASSERT(reporter, bytes == len);
|
|
|
|
REPORTER_ASSERT(reporter, !memcmp(tmp, src, len));
|
|
|
|
}
|
|
|
|
|
|
|
|
// expect EOF
|
|
|
|
size_t bytes = stream->read(tmp, 1);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == bytes);
|
2013-07-19 13:55:41 +00:00
|
|
|
// isAtEnd might not return true until after the first failing read.
|
|
|
|
REPORTER_ASSERT(reporter, stream->isAtEnd());
|
2013-02-25 20:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_filestreams(skiatest::Reporter* reporter, const char* tmpDir) {
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString path = SkOSPath::Join(tmpDir, "wstream_test");
|
2013-02-25 20:24:24 +00:00
|
|
|
|
|
|
|
const char s[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
|
|
|
{
|
|
|
|
SkFILEWStream writer(path.c_str());
|
|
|
|
if (!writer.isValid()) {
|
2014-01-10 14:58:10 +00:00
|
|
|
ERRORF(reporter, "Failed to create tmp file %s\n", path.c_str());
|
2013-02-25 20:24:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
writer.write(s, 26);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
SkFILEStream stream(path.c_str());
|
|
|
|
REPORTER_ASSERT(reporter, stream.isValid());
|
|
|
|
test_loop_stream(reporter, &stream, s, 26, 100);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream2(stream.duplicate());
|
2013-05-29 13:43:31 +00:00
|
|
|
test_loop_stream(reporter, stream2.get(), s, 26, 100);
|
2013-02-25 20:24:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2013-05-29 13:43:31 +00:00
|
|
|
FILE* file = ::fopen(path.c_str(), "rb");
|
2017-03-10 18:08:15 +00:00
|
|
|
SkFILEStream stream(file);
|
2013-02-25 20:24:24 +00:00
|
|
|
REPORTER_ASSERT(reporter, stream.isValid());
|
|
|
|
test_loop_stream(reporter, &stream, s, 26, 100);
|
2013-05-29 13:43:31 +00:00
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream2(stream.duplicate());
|
2013-05-29 13:43:31 +00:00
|
|
|
test_loop_stream(reporter, stream2.get(), s, 26, 100);
|
2013-02-25 20:24:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-02 16:59:40 +00:00
|
|
|
static void TestWStream(skiatest::Reporter* reporter) {
|
|
|
|
SkDynamicMemoryWStream ds;
|
|
|
|
const char s[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
REPORTER_ASSERT(reporter, ds.write(s, 26));
|
2009-03-07 03:39:23 +00:00
|
|
|
}
|
2016-12-16 16:51:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, ds.bytesWritten() == 100 * 26);
|
2013-07-19 13:55:41 +00:00
|
|
|
|
2009-04-02 16:59:40 +00:00
|
|
|
char* dst = new char[100 * 26 + 1];
|
|
|
|
dst[100*26] = '*';
|
|
|
|
ds.copyTo(dst);
|
|
|
|
REPORTER_ASSERT(reporter, dst[100*26] == '*');
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0);
|
2009-03-07 03:39:23 +00:00
|
|
|
}
|
2011-06-23 21:48:04 +00:00
|
|
|
|
|
|
|
{
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream(ds.detachAsStream());
|
2013-07-19 13:55:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, 100 * 26 == stream->getLength());
|
2016-12-16 16:51:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, ds.bytesWritten() == 0);
|
2013-07-19 13:55:41 +00:00
|
|
|
test_loop_stream(reporter, stream.get(), s, 26, 100);
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream2(stream->duplicate());
|
2013-07-19 13:55:41 +00:00
|
|
|
test_loop_stream(reporter, stream2.get(), s, 26, 100);
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream3(stream->fork());
|
2013-07-19 13:55:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, stream3->isAtEnd());
|
|
|
|
char tmp;
|
|
|
|
size_t bytes = stream->read(&tmp, 1);
|
|
|
|
REPORTER_ASSERT(reporter, 0 == bytes);
|
|
|
|
stream3->rewind();
|
|
|
|
test_loop_stream(reporter, stream3.get(), s, 26, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < 100; i++) {
|
|
|
|
REPORTER_ASSERT(reporter, ds.write(s, 26));
|
|
|
|
}
|
2016-12-16 16:51:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, ds.bytesWritten() == 100 * 26);
|
2013-07-19 13:55:41 +00:00
|
|
|
|
|
|
|
{
|
2016-09-12 19:01:44 +00:00
|
|
|
// Test that this works after a snapshot.
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream(ds.detachAsStream());
|
2016-12-16 16:51:41 +00:00
|
|
|
REPORTER_ASSERT(reporter, ds.bytesWritten() == 0);
|
2013-07-19 13:55:41 +00:00
|
|
|
test_loop_stream(reporter, stream.get(), s, 26, 100);
|
|
|
|
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> stream2(stream->duplicate());
|
2013-07-19 13:55:41 +00:00
|
|
|
test_loop_stream(reporter, stream2.get(), s, 26, 100);
|
2011-06-23 21:48:04 +00:00
|
|
|
}
|
2009-04-02 16:59:40 +00:00
|
|
|
delete[] dst;
|
2013-02-25 20:24:24 +00:00
|
|
|
|
2015-01-20 17:30:20 +00:00
|
|
|
SkString tmpDir = skiatest::GetTmpDir();
|
2013-06-06 14:59:56 +00:00
|
|
|
if (!tmpDir.isEmpty()) {
|
|
|
|
test_filestreams(reporter, tmpDir.c_str());
|
2013-02-25 20:24:24 +00:00
|
|
|
}
|
2009-03-07 03:39:23 +00:00
|
|
|
}
|
|
|
|
|
2011-10-18 11:49:52 +00:00
|
|
|
static void TestPackedUInt(skiatest::Reporter* reporter) {
|
|
|
|
// we know that packeduint tries to write 1, 2 or 4 bytes for the length,
|
|
|
|
// so we test values around each of those transitions (and a few others)
|
|
|
|
const size_t sizes[] = {
|
|
|
|
0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769,
|
|
|
|
0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001,
|
|
|
|
0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001,
|
|
|
|
0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF
|
|
|
|
};
|
2012-08-23 18:14:13 +00:00
|
|
|
|
|
|
|
|
2011-10-18 11:49:52 +00:00
|
|
|
size_t i;
|
2017-01-07 16:16:28 +00:00
|
|
|
SkDynamicMemoryWStream wstream;
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-10-18 11:49:52 +00:00
|
|
|
for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) {
|
|
|
|
bool success = wstream.writePackedUInt(sizes[i]);
|
|
|
|
REPORTER_ASSERT(reporter, success);
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2017-01-07 16:16:28 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> rstream(wstream.detachAsStream());
|
2011-10-18 11:49:52 +00:00
|
|
|
for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) {
|
2017-01-07 16:16:28 +00:00
|
|
|
size_t n = rstream->readPackedUInt();
|
2011-10-18 11:49:52 +00:00
|
|
|
if (sizes[i] != n) {
|
2016-02-25 01:59:16 +00:00
|
|
|
ERRORF(reporter, "sizes:%x != n:%x\n", i, sizes[i], n);
|
2011-10-18 11:49:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
// Test that setting an SkMemoryStream to a nullptr data does not result in a crash when calling
|
2013-01-09 22:02:58 +00:00
|
|
|
// methods that access fData.
|
|
|
|
static void TestDereferencingData(SkMemoryStream* memStream) {
|
2015-08-27 14:41:13 +00:00
|
|
|
memStream->read(nullptr, 0);
|
2013-01-09 22:02:58 +00:00
|
|
|
memStream->getMemoryBase();
|
2016-09-12 19:01:44 +00:00
|
|
|
(void)memStream->asData();
|
2013-01-09 22:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void TestNullData() {
|
2016-09-12 19:01:44 +00:00
|
|
|
SkMemoryStream memStream(nullptr);
|
2013-01-09 22:02:58 +00:00
|
|
|
TestDereferencingData(&memStream);
|
|
|
|
|
2016-09-12 19:01:44 +00:00
|
|
|
memStream.setData(nullptr);
|
2013-01-09 22:02:58 +00:00
|
|
|
TestDereferencingData(&memStream);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-12 21:11:12 +00:00
|
|
|
DEF_TEST(Stream, reporter) {
|
2009-03-07 03:39:23 +00:00
|
|
|
TestWStream(reporter);
|
2011-10-18 11:49:52 +00:00
|
|
|
TestPackedUInt(reporter);
|
2013-01-09 22:02:58 +00:00
|
|
|
TestNullData();
|
2009-03-07 03:39:23 +00:00
|
|
|
}
|
2015-04-02 20:19:51 +00:00
|
|
|
|
2016-11-03 18:06:31 +00:00
|
|
|
#ifndef SK_BUILD_FOR_IOS
|
2015-04-02 20:19:51 +00:00
|
|
|
/**
|
|
|
|
* Tests peeking and then reading the same amount. The two should provide the
|
|
|
|
* same results.
|
2015-12-07 19:37:13 +00:00
|
|
|
* Returns the amount successfully read minus the amount successfully peeked.
|
2015-04-02 20:19:51 +00:00
|
|
|
*/
|
2015-12-07 19:37:13 +00:00
|
|
|
static size_t compare_peek_to_read(skiatest::Reporter* reporter,
|
|
|
|
SkStream* stream, size_t bytesToPeek) {
|
2015-04-02 20:19:51 +00:00
|
|
|
// The rest of our tests won't be very interesting if bytesToPeek is zero.
|
|
|
|
REPORTER_ASSERT(reporter, bytesToPeek > 0);
|
|
|
|
SkAutoMalloc peekStorage(bytesToPeek);
|
|
|
|
SkAutoMalloc readStorage(bytesToPeek);
|
|
|
|
void* peekPtr = peekStorage.get();
|
|
|
|
void* readPtr = peekStorage.get();
|
|
|
|
|
2015-12-07 19:37:13 +00:00
|
|
|
const size_t bytesPeeked = stream->peek(peekPtr, bytesToPeek);
|
2015-04-02 20:19:51 +00:00
|
|
|
const size_t bytesRead = stream->read(readPtr, bytesToPeek);
|
|
|
|
|
|
|
|
// bytesRead should only be less than attempted if the stream is at the
|
|
|
|
// end.
|
|
|
|
REPORTER_ASSERT(reporter, bytesRead == bytesToPeek || stream->isAtEnd());
|
|
|
|
|
|
|
|
// peek and read should behave the same, except peek returned to the
|
|
|
|
// original position, so they read the same data.
|
2015-12-07 19:37:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, !memcmp(peekPtr, readPtr, bytesPeeked));
|
|
|
|
|
|
|
|
// A stream should never be able to peek more than it can read.
|
|
|
|
REPORTER_ASSERT(reporter, bytesRead >= bytesPeeked);
|
2015-04-02 20:19:51 +00:00
|
|
|
|
2015-12-07 19:37:13 +00:00
|
|
|
return bytesRead - bytesPeeked;
|
2015-04-02 20:19:51 +00:00
|
|
|
}
|
|
|
|
|
2015-12-07 19:37:13 +00:00
|
|
|
static void test_fully_peekable_stream(skiatest::Reporter* r, SkStream* stream, size_t limit) {
|
2015-04-02 20:19:51 +00:00
|
|
|
for (size_t i = 1; !stream->isAtEnd(); i++) {
|
2015-12-07 19:37:13 +00:00
|
|
|
REPORTER_ASSERT(r, compare_peek_to_read(r, stream, i) == 0);
|
2015-04-02 20:19:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_peeking_front_buffered_stream(skiatest::Reporter* r,
|
|
|
|
const SkStream& original,
|
|
|
|
size_t bufferSize) {
|
2017-09-16 01:39:47 +00:00
|
|
|
std::unique_ptr<SkStream> dupe(original.duplicate());
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(r, dupe != nullptr);
|
2017-09-16 01:39:47 +00:00
|
|
|
auto bufferedStream = SkFrontBufferedStream::Make(std::move(dupe), bufferSize);
|
2015-08-27 14:41:13 +00:00
|
|
|
REPORTER_ASSERT(r, bufferedStream != nullptr);
|
2015-12-07 19:37:13 +00:00
|
|
|
|
|
|
|
size_t peeked = 0;
|
|
|
|
for (size_t i = 1; !bufferedStream->isAtEnd(); i++) {
|
2016-11-03 18:40:50 +00:00
|
|
|
const size_t unpeekableBytes = compare_peek_to_read(r, bufferedStream.get(), i);
|
2015-12-07 19:37:13 +00:00
|
|
|
if (unpeekableBytes > 0) {
|
|
|
|
// This could not have returned a number greater than i.
|
|
|
|
REPORTER_ASSERT(r, unpeekableBytes <= i);
|
|
|
|
|
|
|
|
// We have reached the end of the buffer. Verify that it was at least
|
|
|
|
// bufferSize.
|
|
|
|
REPORTER_ASSERT(r, peeked + i - unpeekableBytes >= bufferSize);
|
|
|
|
// No more peeking is supported.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
peeked += i;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test that attempting to peek beyond the length of the buffer does not prevent rewinding.
|
2017-09-16 01:39:47 +00:00
|
|
|
bufferedStream = SkFrontBufferedStream::Make(original.duplicate(), bufferSize);
|
2015-12-07 19:37:13 +00:00
|
|
|
REPORTER_ASSERT(r, bufferedStream != nullptr);
|
|
|
|
|
|
|
|
const size_t bytesToPeek = bufferSize + 1;
|
|
|
|
SkAutoMalloc peekStorage(bytesToPeek);
|
|
|
|
SkAutoMalloc readStorage(bytesToPeek);
|
|
|
|
|
|
|
|
for (size_t start = 0; start <= bufferSize; start++) {
|
|
|
|
// Skip to the starting point
|
|
|
|
REPORTER_ASSERT(r, bufferedStream->skip(start) == start);
|
|
|
|
|
|
|
|
const size_t bytesPeeked = bufferedStream->peek(peekStorage.get(), bytesToPeek);
|
|
|
|
if (0 == bytesPeeked) {
|
2016-02-18 13:59:25 +00:00
|
|
|
// Peeking should only fail completely if we have read/skipped beyond the buffer.
|
|
|
|
REPORTER_ASSERT(r, start >= bufferSize);
|
2015-12-07 19:37:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only read the amount that was successfully peeked.
|
|
|
|
const size_t bytesRead = bufferedStream->read(readStorage.get(), bytesPeeked);
|
|
|
|
REPORTER_ASSERT(r, bytesRead == bytesPeeked);
|
|
|
|
REPORTER_ASSERT(r, !memcmp(peekStorage.get(), readStorage.get(), bytesPeeked));
|
|
|
|
|
|
|
|
// This should be safe to rewind.
|
|
|
|
REPORTER_ASSERT(r, bufferedStream->rewind());
|
|
|
|
}
|
2015-04-02 20:19:51 +00:00
|
|
|
}
|
|
|
|
|
2016-03-29 16:03:52 +00:00
|
|
|
// This test uses file system operations that don't work out of the
|
|
|
|
// box on iOS. It's likely that we don't need them on iOS. Ignoring for now.
|
|
|
|
// TODO(stephana): Re-evaluate if we need this in the future.
|
2015-04-02 20:19:51 +00:00
|
|
|
DEF_TEST(StreamPeek, reporter) {
|
|
|
|
// Test a memory stream.
|
|
|
|
const char gAbcs[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
SkMemoryStream memStream(gAbcs, strlen(gAbcs), false);
|
2015-12-07 19:37:13 +00:00
|
|
|
test_fully_peekable_stream(reporter, &memStream, memStream.getLength());
|
2015-04-02 20:19:51 +00:00
|
|
|
|
|
|
|
// Test an arbitrary file stream. file streams do not support peeking.
|
|
|
|
SkFILEStream fileStream(GetResourcePath("baby_tux.webp").c_str());
|
|
|
|
REPORTER_ASSERT(reporter, fileStream.isValid());
|
2015-04-14 13:08:04 +00:00
|
|
|
if (!fileStream.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-02 20:19:51 +00:00
|
|
|
SkAutoMalloc storage(fileStream.getLength());
|
|
|
|
for (size_t i = 1; i < fileStream.getLength(); i++) {
|
2015-12-07 19:37:13 +00:00
|
|
|
REPORTER_ASSERT(reporter, fileStream.peek(storage.get(), i) == 0);
|
2015-04-02 20:19:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now test some FrontBufferedStreams
|
|
|
|
for (size_t i = 1; i < memStream.getLength(); i++) {
|
|
|
|
test_peeking_front_buffered_stream(reporter, memStream, i);
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 15:13:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Asserts that asset == expected and is peekable.
|
|
|
|
static void stream_peek_test(skiatest::Reporter* rep,
|
|
|
|
SkStreamAsset* asset,
|
|
|
|
const SkData* expected) {
|
|
|
|
if (asset->getLength() != expected->size()) {
|
|
|
|
ERRORF(rep, "Unexpected length.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SkRandom rand;
|
|
|
|
uint8_t buffer[4096];
|
|
|
|
const uint8_t* expect = expected->bytes();
|
|
|
|
for (size_t i = 0; i < asset->getLength(); ++i) {
|
|
|
|
uint32_t maxSize =
|
|
|
|
SkToU32(SkTMin(sizeof(buffer), asset->getLength() - i));
|
|
|
|
size_t size = rand.nextRangeU(1, maxSize);
|
|
|
|
SkASSERT(size >= 1);
|
|
|
|
SkASSERT(size <= sizeof(buffer));
|
|
|
|
SkASSERT(size + i <= asset->getLength());
|
2015-12-07 19:37:13 +00:00
|
|
|
if (asset->peek(buffer, size) < size) {
|
2015-05-21 15:13:27 +00:00
|
|
|
ERRORF(rep, "Peek Failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (0 != memcmp(buffer, &expect[i], size)) {
|
|
|
|
ERRORF(rep, "Peek returned wrong bytes!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint8_t value;
|
|
|
|
REPORTER_ASSERT(rep, 1 == asset->read(&value, 1));
|
|
|
|
if (value != expect[i]) {
|
|
|
|
ERRORF(rep, "Read Failed!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(StreamPeek_BlockMemoryStream, rep) {
|
|
|
|
const static int kSeed = 1234;
|
|
|
|
SkRandom valueSource(kSeed);
|
|
|
|
SkRandom rand(kSeed << 1);
|
|
|
|
uint8_t buffer[4096];
|
|
|
|
SkDynamicMemoryWStream dynamicMemoryWStream;
|
2016-12-14 17:34:06 +00:00
|
|
|
size_t totalWritten = 0;
|
2015-05-21 15:13:27 +00:00
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
|
|
// Randomize the length of the blocks.
|
|
|
|
size_t size = rand.nextRangeU(1, sizeof(buffer));
|
|
|
|
for (size_t j = 0; j < size; ++j) {
|
|
|
|
buffer[j] = valueSource.nextU() & 0xFF;
|
|
|
|
}
|
|
|
|
dynamicMemoryWStream.write(buffer, size);
|
2016-12-14 17:34:06 +00:00
|
|
|
totalWritten += size;
|
|
|
|
REPORTER_ASSERT(rep, totalWritten == dynamicMemoryWStream.bytesWritten());
|
2015-05-21 15:13:27 +00:00
|
|
|
}
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> asset(dynamicMemoryWStream.detachAsStream());
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> expected(SkData::MakeUninitialized(asset->getLength()));
|
2015-05-21 15:13:27 +00:00
|
|
|
uint8_t* expectedPtr = static_cast<uint8_t*>(expected->writable_data());
|
|
|
|
valueSource.setSeed(kSeed); // reseed.
|
|
|
|
// We want the exact same same "random" string of numbers to put
|
|
|
|
// in expected. i.e.: don't rely on SkDynamicMemoryStream to work
|
|
|
|
// correctly while we are testing SkDynamicMemoryStream.
|
|
|
|
for (size_t i = 0; i < asset->getLength(); ++i) {
|
|
|
|
expectedPtr[i] = valueSource.nextU() & 0xFF;
|
|
|
|
}
|
2016-11-03 18:40:50 +00:00
|
|
|
stream_peek_test(rep, asset.get(), expected.get());
|
2015-05-21 15:13:27 +00:00
|
|
|
}
|
2015-08-18 20:30:25 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DumbStream : public SkStream {
|
|
|
|
public:
|
|
|
|
DumbStream(const uint8_t* data, size_t n)
|
|
|
|
: fData(data), fCount(n), fIdx(0) {}
|
|
|
|
size_t read(void* buffer, size_t size) override {
|
2015-08-19 14:22:04 +00:00
|
|
|
size_t copyCount = SkTMin(fCount - fIdx, size);
|
|
|
|
if (copyCount) {
|
|
|
|
memcpy(buffer, &fData[fIdx], copyCount);
|
|
|
|
fIdx += copyCount;
|
2015-08-18 20:30:25 +00:00
|
|
|
}
|
2015-08-19 14:22:04 +00:00
|
|
|
return copyCount;
|
2015-08-18 20:30:25 +00:00
|
|
|
}
|
|
|
|
bool isAtEnd() const override {
|
2015-08-19 14:22:04 +00:00
|
|
|
return fCount == fIdx;
|
2015-08-18 20:30:25 +00:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
const uint8_t* fData;
|
|
|
|
size_t fCount, fIdx;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
static void stream_copy_test(skiatest::Reporter* reporter,
|
|
|
|
const void* srcData,
|
|
|
|
size_t N,
|
|
|
|
SkStream* stream) {
|
|
|
|
SkDynamicMemoryWStream tgt;
|
|
|
|
if (!SkStreamCopy(&tgt, stream)) {
|
|
|
|
ERRORF(reporter, "SkStreamCopy failed");
|
|
|
|
return;
|
|
|
|
}
|
2016-09-12 19:01:44 +00:00
|
|
|
sk_sp<SkData> data(tgt.detachAsData());
|
2015-08-18 20:30:25 +00:00
|
|
|
if (data->size() != N) {
|
|
|
|
ERRORF(reporter, "SkStreamCopy incorrect size");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (0 != memcmp(data->data(), srcData, N)) {
|
|
|
|
ERRORF(reporter, "SkStreamCopy bad copy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-14 19:52:12 +00:00
|
|
|
DEF_TEST(DynamicMemoryWStream_detachAsData, r) {
|
|
|
|
const char az[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
const unsigned N = 40000;
|
|
|
|
SkDynamicMemoryWStream dmws;
|
|
|
|
for (unsigned i = 0; i < N; ++i) {
|
|
|
|
dmws.writeText(az);
|
|
|
|
}
|
|
|
|
REPORTER_ASSERT(r, dmws.bytesWritten() == N * strlen(az));
|
|
|
|
auto data = dmws.detachAsData();
|
|
|
|
REPORTER_ASSERT(r, data->size() == N * strlen(az));
|
|
|
|
const uint8_t* ptr = data->bytes();
|
|
|
|
for (unsigned i = 0; i < N; ++i) {
|
|
|
|
if (0 != memcmp(ptr, az, strlen(az))) {
|
|
|
|
ERRORF(r, "detachAsData() memcmp failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr += strlen(az);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 20:30:25 +00:00
|
|
|
DEF_TEST(StreamCopy, reporter) {
|
|
|
|
SkRandom random(123456);
|
2015-08-19 13:12:40 +00:00
|
|
|
static const int N = 10000;
|
|
|
|
SkAutoTMalloc<uint8_t> src((size_t)N);
|
|
|
|
for (int j = 0; j < N; ++j) {
|
2015-08-18 20:30:25 +00:00
|
|
|
src[j] = random.nextU() & 0xff;
|
|
|
|
}
|
|
|
|
// SkStreamCopy had two code paths; this test both.
|
2015-08-19 13:12:40 +00:00
|
|
|
DumbStream dumbStream(src.get(), (size_t)N);
|
2015-08-18 20:30:25 +00:00
|
|
|
stream_copy_test(reporter, src, N, &dumbStream);
|
2015-08-19 13:12:40 +00:00
|
|
|
SkMemoryStream smartStream(src.get(), (size_t)N);
|
2015-08-18 20:30:25 +00:00
|
|
|
stream_copy_test(reporter, src, N, &smartStream);
|
2015-09-28 14:29:39 +00:00
|
|
|
}
|
2015-08-18 20:30:25 +00:00
|
|
|
|
2015-09-28 14:29:39 +00:00
|
|
|
DEF_TEST(StreamEmptyStreamMemoryBase, r) {
|
|
|
|
SkDynamicMemoryWStream tmp;
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkStreamAsset> asset(tmp.detachAsStream());
|
2015-09-28 14:29:39 +00:00
|
|
|
REPORTER_ASSERT(r, nullptr == asset->getMemoryBase());
|
2015-08-18 20:30:25 +00:00
|
|
|
}
|
2017-01-08 19:35:29 +00:00
|
|
|
|
|
|
|
#include "SkBuffer.h"
|
|
|
|
|
|
|
|
DEF_TEST(RBuffer, reporter) {
|
|
|
|
int32_t value = 0;
|
|
|
|
SkRBuffer buffer(&value, 4);
|
|
|
|
REPORTER_ASSERT(reporter, buffer.isValid());
|
|
|
|
|
|
|
|
int32_t tmp;
|
|
|
|
REPORTER_ASSERT(reporter, buffer.read(&tmp, 4));
|
|
|
|
REPORTER_ASSERT(reporter, buffer.isValid());
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, !buffer.read(&tmp, 4));
|
|
|
|
REPORTER_ASSERT(reporter, !buffer.isValid());
|
|
|
|
}
|