コード例 #1
0
    def test_get__word_is_in_trie__with_data(self):
        trie = Trie()
        word = 'their'
        payload = {'weight': 25}

        trie.add(word, **payload)
        self.assertDictEqual(trie.get(word), payload)
コード例 #2
0
    def test_insert__zero_length_prefix__no_data(self):
        trie = Trie()

        word = 'their'
        trie.add(word)

        self.assertIn(word, trie)
コード例 #3
0
def test_remove():
    mytrie = Trie()
    mytrie.insert('test', 3)
    mytrie.insert('again', 2)
    mytrie.remove('test')
    with pytest.raises(KeyError):
        mytrie.get('test')
コード例 #4
0
def test_creation():
    mytrie = Trie()
    mytrie.insert('test', 3)
    mytrie.insert('hello', 9)
    assert mytrie.get('hello') == 9
    assert mytrie.get('test') == 3
    with pytest.raises(KeyError):
        mytrie.get('cat')
コード例 #5
0
    def test_insert__zero_length_prefix__with_data(self):
        trie = Trie()

        word = 'their'
        payload = {'weight': 30}
        trie.add(word, **payload)

        self.assertIn(word, trie)
        self.assertDictEqual(trie.get(word), payload)
コード例 #6
0
    def test_search(self):
        words = ['the', 'their', 'there', 'a', 'any', 'answer', 'by', 'bye']

        trie = Trie()
        for word in words:
            trie.add(word)

        self.assertSetEqual({x
                             for x in trie.search('the')},
                            {'the', 'their', 'there'})
コード例 #7
0
    def test_contains(self):
        words = ['the', 'their', 'there', 'a', 'any', 'answer', 'by', 'bye']

        trie = Trie()
        for word in words:
            trie.add(word)

        self.assertIn('answer', trie)
        self.assertIn('any', trie)
        self.assertNotIn('anybody', trie)
コード例 #8
0
    def test_insert__word_is_already_in_trie__update_data(self):
        trie = Trie()
        trie.add('their', weight=25)

        word = 'their'
        payload = {'weight': 30}

        trie.add(word, **payload)
        self.assertDictEqual(trie.get(word), payload)
コード例 #9
0
    def test_insert__word_is_prefix_in_trie__with_data(self):
        trie = Trie()
        trie.add('their')

        word = 'the'
        payload = {'weight': 30}

        trie.add(word, **payload)
        self.assertIn(word, trie)
        self.assertDictEqual(trie.get(word), payload)
コード例 #10
0
    def test_insert__word_is_prefix_in_trie__no_data(self):
        trie = Trie()
        trie.add('their')

        word = 'the'
        trie.add(word)

        self.assertIn(word, trie)
コード例 #11
0
def test_trie():
    stop_count = 100000
    trie_single_adjecent = Trie(root_data='ACCEPT', strides=1)
    trie_double_adjecent = Trie(root_data='ACCEPT', strides=2)

    trie_single_random = Trie(root_data='ACCEPT', strides=1)
    trie_double_random = Trie(root_data='ACCEPT', strides=2)

    count = 0
    adjecent_ips = list()
    with open('data-raw-table.txt', mode='r') as f:
        for line in f:
            subnet = ipaddress.ip_network(line.rstrip().split('\t')[0])
            for idx, host in enumerate(subnet.hosts()):
                count += 1
                if count > stop_count:
                    break
                adjecent_ips.append(host)
    for host in adjecent_ips:
        trie_single_adjecent.add(bitarray('{:0>32b}'.format(int(host))),
                                 'DROP')
        trie_double_adjecent.add(bitarray('{:0>32b}'.format(int(host))),
                                 'DROP')

    count = 0
    random_ips = list()
    with open('ip.txt', mode='r') as f:
        for line in f:
            count += 1
            if count > stop_count:
                break
            random_ips.append(ipaddress.ip_address(line.rstrip()))
    for host in random_ips:
        trie_single_random.add(bitarray('{:0>32b}'.format(int(host))), 'DROP')
        trie_double_random.add(bitarray('{:0>32b}'.format(int(host))), 'DROP')

    ########
    # GET
    ########
    start = time.time()
    for host in adjecent_ips:
        trie_single_adjecent.longest_prefix(
            bitarray('{:0>32b}'.format(int(host))))
    end_trie_single_adjecent = time.time() - start
    print('trie 1 bit adjecent {:f} seconds'.format(end_trie_single_adjecent))

    start = time.time()
    for host in adjecent_ips:
        trie_double_adjecent.longest_prefix(
            bitarray('{:0>32b}'.format(int(host))))
    end_trie_double_adjecent = time.time() - start
    print('trie 2 bit adjecent {:f} seconds'.format(end_trie_double_adjecent))

    start = time.time()
    for host in random_ips:
        trie_single_random.longest_prefix(
            bitarray('{:0>32b}'.format(int(host))))
    end_trie_single_random = time.time() - start
    print('trie 1 bit random {:f} seconds'.format(end_trie_single_random))

    start = time.time()
    for host in random_ips:
        trie_double_random.longest_prefix(
            bitarray('{:0>32b}'.format(int(host))))
    end_trie_double_random = time.time() - start
    print('trie 2 bit random {:f} seconds'.format(end_trie_double_random))
コード例 #12
0
    def test_get__word_not_in_trie(self):
        trie = Trie()
        word = 'their'

        self.assertIsNone(trie.get(word))
コード例 #13
0
    def test_get__word_is_in_trie__no_data(self):
        trie = Trie()
        word = 'their'

        trie.add(word)
        self.assertDictEqual(trie.get(word), {})
コード例 #14
0
ファイル: loader.py プロジェクト: stiven98/SearchEngine
 def __init__(self, path):
     self.path = path
     self.trie = Trie()
     self.parser = Parser()
     self.graph = Graph()
コード例 #15
0
def test_overwrite():
    mytrie = Trie()
    mytrie.insert('test', 3)
    mytrie.insert('test', 4)
    assert mytrie.get('test') == 4