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

Unified Diff: tests/PDFPrimitivesTest.cpp

Issue 1720863003: SkPDF: fix scalar serialization (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 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 side-by-side diff with in-line comments
Download patch
« src/pdf/SkPDFUtils.cpp ('K') | « src/pdf/SkPDFUtils.cpp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
+ }
+}
« src/pdf/SkPDFUtils.cpp ('K') | « src/pdf/SkPDFUtils.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698