def get(self): bt = BTree() for j in range(10): bt.add(j) assert bt.get(2) == True assert bt.get(-1) == None assert bt.get(-1, 5) == 5
def test_delete_case_2c(self): bt = BTree(BNode) for j in 'abcdefghi': bt.add(j) assert bt.root.items == [('d', True)] del bt['d'] assert len(bt) == bt.root.get_count()
def find_extremes(self): bt = BTree() raises(AssertionError, bt.get_min_item) raises(AssertionError, bt.get_max_item) for j in range(100): bt.add(j) assert bt.get_min_item() == (0, True) assert bt.get_max_item() == (99, True)
def clear(self): bt = BTree() for j in range(10): bt.add(j) assert bt.has_key(2) bt.clear() assert not bt.has_key(2) assert bt.keys() == []
def search(self): bt = BTree(BNode) for j in range(100): bt.add(j) assert bt[1] == True try: assert bt[-1] except KeyError: pass
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)))
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())
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)))
def _delete_case_3(self): bt = BTree(BNode) for j in range(100): bt.add(j) assert bt.root.items == [(31, True), (63, True)] assert [n.items for n in bt.root.nodes] == [[(15, True)], [(47, True)], [(79, True)]] assert [[n.items for n in node.nodes] for node in bt.root.nodes] == [[[(7, True)], [(23, True)]], [[(39, True)], [(55, True)]], [[(71, True)], [(87, True)]]] return bt
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)
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)
def _delete_case_3(self): bt = BTree(BNode) for j in range(100): bt.add(j) assert bt.root.items == [(31, True), (63, True)] assert [n.items for n in bt.root.nodes] == [ [(15, True)], [(47, True)], [(79, True)]] assert [[n.items for n in node.nodes] for node in bt.root.nodes] == [ [[(7, True)], [(23, True)]], [[(39, True)], [(55, True)]], [[(71, True)], [(87, True)]]] return bt
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()))
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)
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)
def contains(self): bt = BTree() for j in range(10): bt.add(j) assert 2 in bt assert -1 not in bt
def delete_case_2a(self): bt = BTree(BNode) for j in 'jklmoab': bt.add(j) del bt['k']
def has_key(self): bt = BTree() for j in range(10): bt.add(j) assert bt.has_key(2) assert not bt.has_key(-1)
def reversed(self): bt = BTree() for j in range(100): bt.add(j) assert list(reversed(bt)) == list(reversed(list(bt)))
def delete_case_2c(self): bt = BTree(BNode) for j in 'abcdefghi': bt.add(j) assert bt.root.items == [('d', True)] del bt['d']
def items_backward(self): bt = BTree() for j in range(100): bt.add(j) assert list(reversed(bt.items())) == list(bt.items_backward())
def test_delete_case_2a(self): bt = BTree(BNode) for j in 'jklmoab': bt.add(j) del bt['k'] assert len(bt) == bt.root.get_count()