0a62f34
From fcba671fdaf7973d17d67684685f71787cd0e0c7 Mon Sep 17 00:00:00 2001
0a62f34
From: Michael Nahas <mike@nahas.com>
0a62f34
Date: Tue, 11 Feb 2020 22:42:08 -0600
0a62f34
Subject: [PATCH] Fix for Github issue #143.  Test did not account for
0a62f34
 endianness correctly.
0a62f34
0a62f34
---
0a62f34
 src/reedsolomon_test.cpp | 30 +++++++++++++++++++++++++++---
0a62f34
 1 file changed, 27 insertions(+), 3 deletions(-)
0a62f34
0a62f34
diff --git a/src/reedsolomon_test.cpp b/src/reedsolomon_test.cpp
0a62f34
index 1285b3c..c8d26e9 100644
0a62f34
--- a/src/reedsolomon_test.cpp
0a62f34
+++ b/src/reedsolomon_test.cpp
0a62f34
@@ -433,6 +433,23 @@ int test3() {
0a62f34
 
0a62f34
 
0a62f34
 // Check that the correct constants are being used for Par2
0a62f34
+
0a62f34
+//The test pretends there are 10 input blocks ("NUM_IN") and 1
0a62f34
+//recovery block ("NUM_REC"), each 1024 bytes long ("BUF_SIZE"). These
0a62f34
+//are all stored in data[11][BUF_SIZE], with the input blocks
0a62f34
+//occupying data[0] through data[9] and the recovery block in
0a62f34
+//data[10].
0a62f34
+
0a62f34
+//The test zeroes out the input blocks and then writes a 1 into the
0a62f34
+//first location of the first input block, and into the second
0a62f34
+//location of the second input block, etc. It then generates the
0a62f34
+//recovery block using many calls to ReedSolomon. When that happens,
0a62f34
+//those 1s are multiplied by the coefficients for each input block. So
0a62f34
+//the first location of recovery block holds the coefficient for the
0a62f34
+//first input block, the second location has the coefficient for the
0a62f34
+//second input block, etc. Those values are checked against the
0a62f34
+//expected values passed to the function.
0a62f34
+
0a62f34
 template<typename gtype, typename utype>
0a62f34
 int test4(int NUM_IN, int *expected_bases) {
0a62f34
   //const int NUM_IN  = 10;
0a62f34
@@ -448,8 +465,9 @@ int test4(int NUM_IN, int *expected_bases) {
0a62f34
     for (int k = 0; k < BUF_SIZE; k++) {
0a62f34
       data[i][k] = (u8)0;
0a62f34
     }
0a62f34
-    // EXCEPT put a 1 in a different place for each file
0a62f34
-    ((gtype *)(&(data[i][0])))[i] = (utype) 1;
0a62f34
+    // EXCEPT write a (little endian) 1 in a different place for each file
0a62f34
+    // In the i-th file, it is written into the i-th location
0a62f34
+    data[i][sizeof(utype)*i] = (u8) 1;
0a62f34
   }
0a62f34
   // zero recovery
0a62f34
   for (int j = 0; j < NUM_REC; j++) {
0a62f34
@@ -488,7 +506,13 @@ int test4(int NUM_IN, int *expected_bases) {
0a62f34
   // The recovery file has exponent 1 and should
0a62f34
   // contain each base to the power 1.
0a62f34
   for (int i = 0; i < NUM_IN; i++) {
0a62f34
-    int base = (utype) ((gtype *) &(data[NUM_IN+0][0]))[i];
0a62f34
+    // read little-endian value
0a62f34
+    utype v = 0;
0a62f34
+    for (int byte_index = 0; byte_index < sizeof(utype); byte_index++) {
0a62f34
+      u8 byte = data[NUM_IN+0][sizeof(utype)*i + byte_index];
0a62f34
+      v |= (((utype)byte) << (byte_index*8));
0a62f34
+    }
0a62f34
+    int base = v;
0a62f34
     if (base != expected_bases[i]) {
0a62f34
       cerr << "base at location " << i << " did not match expected." << endl;
0a62f34
       cerr << "   base     = " << base << endl;
0a62f34
-- 
0a62f34
2.25.2
0a62f34