testBTrees.py 24.9 KB
Newer Older
Chris McDonough's avatar
Chris McDonough committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
##############################################################################
# 
# Zope Public License (ZPL) Version 1.0
# -------------------------------------
# 
# Copyright (c) Digital Creations.  All rights reserved.
# 
# This license has been certified as Open Source(tm).
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 
# 1. Redistributions in source code must retain the above copyright
#    notice, this list of conditions, and the following disclaimer.
# 
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions, and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
# 
# 3. Digital Creations requests that attribution be given to Zope
#    in any manner possible. Zope includes a "Powered by Zope"
#    button that is installed by default. While it is not a license
#    violation to remove this button, it is requested that the
#    attribution remain. A significant investment has been put
#    into Zope, and this effort will continue if the Zope community
#    continues to grow. This is one way to assure that growth.
# 
# 4. All advertising materials and documentation mentioning
#    features derived from or use of this software must display
#    the following acknowledgement:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    In the event that the product being advertised includes an
#    intact Zope distribution (with copyright and license included)
#    then this clause is waived.
# 
# 5. Names associated with Zope or Digital Creations must not be used to
#    endorse or promote products derived from this software without
#    prior written permission from Digital Creations.
# 
# 6. Modified redistributions of any form whatsoever must retain
#    the following acknowledgment:
# 
#      "This product includes software developed by Digital Creations
#      for use in the Z Object Publishing Environment
#      (http://www.zope.org/)."
# 
#    Intact (re-)distributions of any official Zope release do not
#    require an external acknowledgement.
# 
# 7. Modifications are encouraged but must be packaged separately as
#    patches to official Zope releases.  Distributions that do not
#    clearly separate the patches from the original work must be clearly
#    labeled as unofficial distributions.  Modifications which do not
#    carry the name Zope may be packaged in any form, as long as they
#    conform to all of the clauses above.
# 
# 
# Disclaimer
# 
#   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
#   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
#   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
#   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#   SUCH DAMAGE.
# 
# 
# This software consists of contributions made by Digital Creations and
# many individuals on behalf of Digital Creations.  Specific
# attributions are listed in the accompanying credits file.
# 
##############################################################################
import sys, os, time, whrandom
86 87 88 89 90 91 92 93

try:
    sys.path.insert(0, '.')
    import ZODB
except:
    sys.path.insert(0, '../..')
    import ZODB

Chris McDonough's avatar
Chris McDonough committed
94 95 96 97
from BTrees.OOBTree import OOBTree, OOBucket, OOSet, OOTreeSet
from BTrees.IOBTree import IOBTree, IOBucket, IOSet, IOTreeSet
from BTrees.IIBTree import IIBTree, IIBucket, IISet, IITreeSet
from BTrees.OIBTree import OIBTree, OIBucket, OISet, OITreeSet
98 99 100
from unittest import TestCase, TestSuite, JUnitTextTestRunner, VerboseTextTestRunner, makeSuite

TextTestRunner = VerboseTextTestRunner
Chris McDonough's avatar
Chris McDonough committed
101

102
class Base:
103
    """ Tests common to all types: sets, buckets, and BTrees """
Chris McDonough's avatar
Chris McDonough committed
104 105 106
    def tearDown(self):
        self.t = None
        del self.t
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124

    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):
125 126 127 128 129 130 131 132 133 134 135 136
        for i in 0, 10, 1000:
            t = self.t.__class__()
            self._populate(t, i)
            try:
                root = self._getRoot()
                root[i] = t
                get_transaction().commit()
            except:
                self._closeDB(root)
                self._delDB()
                raise

137
            self._closeDB(root)
138 139 140 141 142 143 144 145 146 147 148

            try:
                root = self._getRoot()
                #XXX BTree stuff doesn't implement comparison
                if hasattr(t, 'items'):
                    assert list(root[i].items()) == list(t.items())
                else:
                    assert list(root[i].keys()) == list(t.keys())
            finally:
                self._closeDB(root)
                self._delDB()
149 150
            
    def testGhostUnghost(self):
151 152 153 154 155 156 157 158 159 160 161 162
        for i in 0, 10, 1000:
            t = self.t.__class__()
            self._populate(t, i)
            try:
                root = self._getRoot()
                root[i] = t
                get_transaction().commit()
            except:
                self._closeDB(root)
                self._delDB()
                raise

