Add tests for the PostScript truncate operator.

Test how different PostScript truncate operations render using a pixel
test. On the second page, ideally there should be a green square if an
implementation follows the PostScript Language Reference and works with
numbers as floats. In which case, numbers larger than INT_MAX truncate
just fine. However, this test in Acrobat Readers draws a black square.
PDFium also draws a black square, so just add this test to document the
existing behavior, which emulates Acrobat's behavior.

Also add more test cases to the CPDF_PSEngine.Truncate unit test.

BUG=pdfium:1314

Change-Id: I32da10d0cca69fcf5ab97c887090ca3ca0f7775f
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/56414
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfapi/page/cpdf_psengine_unittest.cpp b/core/fpdfapi/page/cpdf_psengine_unittest.cpp
index 03e096d..a922de1 100644
--- a/core/fpdfapi/page/cpdf_psengine_unittest.cpp
+++ b/core/fpdfapi/page/cpdf_psengine_unittest.cpp
@@ -181,4 +181,13 @@
   EXPECT_FLOAT_EQ(-2.0f, DoOperator1(&engine, -2.3f, PSOP_TRUNCATE));
   EXPECT_FLOAT_EQ(-3.0f, DoOperator1(&engine, -3.8f, PSOP_TRUNCATE));
   EXPECT_FLOAT_EQ(-5.0f, DoOperator1(&engine, -5.5f, PSOP_TRUNCATE));
+
+  // Truncate does not behave according to the PostScript Language Reference for
+  // values beyond the range of integers. This seems to match Acrobat's
+  // behavior. See https://crbug.com/1314.
+  float max_int = std::numeric_limits<int>::max();
+  EXPECT_FLOAT_EQ(-max_int,
+                  DoOperator1(&engine, max_int * 2.0f, PSOP_TRUNCATE));
+  EXPECT_FLOAT_EQ(-max_int,
+                  DoOperator1(&engine, max_int * -1.5f, PSOP_TRUNCATE));
 }
diff --git a/testing/resources/pixel/bug_1314.in b/testing/resources/pixel/bug_1314.in
new file mode 100644
index 0000000..8da683e
--- /dev/null
+++ b/testing/resources/pixel/bug_1314.in
@@ -0,0 +1,137 @@
+{{header}}
+{{object 1 0}} <<
+  /Type /Catalog
+  /Pages 2 0 R
+>>
+endobj
+{{object 2 0}} <<
+  /Type /Pages
+  /Count 3
+  /Kids [3 0 R 4 0 R 5 0 R]
+>>
+endobj
+{{object 3 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /MediaBox [0 0 200 300]
+  /Resources <<
+    /Shading <<
+      /Sh0 7 0 R
+    >>
+  >>
+  /Contents 6 0 R
+>>
+endobj
+{{object 4 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /MediaBox [0 0 200 300]
+  /Resources <<
+    /Shading <<
+      /Sh0 8 0 R
+    >>
+  >>
+  /Contents 6 0 R
+>>
+endobj
+{{object 5 0}} <<
+  /Type /Page
+  /Parent 2 0 R
+  /MediaBox [0 0 200 300]
+  /Resources <<
+    /Shading <<
+      /Sh0 9 0 R
+    >>
+  >>
+  /Contents 6 0 R
+>>
+endobj
+{{object 6 0}} <<
+  {{streamlen}}
+>>
+stream
+q
+72 0 0 72 10 10 cm
+/Sh0 sh
+Q
+endstream
+endobj
+{{object 7 0}} <<
+  /ShadingType 1
+  /ColorSpace /DeviceRGB
+  /Background [0 .5 .1]
+  /BBox [0 0 72 72]
+  /Domain [-1 1 -1 1]
+  /Function [10 0 R 13 0 R 13 0 R]
+>>
+endobj
+{{object 8 0}} <<
+  /ShadingType 1
+  /ColorSpace /DeviceRGB
+  /Background [0 .5 .1]
+  /BBox [0 0 72 72]
+  /Domain [-1 1 -1 1]
+  /Function [13 0 R 11 0 R 13 0 R]
+>>
+endobj
+{{object 9 0}} <<
+  /ShadingType 1
+  /ColorSpace /DeviceRGB
+  /Background [0 .5 .1]
+  /BBox [0 0 72 72]
+  /Domain [-1 1 -1 1]
+  /Function [13 0 R 13 0 R 12 0 R]
+>>
+endobj
+{{object 10 0}} <<
+  /FunctionType 4
+  /Domain [-1 1 -1 1]
+  /Range [0 1]
+  {{streamlen}}
+>>
+stream
+{
+1.1 truncate
+}
+endstream
+endobj
+{{object 11 0}} <<
+  /FunctionType 4
+  /Domain [-1 1 -1 1]
+  /Range [0 1]
+  {{streamlen}}
+>>
+stream
+{
+3123412341.1 truncate -3123412340.5 add
+}
+endstream
+endobj
+{{object 12 0}} <<
+  /FunctionType 4
+  /Domain [-1 1 -1 1]
+  /Range [0 1]
+  {{streamlen}}
+>>
+stream
+{
+-1.9 truncate 1.5 add
+}
+endstream
+endobj
+{{object 13 0}} <<
+  /FunctionType 4
+  /Domain [-1 1 -1 1]
+  /Range [0 1]
+  {{streamlen}}
+>>
+stream
+{
+0
+}
+endstream
+endobj
+{{xref}}
+{{trailer}}
+{{startxref}}
+%%EOF
diff --git a/testing/resources/pixel/bug_1314_expected.pdf.0.png b/testing/resources/pixel/bug_1314_expected.pdf.0.png
new file mode 100644
index 0000000..88355ae4
--- /dev/null
+++ b/testing/resources/pixel/bug_1314_expected.pdf.0.png
Binary files differ
diff --git a/testing/resources/pixel/bug_1314_expected.pdf.1.png b/testing/resources/pixel/bug_1314_expected.pdf.1.png
new file mode 100644
index 0000000..cdc3cf4
--- /dev/null
+++ b/testing/resources/pixel/bug_1314_expected.pdf.1.png
Binary files differ
diff --git a/testing/resources/pixel/bug_1314_expected.pdf.2.png b/testing/resources/pixel/bug_1314_expected.pdf.2.png
new file mode 100644
index 0000000..450e4e8
--- /dev/null
+++ b/testing/resources/pixel/bug_1314_expected.pdf.2.png
Binary files differ