Commit 3792faa5 authored by Chris McDonough's avatar Chris McDonough

Set and bucket tests checked in.

parent 175b5a5a
...@@ -91,18 +91,78 @@ except: ...@@ -91,18 +91,78 @@ except:
sys.path.insert(0, '../..') sys.path.insert(0, '../..')
import ZODB import ZODB
from BTrees.OOBTree import OOBTree from BTrees.OOBTree import OOBTree, OOBucket, OOSet
from BTrees.IOBTree import IOBTree from BTrees.IOBTree import IOBTree, IOBucket, IOSet
from BTrees.IIBTree import IIBTree from BTrees.IIBTree import IIBTree, IIBucket, IISet
from BTrees.OIBTree import OIBTree from BTrees.OIBTree import OIBTree, OIBucket, OISet
from unittest import TestCase, TestSuite, TextTestRunner, makeSuite from unittest import TestCase, TestSuite, TextTestRunner, makeSuite
class Base: class Base:
""" Tests common to all types: sets, buckets, and BTrees """
def tearDown(self): def tearDown(self):
self.t = None self.t = None
del self.t del self.t
def _getRoot(self):
from ZODB.FileStorage import FileStorage
from ZODB.DB import DB
n = 'fs_tmp__%s' % os.getpid()
s = FileStorage(n)
db = DB(s)
root = db.open().root()
return root
def _closeDB(self, root):
root._p_jar._db.close()
root = None
def _delDB(self):
os.system('rm fs_tmp__*')
def testLoadAndStore(self):
t = self.t
try:
root = self._getRoot()
root['t'] = t
get_transaction().commit()
except:
self._closeDB(root)
self._delDB()
raise
self._closeDB(root)
try:
root = self._getRoot()
assert root['t'] == t
finally:
self._closeDB(root)
self._delDB()
def testGhostUnghost(self):
t = self.t
try:
root = self._getRoot()
root['t'] = t
get_transaction().commit()
except:
self._closeDB(root)
self._delDB()
raise
self._closeDB(root)
try:
root = self._getRoot()
root['t']._p_changed = None
get_transaction().commit()
assert root['t'] == t
finally:
self._closeDB(root)
self._delDB()
class MappingBase(Base):
""" Tests common to mappings (buckets, btrees) """
def testGetItemFails(self): def testGetItemFails(self):
self.assertRaises(KeyError, self._getitemfail) self.assertRaises(KeyError, self._getitemfail)
...@@ -124,6 +184,16 @@ class Base: ...@@ -124,6 +184,16 @@ class Base:
self.t[1] = 2 self.t[1] = 2
assert self.t[1] == 2, self.t[1] assert self.t[1] == 2, self.t[1]
def testLen(self):
added = {}
r = range(1000)
for x in r:
k = whrandom.choice(r)
self.t[k] = x
added[k] = x
addl = added.keys()
assert len(self.t) == len(addl), len(self.t)
def testHasKeyWorks(self): def testHasKeyWorks(self):
self.t[1] = 1 self.t[1] = 1
assert self.t.has_key(1) assert self.t.has_key(1)
...@@ -165,7 +235,126 @@ class Base: ...@@ -165,7 +235,126 @@ class Base:
def _deletefail(self): def _deletefail(self):
del self.t[1] del self.t[1]
def donttestDeleteNoChildrenWorks(self): def testMaxKeyMinKey(self):
self.t[7] = 6
self.t[3] = 10
self.t[8] = 12
self.t[1] = 100
self.t[5] = 200
self.t[10] = 500
self.t[6] = 99
self.t[4] = 150
del self.t[7]
t = self.t
assert t.maxKey() == 10
assert t.maxKey(6) == 6
assert t.maxKey(9) == 8
assert t.minKey() == 1
assert t.minKey(3) == 3
assert t.minKey(9) == 10
def testClear(self):
r = range(100)
for x in r:
rnd = whrandom.choice(r)
self.t[rnd] = 0
self.t.clear()
diff = lsubtract(list(self.t.keys()), [])
assert diff == [], diff
class SetTests(Base):
""" Test common to all set types """
def testInsertReturnsValue(self):
t = self.t
assert t.insert(5) == 1
def testDuplicateInsert(self):
t = self.t
t.insert(5)
assert t.insert(5) == 0
def testInsert(self):
t = self.t
t.insert(1)
assert t.has_key(1)
def testBigInsert(self):
t = self.t
r = xrange(10000)
for x in r:
t.insert(x)
for x in r:
assert t.has_key(x)
def testRemoveSucceeds(self):
t = self.t
r = xrange(10000)
for x in r: t.insert(x)
for x in r: t.remove(x)
def testRemoveFails(self):
self.assertRaises(KeyError, self._removenonexistent)
def _removenonexistent(self):
self.t.remove(1)
def testHasKeyFails(self):
t = self.t
assert not t.has_key(1)
def testItems(self):
t = self.t
t.insert(1)
t.insert(3)
t.insert(5)
assert lsubtract(t.items(), [1,3,5]) == [], t.items()
def testKeys(self):
t = self.t
r = xrange(1000)
for x in r: t.insert(x)
diff = lsubtract(t.keys(), r)
assert diff == [], diff
def testClear(self):
t = self.t
r = xrange(1000)
for x in r: t.insert(x)
t.clear()
diff = lsubtract(t.keys(), [])
assert diff == [], diff
def testMaxKeyMinKey(self):
t = self.t
t.insert(1)
t.insert(2)
t.insert(3)
t.insert(8)
t.insert(5)
t.insert(10)
t.insert(6)
t.insert(4)
assert t.maxKey() == 10
assert t.maxKey(6) == 6
assert t.maxKey(9) == 8
assert t.minKey() == 1
assert t.minKey(3) == 3
assert t.minKey(9) == 10
def testLen(self):
# should sets know their length?
t = self.t
r = xrange(10000)
for x in r: t.insert(x)
assert len(t) == 1000, len(t)
class BucketTests(MappingBase):
""" Tests common to all buckets """
pass
class BTreeTests(MappingBase):
""" Tests common to all BTrees """
def testDeleteNoChildrenWorks(self):
self.t[5] = 6 self.t[5] = 6
self.t[2] = 10 self.t[2] = 10
self.t[6] = 12 self.t[6] = 12
...@@ -177,7 +366,7 @@ class Base: ...@@ -177,7 +366,7 @@ class Base:
diff = lsubtract(self.t.keys(), [1,2,3,5,6,10]) diff = lsubtract(self.t.keys(), [1,2,3,5,6,10])
assert diff == [], diff assert diff == [], diff
def donttestDeleteOneChildWorks(self): def testDeleteOneChildWorks(self):
self.t[5] = 6 self.t[5] = 6
self.t[2] = 10 self.t[2] = 10
self.t[6] = 12 self.t[6] = 12
...@@ -189,7 +378,7 @@ class Base: ...@@ -189,7 +378,7 @@ class Base:
diff = lsubtract(self.t.keys(), [1,2,4,5,6,10]) diff = lsubtract(self.t.keys(), [1,2,4,5,6,10])
assert diff == [], diff assert diff == [], diff
def donttestDeleteTwoChildrenNoInorderSuccessorWorks(self): def testDeleteTwoChildrenNoInorderSuccessorWorks(self):
self.t[5] = 6 self.t[5] = 6
self.t[2] = 10 self.t[2] = 10
self.t[6] = 12 self.t[6] = 12
...@@ -201,7 +390,7 @@ class Base: ...@@ -201,7 +390,7 @@ class Base:
diff = lsubtract(self.t.keys(), [1,3,4,5,6,10]) diff = lsubtract(self.t.keys(), [1,3,4,5,6,10])
assert diff == [], diff assert diff == [], diff
def donttestDeleteTwoChildrenInorderSuccessorWorks(self): def testDeleteTwoChildrenInorderSuccessorWorks(self):
""" 7, 3, 8, 1, 5, 10, 6, 4 -- del 3 """ """ 7, 3, 8, 1, 5, 10, 6, 4 -- del 3 """
self.t[7] = 6 self.t[7] = 6
self.t[3] = 10 self.t[3] = 10
...@@ -215,7 +404,7 @@ class Base: ...@@ -215,7 +404,7 @@ class Base:
diff = lsubtract(self.t.keys(), [1,4,5,6,7,8,10]) diff = lsubtract(self.t.keys(), [1,4,5,6,7,8,10])
assert diff == [], diff assert diff == [], diff
def donttestDeleteRootWorks(self): def testDeleteRootWorks(self):
""" 7, 3, 8, 1, 5, 10, 6, 4 -- del 7 """ """ 7, 3, 8, 1, 5, 10, 6, 4 -- del 7 """
self.t[7] = 6 self.t[7] = 6
self.t[3] = 10 self.t[3] = 10
...@@ -254,17 +443,7 @@ class Base: ...@@ -254,17 +443,7 @@ class Base:
diff = lsubtract(self.t.keys(), addl) diff = lsubtract(self.t.keys(), addl)
assert diff == [], diff assert diff == [], diff
def testLen(self): def testRandomDeletes(self):
added = {}
r = range(1000)
for x in r:
k = whrandom.choice(r)
self.t[k] = x
added[k] = x
addl = added.keys()
assert len(self.t) == len(addl), len(self.t)
def donttestRandomDeletes(self):
r = range(1000) r = range(1000)
added = [] added = []
for x in r: for x in r:
...@@ -285,7 +464,7 @@ class Base: ...@@ -285,7 +464,7 @@ class Base:
badones.append(x) badones.append(x)
assert badones == [], (badones, added, deleted) assert badones == [], (badones, added, deleted)
def donttestTargetedDeletes(self): def testTargetedDeletes(self):
r = range(1000) r = range(1000)
for x in r: for x in r:
k = whrandom.choice(r) k = whrandom.choice(r)
...@@ -296,9 +475,8 @@ class Base: ...@@ -296,9 +475,8 @@ class Base:
except KeyError: except KeyError:
pass pass
assert realseq(self.t.keys()) == [], realseq(self.t.keys()) assert realseq(self.t.keys()) == [], realseq(self.t.keys())
# this fails 1/22/2001 by segfaulting
def donttestPathologicalRightBranching(self): def testPathologicalRightBranching(self):
r = range(1000) r = range(1000)
for x in r: for x in r:
self.t[x] = 1 self.t[x] = 1
...@@ -307,7 +485,7 @@ class Base: ...@@ -307,7 +485,7 @@ class Base:
del self.t[x] del self.t[x]
assert realseq(self.t.keys()) == [], realseq(self.t.keys()) assert realseq(self.t.keys()) == [], realseq(self.t.keys())
def donttestPathologicalLeftBranching(self): def testPathologicalLeftBranching(self):
r = range(1000) r = range(1000)
revr = r[:] revr = r[:]
revr.reverse() revr.reverse()
...@@ -319,7 +497,7 @@ class Base: ...@@ -319,7 +497,7 @@ class Base:
del self.t[x] del self.t[x]
assert realseq(self.t.keys()) == [], realseq(self.t.keys()) assert realseq(self.t.keys()) == [], realseq(self.t.keys())
def donttestSuccessorChildParentRewriteExerciseCase(self): def testSuccessorChildParentRewriteExerciseCase(self):
add_order = [ add_order = [
85, 73, 165, 273, 215, 142, 233, 67, 86, 166, 235, 225, 255, 85, 73, 165, 273, 215, 142, 233, 67, 86, 166, 235, 225, 255,
73, 175, 171, 285, 162, 108, 28, 283, 258, 232, 199, 260, 73, 175, 171, 285, 162, 108, 28, 283, 258, 232, 199, 260,
...@@ -365,33 +543,6 @@ class Base: ...@@ -365,33 +543,6 @@ class Base:
except KeyError: except KeyError:
if self.t.has_key(x): assert 1==2,"failed to delete %s" % x if self.t.has_key(x): assert 1==2,"failed to delete %s" % x
def testMaxKeyMinKey(self):
self.t[7] = 6
self.t[3] = 10
self.t[8] = 12
self.t[1] = 100
self.t[5] = 200
self.t[10] = 500
self.t[6] = 99
self.t[4] = 150
del self.t[7]
t = self.t
assert t.maxKey() == 10
assert t.maxKey(6) == 6
assert t.maxKey(9) == 8
assert t.minKey() == 1
assert t.minKey(3) == 3
assert t.minKey(9) == 10
def testClear(self):
r = range(100)
for x in r:
rnd = whrandom.choice(r)
self.t[rnd] = 0
self.t.clear()
diff = lsubtract(list(self.t.keys()), [])
assert diff == [], diff
def testRangeSearchAfterSequentialInsert(self): def testRangeSearchAfterSequentialInsert(self):
r = range(100) r = range(100)
for x in r: for x in r:
...@@ -416,65 +567,10 @@ class Base: ...@@ -416,65 +567,10 @@ class Base:
assert t.insert(1, 1) == 1 assert t.insert(1, 1) == 1
assert lsubtract(list(t.keys()), [0,1]) == [] assert lsubtract(list(t.keys()), [0,1]) == []
def _getRoot(self):
from ZODB.FileStorage import FileStorage
from ZODB.DB import DB
n = 'fs_tmp__%s' % os.getpid()
s = FileStorage(n)
db = DB(s)
root = db.open().root()
return root
def _closeDB(self, root):
root._p_jar._db.close()
root = None
def _delDB(self):
os.system('rm fs_tmp__*')
def testLoadAndStore(self):
t = self.t
try:
root = self._getRoot()
root['t'] = t
get_transaction().commit()
except:
self._closeDB(root)
self._delDB()
raise
self._closeDB(root)
try: ## BTree tests
root = self._getRoot()
assert root['t'] == t
finally:
self._closeDB(root)
self._delDB()
def testGhostUnghost(self):
t = self.t
try:
root = self._getRoot()
root['t'] = t
get_transaction().commit()
except:
self._closeDB(root)
self._delDB()
raise
self._closeDB(root)
try: class TestIOBTrees(BTreeTests, TestCase):
root = self._getRoot()
root['t']._p_changed = None
get_transaction().commit()
assert root['t'] == t
finally:
self._closeDB(root)
self._delDB()
class TestIOBTrees(Base, TestCase):
def setUp(self): def setUp(self):
self.t = IOBTree() self.t = IOBTree()
...@@ -488,11 +584,15 @@ class TestIOBTrees(Base, TestCase): ...@@ -488,11 +584,15 @@ class TestIOBTrees(Base, TestCase):
def _floatraises(self): def _floatraises(self):
self.t[2.5] = 1 self.t[2.5] = 1
class TestOOBTrees(Base, TestCase): class TestOOBTrees(BTreeTests, TestCase):
def setUp(self): def setUp(self):
self.t = OOBTree() self.t = OOBTree()
class TestIIBTrees(Base, TestCase): class TestOIBTrees(BTreeTests, TestCase):
def setUp(self):
self.t = OIBTree()
class TestIIBTrees(BTreeTests, TestCase):
def setUp(self): def setUp(self):
self.t = IIBTree() self.t = IIBTree()
...@@ -506,9 +606,65 @@ class TestIIBTrees(Base, TestCase): ...@@ -506,9 +606,65 @@ class TestIIBTrees(Base, TestCase):
def _floatraises(self): def _floatraises(self):
self.t[2.5] = 1 self.t[2.5] = 1
class TestOIBTrees(Base, TestCase): ## Set tests
class TestIOSets(SetTests, TestCase):
def setUp(self): def setUp(self):
self.t = OIBTree() self.t = IOSet()
class TestOOSets(SetTests, TestCase):
def setUp(self):
self.t = OOSet()
class TestIISets(SetTests, TestCase):
def setUp(self):
self.t = IISet()
class TestOISets(SetTests, TestCase):
def setUp(self):
self.t = OISet()
## Bucket tests
class TestIOBuckets(BucketTests, TestCase):
def setUp(self):
self.t = IOBucket()
class TestOOBuckets(BucketTests, TestCase):
def setUp(self):
self.t = OOBucket()
class TestIIBuckets(BucketTests, TestCase):
def setUp(self):
self.t = IIBucket()
class TestOIBuckets(BucketTests, TestCase):
def setUp(self):
self.t = OIBucket()
def main():
TIOBTree = makeSuite(TestIOBTrees, 'test')
TOOBTree = makeSuite(TestOOBTrees, 'test')
TOIBTree = makeSuite(TestOIBTrees, 'test')
TIIBTree = makeSuite(TestIIBTrees, 'test')
TIOSet = makeSuite(TestIOSets, 'test')
TOOSet = makeSuite(TestOOSets, 'test')
TOISet = makeSuite(TestIOSets, 'test')
TIISet = makeSuite(TestOOSets, 'test')
TIOBucket = makeSuite(TestIOBuckets, 'test')
TOOBucket = makeSuite(TestOOBuckets, 'test')
TOIBucket = makeSuite(TestOIBuckets, 'test')
TIIBucket = makeSuite(TestIIBuckets, 'test')
alltests = TestSuite((TIOSet, TOOSet, TOISet, TIISet,
TIOBucket, TOOBucket, TOIBucket, TIIBucket,
TOOBTree, TIOBTree, TOIBTree, TIIBTree))
runner = TextTestRunner()
runner.run(alltests)
## utility functions
def lsubtract(l1, l2): def lsubtract(l1, l2):
l = filter(lambda x, l1=l1: x not in l1, l2) l = filter(lambda x, l1=l1: x not in l1, l2)
...@@ -518,14 +674,6 @@ def lsubtract(l1, l2): ...@@ -518,14 +674,6 @@ def lsubtract(l1, l2):
def realseq(itemsob): def realseq(itemsob):
return map(lambda x: x, itemsob) return map(lambda x: x, itemsob)
def main():
OOBTree = makeSuite(TestOOBTrees, 'test')
IOBTree = makeSuite(TestIOBTrees, 'test')
OIBTree = makeSuite(TestOIBTrees, 'test')
IIBTree = makeSuite(TestIIBTrees, 'test')
alltests = TestSuite((OOBTree, IOBTree, OIBTree, IIBTree))
runner = TextTestRunner()
runner.run(alltests)
if __name__ == '__main__': main() if __name__ == '__main__': main()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment