예제 #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_insert__word_is_prefix_in_trie__no_data(self):
        trie = Trie()
        trie.add('their')

        word = 'the'
        trie.add(word)

        self.assertIn(word, trie)
예제 #4
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)
예제 #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_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)
예제 #8
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)
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))
예제 #10
0
    def test_get__word_is_in_trie__no_data(self):
        trie = Trie()
        word = 'their'

        trie.add(word)
        self.assertDictEqual(trie.get(word), {})