OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "Test.h" | 8 #include "Test.h" |
9 #include "SkRandom.h" | 9 #include "SkRandom.h" |
| 10 #include "SkOSFile.h" |
10 #include "SkStream.h" | 11 #include "SkStream.h" |
11 #include "SkData.h" | 12 #include "SkData.h" |
12 | 13 |
13 #ifndef SK_BUILD_FOR_WIN | 14 #ifndef SK_BUILD_FOR_WIN |
14 #include <unistd.h> | 15 #include <unistd.h> |
15 #include <fcntl.h> | 16 #include <fcntl.h> |
16 #endif | 17 #endif |
17 | 18 |
18 #define MAX_SIZE (256 * 1024) | 19 #define MAX_SIZE (256 * 1024) |
19 | 20 |
20 static void random_fill(SkMWCRandom& rand, void* buffer, size_t size) { | |
21 char* p = (char*)buffer; | |
22 char* stop = p + size; | |
23 while (p < stop) { | |
24 *p++ = (char)(rand.nextU() >> 8); | |
25 } | |
26 } | |
27 | |
28 static void test_buffer(skiatest::Reporter* reporter) { | |
29 SkMWCRandom rand; | |
30 SkAutoMalloc am(MAX_SIZE * 2); | |
31 char* storage = (char*)am.get(); | |
32 char* storage2 = storage + MAX_SIZE; | |
33 | |
34 random_fill(rand, storage, MAX_SIZE); | |
35 | |
36 for (int sizeTimes = 0; sizeTimes < 100; sizeTimes++) { | |
37 int size = rand.nextU() % MAX_SIZE; | |
38 if (size == 0) { | |
39 size = MAX_SIZE; | |
40 } | |
41 for (int times = 0; times < 100; times++) { | |
42 int bufferSize = 1 + (rand.nextU() & 0xFFFF); | |
43 SkMemoryStream mstream(storage, size); | |
44 SkBufferStream bstream(&mstream, bufferSize); | |
45 | |
46 int bytesRead = 0; | |
47 while (bytesRead < size) { | |
48 int s = 17 + (rand.nextU() & 0xFFFF); | |
49 int ss = bstream.read(storage2, s); | |
50 REPORTER_ASSERT(reporter, ss > 0 && ss <= s); | |
51 REPORTER_ASSERT(reporter, bytesRead + ss <= size); | |
52 REPORTER_ASSERT(reporter, | |
53 memcmp(storage + bytesRead, storage2, ss) == 0); | |
54 bytesRead += ss; | |
55 } | |
56 REPORTER_ASSERT(reporter, bytesRead == size); | |
57 } | |
58 } | |
59 } | |
60 | |
61 static void TestRStream(skiatest::Reporter* reporter) { | |
62 static const char s[] = | |
63 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
64 char copy[sizeof(s)]; | |
65 SkMWCRandom rand; | |
66 | |
67 for (int i = 0; i < 65; i++) { | |
68 char* copyPtr = copy; | |
69 SkMemoryStream mem(s, sizeof(s)); | |
70 SkBufferStream buff(&mem, i); | |
71 | |
72 do { | |
73 copyPtr += buff.read(copyPtr, rand.nextU() & 15); | |
74 } while (copyPtr < copy + sizeof(s)); | |
75 REPORTER_ASSERT(reporter, copyPtr == copy + sizeof(s)); | |
76 REPORTER_ASSERT(reporter, memcmp(s, copy, sizeof(s)) == 0); | |
77 } | |
78 test_buffer(reporter); | |
79 } | |
80 | |
81 static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, | 21 static void test_loop_stream(skiatest::Reporter* reporter, SkStream* stream, |
82 const void* src, size_t len, int repeat) { | 22 const void* src, size_t len, int repeat) { |
83 SkAutoSMalloc<256> storage(len); | 23 SkAutoSMalloc<256> storage(len); |
84 void* tmp = storage.get(); | 24 void* tmp = storage.get(); |
85 | 25 |
86 for (int i = 0; i < repeat; ++i) { | 26 for (int i = 0; i < repeat; ++i) { |
87 size_t bytes = stream->read(tmp, len); | 27 size_t bytes = stream->read(tmp, len); |
88 REPORTER_ASSERT(reporter, bytes == len); | 28 REPORTER_ASSERT(reporter, bytes == len); |
89 REPORTER_ASSERT(reporter, !memcmp(tmp, src, len)); | 29 REPORTER_ASSERT(reporter, !memcmp(tmp, src, len)); |
90 } | 30 } |
(...skipping 20 matching lines...) Expand all Loading... |
111 | 51 |
112 for (int i = 0; i < 100; ++i) { | 52 for (int i = 0; i < 100; ++i) { |
113 writer.write(s, 26); | 53 writer.write(s, 26); |
114 } | 54 } |
115 } | 55 } |
116 | 56 |
117 { | 57 { |
118 SkFILEStream stream(path.c_str()); | 58 SkFILEStream stream(path.c_str()); |
119 REPORTER_ASSERT(reporter, stream.isValid()); | 59 REPORTER_ASSERT(reporter, stream.isValid()); |
120 test_loop_stream(reporter, &stream, s, 26, 100); | 60 test_loop_stream(reporter, &stream, s, 26, 100); |
| 61 |
| 62 SkAutoTUnref<SkStreamAsset> stream2(stream.duplicate()); |
| 63 test_loop_stream(reporter, stream2.get(), s, 26, 100); |
121 } | 64 } |
122 | 65 |
123 #ifndef SK_BUILD_FOR_WIN | |
124 { | 66 { |
125 int fd = ::open(path.c_str(), O_RDONLY); | 67 FILE* file = ::fopen(path.c_str(), "rb"); |
126 SkFDStream stream(fd, true); | 68 SkFILEStream stream(file, SkFILEStream::kCallerPasses_Ownership); |
127 REPORTER_ASSERT(reporter, stream.isValid()); | 69 REPORTER_ASSERT(reporter, stream.isValid()); |
128 test_loop_stream(reporter, &stream, s, 26, 100); | 70 test_loop_stream(reporter, &stream, s, 26, 100); |
| 71 |
| 72 SkAutoTUnref<SkStreamAsset> stream2(stream.duplicate()); |
| 73 test_loop_stream(reporter, stream2.get(), s, 26, 100); |
129 } | 74 } |
130 #endif | |
131 } | 75 } |
132 | 76 |
133 static void TestWStream(skiatest::Reporter* reporter) { | 77 static void TestWStream(skiatest::Reporter* reporter) { |
134 SkDynamicMemoryWStream ds; | 78 SkDynamicMemoryWStream ds; |
135 const char s[] = "abcdefghijklmnopqrstuvwxyz"; | 79 const char s[] = "abcdefghijklmnopqrstuvwxyz"; |
136 int i; | 80 int i; |
137 for (i = 0; i < 100; i++) { | 81 for (i = 0; i < 100; i++) { |
138 REPORTER_ASSERT(reporter, ds.write(s, 26)); | 82 REPORTER_ASSERT(reporter, ds.write(s, 26)); |
139 } | 83 } |
140 REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); | 84 REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26); |
141 char* dst = new char[100 * 26 + 1]; | 85 char* dst = new char[100 * 26 + 1]; |
142 dst[100*26] = '*'; | 86 dst[100*26] = '*'; |
143 ds.copyTo(dst); | 87 ds.copyTo(dst); |
144 REPORTER_ASSERT(reporter, dst[100*26] == '*'); | 88 REPORTER_ASSERT(reporter, dst[100*26] == '*'); |
145 // char* p = dst; | |
146 for (i = 0; i < 100; i++) { | 89 for (i = 0; i < 100; i++) { |
147 REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0); | 90 REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0); |
148 } | 91 } |
149 | 92 |
150 { | 93 { |
151 SkData* data = ds.copyToData(); | 94 SkData* data = ds.copyToData(); |
152 REPORTER_ASSERT(reporter, 100 * 26 == data->size()); | 95 REPORTER_ASSERT(reporter, 100 * 26 == data->size()); |
153 REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); | 96 REPORTER_ASSERT(reporter, memcmp(dst, data->data(), data->size()) == 0); |
154 data->unref(); | 97 data->unref(); |
155 } | 98 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 SkData* nullData = NULL; | 146 SkData* nullData = NULL; |
204 SkMemoryStream memStream(nullData); | 147 SkMemoryStream memStream(nullData); |
205 TestDereferencingData(&memStream); | 148 TestDereferencingData(&memStream); |
206 | 149 |
207 memStream.setData(nullData); | 150 memStream.setData(nullData); |
208 TestDereferencingData(&memStream); | 151 TestDereferencingData(&memStream); |
209 | 152 |
210 } | 153 } |
211 | 154 |
212 static void TestStreams(skiatest::Reporter* reporter) { | 155 static void TestStreams(skiatest::Reporter* reporter) { |
213 TestRStream(reporter); | |
214 TestWStream(reporter); | 156 TestWStream(reporter); |
215 TestPackedUInt(reporter); | 157 TestPackedUInt(reporter); |
216 TestNullData(); | 158 TestNullData(); |
217 } | 159 } |
218 | 160 |
219 #include "TestClassDef.h" | 161 #include "TestClassDef.h" |
220 DEFINE_TESTCLASS("Stream", StreamTestClass, TestStreams) | 162 DEFINE_TESTCLASS("Stream", StreamTestClass, TestStreams) |
OLD | NEW |