163
            self._closeDB(root)
164 165 166 167 168 169 170 171 172 173 174 175

            try:
                root = self._getRoot()
                root[i]._p_changed = None
                get_transaction().commit()
                if hasattr(t,'items'):
                    assert list(root[i].items()) == list(t.items())
                else:
                    assert list(root[i].keys()) == list(t.keys())
            finally:
                self._closeDB(root)
                self._delDB()
176 177 178

class MappingBase(Base):
    """ Tests common to mappings (buckets, btrees) """
179 180 181 182 183

    def _populate(self, t, l):
        # Make some data
        for i in range(l): t[i]=i
    
Chris McDonough's avatar
Chris McDonough committed
184 185 186 187 188 189 190 191 192
    def testGetItemFails(self):
        self.assertRaises(KeyError, self._getitemfail)

    def _getitemfail(self):
        return self.t[1]

    def testGetReturnsDefault(self):
        assert self.t.get(1) == None
        assert self.t.get(1, 'foo') == 'foo'
193
        
Chris McDonough's avatar
Chris McDonough committed
194 195 196 197 198 199 200 201 202 203 204
    def testSetItemGetItemWorks(self):
        self.t[1] = 1
        a = self.t[1]
        assert a == 1, `a`

    def testReplaceWorks(self):
        self.t[1] = 1
        assert self.t[1] == 1, self.t[1]
        self.t[1] = 2
        assert self.t[1] == 2, self.t[1]

205 206 207 208 209 210 211 212 213 214
    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)

Chris McDonough's avatar
Chris McDonough committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    def testHasKeyWorks(self):
        self.t[1] = 1
        assert self.t.has_key(1)

    def testValuesWorks(self):
        for x in range(100):
            self.t[x] = x
        v = self.t.values()
        i = 0
        for x in v:
            assert x == i, (x,i)
            i = i + 1
            
    def testKeysWorks(self):
        for x in range(100):
            self.t[x] = x
        v = self.t.keys()
        i = 0
        for x in v:
            assert x == i, (x,i)
            i = i + 1
236
        # BTree items must lie about their lengths, so we convert to list
237
        assert len(v) == 100, len(v)
238
        #assert len(v) == 100, len(v)
Chris McDonough's avatar
Chris McDonough committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

    def testItemsWorks(self):
        for x in range(100):
            self.t[x] = x
        v = self.t.items()
        i = 0
        for x in v:
            assert x[0] == i, (x[0], i)
            assert x[1] == i, (x[0], i)
            i = i + 1

    def testDeleteInvalidKeyRaisesKeyError(self):
        self.assertRaises(KeyError, self._deletefail)

    def _deletefail(self):
        del self.t[1]

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    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

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
    def testUpdate(self):
        "mapping update"
        d={}
        l=[]
        for i in range(10000):
            k=whrandom.randint(-2000, 2000)
            d[k]=i
            l.append((k, i))
            
        items=d.items()
        items.sort()

        self.t.update(d)
        assert list(self.t.items()) == items

        self.t.clear()
        assert list(self.t.items()) == []

        self.t.update(l)
        assert list(self.t.items()) == items

    def testEmptyRangeSearches(self):
        t=self.t
        t.update([(1,1),(5,5),(9,9)])
        assert list(t.keys(-6,-4))==[], list(t.keys(-6,-4))
        assert list(t.keys(2,4))==[], list(t.keys(2,4))
        assert list(t.keys(6,8))==[], list(t.keys(6,8))
        assert list(t.keys(10,12))==[], list(t.keys(10,12))
        

Chris McDonough's avatar
Chris McDonough committed
313
class NormalSetTests(Base):
314
    """ Test common to all set types """
315 316 317 318 319 320 321


    def _populate(self, t, l): 
        # Make some data
        t.update(range(l))


322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
    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 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

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
    def testUpdate(self):
        "mapping update"
        d={}
        l=[]
        for i in range(10000):
            k=whrandom.randint(-2000, 2000)
            d[k]=i
            l.append(k)
            
        items=d.keys()
        items.sort()

        self.t.update(l)
        assert list(self.t.keys()) == items

    def testEmptyRangeSearches(self):
        t=self.t
        t.update([1,5,9])
        assert list(t.keys(-6,-4))==[], list(t.keys(-6,-4))
        assert list(t.keys(2,4))==[], list(t.keys(2,4))
        assert list(t.keys(6,8))==[], list(t.keys(6,8))
        assert list(t.keys(10,12))==[], list(t.keys(10,12))

