blob: b5e3cd98b8d7ccd8e8a8a1f7fef9104a4e1fc172 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com5e5adfd2009-03-07 03:39:23 +00008#include "Test.h"
9#include "SkRandom.h"
10#include "SkStream.h"
reed@google.com8d0b5772011-06-24 13:07:31 +000011#include "SkData.h"
reed@android.com5e5adfd2009-03-07 03:39:23 +000012
13#define MAX_SIZE (256 * 1024)
14
15static 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
23static 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.com80e39a72009-04-02 16:59:40 +000040
reed@android.com5e5adfd2009-03-07 03:39:23 +000041 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.com80e39a72009-04-02 16:59:40 +000047 REPORTER_ASSERT(reporter,
48 memcmp(storage + bytesRead, storage2, ss) == 0);
reed@android.com5e5adfd2009-03-07 03:39:23 +000049 bytesRead += ss;
50 }
51 REPORTER_ASSERT(reporter, bytesRead == size);
52 }
53 }
54}
55
56static void TestRStream(skiatest::Reporter* reporter) {
reed@android.com80e39a72009-04-02 16:59:40 +000057 static const char s[] =
58 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
reed@android.com5e5adfd2009-03-07 03:39:23 +000059 char copy[sizeof(s)];
60 SkRandom rand;
reed@android.com80e39a72009-04-02 16:59:40 +000061
62 for (int i = 0; i < 65; i++) {
reed@android.com5e5adfd2009-03-07 03:39:23 +000063 char* copyPtr = copy;
64 SkMemoryStream mem(s, sizeof(s));
65 SkBufferStream buff(&mem, i);
reed@android.com80e39a72009-04-02 16:59:40 +000066
reed@android.com5e5adfd2009-03-07 03:39:23 +000067 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.com80e39a72009-04-02 16:59:40 +000076static 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.com5e5adfd2009-03-07 03:39:23 +000082 }
reed@android.com80e39a72009-04-02 16:59:40 +000083 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.com5e5adfd2009-03-07 03:39:23 +000091 }
reed@google.com70442a62011-06-23 21:48:04 +000092
93 {
reed@google.com8d0b5772011-06-24 13:07:31 +000094 SkData* data = ds.copyToData();
reed@google.com70442a62011-06-23 21:48:04 +000095 REPORTER_ASSERT(reporter, 100 * 26 == data->size());
96 REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0);
97 data->unref();
98 }
reed@android.com80e39a72009-04-02 16:59:40 +000099 delete[] dst;
reed@android.com5e5adfd2009-03-07 03:39:23 +0000100}
101
reed@google.com19f286b2011-10-18 11:49:52 +0000102static 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.comd6176b02012-08-23 18:14:13 +0000111
112
reed@google.com19f286b2011-10-18 11:49:52 +0000113 size_t i;
114 char buffer[sizeof(sizes) * 4];
rmistry@google.comd6176b02012-08-23 18:14:13 +0000115
reed@google.com19f286b2011-10-18 11:49:52 +0000116 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.comd6176b02012-08-23 18:14:13 +0000122
reed@google.com19f286b2011-10-18 11:49:52 +0000123 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
reed@android.com5e5adfd2009-03-07 03:39:23 +0000133static void TestStreams(skiatest::Reporter* reporter) {
134 TestRStream(reporter);
135 TestWStream(reporter);
reed@google.com19f286b2011-10-18 11:49:52 +0000136 TestPackedUInt(reporter);
reed@android.com5e5adfd2009-03-07 03:39:23 +0000137}
138
139#include "TestClassDef.h"
140DEFINE_TESTCLASS("Stream", StreamTestClass, TestStreams)