Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1240)

Side by Side Diff: tests/Writer32Test.cpp

Issue 130913018: Templetized SkWriter32 readTAt() & overwriteTAt() (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Rebased Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/core/SkWriter32.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkRandom.h" 8 #include "SkRandom.h"
9 #include "SkReader32.h" 9 #include "SkReader32.h"
10 #include "SkWriter32.h" 10 #include "SkWriter32.h"
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 REPORTER_ASSERT(reporter, reader.readInt() == 0x33); 95 REPORTER_ASSERT(reporter, reader.readInt() == 0x33);
96 REPORTER_ASSERT(reporter, reader.readPtr() == p1); 96 REPORTER_ASSERT(reporter, reader.readPtr() == p1);
97 REPORTER_ASSERT(reporter, reader.readInt() == 0x66); 97 REPORTER_ASSERT(reporter, reader.readInt() == 0x66);
98 } 98 }
99 99
100 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) { 100 static void test1(skiatest::Reporter* reporter, SkWriter32* writer) {
101 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 101 const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
102 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { 102 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
103 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten()); 103 REPORTER_ASSERT(reporter, i*4 == writer->bytesWritten());
104 writer->write32(data[i]); 104 writer->write32(data[i]);
105 REPORTER_ASSERT(reporter, data[i] == writer->read32At(i*4)); 105 REPORTER_ASSERT(reporter, data[i] == writer->readTAt<uint32_t>(i * 4));
106 } 106 }
107 107
108 char buffer[sizeof(data)]; 108 char buffer[sizeof(data)];
109 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten()); 109 REPORTER_ASSERT(reporter, sizeof(buffer) == writer->bytesWritten());
110 writer->flatten(buffer); 110 writer->flatten(buffer);
111 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer))); 111 REPORTER_ASSERT(reporter, !memcmp(data, buffer, sizeof(buffer)));
112 } 112 }
113 113
114 static void test2(skiatest::Reporter* reporter, SkWriter32* writer) { 114 static void test2(skiatest::Reporter* reporter, SkWriter32* writer) {
115 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz"; 115 static const char gStr[] = "abcdefghimjklmnopqrstuvwxyz";
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0) ; 177 REPORTER_ASSERT(reporter, memcmp(readPtr, originalData.get(), len) == 0) ;
178 // Ensure that the rest is padded with zeroes. 178 // Ensure that the rest is padded with zeroes.
179 const char* stop = readPtr + SkAlign4(len); 179 const char* stop = readPtr + SkAlign4(len);
180 readPtr += len; 180 readPtr += len;
181 while (readPtr < stop) { 181 while (readPtr < stop) {
182 REPORTER_ASSERT(reporter, *readPtr++ == 0); 182 REPORTER_ASSERT(reporter, *readPtr++ == 0);
183 } 183 }
184 } 184 }
185 } 185 }
186 186
187 static void testOverwriteT(skiatest::Reporter* reporter, SkWriter32* writer) {
188 const size_t padding = 64;
189
190 const uint32_t uint1 = 0x12345678;
191 const uint32_t uint2 = 0x98765432;
192 const SkScalar scalar1 = 1234.5678f;
193 const SkScalar scalar2 = 9876.5432f;
194 const SkRect rect1 = SkRect::MakeXYWH(1, 2, 3, 4);
195 const SkRect rect2 = SkRect::MakeXYWH(5, 6, 7, 8);
196
197 for (size_t i = 0; i < (padding / 4); ++i) {
198 writer->write32(0);
199 }
200
201 writer->write32(uint1);
202 writer->writeRect(rect1);
203 writer->writeScalar(scalar1);
204
205 for (size_t i = 0; i < (padding / 4); ++i) {
206 writer->write32(0);
207 }
208
209 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint1);
210 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t) ) == rect1);
211 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
212 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar1) ;
213
214 writer->overwriteTAt(padding, uint2);
215 writer->overwriteTAt(padding + sizeof(uint32_t), rect2);
216 writer->overwriteTAt(padding + sizeof(uint32_t) + sizeof(SkRect), scalar2);
217
218 REPORTER_ASSERT(reporter, writer->readTAt<uint32_t>(padding) == uint2);
219 REPORTER_ASSERT(reporter, writer->readTAt<SkRect>(padding + sizeof(uint32_t) ) == rect2);
220 REPORTER_ASSERT(reporter, writer->readTAt<SkScalar>(
221 padding + sizeof(uint32_t) + sizeof(SkRect)) == scalar2) ;
222 }
223
187 DEF_TEST(Writer32_dynamic, reporter) { 224 DEF_TEST(Writer32_dynamic, reporter) {
188 SkWriter32 writer; 225 SkWriter32 writer;
189 test1(reporter, &writer); 226 test1(reporter, &writer);
190 227
191 writer.reset(); 228 writer.reset();
192 test2(reporter, &writer); 229 test2(reporter, &writer);
193 230
194 writer.reset(); 231 writer.reset();
195 testWritePad(reporter, &writer); 232 testWritePad(reporter, &writer);
233
234 writer.reset();
235 testOverwriteT(reporter, &writer);
196 } 236 }
197 237
198 DEF_TEST(Writer32_contiguous, reporter) { 238 DEF_TEST(Writer32_contiguous, reporter) {
199 uint32_t storage[256]; 239 uint32_t storage[256];
200 SkWriter32 writer; 240 SkWriter32 writer;
201 writer.reset(storage, sizeof(storage)); 241 writer.reset(storage, sizeof(storage));
202 // This write is small enough to fit in storage, so it's contiguous. 242 // This write is small enough to fit in storage, so it's contiguous.
203 test1(reporter, &writer); 243 test1(reporter, &writer);
204 REPORTER_ASSERT(reporter, writer.contiguousArray() != NULL); 244 REPORTER_ASSERT(reporter, writer.contiguousArray() != NULL);
205 245
206 // Everything other aspect of contiguous/non-contiguous is an 246 // Everything other aspect of contiguous/non-contiguous is an
207 // implementation detail, not part of the public contract for 247 // implementation detail, not part of the public contract for
208 // SkWriter32, and so not tested here. 248 // SkWriter32, and so not tested here.
209 } 249 }
210 250
211 DEF_TEST(Writer32_small, reporter) { 251 DEF_TEST(Writer32_small, reporter) {
212 SkSWriter32<8 * sizeof(intptr_t)> writer; 252 SkSWriter32<8 * sizeof(intptr_t)> writer;
213 test1(reporter, &writer); 253 test1(reporter, &writer);
214 writer.reset(); // should just rewind our storage 254 writer.reset(); // should just rewind our storage
215 test2(reporter, &writer); 255 test2(reporter, &writer);
216 256
217 writer.reset(); 257 writer.reset();
218 testWritePad(reporter, &writer); 258 testWritePad(reporter, &writer);
259
260 writer.reset();
261 testOverwriteT(reporter, &writer);
219 } 262 }
220 263
221 DEF_TEST(Writer32_large, reporter) { 264 DEF_TEST(Writer32_large, reporter) {
222 SkSWriter32<1024 * sizeof(intptr_t)> writer; 265 SkSWriter32<1024 * sizeof(intptr_t)> writer;
223 test1(reporter, &writer); 266 test1(reporter, &writer);
224 writer.reset(); // should just rewind our storage 267 writer.reset(); // should just rewind our storage
225 test2(reporter, &writer); 268 test2(reporter, &writer);
226 269
227 writer.reset(); 270 writer.reset();
228 testWritePad(reporter, &writer); 271 testWritePad(reporter, &writer);
272
273 writer.reset();
274 testOverwriteT(reporter, &writer);
229 } 275 }
230 276
231 DEF_TEST(Writer32_misc, reporter) { 277 DEF_TEST(Writer32_misc, reporter) {
232 test_reserve(reporter); 278 test_reserve(reporter);
233 test_string_null(reporter); 279 test_string_null(reporter);
234 test_ptr(reporter); 280 test_ptr(reporter);
235 test_rewind(reporter); 281 test_rewind(reporter);
236 } 282 }
OLDNEW
« no previous file with comments | « src/core/SkWriter32.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698