Exemplo n.º 1
0
 def items_from(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     for cutoff in (-1, 1, 50.1, 100, 102):
         assert (list([(x, y) for (x, y) in bt.items() if x >= cutoff]) ==
                 list(bt.items_from(cutoff)))
         assert (list([(x, y) for (x, y) in bt.items() if x > cutoff]) ==
                 list(bt.items_from(cutoff, closed=False)))
Exemplo n.º 2
0
 def items_from(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     for cutoff in (-1, 1, 50.1, 100, 102):
         assert (list([(x, y) for (x, y) in bt.items()
                       if x >= cutoff]) == list(bt.items_from(cutoff)))
         assert (list([(x, y) for (x, y) in bt.items() if x > cutoff
                       ]) == list(bt.items_from(cutoff, closed=False)))
Exemplo n.º 3
0
 def items_backward_from(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     for cutoff in (-1, 1, 50.1, 100, 102):
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                                 if x < cutoff]))
         got = list(bt.items_backward_from(cutoff))
         assert expect == got, (cutoff, expect, got)
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                                 if x <= cutoff]))
         got = list(bt.items_backward_from(cutoff, closed=True))
         assert expect == got, (cutoff, expect, got)
Exemplo n.º 4
0
 def test_iter(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     if hasattr(bt, 'iterkeys'):
         assert list(bt) == list(bt.iterkeys())
         assert list(bt.iteritems()) == list(zip(bt, bt.itervalues()))
         assert list(bt.iterkeys()) == list(bt.keys())
         assert list(bt.itervalues()) == list(bt.values())
         assert list(bt.iteritems()) == list(bt.items())
     else:
         assert list(bt) == list(bt.keys())
         assert list(bt.items()) == list(zip(bt, bt.values()))
Exemplo n.º 5
0
 def items_backward_from(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     for cutoff in (-1, 1, 50.1, 100, 102):
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if x < cutoff]))
         got = list(bt.items_backward_from(cutoff))
         assert expect == got, (cutoff, expect, got)
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if x <= cutoff]))
         got = list(bt.items_backward_from(cutoff, closed=True))
         assert expect == got, (cutoff, expect, got)
Exemplo n.º 6
0
 def iter(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     assert list(bt) == list(bt.iterkeys())
     assert list(bt.iteritems()) == list(zip(bt, bt.itervalues()))
     assert list(bt.iterkeys()) == list(bt.keys())
     assert list(bt.itervalues()) == list(bt.values())
     assert list(bt.iteritems()) == list(bt.items())
Exemplo n.º 7
0
 def iter(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     assert list(bt) == list(bt.iterkeys())
     assert list(bt.iteritems()) == list(zip(bt, bt.itervalues()))
     assert list(bt.iterkeys()) == list(bt.keys())
     assert list(bt.itervalues()) == list(bt.values())
     assert list(bt.iteritems()) == list(bt.items())
Exemplo n.º 8
0
 def items_range(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     lo = 0
     hi = 40
     for lo, hi in [(-1,10), (3, 9), (30, 200), (-10, 200)]:
         expect = list([(x, y) for (x, y) in bt.items()
                     if lo <= x < hi])
         got = list(bt.items_range(lo, hi))
         assert expect == got, (lo, hi, expect, got)
         expect = list([(x, y) for (x, y) in bt.items()
                     if lo < x < hi])
         got = list(bt.items_range(lo, hi, closed_start=False))
         assert expect == got, (lo, hi, expect, got)
         expect = list([(x, y) for (x, y) in bt.items()
                     if lo < x <= hi])
         got = list(bt.items_range(
             lo, hi, closed_start=False, closed_end=True))
         assert expect == got, (lo, hi, expect, got)
         expect = list([(x, y) for (x, y) in bt.items()
                     if lo <= x <= hi])
         got = list(bt.items_range(
             lo, hi, closed_start=True, closed_end=True))
         assert expect == got, (lo, hi, expect, got)
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                     if lo < x <= hi]))
         got = list(bt.items_range(hi, lo))
         assert expect == got, (hi, lo, expect, got)
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                     if lo <= x <= hi]))
         got = list(bt.items_range(hi, lo, closed_end=True))
         assert expect == got, (hi, lo, expect, got)
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                     if lo <= x < hi]))
         got = list(bt.items_range(
             hi, lo, closed_start=False, closed_end=True))
         assert expect == got, (hi, lo, expect, got)
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                     if lo < x < hi]))
         got = list(bt.items_range(
             hi, lo, closed_start=False, closed_end=False))
         assert expect == got, (hi, lo, expect, got)
         expect = list(reversed([(x, y) for (x, y) in bt.items()
                     if lo <= x <= hi]))
         got = list(bt.items_range(hi, lo, closed_end=True))
         assert expect == got, (hi, lo, expect, got)
Exemplo n.º 9
0
 def update(self):
     bt = BTree()
     bt.update()
     assert not list(bt.items())
     bt.update(a=1)
     assert list(bt.items()) == [('a', 1)]
     bt = BTree()
     bt.update(dict(b=2), a=1)
     assert len(list(bt.items())) == 2
     assert bt['b'] == 2
     assert bt['a'] == 1
     bt = BTree()
     bt.update([('b', 2)], a=1)
     assert len(list(bt.items())) == 2
     assert bt['b'] == 2
     assert bt['a'] == 1
     class Fake(object):
         def items(self):
             return [('1',2)]
     bt.update(Fake())
     raises(TypeError, bt.update, 1, 2)
