blob: d2c692526f62bf3a673aba38568f2a19a7b6f95a [file] [log] [blame]
commit-bot@chromium.org02512882013-10-31 18:37:50 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkOrderedWriteBuffer.h"
9#include "SkValidatingReadBuffer.h"
10#include "Test.h"
11
12static void Tests(skiatest::Reporter* reporter) {
13 {
14 static const uint32_t arraySize = 512;
15 unsigned char data[arraySize] = {0};
16 SkOrderedWriteBuffer writer(1024);
17 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
18 writer.writeByteArray(data, arraySize);
19 uint32_t bytesWritten = writer.bytesWritten();
20 // This should write the length (in 4 bytes) and the array
21 REPORTER_ASSERT(reporter, (4 + arraySize) == 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);
28 unsigned char dataRead[arraySize];
29 bool success = buffer.readByteArray(dataRead, 256);
30 // This should have failed, since 256 < sizeInBytes
31 REPORTER_ASSERT(reporter, !success);
32
33 // Make sure this succeeds when it should
34 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
35 success = buffer2.readByteArray(dataRead, arraySize);
36 // This should have succeeded, since there are enough bytes to read this
37 REPORTER_ASSERT(reporter, success);
38 }
39
40 {
41 static const uint32_t arraySize = 64;
42 SkColor data[arraySize];
43 SkOrderedWriteBuffer writer(1024);
44 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
45 writer.writeColorArray(data, arraySize);
46 uint32_t bytesWritten = writer.bytesWritten();
47 // This should write the length (in 4 bytes) and the array
48 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkColor)) == bytesWritten);
49
50 unsigned char dataWritten[1024];
51 writer.writeToMemory(dataWritten);
52
53 // Make sure this fails when it should
54 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
55 SkColor dataRead[arraySize];
56 bool success = buffer.readColorArray(dataRead, 32);
57 // This should have failed, since 256 < sizeInBytes
58 REPORTER_ASSERT(reporter, !success);
59
60 // Make sure this succeeds when it should
61 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
62 success = buffer2.readColorArray(dataRead, arraySize);
63 // This should have succeeded, since there are enough bytes to read this
64 REPORTER_ASSERT(reporter, success);
65 }
66
67 {
68 static const uint32_t arraySize = 64;
69 int32_t data[arraySize];
70 SkOrderedWriteBuffer writer(1024);
71 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
72 writer.writeIntArray(data, arraySize);
73 uint32_t bytesWritten = writer.bytesWritten();
74 // This should write the length (in 4 bytes) and the array
75 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(int32_t)) == bytesWritten);
76
77 unsigned char dataWritten[1024];
78 writer.writeToMemory(dataWritten);
79
80 // Make sure this fails when it should
81 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
82 int32_t dataRead[arraySize];
83 bool success = buffer.readIntArray(dataRead, 32);
84 // This should have failed, since 256 < sizeInBytes
85 REPORTER_ASSERT(reporter, !success);
86
87 // Make sure this succeeds when it should
88 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
89 success = buffer2.readIntArray(dataRead, arraySize);
90 // This should have succeeded, since there are enough bytes to read this
91 REPORTER_ASSERT(reporter, success);
92 }
93
94 {
95 static const uint32_t arraySize = 64;
96 SkPoint data[arraySize];
97 SkOrderedWriteBuffer writer(1024);
98 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
99 writer.writePointArray(data, arraySize);
100 uint32_t bytesWritten = writer.bytesWritten();
101 // This should write the length (in 4 bytes) and the array
102 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkPoint)) == bytesWritten);
103
104 unsigned char dataWritten[1024];
105 writer.writeToMemory(dataWritten);
106
107 // Make sure this fails when it should
108 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
109 SkPoint dataRead[arraySize];
110 bool success = buffer.readPointArray(dataRead, 32);
111 // This should have failed, since 256 < sizeInBytes
112 REPORTER_ASSERT(reporter, !success);
113
114 // Make sure this succeeds when it should
115 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
116 success = buffer2.readPointArray(dataRead, arraySize);
117 // This should have succeeded, since there are enough bytes to read this
118 REPORTER_ASSERT(reporter, success);
119 }
120
121 {
122 static const uint32_t arraySize = 64;
123 SkScalar data[arraySize];
124 SkOrderedWriteBuffer writer(1024);
125 writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag);
126 writer.writeScalarArray(data, arraySize);
127 uint32_t bytesWritten = writer.bytesWritten();
128 // This should write the length (in 4 bytes) and the array
129 REPORTER_ASSERT(reporter, (4 + arraySize * sizeof(SkScalar)) == bytesWritten);
130
131 unsigned char dataWritten[1024];
132 writer.writeToMemory(dataWritten);
133
134 // Make sure this fails when it should
135 SkValidatingReadBuffer buffer(dataWritten, bytesWritten);
136 SkScalar dataRead[arraySize];
137 bool success = buffer.readScalarArray(dataRead, 32);
138 // This should have failed, since 256 < sizeInBytes
139 REPORTER_ASSERT(reporter, !success);
140
141 // Make sure this succeeds when it should
142 SkValidatingReadBuffer buffer2(dataWritten, bytesWritten);
143 success = buffer2.readScalarArray(dataRead, arraySize);
144 // This should have succeeded, since there are enough bytes to read this
145 REPORTER_ASSERT(reporter, success);
146 }
147}
148
149#include "TestClassDef.h"
150DEFINE_TESTCLASS("Serialization", SerializationClass, Tests)