moved forest tests to test/ and fixed them up
authorForrest Voight <forrest@forre.st>
Mon, 5 Dec 2011 09:26:10 +0000 (04:26 -0500)
committerForrest Voight <forrest@forre.st>
Mon, 5 Dec 2011 09:26:10 +0000 (04:26 -0500)
p2pool/test/util/test_forest.py [new file with mode: 0644]
p2pool/util/forest.py

diff --git a/p2pool/test/util/test_forest.py b/p2pool/test/util/test_forest.py
new file mode 100644 (file)
index 0000000..e777468
--- /dev/null
@@ -0,0 +1,63 @@
+import random
+import unittest
+
+from p2pool.util import forest
+
+class FakeShare(object):
+    def __init__(self, **kwargs):
+        self.__dict__.update(kwargs)
+
+class Test(unittest.TestCase):
+    def test_distanceskiplist(self):
+        t = forest.Tracker()
+        d = forest.DistanceSkipList(t)
+        for i in xrange(2000):
+            t.add(FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None))
+        for i in xrange(2000):
+            a = random.randrange(2000)
+            b = random.randrange(a + 1)
+            res = d(a, b)
+            assert res == a - b, (a, b, res)
+    
+    def test_tracker(self):
+        t = forest.Tracker()
+
+        for i in xrange(10000):
+            t.add(FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None))
+
+        #t.remove(99)
+
+        assert t.heads == {9999: None}
+        assert t.tails == {None: set([9999])}
+
+        #for share_hash, share in sorted(t.shares.iteritems()):
+        #    print share_hash, share.previous_hash, t.heads.get(share_hash), t.tails.get(share_hash)
+
+        assert t.get_nth_parent_hash(9000, 5000) == 9000 - 5000
+        assert t.get_nth_parent_hash(9001, 412) == 9001 - 412
+        #print t.get_nth_parent_hash(90, 51)
+
+        for ii in xrange(5):
+            t = forest.Tracker()
+            for i in xrange(random.randrange(300)):
+                x = random.choice(list(t.shares) + [None])
+                #print i, '->', x
+                t.add(FakeShare(hash=i, previous_hash=x, target=5))
+            while t.shares:
+                x = random.choice(list(t.shares))
+                #print 'DEL', x, t.__dict__
+                try:
+                    t.remove(x)
+                except NotImplementedError:
+                    pass # print 'aborted; not implemented'
+            #print 'HEADS', t.heads
+            #print 'TAILS', t.tails
+
+        #for share_hash in sorted(t.shares):
+        #    print str(share_hash).rjust(4),
+        #    x = t.skips.get(share_hash, None)
+        #    if x is not None:
+        #        print str(x[0]).rjust(4),
+        #        for a in x[1]:
+        #            print str(a).rjust(10),
+        #    print
index 8682342..8234f02 100644 (file)
@@ -39,18 +39,6 @@ class DistanceSkipList(skiplist.SkipList):
     def finalize(self, (dist, hash)):
         return hash
 
-if __name__ == '__main__':
-    import random
-    from p2pool.bitcoin import data
-    t = data.Tracker()
-    d = DistanceSkipList(t)
-    for i in xrange(2000):
-        t.add(data.FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None))
-    for i in xrange(2000):
-        a = random.randrange(2000)
-        b = random.randrange(a + 1)
-        res = d(a, b)
-        assert res == a - b, (a, b, res)
 
 # linked list tracker
 
@@ -296,60 +284,3 @@ class Tracker(object):
             return None # not connected, so can't be determined
         height_up = child_height - height
         return height_up >= 0 and self.get_nth_parent_hash(possible_child_hash, height_up) == share_hash
-
-class FakeShare(object):
-    def __init__(self, **kwargs):
-        self.__dict__.update(kwargs)
-
-if __name__ == '__main__':
-    
-    t = Tracker()
-    
-    for i in xrange(10000):
-        t.add(FakeShare(hash=i, previous_hash=i - 1 if i > 0 else None))
-    
-    #t.remove(99)
-    
-    print 'HEADS', t.heads
-    print 'TAILS', t.tails
-    
-    import random
-    
-    while False:
-        print
-        print '-'*30
-        print
-        t = Tracker()
-        for i in xrange(random.randrange(100)):
-            x = random.choice(list(t.shares) + [None])
-            print i, '->', x
-            t.add(FakeShare(i, x))
-        while t.shares:
-            x = random.choice(list(t.shares))
-            print 'DEL', x, t.__dict__
-            try:
-                t.remove(x)
-            except NotImplementedError:
-                print 'aborted; not implemented'
-        import time
-        time.sleep(.1)
-        print 'HEADS', t.heads
-        print 'TAILS', t.tails
-    
-    #for share_hash, share in sorted(t.shares.iteritems()):
-    #    print share_hash, share.previous_hash, t.heads.get(share_hash), t.tails.get(share_hash)
-    
-    #import sys;sys.exit()
-    
-    print t.get_nth_parent_hash(9000, 5000)
-    print t.get_nth_parent_hash(9001, 412)
-    #print t.get_nth_parent_hash(90, 51)
-    
-    for share_hash in sorted(t.shares):
-        print str(share_hash).rjust(4),
-        x = t.skips.get(share_hash, None)
-        if x is not None:
-            print str(x[0]).rjust(4),
-            for a in x[1]:
-                print str(a).rjust(10),
-        print