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" |
| 10 | #include "SkStream.h" |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 11 | #include "SkData.h" |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 12 | |
| 13 | #define MAX_SIZE (256 * 1024) |
| 14 | |
| 15 | static void random_fill(SkRandom& rand, void* buffer, size_t size) { |
| 16 | char* p = (char*)buffer; |
| 17 | char* stop = p + size; |
| 18 | while (p < stop) { |
| 19 | *p++ = (char)(rand.nextU() >> 8); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | static void test_buffer(skiatest::Reporter* reporter) { |
| 24 | SkRandom rand; |
| 25 | SkAutoMalloc am(MAX_SIZE * 2); |
| 26 | char* storage = (char*)am.get(); |
| 27 | char* storage2 = storage + MAX_SIZE; |
| 28 | |
| 29 | random_fill(rand, storage, MAX_SIZE); |
| 30 | |
| 31 | for (int sizeTimes = 0; sizeTimes < 100; sizeTimes++) { |
| 32 | int size = rand.nextU() % MAX_SIZE; |
| 33 | if (size == 0) { |
| 34 | size = MAX_SIZE; |
| 35 | } |
| 36 | for (int times = 0; times < 100; times++) { |
| 37 | int bufferSize = 1 + (rand.nextU() & 0xFFFF); |
| 38 | SkMemoryStream mstream(storage, size); |
| 39 | SkBufferStream bstream(&mstream, bufferSize); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 40 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 41 | int bytesRead = 0; |
| 42 | while (bytesRead < size) { |
| 43 | int s = 17 + (rand.nextU() & 0xFFFF); |
| 44 | int ss = bstream.read(storage2, s); |
| 45 | REPORTER_ASSERT(reporter, ss > 0 && ss <= s); |
| 46 | REPORTER_ASSERT(reporter, bytesRead + ss <= size); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 47 | REPORTER_ASSERT(reporter, |
| 48 | memcmp(storage + bytesRead, storage2, ss) == 0); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 49 | bytesRead += ss; |
| 50 | } |
| 51 | REPORTER_ASSERT(reporter, bytesRead == size); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | static void TestRStream(skiatest::Reporter* reporter) { |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 57 | static const char s[] = |
| 58 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 59 | char copy[sizeof(s)]; |
| 60 | SkRandom rand; |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 61 | |
| 62 | for (int i = 0; i < 65; i++) { |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 63 | char* copyPtr = copy; |
| 64 | SkMemoryStream mem(s, sizeof(s)); |
| 65 | SkBufferStream buff(&mem, i); |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 66 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 67 | do { |
| 68 | copyPtr += buff.read(copyPtr, rand.nextU() & 15); |
| 69 | } while (copyPtr < copy + sizeof(s)); |
| 70 | REPORTER_ASSERT(reporter, copyPtr == copy + sizeof(s)); |
| 71 | REPORTER_ASSERT(reporter, memcmp(s, copy, sizeof(s)) == 0); |
| 72 | } |
| 73 | test_buffer(reporter); |
| 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] == '*'); |
| 88 | // char* p = dst; |
| 89 | for (i = 0; i < 100; i++) { |
| 90 | REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 91 | } |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 92 | |
| 93 | { |
reed@google.com | 8d0b577 | 2011-06-24 13:07:31 +0000 | [diff] [blame] | 94 | SkData* data = ds.copyToData(); |
reed@google.com | 70442a6 | 2011-06-23 21:48:04 +0000 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, 100 * 26 == data->size()); |
| 96 | REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); |
| 97 | data->unref(); |
| 98 | } |
reed@android.com | 80e39a7 | 2009-04-02 16:59:40 +0000 | [diff] [blame] | 99 | delete[] dst; |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 100 | } |
| 101 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 102 | static void TestPackedUInt(skiatest::Reporter* reporter) { |
| 103 | // we know that packeduint tries to write 1, 2 or 4 bytes for the length, |
| 104 | // so we test values around each of those transitions (and a few others) |
| 105 | const size_t sizes[] = { |
| 106 | 0, 1, 2, 0xFC, 0xFD, 0xFE, 0xFF, 0x100, 0x101, 32767, 32768, 32769, |
| 107 | 0xFFFD, 0xFFFE, 0xFFFF, 0x10000, 0x10001, |
| 108 | 0xFFFFFD, 0xFFFFFE, 0xFFFFFF, 0x1000000, 0x1000001, |
| 109 | 0x7FFFFFFE, 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFE, 0xFFFFFFFF |
| 110 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | |
| 112 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 113 | size_t i; |
| 114 | char buffer[sizeof(sizes) * 4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 116 | SkMemoryWStream wstream(buffer, sizeof(buffer)); |
| 117 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 118 | bool success = wstream.writePackedUInt(sizes[i]); |
| 119 | REPORTER_ASSERT(reporter, success); |
| 120 | } |
| 121 | wstream.flush(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 122 | |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 123 | SkMemoryStream rstream(buffer, sizeof(buffer)); |
| 124 | for (i = 0; i < SK_ARRAY_COUNT(sizes); ++i) { |
| 125 | size_t n = rstream.readPackedUInt(); |
| 126 | if (sizes[i] != n) { |
| 127 | SkDebugf("-- %d: sizes:%x n:%x\n", i, sizes[i], n); |
| 128 | } |
| 129 | REPORTER_ASSERT(reporter, sizes[i] == n); |
| 130 | } |
| 131 | } |
| 132 | |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 133 | // Test that setting an SkMemoryStream to a NULL data does not result in a crash when calling |
| 134 | // methods that access fData. |
| 135 | static void TestDereferencingData(SkMemoryStream* memStream) { |
| 136 | memStream->read(NULL, 0); |
| 137 | memStream->getMemoryBase(); |
| 138 | SkAutoDataUnref data(memStream->copyToData()); |
| 139 | } |
| 140 | |
| 141 | static void TestNullData() { |
| 142 | SkData* nullData = NULL; |
| 143 | SkMemoryStream memStream(nullData); |
| 144 | TestDereferencingData(&memStream); |
| 145 | |
| 146 | memStream.setData(nullData); |
| 147 | TestDereferencingData(&memStream); |
| 148 | |
| 149 | } |
| 150 | |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 151 | static void TestStreams(skiatest::Reporter* reporter) { |
| 152 | TestRStream(reporter); |
| 153 | TestWStream(reporter); |
reed@google.com | 19f286b | 2011-10-18 11:49:52 +0000 | [diff] [blame] | 154 | TestPackedUInt(reporter); |
scroggo@google.com | e490420 | 2013-01-09 22:02:58 +0000 | [diff] [blame] | 155 | TestNullData(); |
reed@android.com | 5e5adfd | 2009-03-07 03:39:23 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | #include "TestClassDef.h" |
| 159 | DEFINE_TESTCLASS("Stream", StreamTestClass, TestStreams) |