Index: tests/PDFPrimitivesTest.cpp |
diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp |
index 9ca1bd619a23c1c4cb8af0599729db5a548263b3..952e49994ad3c0723b96ed41f77602ec49c56fbd 100644 |
--- a/tests/PDFPrimitivesTest.cpp |
+++ b/tests/PDFPrimitivesTest.cpp |
@@ -18,6 +18,7 @@ |
#include "SkPDFFont.h" |
#include "SkPDFStream.h" |
#include "SkPDFTypes.h" |
+#include "SkPDFUtils.h" |
#include "SkReadBuffer.h" |
#include "SkScalar.h" |
#include "SkStream.h" |
@@ -341,7 +342,7 @@ static void TestPDFDict(skiatest::Reporter* reporter) { |
dict.reset(new SkPDFDict("DType")); |
ASSERT_EMIT_EQ(reporter, *dict, "<</Type /DType>>"); |
- |
+ |
SkAutoTUnref<SkPDFArray> referencedArray(new SkPDFArray); |
Catalog catalog; |
catalog.numbers.addObject(referencedArray.get()); |
@@ -435,3 +436,52 @@ DEF_TEST(PDFFontCanEmbedTypeface, reporter) { |
REPORTER_ASSERT(reporter, |
SkPDFFont::CanEmbedTypeface(portableTypeface, &canon)); |
} |
+ |
+ |
+// test to see that all finite scalars round trip via scanf(). |
+static void check_pdf_scalar_serialization(skiatest::Reporter* reporter, |
+ float inputFloat) { |
+ SkDynamicMemoryWStream buffer; |
+ SkPDFUtils::AppendScalar(inputFloat, &buffer); |
+ char floatString[2048]; |
+ if (buffer.bytesWritten() >= sizeof(floatString)) { |
+ ERRORF(reporter, "absurdly long output."); |
+ return; |
+ } |
+ buffer.copyTo(floatString); |
+ floatString[buffer.bytesWritten()] = '\0'; |
+ if (reporter->verbose()) { |
+ SkDebugf("%.9g = \"%s\"\n", inputFloat, floatString); |
+ } |
+ float roundTripFloat; |
+ if (1 != sscanf(floatString, "%f", &roundTripFloat)) { |
+ ERRORF(reporter, "unscannable result"); |
+ return; |
+ } |
+ if (isfinite(inputFloat)) { |
+ REPORTER_ASSERT(reporter, roundTripFloat == inputFloat); |
+ } |
+} |
+ |
+// Test SkPDFUtils::AppendScalar for accuracy. |
+DEF_TEST(PDFPrimitives_Scalar, reporter) { |
+ const int kRandomSeed = 0xBEEF; |
+ int iterationCount = 500; |
+ SkRandom random(kRandomSeed); |
+ while (iterationCount-- > 0) { |
+ union { |
+ float inputFloat; |
+ uint32_t u; |
+ }; |
+ static_assert(sizeof(float) == sizeof(uint32_t), ""); |
+ u = random.nextU(); // Pick any random float. |
+ check_pdf_scalar_serialization(reporter, inputFloat); |
+ } |
+ float alwaysCheck[] = { 0.0f, -0.0f, 1.0f, -1.0f, FLT_MIN, FLT_MAX, |
+ -FLT_MIN, -FLT_MAX, SK_FloatInfinity, |
+ SK_FloatNegativeInfinity, SK_FloatNaN, SK_ScalarPI, |
+ static_cast<float>(1.0 / 10.0)}; |
+ for (float inputFloat: alwaysCheck) { |
+ check_pdf_scalar_serialization(reporter, inputFloat); |
+ } |
+} |