Chris McDonough's avatar
Chris McDonough committed
415
class ExtendedSetTests(NormalSetTests):
416 417 418 419
    def testLen(self):
        t = self.t
        r = xrange(10000)
        for x in r: t.insert(x)
420
        assert len(t) == 10000, len(t)
Chris McDonough's avatar
Chris McDonough committed
421 422 423 424 425 426 427

    def testGetItem(self):
        t = self.t
        r = xrange(10000)
        for x in r: t.insert(x)
        for x in r:
            assert t[x] == x
428 429 430 431 432 433 434 435
        
class BucketTests(MappingBase):
    """ Tests common to all buckets """
    pass

class BTreeTests(MappingBase):
    """ Tests common to all BTrees """
    def testDeleteNoChildrenWorks(self):
Chris McDonough's avatar
Chris McDonough committed
436 437 438 439 440 441 442 443 444 445 446
        self.t[5] = 6
        self.t[2] = 10
        self.t[6] = 12
        self.t[1] = 100
        self.t[3] = 200
        self.t[10] = 500
        self.t[4] = 99
        del self.t[4]
        diff = lsubtract(self.t.keys(), [1,2,3,5,6,10])
        assert diff == [], diff

447
    def testDeleteOneChildWorks(self):
Chris McDonough's avatar
Chris McDonough committed
448 449 450 451 452 453 454 455 456 457 458
        self.t[5] = 6
        self.t[2] = 10
        self.t[6] = 12
        self.t[1] = 100
        self.t[3] = 200
        self.t[10] = 500
        self.t[4] = 99
        del self.t[3]
        diff = lsubtract(self.t.keys(), [1,2,4,5,6,10])
        assert diff == [], diff

459
    def testDeleteTwoChildrenNoInorderSuccessorWorks(self):
Chris McDonough's avatar
Chris McDonough committed
460 461 462 463 464 465 466 467 468 469 470
        self.t[5] = 6
        self.t[2] = 10
        self.t[6] = 12
        self.t[1] = 100
        self.t[3] = 200
        self.t[10] = 500
        self.t[4] = 99
        del self.t[2]
        diff = lsubtract(self.t.keys(), [1,3,4,5,6,10])
        assert diff == [], diff
        
471
    def testDeleteTwoChildrenInorderSuccessorWorks(self):
472 473 474 475
        """ 7, 3, 8, 1, 5, 10, 6, 4 -- del 3 """
        self.t[7] = 6
        self.t[3] = 10
        self.t[8] = 12
Chris McDonough's avatar
Chris McDonough committed
476
        self.t[1] = 100
477
        self.t[5] = 200
Chris McDonough's avatar
Chris McDonough committed
478
        self.t[10] = 500
479 480 481 482
        self.t[6] = 99
        self.t[4] = 150
        del self.t[3]
        diff = lsubtract(self.t.keys(), [1,4,5,6,7,8,10])
Chris McDonough's avatar
Chris McDonough committed
483 484
        assert diff == [], diff

485
    def testDeleteRootWorks(self):
486 487 488 489
        """ 7, 3, 8, 1, 5, 10, 6, 4 -- del 7 """
        self.t[7] = 6
        self.t[3] = 10
        self.t[8] = 12
Chris McDonough's avatar
Chris McDonough committed
490
        self.t[1] = 100
491
        self.t[5] = 200
Chris McDonough's avatar
Chris McDonough committed
492
        self.t[10] = 500
493 494 495 496
        self.t[6] = 99
        self.t[4] = 150
        del self.t[7]
        diff = lsubtract(self.t.keys(), [1,3,4,5,6,8,10])
Chris McDonough's avatar
Chris McDonough committed
497 498 499 500 501 502 503 504 505 506 507 508
        assert diff == [], diff

    def testRandomNonOverlappingInserts(self):
        added = {}
        r = range(100)
        for x in r:
            k = whrandom.choice(r)
            if not added.has_key(k):
                self.t[k] = x
                added[k] = 1
        addl = added.keys()
        addl.sort()
509 510
        diff = lsubtract(list(self.t.keys()), addl)
        assert diff == [], (diff, addl, list(self.t.keys()))
