def get_default_tst(): tst = Tst() data = [(u'ab', 1), (u'bc', 2), (u'abc', 3), (u'd',4), (u'a', 5), (u'bcd', 6)] data.sort(lambda x, y: cmp(x[0], y[0])) tst.insert_sorted_array(data) return tst
def setup(self): self.tst = Tst()
class TestBasic(object): def setup(self): self.tst = Tst() # Now py.test can do its thing too: def setup_method(self, method): self.setup() def test_happy_insert(self): self.tst.insert(u"abc", 1) def test_happy_insert_gotochild(self): self.tst.insert(u"abc", 1) assert self.tst.goto_child(self.tst.get_root()) == 2 assert self.tst.goto_child(2) == 3 def test_get_value(self): self.tst.insert(u"abc", 1) self.tst.get_value(2) == 1 def test_find_sibling(self): self.tst.insert(u"abc", 1) assert self.tst.find_sibling(1, ord(u"a")) == 1 def test_get_root(self): assert self.tst.get_root() == 1 def test_get_iterator(self): self.tst.insert(u"abc", 1) i = self.tst.iterator() assert i.apply(u"a") == True assert i.apply(u"b") == True assert i.apply(u"c") == True def test_goto_child(self): assert self.tst.goto_child(self.tst.get_root()) == None def test_has_key_empty(self): assert self.tst.has_key("abc") == False def test_has_key_happy(self): self.tst.insert(u"abc", 1) assert self.tst.has_key("abc") == True def test_has_key_prefix(self): self.tst.insert(u"abc", 1) assert self.tst.has_key("ab") == False def test_has_key_prefix(self): self.tst.insert(u"abc", 1) assert self.tst.has_prefix(u"a") == True assert self.tst.has_prefix(u"ab") == True assert self.tst.has_prefix(u"abc") == False assert self.tst.has_prefix(u"x") == False def test_is_break_at_start_state(self): i = self.tst.iterator() assert i.is_break_pos() == False def test_is_break_at_false_state(self): i = self.tst.iterator() i.apply(u"k") assert i.is_break_pos() == False def test_remove_simple(self): self.tst.insert('foobar', 1) self.tst.remove('foobar') assert not self.tst.has_key('foobar') assert not self.tst.has_prefix('foobar') assert len(self.tst._free) == 6 def test_remove_multiple(self): lmnts = set([u"foo", u"bar", u"baz", u"bard", u"bicker", "tomb"]) for lmnt in lmnts: self.tst.insert(lmnt, 1) self.tst.remove(u"bicker") assert not self.tst.has_key(u"bicker") assert not self.tst.has_prefix(u"bi") assert len(self.tst._free) == 5 # reclaimed 'icker' tail for lmnt in lmnts - set([u"bicker"]): assert self.tst.has_key(lmnt) self.tst.remove(u"bar") assert not self.tst.has_key(u"bar") assert self.tst.has_prefix(u"bar") assert len(self.tst._free) == 5 # no 'tail' could be reclaimed for lmnt in lmnts - set([u"bicker", u"bar"]): assert self.tst.has_key(lmnt) def test_insert_remove_mix(self): self.tst.insert('foo', 1) self.tst.insert('foobar', 1) self.tst.remove('foo') assert not self.tst.has_key('foo') assert self.tst.has_key('foobar') self.tst.insert('foo', 1) assert self.tst.has_key('foo') def test_prefix_matches(self): for lmnt in [u"foo", u"bar", u"baz", u"baddie", u"bicker", "tomb"]: self.tst.insert(lmnt, 1) matches = list(self.tst.prefix_matches(u"ba")) assert set([key for key, val in matches]) == \ set([u"baddie", u"bar", u"baz"]) matches = list(self.tst.prefix_matches(u"noo")) assert not matches