From a2ced72c99cb8dc0566cf4701f7a707b69688511 Mon Sep 17 00:00:00 2001
From: zooko <zooko@zooko.com>
Date: Sat, 19 Jan 2008 08:38:57 +0530
Subject: [PATCH] add unit tests of the bug agl had, add assertion and doc
 explaining the C API better

darcs-hash:61f313f9913bbf28d02082d278fdd0b39adc04f2
---
 zfec/zfec/_fecmodule.c      | 51 +++++++++++++++++++++++--------------
 zfec/zfec/fec.c             |  1 +
 zfec/zfec/fec.h             |  3 ++-
 zfec/zfec/test/test_zfec.py | 15 ++++++++---
 4 files changed, 46 insertions(+), 24 deletions(-)

diff --git a/zfec/zfec/_fecmodule.c b/zfec/zfec/_fecmodule.c
index 05b24d6..d42e635 100644
--- a/zfec/zfec/_fecmodule.c
+++ b/zfec/zfec/_fecmodule.c
@@ -549,38 +549,51 @@ static PyTypeObject Decoder_type = {
     Decoder_new,                 /* tp_new */
 };
 
+void
+_hexwrite(unsigned char*s, size_t l) {
+  for (size_t i = 0; i < l; i++)
+    printf("%.2x", s[i]);
+}
+
 PyObject*
 test_from_agl() {
-  unsigned char b1c[8], b2c[8];
-  unsigned char b1[8], b2[8], b3[8], b4[8], b5[8];
-  memset(b1, 1, 8);
-  memset(b2, 2, 8);
-  memset(b3, 3, 8);
-  const unsigned char *blocks[3] = {b1, b2, b3};
-  unsigned char *outblocks[2] = {b4, b5};
+  unsigned char b0c[8], b1c[8];
+  unsigned char b0[8], b1[8], b2[8], b3[8], b4[8];
+  memset(b0, 1, 8);
+  memset(b1, 2, 8);
+  memset(b2, 3, 8);
+  const unsigned char *blocks[3] = {b0, b1, b2};
+  unsigned char *outblocks[2] = {b3, b4};
   unsigned block_nums[] = {3, 4};
 
+  /*printf("_from_c before encoding:\n");
+  printf("b0: "); _hexwrite(b0, 8); printf(", ");
+  printf("b1: "); _hexwrite(b1, 8); printf(", ");
+  printf("b2: "); _hexwrite(b2, 8); printf(", ");
+  printf("\n");*/
+
   fec_t *const fec = fec_new(3, 5);
   fec_encode(fec, blocks, outblocks, block_nums, 2, 8);
 
-  write(1, b1, 8);
-  write(1, b2, 8);
-  write(1, b3, 8);
-  write(1, b4, 8);
-  write(1, b5, 8);
+  /*printf("after encoding:\n");
+  printf("b3: "); _hexwrite(b3, 8); printf(", ");
+  printf("b4: "); _hexwrite(b4, 8); printf(", ");
+  printf("\n");*/
 
-  memcpy(b1c, b1, 8); memcpy(b2c, b2, 8);
+  memcpy(b0c, b0, 8); memcpy(b1c, b1, 8);
 
-  const unsigned char *inpkts[] = {b3, b4, b5};
-  unsigned char *outpkts[] = {b1, b2};
-  unsigned indexes[] = {2, 3, 4};
+  const unsigned char *inpkts[] = {b3, b4, b2};
+  unsigned char *outpkts[] = {b0, b1};
+  unsigned indexes[] = {3, 4, 2};
 
   fec_decode(fec, inpkts, outpkts, indexes, 8);
 
-  write(1, b1, 8);
-  write(1, b2, 8);
+  /*printf("after decoding:\n");
+  printf("b0: "); _hexwrite(b0, 8); printf(", ");
+  printf("b1: "); _hexwrite(b1, 8);
+  printf("\n");*/
 
-  if ((memcmp(b1, b1c,8) == 0) && (memcmp(b2, b2c,8) == 0))
+  if ((memcmp(b0, b0c,8) == 0) && (memcmp(b1, b1c,8) == 0))
     Py_RETURN_TRUE;
   else
     Py_RETURN_FALSE;
diff --git a/zfec/zfec/fec.c b/zfec/zfec/fec.c
index 3f852c9..ba6e347 100644
--- a/zfec/zfec/fec.c
+++ b/zfec/zfec/fec.c
@@ -509,6 +509,7 @@ fec_decode(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*
     build_decode_matrix_into_space(code, index, code->k, m_dec);
 
     for (row=0; row<code->k; row++) {
+        assert ((index[row] >= code->k) || (index[row] == row)); /* If the block whose number is i is present, then it is required to be in the i'th element. */
         if (index[row] >= code->k) {
             memset(outpkts[outix], 0, sz);
             for (col=0; col < code->k; col++)
diff --git a/zfec/zfec/fec.h b/zfec/zfec/fec.h
index 6252f54..67077d6 100644
--- a/zfec/zfec/fec.h
+++ b/zfec/zfec/fec.h
@@ -30,11 +30,12 @@ void fec_free(fec_t* p);
  * @param fecs buffers into which the secondary blocks will be written
  * @param block_nums the numbers of the desired check blocks (the id >= k) which fec_encode() will produce and store into the buffers of the fecs parameter
  * @param num_block_nums the length of the block_nums array
+ * @param sz size of a packet in bytes
  */
 void fec_encode(const fec_t* code, const gf*restrict const*restrict const src, gf*restrict const*restrict const fecs, const unsigned*restrict const block_nums, size_t num_block_nums, size_t sz);
 
 /**
- * @param inpkts an array of packets (size k)
+ * @param inpkts an array of packets (size k); If the block whose number is i is present, then it is required to be in the i'th element.
  * @param outpkts an array of buffers into which the reconstructed output packets will be written (only packets which are not present in the inpkts input will be reconstructed and written to outpkts)
  * @param index an array of the blocknums of the packets in inpkts
  * @param sz size of a packet in bytes
diff --git a/zfec/zfec/test/test_zfec.py b/zfec/zfec/test/test_zfec.py
index fbb630e..3e51a78 100755
--- a/zfec/zfec/test/test_zfec.py
+++ b/zfec/zfec/test/test_zfec.py
@@ -84,12 +84,19 @@ class ZFecTest(unittest.TestCase):
             
     def test_from_agl_py(self):
         e = zfec.Encoder(3, 5)
-        b1 = '\x01'*8 ; b2 = '\x02'*8 ; b3 = '\x03'*8
-        b4, b5 = e.encode([b1, b2, b3], (3, 4))
+        b0 = '\x01'*8 ; b1 = '\x02'*8 ; b2 = '\x03'*8
+        # print "_from_py before encoding:"
+        # print "b0: %s, b1: %s, b2: %s" % tuple(base64.b16encode(x) for x in [b0, b1, b2])
+
+        b3, b4 = e.encode([b0, b1, b2], (3, 4))
+        # print "after encoding:"
+        # print "b3: %s, b4: %s" % tuple(base64.b16encode(x) for x in [b3, b4])
+
         d = zfec.Decoder(3, 5)
-        resblocks = d.decode((b3, b4, b5), (2, 3, 4))
+        r0, r1, r2 = d.decode((b2, b3, b4), (1, 2, 3))
         
-        print "b1: %r, b2: %r, b3: %r, b4: %r, b5: %r, resblocks: %r" % (b1, b2, b3, b4, b5, resblocks)
+        # print "after decoding:"
+        # print "b0: %s, b1: %s" % tuple(base64.b16encode(x) for x in [b0, b1])
 
     def test_small(self):
         for i in range(16):
-- 
2.45.2