コード例 #1
0
ファイル: test_utils.py プロジェクト: BitPhone/drogulus
 def test_distance(self):
     """
     Sanity check to ensure the XOR'd values return the correct distance.
     """
     key1 = 'abc'
     key2 = 'xyz'
     expected = 1645337L
     actual = distance(key1, key2)
     self.assertEqual(expected, actual)
コード例 #2
0
ファイル: test_utils.py プロジェクト: BitPhone/drogulus
    def test_sort_contacts(self):
        """
        Ensures that the sort_contacts function returns the list ordered in
        such a way that the contacts closest to the target key are at the head
        of the list.
        """
        contacts = []
        for i in range(512):
            contact = Contact(2 ** i, "192.168.0.%d" % i, 9999, self.version,
                              0)
            contacts.append(contact)
        target_key = long_to_hex(2 ** 256)
        result = sort_contacts(contacts, target_key)

        # Ensure results are in the correct order.
        def key(node):
            return distance(node.id, target_key)
        sorted_nodes = sorted(result, key=key)
        self.assertEqual(sorted_nodes, result)
        # Ensure the order is from lowest to highest in terms of distance
        distances = [distance(x.id, target_key) for x in result]
        self.assertEqual(sorted(distances), distances)
コード例 #3
0
    def test_find_close_nodes_in_correct_order(self):
        """
        Ensures that the nearest nodes are returned in the correct order: from
        the node closest to the target key to the node furthest away.
        """
        parent_node_id = 'abc'
        r = RoutingTable(parent_node_id)
        # Fill up the bucket and replacement cache
        for i in range(512):
            contact = Contact(2 ** i, "192.168.0.%d" % i, 9999, self.version,
                              0)
            r.add_contact(contact)
        target_key = long_to_hex(2 ** 256)
        result = r.find_close_nodes(target_key)
        self.assertEqual(constants.K, len(result))

        # Ensure results are in the correct order.
        def key(node):
            return distance(node.id, target_key)
        sorted_nodes = sorted(result, key=key)
        self.assertEqual(sorted_nodes, result)
        # Ensure the order is from lowest to highest in terms of distance
        distances = [distance(x.id, target_key) for x in result]
        self.assertEqual(sorted(distances), distances)
コード例 #4
0
ファイル: test_utils.py プロジェクト: BitPhone/drogulus
 def key(node):
     return distance(node.id, target_key)