Add accumulator overflow test for masked SAD functions

Accumulator overflow is unlikely to be caught by purely random testing.
This is due to the fact that a sum of uniformly distributed random
numbers will only hit the max/min possible sum with exponentially
reducing likelihood as the problem size grows.

The maximum accumulator value for an 8-bit masked SAD occurs when doing
an absolute difference of 255 and 0. To ensure that we correctly handle
large accumulator values, this commit adds an explicit test case with
such values.

Change-Id: Ida4544e908d8416ebd9341abb8abe7111186ec3c
diff --git a/test/masked_sad_test.cc b/test/masked_sad_test.cc
index cdd6166..158bcd5 100644
--- a/test/masked_sad_test.cc
+++ b/test/masked_sad_test.cc
@@ -187,13 +187,30 @@
   int msk_stride = MAX_SB_SIZE;
   const int iters = run_times == 1 ? number_of_iterations : 1;
   for (int i = 0; i < iters; ++i) {
+    if (run_times == 1 && i == 0) {
+      // The maximum accumulator value occurs when src=0 and
+      // ref/second_pref=255 (or vice-versa, since we take the absolute
+      // difference). Check this case explicitly to ensure we do not overflow
+      // during accumulation.
+      for (int j = 0; j < MAX_SB_SIZE * MAX_SB_SIZE; j++) {
+        src_ptr[j] = 0;
+        ref_ptr[j] = 255;
+        (ref_ptr + kBlockSize)[j] = 255;
+        (ref_ptr + 2 * kBlockSize)[j] = 255;
+        (ref_ptr + 3 * kBlockSize)[j] = 255;
+        second_pred_ptr[j] = 255;
+      }
+    } else {
+      for (int j = 0; j < MAX_SB_SIZE * MAX_SB_SIZE; j++) {
+        src_ptr[j] = rnd.Rand8();
+        ref_ptr[j] = rnd.Rand8();
+        (ref_ptr + kBlockSize)[j] = rnd.Rand8();
+        (ref_ptr + 2 * kBlockSize)[j] = rnd.Rand8();
+        (ref_ptr + 3 * kBlockSize)[j] = rnd.Rand8();
+        second_pred_ptr[j] = rnd.Rand8();
+      }
+    }
     for (int j = 0; j < MAX_SB_SIZE * MAX_SB_SIZE; j++) {
-      src_ptr[j] = rnd.Rand8();
-      ref_ptr[j] = rnd.Rand8();
-      (ref_ptr + kBlockSize)[j] = rnd.Rand8();
-      (ref_ptr + 2 * kBlockSize)[j] = rnd.Rand8();
-      (ref_ptr + 3 * kBlockSize)[j] = rnd.Rand8();
-      second_pred_ptr[j] = rnd.Rand8();
       msk_ptr[j] = ((rnd.Rand8() & 0x7f) > 64) ? rnd.Rand8() & 0x3f : 64;
       assert(msk_ptr[j] <= 64);
     }