Chris McDonough's avatar
Chris McDonough committed
511 512 513 514 515 516 517 518 519 520 521 522 523

    def testRandomOverlappingInserts(self):
        added = {}
        r = range(100)
        for x in r:
            k = whrandom.choice(r)
            self.t[k] = x
            added[k] = 1
        addl = added.keys()
        addl.sort()
        diff = lsubtract(self.t.keys(), addl)
        assert diff == [], diff

524
    def testRandomDeletes(self):
Chris McDonough's avatar
Chris McDonough committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
        r = range(1000)
        added = []
        for x in r:
            k = whrandom.choice(r)
            self.t[k] = x
            added.append(k)
        deleted = []
        for x in r:
            k = whrandom.choice(r)
            if self.t.has_key(k):
                del self.t[k]
                deleted.append(k)
                if self.t.has_key(k):
                    raise "had problems deleting %s" % k
        badones = []
        for x in deleted:
            if self.t.has_key(x):
                badones.append(x)
        assert badones == [], (badones, added, deleted)

545
    def testTargetedDeletes(self):
Chris McDonough's avatar
Chris McDonough committed
546 547 548 549 550 551 552 553 554
        r = range(1000)
        for x in r:
            k = whrandom.choice(r)
            self.t[k] = x
        for x in r:
            try:
                del self.t[x]
            except KeyError:
                pass
555
        assert realseq(self.t.keys()) == [], realseq(self.t.keys())
Chris McDonough's avatar
Chris McDonough committed
556
        
557
    def testPathologicalRightBranching(self):
Chris McDonough's avatar
Chris McDonough committed
558 559 560 561 562 563 564 565
        r = range(1000)
        for x in r:
            self.t[x] = 1
        assert realseq(self.t.keys()) == r, realseq(self.t.keys())
        for x in r:
            del self.t[x]
        assert realseq(self.t.keys()) == [], realseq(self.t.keys())

566
    def testPathologicalLeftBranching(self):
Chris McDonough's avatar
Chris McDonough committed
567 568 569 570 571 572 573 574 575 576 577
        r = range(1000)
        revr = r[:]
        revr.reverse()
        for x in revr:
            self.t[x] = 1
        assert realseq(self.t.keys()) == r, realseq(self.t.keys())

        for x in revr:
            del self.t[x]
        assert realseq(self.t.keys()) == [], realseq(self.t.keys())

578
    def testSuccessorChildParentRewriteExerciseCase(self):
Chris McDonough's avatar
Chris McDonough committed
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
        add_order = [
            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,
            298, 275, 44, 261, 291, 4, 181, 285, 289, 216, 212, 129,
            243, 97, 48, 48, 159, 22, 285, 92, 110, 27, 55, 202, 294,
            113, 251, 193, 290, 55, 58, 239, 71, 4, 75, 129, 91, 111,
            271, 101, 289, 194, 218, 77, 142, 94, 100, 115, 101, 226,
            17, 94, 56, 18, 163, 93, 199, 286, 213, 126, 240, 245, 190,
            195, 204, 100, 199, 161, 292, 202, 48, 165, 6, 173, 40, 218,
            271, 228, 7, 166, 173, 138, 93, 22, 140, 41, 234, 17, 249,
            215, 12, 292, 246, 272, 260, 140, 58, 2, 91, 246, 189, 116,
            72, 259, 34, 120, 263, 168, 298, 118, 18, 28, 299, 192, 252,
            112, 60, 277, 273, 286, 15, 263, 141, 241, 172, 255, 52, 89,
            127, 119, 255, 184, 213, 44, 116, 231, 173, 298, 178, 196,
            89, 184, 289, 98, 216, 115, 35, 132, 278, 238, 20, 241, 128,
            179, 159, 107, 206, 194, 31, 260, 122, 56, 144, 118, 283,
            183, 215, 214, 87, 33, 205, 183, 212, 221, 216, 296, 40,
            108, 45, 188, 139, 38, 256, 276, 114, 270, 112, 214, 191,
            147, 111, 299, 107, 101, 43, 84, 127, 67, 205, 251, 38, 91,
            297, 26, 165, 187, 19, 6, 73, 4, 176, 195, 90, 71, 30, 82,
            139, 210, 8, 41, 253, 127, 190, 102, 280, 26, 233, 32, 257,
            194, 263, 203, 190, 111, 218, 199, 29, 81, 207, 18, 180,
            157, 172, 192, 135, 163, 275, 74, 296, 298, 265, 105, 191,
            282, 277, 83, 188, 144, 259, 6, 173, 81, 107, 292, 231,
            129, 65, 161, 113, 103, 136, 255, 285, 289, 1
            ]
        delete_order = [
            276, 273, 12, 275, 2, 286, 127, 83, 92, 33, 101, 195,
            299, 191, 22, 232, 291, 226, 110, 94, 257, 233, 215, 184,
            35, 178, 18, 74, 296, 210, 298, 81, 265, 175, 116, 261,
            212, 277, 260, 234, 6, 129, 31, 4, 235, 249, 34, 289, 105,
            259, 91, 93, 119, 7, 183, 240, 41, 253, 290, 136, 75, 292,
            67, 112, 111, 256, 163, 38, 126, 139, 98, 56, 282, 60, 26,
            55, 245, 225, 32, 52, 40, 271, 29, 252, 239, 89, 87, 205,
            213, 180, 97, 108, 120, 218, 44, 187, 196, 251, 202, 203,
            172, 28, 188, 77, 90, 199, 297, 282, 141, 100, 161, 216,
            73, 19, 17, 189, 30, 258
            ]
        for x in add_order:
            self.t[x] = 1
        for x in delete_order:
            try: del self.t[x]
            except KeyError:
                if self.t.has_key(x): assert 1==2,"failed to delete %s" % x

