OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkOrderedWriteBuffer.h" | 8 #include "SkOrderedWriteBuffer.h" |
9 #include "SkValidatingReadBuffer.h" | 9 #include "SkValidatingReadBuffer.h" |
10 #include "Test.h" | 10 #include "Test.h" |
11 | 11 |
12 static void Tests(skiatest::Reporter* reporter) { | 12 static void Tests(skiatest::Reporter* reporter) { |
13 // Test matrix serialization | |
reed1
2013/11/04 14:26:54
Looks like a lot of similar code. Can any of this
sugoi1
2013/11/04 15:26:14
Done.
| |
14 { | |
15 SkMatrix matrix = SkMatrix::I(); | |
16 SkOrderedWriteBuffer writer(1024); | |
17 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
18 writer.writeMatrix(matrix); | |
19 uint32_t bytesWritten = writer.bytesWritten(); | |
20 // This should write the length (in 4 bytes) and the array | |
21 REPORTER_ASSERT(reporter, (9 * sizeof(SkScalar)) == bytesWritten); | |
22 | |
23 unsigned char dataWritten[1024]; | |
24 writer.writeToMemory(dataWritten); | |
25 | |
26 // Make sure this fails when it should | |
27 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1); | |
28 const unsigned char* peekBefore = static_cast<const unsigned char*>(buff er.skip(0)); | |
29 buffer.readMatrix(&matrix); | |
30 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffe r.skip(0)); | |
31 // This should have failed, since the buffer is too small to read a matr ix from it | |
32 REPORTER_ASSERT(reporter, peekBefore == peekAfter); | |
33 | |
34 // Make sure this succeeds when it should | |
35 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
36 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); | |
37 buffer2.readMatrix(&matrix); | |
38 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); | |
39 // This should have succeeded, since there are enough bytes to read this | |
40 REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten); | |
41 | |
42 // Test memory read/write functions directly | |
43 size_t bytesWrittenToMemory = matrix.writeToMemory(dataWritten); | |
44 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWritten ToMemory); | |
45 size_t bytesReadFromMemory = matrix.readFromMemory(dataWritten, bytesWri ttenToMemory); | |
46 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFrom Memory); | |
47 } | |
48 | |
49 // Test path serialization | |
50 { | |
51 SkPath path; | |
52 SkOrderedWriteBuffer writer(1024); | |
53 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
54 writer.writePath(path); | |
55 uint32_t bytesWritten = writer.bytesWritten(); | |
56 | |
57 unsigned char dataWritten[1024]; | |
58 writer.writeToMemory(dataWritten); | |
59 | |
60 // Make sure this fails when it should | |
61 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1); | |
62 const unsigned char* peekBefore = static_cast<const unsigned char*>(buff er.skip(0)); | |
63 buffer.readPath(&path); | |
64 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffe r.skip(0)); | |
65 // This should have failed, since the buffer is too small to read a matr ix from it | |
66 REPORTER_ASSERT(reporter, peekBefore == peekAfter); | |
67 | |
68 // Make sure this succeeds when it should | |
69 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
70 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); | |
71 buffer2.readPath(&path); | |
72 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); | |
73 // This should have succeeded, since there are enough bytes to read this | |
74 REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten); | |
75 | |
76 // Test memory read/write functions directly | |
77 size_t bytesWrittenToMemory = path.writeToMemory(dataWritten); | |
78 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWritten ToMemory); | |
79 size_t bytesReadFromMemory = path.readFromMemory(dataWritten, bytesWritt enToMemory); | |
80 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFrom Memory); | |
81 } | |
82 | |
83 // Test region serialization | |
84 { | |
85 SkRegion region; | |
86 SkOrderedWriteBuffer writer(1024); | |
87 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | |
88 writer.writeRegion(region); | |
89 uint32_t bytesWritten = writer.bytesWritten(); | |
90 | |
91 unsigned char dataWritten[1024]; | |
92 writer.writeToMemory(dataWritten); | |
93 | |
94 // Make sure this fails when it should | |
95 SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1); | |
96 const unsigned char* peekBefore = static_cast<const unsigned char*>(buff er.skip(0)); | |
97 buffer.readRegion(®ion); | |
98 const unsigned char* peekAfter = static_cast<const unsigned char*>(buffe r.skip(0)); | |
99 // This should have failed, since the buffer is too small to read a matr ix from it | |
100 REPORTER_ASSERT(reporter, peekBefore == peekAfter); | |
101 | |
102 // Make sure this succeeds when it should | |
103 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | |
104 peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); | |
105 buffer2.readRegion(®ion); | |
106 peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); | |
107 // This should have succeeded, since there are enough bytes to read this | |
108 REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten); | |
109 | |
110 // Test memory read/write functions directly | |
111 size_t bytesWrittenToMemory = region.writeToMemory(dataWritten); | |
112 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWritten ToMemory); | |
113 size_t bytesReadFromMemory = region.readFromMemory(dataWritten, bytesWri ttenToMemory); | |
114 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFrom Memory); | |
115 } | |
116 | |
117 // Test rrect serialization | |
118 { | |
119 SkRRect rrect; | |
120 unsigned char dataWritten[1024]; | |
121 | |
122 // Test memory read/write functions directly | |
123 size_t bytesWrittenToMemory = rrect.writeToMemory(dataWritten); | |
124 REPORTER_ASSERT(reporter, SkAlign4(bytesWrittenToMemory) == bytesWritten ToMemory); | |
125 size_t bytesReadFromMemory = rrect.readFromMemory(dataWritten, bytesWrit tenToMemory); | |
126 REPORTER_ASSERT(reporter, SkAlign4(bytesReadFromMemory) == bytesReadFrom Memory); | |
127 } | |
128 | |
129 // Test readByteArray | |
13 { | 130 { |
14 static const uint32_t arraySize = 512; | 131 static const uint32_t arraySize = 512; |
15 unsigned char data[arraySize] = {0}; | 132 unsigned char data[arraySize] = {0}; |
16 SkOrderedWriteBuffer writer(1024); | 133 SkOrderedWriteBuffer writer(1024); |
17 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 134 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
18 writer.writeByteArray(data, arraySize); | 135 writer.writeByteArray(data, arraySize); |
19 uint32_t bytesWritten = writer.bytesWritten(); | 136 uint32_t bytesWritten = writer.bytesWritten(); |
20 // This should write the length (in 4 bytes) and the array | 137 // This should write the length (in 4 bytes) and the array |
21 REPORTER_ASSERT(reporter, (4 + arraySize) == bytesWritten); | 138 REPORTER_ASSERT(reporter, (4 + arraySize) == bytesWritten); |
22 | 139 |
23 unsigned char dataWritten[1024]; | 140 unsigned char dataWritten[1024]; |
24 writer.writeToMemory(dataWritten); | 141 writer.writeToMemory(dataWritten); |
25 | 142 |
26 // Make sure this fails when it should | 143 // Make sure this fails when it should |
27 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 144 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
28 unsigned char dataRead[arraySize]; | 145 unsigned char dataRead[arraySize]; |
29 bool success = buffer.readByteArray(dataRead, 256); | 146 bool success = buffer.readByteArray(dataRead, 256); |
30 // This should have failed, since 256 < sizeInBytes | 147 // This should have failed, since 256 < sizeInBytes |
31 REPORTER_ASSERT(reporter, !success); | 148 REPORTER_ASSERT(reporter, !success); |
32 | 149 |
33 // Make sure this succeeds when it should | 150 // Make sure this succeeds when it should |
34 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 151 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
35 success = buffer2.readByteArray(dataRead, arraySize); | 152 success = buffer2.readByteArray(dataRead, arraySize); |
36 // This should have succeeded, since there are enough bytes to read this | 153 // This should have succeeded, since there are enough bytes to read this |
37 REPORTER_ASSERT(reporter, success); | 154 REPORTER_ASSERT(reporter, success); |
38 } | 155 } |
39 | 156 |
157 // Test readColorArray | |
40 { | 158 { |
41 static const uint32_t arraySize = 64; | 159 static const uint32_t arraySize = 64; |
42 SkColor data[arraySize]; | 160 SkColor data[arraySize]; |
43 SkOrderedWriteBuffer writer(1024); | 161 SkOrderedWriteBuffer writer(1024); |
44 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 162 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
45 writer.writeColorArray(data, arraySize); | 163 writer.writeColorArray(data, arraySize); |
46 uint32_t bytesWritten = writer.bytesWritten(); | 164 uint32_t bytesWritten = writer.bytesWritten(); |
47 // This should write the length (in 4 bytes) and the array | 165 // This should write the length (in 4 bytes) and the array |
48 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkColor)) == bytesWrit ten); | 166 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkColor)) == bytesWrit ten); |
49 | 167 |
50 unsigned char dataWritten[1024]; | 168 unsigned char dataWritten[1024]; |
51 writer.writeToMemory(dataWritten); | 169 writer.writeToMemory(dataWritten); |
52 | 170 |
53 // Make sure this fails when it should | 171 // Make sure this fails when it should |
54 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 172 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
55 SkColor dataRead[arraySize]; | 173 SkColor dataRead[arraySize]; |
56 bool success = buffer.readColorArray(dataRead, 32); | 174 bool success = buffer.readColorArray(dataRead, 32); |
57 // This should have failed, since 256 < sizeInBytes | 175 // This should have failed, since 256 < sizeInBytes |
58 REPORTER_ASSERT(reporter, !success); | 176 REPORTER_ASSERT(reporter, !success); |
59 | 177 |
60 // Make sure this succeeds when it should | 178 // Make sure this succeeds when it should |
61 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 179 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
62 success = buffer2.readColorArray(dataRead, arraySize); | 180 success = buffer2.readColorArray(dataRead, arraySize); |
63 // This should have succeeded, since there are enough bytes to read this | 181 // This should have succeeded, since there are enough bytes to read this |
64 REPORTER_ASSERT(reporter, success); | 182 REPORTER_ASSERT(reporter, success); |
65 } | 183 } |
66 | 184 |
185 // Test readIntArray | |
67 { | 186 { |
68 static const uint32_t arraySize = 64; | 187 static const uint32_t arraySize = 64; |
69 int32_t data[arraySize]; | 188 int32_t data[arraySize]; |
70 SkOrderedWriteBuffer writer(1024); | 189 SkOrderedWriteBuffer writer(1024); |
71 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 190 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
72 writer.writeIntArray(data, arraySize); | 191 writer.writeIntArray(data, arraySize); |
73 uint32_t bytesWritten = writer.bytesWritten(); | 192 uint32_t bytesWritten = writer.bytesWritten(); |
74 // This should write the length (in 4 bytes) and the array | 193 // This should write the length (in 4 bytes) and the array |
75 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(int32_t)) == bytesWrit ten); | 194 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(int32_t)) == bytesWrit ten); |
76 | 195 |
77 unsigned char dataWritten[1024]; | 196 unsigned char dataWritten[1024]; |
78 writer.writeToMemory(dataWritten); | 197 writer.writeToMemory(dataWritten); |
79 | 198 |
80 // Make sure this fails when it should | 199 // Make sure this fails when it should |
81 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 200 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
82 int32_t dataRead[arraySize]; | 201 int32_t dataRead[arraySize]; |
83 bool success = buffer.readIntArray(dataRead, 32); | 202 bool success = buffer.readIntArray(dataRead, 32); |
84 // This should have failed, since 256 < sizeInBytes | 203 // This should have failed, since 256 < sizeInBytes |
85 REPORTER_ASSERT(reporter, !success); | 204 REPORTER_ASSERT(reporter, !success); |
86 | 205 |
87 // Make sure this succeeds when it should | 206 // Make sure this succeeds when it should |
88 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 207 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
89 success = buffer2.readIntArray(dataRead, arraySize); | 208 success = buffer2.readIntArray(dataRead, arraySize); |
90 // This should have succeeded, since there are enough bytes to read this | 209 // This should have succeeded, since there are enough bytes to read this |
91 REPORTER_ASSERT(reporter, success); | 210 REPORTER_ASSERT(reporter, success); |
92 } | 211 } |
93 | 212 |
213 // Test readPointArray | |
94 { | 214 { |
95 static const uint32_t arraySize = 64; | 215 static const uint32_t arraySize = 64; |
96 SkPoint data[arraySize]; | 216 SkPoint data[arraySize]; |
97 SkOrderedWriteBuffer writer(1024); | 217 SkOrderedWriteBuffer writer(1024); |
98 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 218 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
99 writer.writePointArray(data, arraySize); | 219 writer.writePointArray(data, arraySize); |
100 uint32_t bytesWritten = writer.bytesWritten(); | 220 uint32_t bytesWritten = writer.bytesWritten(); |
101 // This should write the length (in 4 bytes) and the array | 221 // This should write the length (in 4 bytes) and the array |
102 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkPoint)) == bytesWrit ten); | 222 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkPoint)) == bytesWrit ten); |
103 | 223 |
104 unsigned char dataWritten[1024]; | 224 unsigned char dataWritten[1024]; |
105 writer.writeToMemory(dataWritten); | 225 writer.writeToMemory(dataWritten); |
106 | 226 |
107 // Make sure this fails when it should | 227 // Make sure this fails when it should |
108 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); | 228 SkValidatingReadBuffer buffer(dataWritten, bytesWritten); |
109 SkPoint dataRead[arraySize]; | 229 SkPoint dataRead[arraySize]; |
110 bool success = buffer.readPointArray(dataRead, 32); | 230 bool success = buffer.readPointArray(dataRead, 32); |
111 // This should have failed, since 256 < sizeInBytes | 231 // This should have failed, since 256 < sizeInBytes |
112 REPORTER_ASSERT(reporter, !success); | 232 REPORTER_ASSERT(reporter, !success); |
113 | 233 |
114 // Make sure this succeeds when it should | 234 // Make sure this succeeds when it should |
115 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 235 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
116 success = buffer2.readPointArray(dataRead, arraySize); | 236 success = buffer2.readPointArray(dataRead, arraySize); |
117 // This should have succeeded, since there are enough bytes to read this | 237 // This should have succeeded, since there are enough bytes to read this |
118 REPORTER_ASSERT(reporter, success); | 238 REPORTER_ASSERT(reporter, success); |
119 } | 239 } |
120 | 240 |
241 // Test readScalarArray | |
121 { | 242 { |
122 static const uint32_t arraySize = 64; | 243 static const uint32_t arraySize = 64; |
123 SkScalar data[arraySize]; | 244 SkScalar data[arraySize]; |
124 SkOrderedWriteBuffer writer(1024); | 245 SkOrderedWriteBuffer writer(1024); |
125 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); | 246 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
126 writer.writeScalarArray(data, arraySize); | 247 writer.writeScalarArray(data, arraySize); |
127 uint32_t bytesWritten = writer.bytesWritten(); | 248 uint32_t bytesWritten = writer.bytesWritten(); |
128 // This should write the length (in 4 bytes) and the array | 249 // This should write the length (in 4 bytes) and the array |
129 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkScalar)) == bytesWri tten); | 250 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkScalar)) == bytesWri tten); |
130 | 251 |
(...skipping 10 matching lines...) Expand all Loading... | |
141 // Make sure this succeeds when it should | 262 // Make sure this succeeds when it should |
142 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); | 263 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
143 success = buffer2.readScalarArray(dataRead, arraySize); | 264 success = buffer2.readScalarArray(dataRead, arraySize); |
144 // This should have succeeded, since there are enough bytes to read this | 265 // This should have succeeded, since there are enough bytes to read this |
145 REPORTER_ASSERT(reporter, success); | 266 REPORTER_ASSERT(reporter, success); |
146 } | 267 } |
147 } | 268 } |
148 | 269 |
149 #include "TestClassDef.h" | 270 #include "TestClassDef.h" |
150 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) | 271 DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) |
OLD | NEW |