def test_multi_1(): # Let's try this: # # a -> b -> abc # \- c -> abcd # # Which is to say: # ababc # acabcd bytestrings = { 'abc': [bs(0,0,1), bs(1,0,1), ord('a'), ord('b'), bs(0,1,0), ord('c'), 0, 1], 'abcd': [bs(0,0,1), bs(1,0,2), ord('a'), ord('b'), ord('c'), bs(0,1,0), ord('d'), 0, 2], 'ab/ac': [bs(0,0,1), bs(0,0,2), ord('a'), bs(0,1,0), ord('b'), 128, 1, bs(0,1,0), ord('c'), 128, 2], } st0 = subtrie.SubTrie(dps, bytestrings['ab/ac']) st1 = subtrie.SubTrie(dps, bytestrings['abc']) st2 = subtrie.SubTrie(dps, bytestrings['abcd']) jt = jtrie.JTrie(root=st0, pointer_size=dps, next_string_id=3) jt.subtries[1] = st1 jt.subtries[2] = st2 assert jt.find('ababc') == 1 assert jt.find('acabcd') == 2 jt.insert('ad') jt.insert('ababe') jt.insert('acabf') return jt
def test_read_continuation(): strings = [chr(x) for x in xrange(33, 33+94)] first_batch, second_batch = strings[:62], strings[62:] fanout_1 = {} next_id = 1 for s in first_batch: fanout_1[s] = (True, next_id, {}) next_id += 1 fanout_2 = {chr(0): {}} for s in second_batch: fanout_2[chr(0)][s] = (True, next_id, {}) next_id += 1 fanout_1[chr(0)] = fanout_2 def make_matching_nodes(batch): next_id = 1 for c in batch: yield bs(0,1,0) yield ord(c) for item in unpack_bytes(next_id, dps): yield item next_id += 1 first_batch_items = list(make_matching_nodes(first_batch)) second_batch_items = list(make_matching_nodes(second_batch)) block = [bs(0,0,63)] + first_batch_items \ + [bs(0,0,len(second_batch)), 0] + second_batch_items st = subtrie.SubTrie(dps, block) assert check_structure(st, fanout_1)
def listenConn(self, user, con): while True: try: data = con.recv(1024) except: self.closeConnection(user, 1) return if not data: self.closeConnection(user, 1) return else: util.printLog("Got message from conn id: " + str(user.id)) util.printLog(util.bs(data)) if not self.msgHandler(user, util.bs(data)): self.closeConnection(user) return
def make_matching_nodes(batch): next_id = 1 for c in batch: yield bs(0,1,0) yield ord(c) for item in unpack_bytes(next_id, dps): yield item next_id += 1
def test_follow_subtrie_roots(): # ab # abc # abd bytestrings = { 'ab1': [bs(0,0,1), bs(0,0,1), ord('a'), bs(0,1,0), ord('b'), 128, 1], 'b2c': [bs(0,1,1), 128, 2, bs(0,1,0), ord('c'), 0, 1], 'b3d': [bs(0,1,1), 0, 2, bs(0,1,0), ord('d'), 0, 3], } st0 = subtrie.SubTrie(dps, bytestrings['ab1']) st1 = subtrie.SubTrie(dps, bytestrings['b2c']) st2 = subtrie.SubTrie(dps, bytestrings['b3d']) jt = jtrie.JTrie(root=st0, pointer_size=dps) jt.subtries[1] = st1 jt.subtries[2] = st2 assert jt.find('ab') == 2 assert jt.find('abc') == 1 assert jt.find('abd') == 3
import subtrie from collections import deque from util import bs, unpack_bytes dps = 1 targets = { 'a': ({ 'a': (True, 1, {}) }, [bs(0,0,1), bs(0,1,0), ord('a'), 1]), 'abc': ({ 'ab': (False, { 'c': (True, 1, {}) }) }, [bs(0,0,1), bs(1,0,1), ord('a'), ord('b'), bs(0,1,0), ord('c'), 1]), 'ab/ac': ({ 'a': (False, { 'b': (True, 1, {}), 'c': (True, 2, {}) }) }, [bs(0,0,1), bs(0,0,2), ord('a'), bs(0,1,0), ord('b'), 1, bs(0,1,0), ord('c'), 2]), 'al/alfred/alley': ({ 'a': (False, { 'l': (True, 1, { 'fre': (False, { 'd': (True, 2, {}) }), 'le': (False, { 'y': (True, 3, {}) }) })
}) }) }), (['a'*130], { 'a': (False, { 'a'*64: (False, { 'a'*64: (False, { 'a': (True, 1, {}) }) }) }) }) ] bytestrings = { 'abc': [bs(0,0,1), bs(1,0,1), ord('a'), ord('b'), bs(0,1,0), ord('c'), 1], 'abcd': [bs(0,0,1), bs(1,0,2), ord('a'), ord('b'), ord('c'), bs(0,1,0), ord('d'), 1], 'abcdef': [bs(0,0,1), bs(1,0,4), ord('a'), ord('b'), ord('c'), ord('d'), ord('e'), bs(0,1,0), ord('f'), 1], 'ab/ac': [bs(0,0,1), bs(0,0,2), ord('a'), bs(0,1,0), ord('b'), 0, 1, bs(0,1,0), ord('c'), 0, 2], } def test_raw_inserts(): for strings, target in insert_test_cases: yield check_raw_inserts, strings, target def check_raw_inserts(strings, target): jt = jtrie.JTrie(pointer_size=dps) for s in strings: jt.insert(s)