from sha import sha
from os import urandom
+from twisted.trial import unittest
+
def intify(hstr):
"""20 bit hash, big-endian -> long python integer"""
assert len(hstr) == 20
def newTID():
return randRange(-2**30, 2**30)
-### Test Cases ###
-import unittest
-
-class NewID(unittest.TestCase):
+class TestNewID(unittest.TestCase):
def testLength(self):
- self.assertEqual(len(newID()), 20)
+ self.failUnlessEqual(len(newID()), 20)
def testHundreds(self):
for x in xrange(100):
self.testLength
-class Intify(unittest.TestCase):
+class TestIntify(unittest.TestCase):
known = [('\0' * 20, 0),
('\xff' * 20, 2L**160 - 1),
]
def testKnown(self):
for str, value in self.known:
- self.assertEqual(intify(str), value)
+ self.failUnlessEqual(intify(str), value)
def testEndianessOnce(self):
h = newID()
while h[-1] == '\xff':
h = newID()
k = h[:-1] + chr(ord(h[-1]) + 1)
- self.assertEqual(intify(k) - intify(h), 1)
+ self.failUnlessEqual(intify(k) - intify(h), 1)
def testEndianessLots(self):
for x in xrange(100):
self.testEndianessOnce()
-class Disantance(unittest.TestCase):
+class TestDisantance(unittest.TestCase):
known = [
(("\0" * 20, "\xff" * 20), 2**160L -1),
((sha("foo").digest(), sha("foo").digest()), 0),
]
def testKnown(self):
for pair, dist in self.known:
- self.assertEqual(distance(pair[0], pair[1]), dist)
+ self.failUnlessEqual(distance(pair[0], pair[1]), dist)
def testCommutitive(self):
for i in xrange(100):
x, y, z = newID(), newID(), newID()
- self.assertEqual(distance(x,y) ^ distance(y, z), distance(x, z))
+ self.failUnlessEqual(distance(x,y) ^ distance(y, z), distance(x, z))
-class RandRange(unittest.TestCase):
+class TestRandRange(unittest.TestCase):
def testOnce(self):
a = intify(newID())
b = intify(newID())
if a < b:
c = randRange(a, b)
- self.assertEqual(a <= c < b, 1, "output out of range %d %d %d" % (b, c, a))
+ self.failUnlessEqual(a <= c < b, True, "output out of range %d %d %d" % (b, c, a))
else:
c = randRange(b, a)
- assert b <= c < a, "output out of range %d %d %d" % (b, c, a)
+ self.failUnlessEqual(b <= c < a, True, "output out of range %d %d %d" % (b, c, a))
def testOneHundredTimes(self):
for i in xrange(100):
self.testOnce()
-
-
-
-if __name__ == '__main__':
- unittest.main()
-
-