624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
    def testRangeSearchAfterSequentialInsert(self):
        r = range(100)
        for x in r:
            self.t[x] = 0
        diff = lsubtract(list(self.t.keys(0, 100)), r)
        assert diff == [], diff

    def testRangeSearchAfterRandomInsert(self):
        r = range(100)
        a = {}
        for x in r:
            rnd = whrandom.choice(r)
            self.t[rnd] = 0
            a[rnd] = 0
        diff = lsubtract(list(self.t.keys(0, 100)), a.keys())
        assert diff == [], diff

    def testInsertMethod(self):
        t = self.t
        t[0] = 1
        assert t.insert(0, 1) == 0
        assert t.insert(1, 1) == 1
        assert lsubtract(list(t.keys()), [0,1]) == []

648
## BTree tests
649

650
class TestIOBTrees(BTreeTests, TestCase):
651 652 653 654 655 656
    def setUp(self):
        self.t = IOBTree()

    def nonIntegerKeyRaises(self):
        self.assertRaises(TypeError, self._stringraises)
        self.assertRaises(TypeError, self._floatraises)
657
        self.assertRaises(TypeError, self._noneraises)
658 659 660 661 662 663 664

    def _stringraises(self):
        self.t['c'] = 1

    def _floatraises(self):
        self.t[2.5] = 1

665 666 667
    def _noneraises(self):
        self.t[None] = 1

668
class TestOOBTrees(BTreeTests, TestCase):
669 670 671
    def setUp(self):
        self.t = OOBTree()

672 673 674 675
class TestOIBTrees(BTreeTests, TestCase):
    def setUp(self):
        self.t = OIBTree()

676 677 678 679 680 681 682 683 684 685 686 687 688 689
    def testNonIntegerValueRaises(self):
        self.assertRaises(TypeError, self._stringraises)
        self.assertRaises(TypeError, self._floatraises)
        self.assertRaises(TypeError, self._noneraises)

    def _stringraises(self):
        self.t[1] = 'c'

    def _floatraises(self):
        self.t[1] = 1.4

    def _noneraises(self):
        self.t[1] = None

690
class TestIIBTrees(BTreeTests, TestCase):
691 692 693
    def setUp(self):
        self.t = IIBTree()

694 695 696 697
    def testNonIntegerKeyRaises(self):
        self.assertRaises(TypeError, self._stringraiseskey)
        self.assertRaises(TypeError, self._floatraiseskey)
        self.assertRaises(TypeError, self._noneraiseskey)
Chris McDonough's avatar
Chris McDonough committed
698

699 700 701 702 703 704
    def testNonIntegerValueRaises(self):
        self.assertRaises(TypeError, self._stringraisesvalue)
        self.assertRaises(TypeError, self._floatraisesvalue)
        self.assertRaises(TypeError, self._noneraisesvalue)

    def _stringraiseskey(self):
Chris McDonough's avatar
Chris McDonough committed
705 706
        self.t['c'] = 1

707
    def _floatraiseskey(self):
