Index: tests/SerializationTest.cpp |
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fa08c833e330ba95a96dae4dd574cfe87f1d9fd4 |
--- /dev/null |
+++ b/tests/SerializationTest.cpp |
@@ -0,0 +1,101 @@ |
+/* |
+ * Copyright 2013 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+ |
+#include "SkOrderedWriteBuffer.h" |
+#include "SkValidatingReadBuffer.h" |
+#include "Test.h" |
+ |
+static void Tests(skiatest::Reporter* reporter) { |
+ // Test matrix serialization |
+ { |
+ SkMatrix matrix = SkMatrix::I(); |
+ SkOrderedWriteBuffer writer(1024); |
+ writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
+ writer.writeMatrix(matrix); |
+ uint32_t bytesWritten = writer.bytesWritten(); |
+ // This should write the length (in 4 bytes) and the array |
+ REPORTER_ASSERT(reporter, (9 * sizeof(SkScalar)) == bytesWritten); |
+ |
+ unsigned char dataWritten[1024]; |
+ writer.writeToMemory(dataWritten); |
+ |
+ // Make sure this fails when it should |
+ SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1); |
+ const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0)); |
+ buffer.readMatrix(&matrix); |
+ const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0)); |
+ // This should have failed, since the buffer is too small to read a matrix from it |
+ REPORTER_ASSERT(reporter, peekBefore == peekAfter); |
+ |
+ // Make sure this succeeds when it should |
+ SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
+ peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
+ buffer2.readMatrix(&matrix); |
+ peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
+ // This should have succeeded, since there are enough bytes to read this |
+ REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten); |
+ } |
+ |
+ // Test path serialization |
+ { |
+ SkPath path; |
+ SkOrderedWriteBuffer writer(1024); |
+ writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
+ writer.writePath(path); |
+ uint32_t bytesWritten = writer.bytesWritten(); |
+ |
+ unsigned char dataWritten[1024]; |
+ writer.writeToMemory(dataWritten); |
+ |
+ // Make sure this fails when it should |
+ SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1); |
+ const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0)); |
+ buffer.readPath(&path); |
+ const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0)); |
+ // This should have failed, since the buffer is too small to read a matrix from it |
+ REPORTER_ASSERT(reporter, peekBefore == peekAfter); |
+ |
+ // Make sure this succeeds when it should |
+ SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
+ peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
+ buffer2.readPath(&path); |
+ peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
+ // This should have succeeded, since there are enough bytes to read this |
+ REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten); |
+ } |
+ |
+ // Test region serialization |
+ { |
+ SkRegion region; |
+ SkOrderedWriteBuffer writer(1024); |
+ writer.setFlags(SkOrderedWriteBuffer::kValidation_Flag); |
+ writer.writeRegion(region); |
+ uint32_t bytesWritten = writer.bytesWritten(); |
+ |
+ unsigned char dataWritten[1024]; |
+ writer.writeToMemory(dataWritten); |
+ |
+ // Make sure this fails when it should |
+ SkValidatingReadBuffer buffer(dataWritten, bytesWritten - 1); |
+ const unsigned char* peekBefore = static_cast<const unsigned char*>(buffer.skip(0)); |
+ buffer.readRegion(®ion); |
+ const unsigned char* peekAfter = static_cast<const unsigned char*>(buffer.skip(0)); |
+ // This should have failed, since the buffer is too small to read a matrix from it |
+ REPORTER_ASSERT(reporter, peekBefore == peekAfter); |
+ |
+ // Make sure this succeeds when it should |
+ SkValidatingReadBuffer buffer2(dataWritten, bytesWritten); |
+ peekBefore = static_cast<const unsigned char*>(buffer2.skip(0)); |
+ buffer2.readRegion(®ion); |
+ peekAfter = static_cast<const unsigned char*>(buffer2.skip(0)); |
+ // This should have succeeded, since there are enough bytes to read this |
+ REPORTER_ASSERT(reporter, (peekAfter - peekBefore) == bytesWritten); |
+ } |
+} |
+ |
+#include "TestClassDef.h" |
+DEFINE_TESTCLASS("Serialization", SerializationClass, Tests) |