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@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | #include "SkRandom.h" |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 10 | #include "SkOSFile.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 11 | #include "SkStream.h" |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 12 | #include "SkData.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 13 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 14 | #ifndef SK_BUILD_FOR_WIN |
| 15 | #include <unistd.h> |
| 16 | #include <fcntl.h> |
| 17 | #endif |
| 18 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 19 | #define MAX_SIZE (256 * 1024) |
| 20 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 21 | static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, |
| 22 | const void* src, size_t len, int repeat) { |
| 23 | SkAutoSMalloc<256> storage(len); |
| 24 | void* tmp = storage.get(); |
| 25 | |
| 26 | for (int i = 0; i < repeat; ++i) { |
| 27 | size_t bytes = stream->read(tmp, len); |
| 28 | REPORTER_ASSERT(reporter, bytes == len); |
| 29 | REPORTER_ASSERT(reporter, !memcmp(tmp, src, len)); |
| 30 | } |
| 31 | |
| 32 | // expect EOF |
| 33 | size_t bytes = stream->read(tmp, 1); |
| 34 | REPORTER_ASSERT(reporter, 0 == bytes); |
| 35 | } |
| 36 | |
| 37 | static void test_filestreams(skiatest::Reporter* reporter, const char* tmpDir) { |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 38 | SkString path = SkOSPath::SkPathJoin(tmpDir, "wstream_test"); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 39 | |
| 40 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 41 | |
| 42 | { |
| 43 | SkFILEWStream writer(path.c_str()); |
| 44 | if (!writer.isValid()) { |
| 45 | SkString msg; |
| 46 | msg.printf("Failed to create tmp file %s\n", path.c_str()); |
| 47 | reporter->reportFailed(msg.c_str()); |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | for (int i = 0; i < 100; ++i) { |
| 52 | writer.write(s, 26); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | { |
| 57 | SkFILEStream stream(path.c_str()); |
| 58 | REPORTER_ASSERT(reporter, stream.isValid()); |
| 59 | test_loop_stream(reporter, &stream, s, 26, 100); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 60 | |
| 61 | SkAutoTUnref<SkStreamAsset> stream2(stream.duplicate()); |
| 62 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 63 | } |
| 64 | |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 65 | { |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 66 | FILE* file = ::fopen(path.c_str(), "rb"); |
| 67 | SkFILEStream stream(file, SkFILEStream::kCallerPasses_Ownership); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 68 | REPORTER_ASSERT(reporter, stream.isValid()); |
| 69 | test_loop_stream(reporter, &stream, s, 26, 100); |
bungeman@google.com | 6cab1a4 | 2013-05-29 13:43:31 +0000 | [diff] [blame] | 70 | |
| 71 | SkAutoTUnref<SkStreamAsset> stream2(stream.duplicate()); |
| 72 | test_loop_stream(reporter, stream2.get(), s, 26, 100); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 73 | } |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 76 | static void TestWStream(skiatest::Reporter* reporter) { |
| 77 | SkDynamicMemoryWStream ds; |
| 78 | const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
| 79 | int i; |
| 80 | for (i = 0; i < 100; i++) { |
| 81 | REPORTER_ASSERT(reporter, ds.write(s, 26)); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 82 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 83 | REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); |
| 84 | char* dst = new char[100 * 26 + 1]; |
| 85 | dst[100*26] = '*'; |
| 86 | ds.copyTo(dst); |
| 87 | REPORTER_ASSERT(reporter, dst[100*26] == '*'); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 88 | for (i = 0; i < 100; i++) { |
| 89 | REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 90 | } |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 91 | |
| 92 | { |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 93 | SkData* data = ds.copyToData(); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 94 | REPORTER_ASSERT(reporter, 100 * 26 == data->size()); |
| 95 | REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); |
| 96 | data->unref(); |
| 97 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 98 | delete[] dst; |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 99 | |
scroggo@google.com | c76218d | 2013-06-06 14:59:56 +0000 | [diff] [blame] | 100 | SkString tmpDir = skiatest::Test::GetTmpDir(); |
| 101 | if (!tmpDir.isEmpty()) { |
| 102 | test_filestreams(reporter, tmpDir.c_str()); |
reed@google.com | 789c6f2 | 2013-02-25 20:24:24 +0000 | [diff] [blame] | 103 | } |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 104 | } |
| 105 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 106 | static void TestPackedUInt(skiatest::Reporter* reporter) { |
| 107 | // we know that packeduint tries to write 1, 2 or 4 bytes for the length, |
| 108 | // so we test values around each of those transitions (and a few others) |
| 109 | const size_t sizes[] = { |
| 110 | 0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769, |
| 111 | 0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001, |
| 112 | 0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001, |
| 113 | 0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF |
| 114 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 | |
| 116 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 117 | size_t i; |
| 118 | char buffer[sizeof(sizes) * 4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 119 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 120 | SkMemoryWStream wstream(buffer, sizeof(buffer)); |
| 121 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 122 | bool success = wstream.writePackedUInt(sizes[i]); |
| 123 | REPORTER_ASSERT(reporter, success); |
| 124 | } |
| 125 | wstream.flush(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 126 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 127 | SkMemoryStream rstream(buffer, sizeof(buffer)); |
| 128 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 129 | size_t n = rstream.readPackedUInt(); |
| 130 | if (sizes[i] != n) { |
| 131 | SkDebugf("-- %d: sizes:%x n:%x\n", i, sizes[i], n); |
| 132 | } |
| 133 | REPORTER_ASSERT(reporter, sizes[i] == n); |
| 134 | } |
| 135 | } |
| 136 | |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 137 | // Test that setting an SkMemoryStream to a NULL data does not result in a crash when calling |
| 138 | // methods that access fData. |
| 139 | static void TestDereferencingData(SkMemoryStream* memStream) { |
| 140 | memStream->read(NULL, 0); |
| 141 | memStream->getMemoryBase(); |
| 142 | SkAutoDataUnref data(memStream->copyToData()); |
| 143 | } |
| 144 | |
| 145 | static void TestNullData() { |
| 146 | SkData* nullData = NULL; |
| 147 | SkMemoryStream memStream(nullData); |
| 148 | TestDereferencingData(&memStream); |
| 149 | |
| 150 | memStream.setData(nullData); |
| 151 | TestDereferencingData(&memStream); |
| 152 | |
| 153 | } |
| 154 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 155 | static void TestStreams(skiatest::Reporter* reporter) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 156 | TestWStream(reporter); |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 157 | TestPackedUInt(reporter); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 158 | TestNullData(); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | #include "TestClassDef.h" |
| 162 | DEFINE_TESTCLASS("Stream", StreamTestClass, TestStreams) |