From d4008add302ceb0e7a67a0ac91e5d88f069d7819 Mon Sep 17 00:00:00 2001
From: zooko <zooko@zooko.com>
Date: Fri, 18 Jan 2008 20:18:44 +0530
Subject: [PATCH] tests: add test suggested by agl's bug report

darcs-hash:d0f2e6069fe17e2c115c76511d9f8de0989c3685
---
 zfec/zfec/_fecmodule.c      | 42 +++++++++++++++++++++++++++++++++++--
 zfec/zfec/test/test_zfec.py | 12 +++++++++++
 2 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/zfec/zfec/_fecmodule.c b/zfec/zfec/_fecmodule.c
index 63b07cb..f0d8d6d 100644
--- a/zfec/zfec/_fecmodule.c
+++ b/zfec/zfec/_fecmodule.c
@@ -547,7 +547,45 @@ static PyTypeObject Decoder_type = {
     Decoder_new,                 /* tp_new */
 };
 
-static PyMethodDef fec_methods[] = { 
+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 block_nums[] = {3, 4};
+
+  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);
+
+  memcpy(b1c, b1, 8); memcpy(b2c, b2, 8);
+
+  const unsigned char *inpkts[] = {b3, b4, b5};
+  unsigned char *outpkts[] = {b1, b2};
+  unsigned indexes[] = {2, 3, 4};
+
+  fec_decode(fec, inpkts, outpkts, indexes, 8);
+
+  write(1, b1, 8);
+  write(1, b2, 8);
+
+  if ((memcmp(b1, b1c,8) == 0) && (memcmp(b2, b2c,8) == 0))
+    Py_RETURN_TRUE;
+  else
+    Py_RETURN_FALSE;
+}
+
+static PyMethodDef fec_functions[] = { 
+  {"test_from_agl", test_from_agl, METH_NOARGS, NULL},
     {NULL} 
 };
 
@@ -564,7 +602,7 @@ init_fec(void) {
     if (PyType_Ready(&Decoder_type) < 0)
         return;
 
-    module = Py_InitModule3("_fec", fec_methods, fec__doc__);
+    module = Py_InitModule3("_fec", fec_functions, fec__doc__);
     if (module == NULL)
       return;
 
diff --git a/zfec/zfec/test/test_zfec.py b/zfec/zfec/test/test_zfec.py
index 130ef6f..1143cfc 100755
--- a/zfec/zfec/test/test_zfec.py
+++ b/zfec/zfec/test/test_zfec.py
@@ -79,6 +79,18 @@ def _help_test_random_with_l_easy(l):
     _h_easy(k, m, s)
     
 class ZFecTest(unittest.TestCase):
+    def test_from_agl_c(self):
+        self.failUnless(zfec._fec.test_from_agl())
+            
+    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))
+        d = zfec.Decoder(3, 5)
+        resblocks = d.decode((b3, b4, b5), (2, 3, 4))
+        
+        print "b1: %r, b2: %r, b3: %r, b4: %r, b5: %r, resblocks: %r" % (b1, b2, b3, b4, b5, resblocks)
+
     def test_small(self):
         for i in range(16):
             _help_test_random_with_l(i)
-- 
2.45.2