Add more test cases for AFSimple_Calculate().

Use them to illustrate rounding errors in the implementation.

Also add some notes for other test cases where AFSimple_Calculate() does
not behave the same way as Acrobat Reader.

Bug: chromium:611744,pdfium:1396
Change-Id: Ic87bd74245524d2cf6000a88620b800cdaba9129
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/60330
Reviewed-by: Henrique Nakashima <hnakashima@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/testing/resources/javascript/public_methods.in b/testing/resources/javascript/public_methods.in
index 408307e..8fd9ee9 100644
--- a/testing/resources/javascript/public_methods.in
+++ b/testing/resources/javascript/public_methods.in
@@ -7,6 +7,7 @@
       4 0 R
       10 0 R
       11 0 R
+      12 0 R
     ]
     /DR 5 0 R
   >>
@@ -28,6 +29,7 @@
     4 0 R
     10 0 R
     11 0 R
+    12 0 R
   ]
 >>
 endobj
@@ -36,7 +38,7 @@
   /FT /Tx
   /T (Text Box)
   /DA (0 0 0 rg /F1 12 Tf)
-  /Rect [ 100 100 200 130 ]
+  /Rect [ 100 160 200 190 ]
   /Subtype /Widget
   /AA <<
     /F 9 0 R
@@ -462,9 +464,15 @@
 
       app.alert("**********************");
 
+      // These two should throw "TypeError", not "AFSimple_Calculate".
+      // https://crbug.com/pdfium/1396
       expectError('', "AFSimple_Calculate()");
       expectError('', "AFSimple_Calculate(1)");
+      // This should "pass", but currently throws an error.
+      // https://crbug.com/pdfium/1396
       expectEventValue('', "AFSimple_Calculate('blooey', ['Text2', 'Text3'])", 0);
+      // This should fail with TypeError, since a field does not exist, but
+      // currently passes. https://crbug.com/pdfium/1396
       expectEventValue('', "AFSimple_Calculate('AVG', [1, 'nonesuch', {'crud': 32}])", 0);
       expectEventValue('', "AFSimple_Calculate('AVG', ['Text2', 'Text3'])", (123 + 456) / 2);
       expectEventValue('', "AFSimple_Calculate('SUM', ['Text2', 'Text3'])", 123 + 456);
@@ -472,6 +480,17 @@
       expectEventValue('', "AFSimple_Calculate('MIN', ['Text2', 'Text3'])", 123);
       expectEventValue('', "AFSimple_Calculate('MAX', ['Text2', 'Text3'])", 456);
       expectEventValue('', "AFSimple_Calculate('AVG', 'Text2, Text3')", (123 + 456) / 2);
+      // The next 6 currently have rounding issues. https://crbug.com/611744.
+      expectEventValue('', "AFSimple_Calculate('AVG', ['Text4'])", 407.96);
+      expectEventValue('', "AFSimple_Calculate('SUM', ['Text4'])", 407.96);
+      expectEventValue('', "AFSimple_Calculate('MIN', ['Text4'])", 407.96);
+      expectEventValue('', "AFSimple_Calculate('MAX', ['Text4'])", 407.96);
+      expectEventValue('', "AFSimple_Calculate('AVG', ['Text2', 'Text4'])", (123 + 407.96) / 2);
+      expectEventValue('', "AFSimple_Calculate('SUM', ['Text2', 'Text4'])", 123 + 407.96);
+      // Note: 123 * 407.96 = 50179.08, but Acrobat has rounding artifacts in
+      // its implementation as well. It returns 50179.0799999999994, which is
+      // effectively 50179.08.
+      expectEventValue('', "AFSimple_Calculate('PRD', ['Text2', 'Text4'])", 50179.0799999999994);
 
       app.alert("**********************");
 
@@ -539,7 +558,7 @@
   /FT /Tx
   /T (Text2)
   /DA (0 0 0 rg /F1 12 Tf)
-  /Rect [100 40 200 70]
+  /Rect [100 0 200 30]
   /Subtype /Widget
   /V (123)
 >>
@@ -549,11 +568,21 @@
   /FT /Tx
   /T (Text3)
   /DA (0 0 0 rg /F1 12 Tf)
-  /Rect [100 0 200 30]
+  /Rect [100 40 200 70]
   /Subtype /Widget
   /V (456)
 >>
 endobj
+{{object 12 0}} <<
+  /Type /Annot
+  /FT /Tx
+  /T (Text4)
+  /DA (0 0 0 rg /F1 12 Tf)
+  /Rect [100 80 200 110]
+  /Subtype /Widget
+  /V (407.96)
+>>
+endobj
 {{xref}}
 {{trailer}}
 {{startxref}}
diff --git a/testing/resources/javascript/public_methods_expected.txt b/testing/resources/javascript/public_methods_expected.txt
index d6b2566..b7b60e6 100644
--- a/testing/resources/javascript/public_methods_expected.txt
+++ b/testing/resources/javascript/public_methods_expected.txt
@@ -318,6 +318,13 @@
 Alert: PASS: AFSimple_Calculate('MIN', ['Text2', 'Text3']) = 123
 Alert: PASS: AFSimple_Calculate('MAX', ['Text2', 'Text3']) = 456
 Alert: PASS: AFSimple_Calculate('AVG', 'Text2, Text3') = 289.5
+Alert: FAIL: AFSimple_Calculate('AVG', ['Text4']) = 407.959991, expected 407.96 
+Alert: FAIL: AFSimple_Calculate('SUM', ['Text4']) = 407.959991, expected 407.96 
+Alert: FAIL: AFSimple_Calculate('MIN', ['Text4']) = 407.959991, expected 407.96 
+Alert: FAIL: AFSimple_Calculate('MAX', ['Text4']) = 407.959991, expected 407.96 
+Alert: FAIL: AFSimple_Calculate('AVG', ['Text2', 'Text4']) = 265.479996, expected 265.48 
+Alert: FAIL: AFSimple_Calculate('SUM', ['Text2', 'Text4']) = 530.959991, expected 530.96 
+Alert: FAIL: AFSimple_Calculate('PRD', ['Text2', 'Text4']) = 50179.078949, expected 50179.08 
 Alert: **********************
 Alert: PASS: AFSpecial_Format() threw AFSpecial_Format: Incorrect number of parameters passed to function.
 Alert: PASS: AFSpecial_Format(1, 2) threw AFSpecial_Format: Incorrect number of parameters passed to function.