moved bitcoin.sha256 tests
authorForrest Voight <forrest@forre.st>
Mon, 5 Dec 2011 09:36:47 +0000 (04:36 -0500)
committerForrest Voight <forrest@forre.st>
Mon, 5 Dec 2011 09:36:47 +0000 (04:36 -0500)
p2pool/bitcoin/sha256.py
p2pool/test/bitcoin/test_sha256.py [new file with mode: 0644]

index 3ce773c..76de752 100644 (file)
@@ -87,39 +87,3 @@ class sha256(object):
     
     def hexdigest(self):
         return self.digest().encode('hex')
-
-if __name__ == '__main__':
-    import hashlib
-    import random
-    for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
-        print test
-        print sha256(test).hexdigest()
-        print hashlib.sha256(test).hexdigest()
-        print
-    def random_str(l):
-        return ''.join(chr(random.randrange(256)) for i in xrange(l))
-    for length in xrange(1500):
-        test = random_str(length)
-        a = sha256(test).hexdigest()
-        b = hashlib.sha256(test).hexdigest()
-        print length, a, b
-        if a != b:
-            print 'ERROR!'
-            raise ValueError()
-    while True:
-        test = random_str(int(random.expovariate(1/1000)))
-        test2 = random_str(int(random.expovariate(1/1000)))
-        
-        a = sha256(test)
-        a = a.copy()
-        a.update(test2)
-        a = a.hexdigest()
-        
-        b = hashlib.sha256(test)
-        b = b.copy()
-        b.update(test2)
-        b = b.hexdigest()
-        print a, b
-        if a != b:
-            print 'ERROR!'
-            raise ValueError()
diff --git a/p2pool/test/bitcoin/test_sha256.py b/p2pool/test/bitcoin/test_sha256.py
new file mode 100644 (file)
index 0000000..16d3238
--- /dev/null
@@ -0,0 +1,43 @@
+from __future__ import division
+
+import unittest
+import hashlib
+import random
+
+from p2pool.bitcoin import sha256
+
+class Test(unittest.TestCase):
+    def test_all(self):
+        for test in ['', 'a', 'b', 'abc', 'abc'*50, 'hello world']:
+            #print test
+            #print sha256.sha256(test).hexdigest()
+            #print hashlib.sha256(test).hexdigest()
+            #print
+            assert sha256.sha256(test).hexdigest() == hashlib.sha256(test).hexdigest()
+        def random_str(l):
+            return ''.join(chr(random.randrange(256)) for i in xrange(l))
+        for length in xrange(150):
+            test = random_str(length)
+            a = sha256.sha256(test).hexdigest()
+            b = hashlib.sha256(test).hexdigest()
+            #print length, a, b
+            if a != b:
+                print 'ERROR!'
+                raise ValueError()
+        for i in xrange(100):
+            test = random_str(int(random.expovariate(1/100)))
+            test2 = random_str(int(random.expovariate(1/100)))
+
+            a = sha256.sha256(test)
+            a = a.copy()
+            a.update(test2)
+            a = a.hexdigest()
+
+            b = hashlib.sha256(test)
+            b = b.copy()
+            b.update(test2)
+            b = b.hexdigest()
+            #print a, b
+            if a != b:
+                print 'ERROR!'
+                raise ValueError()