]> git.rkrishnan.org Git - tahoe-lafs/zfec.git/blobdiff - zfec/zfec/test/test_zfec.py
tests: cleaner tests by writing "self.fail(why)" instead of "raise why"
[tahoe-lafs/zfec.git] / zfec / zfec / test / test_zfec.py
index 1143cfc580604db9fca86749924761283083c964..5b40d0a90b6711dbbdf41a504a7ab2267add5dec 100755 (executable)
@@ -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):
@@ -103,6 +110,43 @@ class ZFecTest(unittest.TestCase):
         if VERBOSE:
             print "%d randomized tests pass." % (i+1)
 
+    def test_bad_args_construct_decoder(self):
+        try:
+            zfec.Decoder(-1, -1)
+        except zfec.Error, e:
+            assert "argument is required to be greater than or equal to 1" in str(e), e
+        else:
+            self.fail("Should have gotten an exception from out-of-range arguments.")
+
+        try:
+            zfec.Decoder(1, 257)
+        except zfec.Error, e:
+            assert "argument is required to be less than or equal to 256" in str(e), e
+        else:
+            self.fail("Should have gotten an exception from out-of-range arguments.")
+
+        try:
+            zfec.Decoder(3, 2)
+        except zfec.Error, e:
+            assert "first argument is required to be less than or equal to the second argument" in str(e), e
+        else:
+            self.fail("Should have gotten an exception from out-of-range arguments.")
+
+    def test_bad_args_construct_encoder(self):
+        try:
+            zfec.Encoder(-1, -1)
+        except zfec.Error, e:
+            assert "argument is required to be greater than or equal to 1" in str(e), e
+        else:
+            self.fail("Should have gotten an exception from out-of-range arguments.")
+
+        try:
+            zfec.Encoder(1, 257)
+        except zfec.Error, e:
+            assert "argument is required to be less than or equal to 256" in str(e), e
+        else:
+            self.fail("Should have gotten an exception from out-of-range arguments.")
+
     def test_bad_args_dec(self):
         decer = zfec.Decoder(2, 4)
 
@@ -111,21 +155,21 @@ class ZFecTest(unittest.TestCase):
         except TypeError, e:
             assert "First argument was not a sequence" in str(e), e
         else:
-            raise "Should have gotten TypeError for wrong type of second argument."
+            self.fail("Should have gotten TypeError for wrong type of second argument.")
 
         try:
             decer.decode(["a", "b", ], ["c", "d",])
         except zfec.Error, e:
             assert "Precondition violation: second argument is required to contain int" in str(e), e
         else:
-            raise "Should have gotten zfec.Error for wrong type of second argument."
+            self.fail("Should have gotten zfec.Error for wrong type of second argument.")
 
         try:
             decer.decode(["a", "b", ], 98) # not a sequence at all
         except TypeError, e:
             assert "Second argument was not a sequence" in str(e), e
         else:
-            raise "Should have gotten TypeError for wrong type of second argument."
+            self.fail("Should have gotten TypeError for wrong type of second argument.")
 
 class EasyFecTest(unittest.TestCase):
     def test_small(self):
@@ -148,21 +192,21 @@ class EasyFecTest(unittest.TestCase):
         except TypeError, e:
             assert "First argument was not a sequence" in str(e), e
         else:
-            raise "Should have gotten TypeError for wrong type of second argument."
+            self.fail("Should have gotten TypeError for wrong type of second argument.")
 
         try:
             decer.decode("ab", ["c", "d",], 0)
         except zfec.Error, e:
             assert "Precondition violation: second argument is required to contain int" in str(e), e
         else:
-            raise "Should have gotten zfec.Error for wrong type of second argument."
+            self.fail("Should have gotten zfec.Error for wrong type of second argument.")
 
         try:
             decer.decode("ab", 98, 0) # not a sequence at all
         except TypeError, e:
             assert "Second argument was not a sequence" in str(e), e
         else:
-            raise "Should have gotten TypeError for wrong type of second argument."
+            self.fail("Should have gotten TypeError for wrong type of second argument.")
 
 class FileFec(unittest.TestCase):
     def test_filefec_header(self):
@@ -294,12 +338,3 @@ class Cmdline(unittest.TestCase):
 
 if __name__ == "__main__":
     unittest.main()
-
-# zfec -- fast forward error correction library with Python interface
-# 
-# Copyright (C) 2007 Allmydata, Inc.
-# Author: Zooko Wilcox-O'Hearn
-# 
-# This file is part of zfec.
-#
-# See README.txt for licensing information.