Exemplo n.º 10
0
 def items_range(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     lo = 0
     hi = 40
     for lo, hi in [(-1, 10), (3, 9), (30, 200), (-10, 200)]:
         expect = list([(x, y) for (x, y) in bt.items() if lo <= x < hi])
         got = list(bt.items_range(lo, hi))
         assert expect == got, (lo, hi, expect, got)
         expect = list([(x, y) for (x, y) in bt.items() if lo < x < hi])
         got = list(bt.items_range(lo, hi, closed_start=False))
         assert expect == got, (lo, hi, expect, got)
         expect = list([(x, y) for (x, y) in bt.items() if lo < x <= hi])
         got = list(
             bt.items_range(lo, hi, closed_start=False, closed_end=True))
         assert expect == got, (lo, hi, expect, got)
         expect = list([(x, y) for (x, y) in bt.items() if lo <= x <= hi])
         got = list(
             bt.items_range(lo, hi, closed_start=True, closed_end=True))
         assert expect == got, (lo, hi, expect, got)
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if lo < x <= hi]))
         got = list(bt.items_range(hi, lo))
         assert expect == got, (hi, lo, expect, got)
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if lo <= x <= hi]))
         got = list(bt.items_range(hi, lo, closed_end=True))
         assert expect == got, (hi, lo, expect, got)
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if lo <= x < hi]))
         got = list(
             bt.items_range(hi, lo, closed_start=False, closed_end=True))
         assert expect == got, (hi, lo, expect, got)
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if lo < x < hi]))
         got = list(
             bt.items_range(hi, lo, closed_start=False, closed_end=False))
         assert expect == got, (hi, lo, expect, got)
         expect = list(
             reversed([(x, y) for (x, y) in bt.items() if lo <= x <= hi]))
         got = list(bt.items_range(hi, lo, closed_end=True))
         assert expect == got, (hi, lo, expect, got)
Exemplo n.º 11
0
    def update(self):
        bt = BTree()
        bt.update()
        assert not list(bt.items())
        bt.update(a=1)
        assert list(bt.items()) == [('a', 1)]
        bt = BTree()
        bt.update(dict(b=2), a=1)
        assert len(list(bt.items())) == 2
        assert bt['b'] == 2
        assert bt['a'] == 1
        bt = BTree()
        bt.update([('b', 2)], a=1)
        assert len(list(bt.items())) == 2
        assert bt['b'] == 2
        assert bt['a'] == 1

        class Fake(object):
            def items(self):
                return [('1', 2)]

        bt.update(Fake())
        raises(TypeError, bt.update, 1, 2)
Exemplo n.º 12
0
    def test_slow(self):
        if '--slow' not in sys.argv:
            skip('test not run because it is slow')

        if SAME_RANDOM:
            seed(42)

        for bnode_class in BNode.__subclasses__():
            if bnode_class.minimum_degree not in (4, 16):
                continue
            sys.stdout.write('\nbnode_class=%s' % bnode_class)
            bt = BTree(bnode_class)
            d = {}
            number = 0
            limit = 10000
            for k in range(limit*10):
                number = randint(0, limit)
                if DEBUG:
                    bt_bak = deepcopy(bt)
                    d_bak = deepcopy(d)
                if number in bt:
                    assert number in d
                    if randint(0, 1) == 1:
                        del bt[number]
                        del d[number]
                        op = 'del'
                        #sys.stdout.write('\ndel bt[%s]' % number)
                else:
                    bt[number] = 1
                    d[number] = 1
                    op = 'ins'
                if DEBUG or k % limit == 0:
                    d_items = sorted(d.items())
                    assert d_items == list(bt.items())
                    if DEBUG and not len(d_items) == len(bt):
                        # set a breakpoint here for interactive debugging
                        if op == 'ins':
                            bt_bak[number] = 1
                        else:
                            del bt_bak[number]
                    assert len(d_items) == len(bt)
Exemplo n.º 13
0
 def slow(self):
     for bnode_class in BNode.__subclasses__():
         if bnode_class.minimum_degree not in (4, 16):
             continue
         sys.stdout.write('\nbnode_class=%s' % bnode_class)
         bt = BTree(bnode_class)
         d = {}
         number = 0
         limit = 10000
         for k in range(limit*10):
             number = randint(0, limit)
             if number in bt:
                 assert number in d
                 if randint(0, 1) == 1:
                     del bt[number]
                     del d[number]
                     sys.stdout.write('\ndel bt[%s]' % number)
             else:
                 bt[number] = 1
                 d[number] = 1
             if k % limit == 0:
                 d_items = sorted(d.items())
                 assert d_items == list(bt.items())
                 assert len(d_items) == len(bt)
Exemplo n.º 14
0
 def slow(self):
     for bnode_class in BNode.__subclasses__():
         if bnode_class.minimum_degree not in (4, 16):
             continue
         sys.stdout.write('\nbnode_class=%s' % bnode_class)
         bt = BTree(bnode_class)
         d = {}
         number = 0
         limit = 10000
         for k in range(limit * 10):
             number = randint(0, limit)
             if number in bt:
                 assert number in d
                 if randint(0, 1) == 1:
                     del bt[number]
                     del d[number]
                     sys.stdout.write('\ndel bt[%s]' % number)
             else:
                 bt[number] = 1
                 d[number] = 1
             if k % limit == 0:
                 d_items = sorted(d.items())
                 assert d_items == list(bt.items())
                 assert len(d_items) == len(bt)
Exemplo n.º 15
0
 def items_backward(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     assert list(reversed(bt.items())) == list(bt.items_backward())
Exemplo n.º 16
0
 def items_backward(self):
     bt = BTree()
     for j in range(100):
         bt.add(j)
     assert list(reversed(bt.items())) == list(bt.items_backward())