Chris McDonough's avatar
Chris McDonough committed
708 709
        self.t[2.5] = 1

710 711 712 713 714 715 716 717 718 719 720 721
    def _noneraiseskey(self):
        self.t[None] = 1

    def _stringraisesvalue(self):
        self.t[1] = 'c'

    def _floatraisesvalue(self):
        self.t[1] = 1.4

    def _noneraisesvalue(self):
        self.t[1] = None

722 723
## Set tests

Chris McDonough's avatar
Chris McDonough committed
724
class TestIOSets(ExtendedSetTests, TestCase):
725
    def setUp(self):
726 727
        self.t = IOSet()

728 729 730 731 732 733 734 735 736 737 738 739 740 741
    def testNonIntegerInsertRaises(self):
        self.assertRaises(TypeError,self._insertstringraises)
        self.assertRaises(TypeError,self._insertfloatraises)
        self.assertRaises(TypeError,self._insertnoneraises)

    def _insertstringraises(self):
        self.t.insert('a')

    def _insertfloatraises(self):
        self.t.insert(1.4)

    def _insertnoneraises(self):
        self.t.insert(None)

Chris McDonough's avatar
Chris McDonough committed
742
class TestOOSets(ExtendedSetTests, TestCase):
743 744 745
    def setUp(self):
        self.t = OOSet()

Chris McDonough's avatar
Chris McDonough committed
746
class TestIISets(ExtendedSetTests, TestCase):
747 748 749
    def setUp(self):
        self.t = IISet()

Chris McDonough's avatar
Chris McDonough committed
750
class TestOISets(ExtendedSetTests, TestCase):
751 752 753
    def setUp(self):
        self.t = OISet()

Chris McDonough's avatar
Chris McDonough committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
class TestIOTreeSets(NormalSetTests, TestCase):
    def setUp(self):
        self.t = IOTreeSet()
        
class TestOOTreeSets(NormalSetTests, TestCase):
    def setUp(self):
        self.t = OOTreeSet()

class TestIITreeSets(NormalSetTests, TestCase):
    def setUp(self):
        self.t = IITreeSet()

class TestOITreeSets(NormalSetTests, TestCase):
    def setUp(self):
        self.t = OITreeSet()
        
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
## 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()

788
def test_suite():
789 790 791 792 793 794 795 796 797 798
    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')

Chris McDonough's avatar
Chris McDonough committed
799 800 801 802 803
    TIOTreeSet = makeSuite(TestIOTreeSets, 'test')
    TOOTreeSet = makeSuite(TestOOTreeSets, 'test')
    TOITreeSet = makeSuite(TestIOTreeSets, 'test')
    TIITreeSet = makeSuite(TestOOTreeSets, 'test')

804 805 806 807 808 809
    TIOBucket = makeSuite(TestIOBuckets, 'test')
    TOOBucket = makeSuite(TestOOBuckets, 'test')
    TOIBucket = makeSuite(TestOIBuckets, 'test')
    TIIBucket = makeSuite(TestIIBuckets, 'test')
    
    alltests = TestSuite((TIOSet, TOOSet, TOISet, TIISet,
Chris McDonough's avatar
Chris McDonough committed
810
                          TIOTreeSet, TOOTreeSet, TOITreeSet, TIITreeSet,
811 812
                          TIOBucket, TOOBucket, TOIBucket, TIIBucket,
                          TOOBTree, TIOBTree, TOIBTree, TIIBTree))
813 814 815 816 817

    return alltests

def main():
    alltests=test_suite()
818 819 820
    runner = TextTestRunner()
    runner.run(alltests)

821 822 823 824
def debug():
   test_suite().debug()
    

825
## utility functions
826

Chris McDonough's avatar
Chris McDonough committed
827
def lsubtract(l1, l2):
828 829 830 831 832
   l1=list(l1)
   l2=list(l2)
   l = filter(lambda x, l1=l1: x not in l1, l2)
   l = l + filter(lambda x, l2=l2: x not in l2, l1)
   return l
Chris McDonough's avatar
Chris McDonough committed
833 834 835 836

def realseq(itemsob):
    return map(lambda x: x, itemsob)

837 838 839 840 841
if __name__=='__main__':
   if len(sys.argv) > 1:
      globals()[sys.argv[1]]()
   else:
      main()
Chris McDonough's avatar
Chris McDonough committed
842