def test_update_coordinate_bounds() -> None: bounds = np.array([-1.0, -1.0, -1.0]) node = VectorNode(3, coordinates=[1.0, -2.0, -1.0]) bounds = VectorLinkedList.update_coordinate_bounds(bounds, node, 0 + 1) assert list(bounds) == [-1, -1, -1] bounds = VectorLinkedList.update_coordinate_bounds(bounds, node, 1 + 1) assert list(bounds) == [-1, -2, -1] bounds = VectorLinkedList.update_coordinate_bounds(bounds, node, 2 + 1) assert list(bounds) == [-1, -2, -1]
def test_sort_by_index() -> None: nodes = [ VectorNode(3, [1, 2, 3]), VectorNode(3, [2, 3, 1]), VectorNode(3, [3, 1, 2]) ] new_nodes = VectorLinkedList.sort_by_index(nodes, 0) assert new_nodes == nodes new_nodes = VectorLinkedList.sort_by_index(nodes, 1) assert new_nodes == [nodes[2], nodes[0], nodes[1]] new_nodes = VectorLinkedList.sort_by_index(nodes, 2) assert new_nodes == [nodes[1], nodes[2], nodes[0]]
def test_pop() -> None: dim = 4 multilist = VectorLinkedList(dimension=dim) new_node = VectorNode(dim) multilist.append(new_node, 0) popped_node = multilist.pop(new_node, 0 + 1) assert popped_node is new_node assert new_node.next[0] is multilist.sentinel assert new_node.prev[0] is multilist.sentinel for i in range(dim): assert multilist.sentinel.next[i] is multilist.sentinel assert multilist.sentinel.prev[i] is multilist.sentinel
def test_iterate() -> None: dim = 1 multilist = VectorLinkedList(dimension=dim) new_node = VectorNode(dim) another_node = VectorNode(dim) multilist.append(new_node, 0) multilist.append(another_node, 0) gen = multilist.iterate(0) assert next(gen) is new_node assert next(gen) is another_node yet_another_node = VectorNode(dim) multilist.append(yet_another_node, 0) gen = multilist.iterate(0, start=another_node) assert next(gen) is another_node assert next(gen) is yet_another_node
def test_create_sorted() -> None: dimension = 3 coordinates = [[1, 2, 3], [2, 3, 1], [3, 1, 2]] linked_list = VectorLinkedList.create_sorted(dimension, coordinates) assert isinstance(linked_list, VectorLinkedList) assert list(linked_list.sentinel.next[0].coordinates) == [1, 2, 3] assert list(linked_list.sentinel.next[1].coordinates) == [3, 1, 2] assert list(linked_list.sentinel.next[2].coordinates) == [2, 3, 1] assert list(linked_list.sentinel.next[0].next[0].coordinates) == [2, 3, 1] assert list(linked_list.sentinel.next[1].next[1].coordinates) == [1, 2, 3] assert list(linked_list.sentinel.next[2].next[2].coordinates) == [3, 1, 2]
def test_append() -> None: dim = 4 multilist = VectorLinkedList(dimension=dim) new_node = VectorNode(dim) multilist.append(new_node, 0) for i in range(1, dim): assert new_node.next[i] is new_node assert new_node.prev[i] is new_node assert multilist.sentinel.next[i] is multilist.sentinel assert multilist.sentinel.prev[i] is multilist.sentinel assert new_node.next[0] is multilist.sentinel assert new_node.prev[0] is multilist.sentinel assert multilist.sentinel.next[0] is new_node assert multilist.sentinel.prev[0] is new_node another_node = VectorNode(dim) multilist.append(another_node, 0) for i in range(1, dim): assert new_node.next[i] is new_node assert new_node.prev[i] is new_node assert multilist.sentinel.next[i] is multilist.sentinel assert multilist.sentinel.prev[i] is multilist.sentinel assert new_node.next[0] is another_node assert new_node.prev[0] is multilist.sentinel assert multilist.sentinel.next[0] is new_node assert multilist.sentinel.prev[0] is another_node
def test_initialize_linked_list() -> None: dim = 4 multilist = VectorLinkedList(dimension=dim) assert dim == multilist.dimension assert isinstance(multilist.sentinel, VectorNode) assert len(multilist.sentinel.prev) == 4 assert len(multilist.sentinel.next) == 4 assert len(multilist) == 4 for d in range(dim): assert multilist.sentinel is multilist.sentinel.next[d] assert multilist.sentinel is multilist.sentinel.prev[d] assert len(multilist.sentinel.next) == len(multilist.sentinel.prev) assert len(multilist.sentinel.next) == len(multilist.sentinel.next[0].next) assert str(multilist) == "\n".join([str([])] * dim)
def test_reinsert() -> None: dim = 2 multilist = VectorLinkedList(dimension=dim) new_node = VectorNode(dim) another_node = VectorNode(dim) multilist.append(new_node, 0) multilist.append(another_node, 0) multilist.append(another_node, 1) multilist.append(new_node, 1) popped_node = multilist.pop(new_node, 1 + 1) multilist.reinsert(new_node, 0 + 1) assert multilist.chain_length(0) == 2 assert multilist.chain_length(1) == 1 assert new_node.next[0] is another_node assert new_node.prev[0] is multilist.sentinel assert another_node.prev[0] is new_node assert another_node.next[0] is multilist.sentinel assert another_node.prev[1] is multilist.sentinel assert another_node.next[1] is multilist.sentinel multilist.reinsert(popped_node, 1 + 1) assert multilist.chain_length(0) == 2 assert multilist.chain_length(1) == 2 assert another_node.prev[1] is multilist.sentinel assert another_node.next[1] is new_node assert new_node.prev[1] is another_node assert new_node.next[1] is multilist.sentinel
def test_chain_length() -> None: dim = 3 multilist = VectorLinkedList(dimension=dim) new_node = VectorNode(dim) multilist.append(new_node, 0) assert multilist.chain_length(0) == 1 assert multilist.chain_length(1) == 0 assert multilist.chain_length(2) == 0 another_node = VectorNode(dim) multilist.append(another_node, 0) assert multilist.chain_length(0) == 2 assert multilist.chain_length(1) == 0 assert multilist.chain_length(2) == 0 multilist.append(another_node, 1) assert multilist.chain_length(0) == 2 assert multilist.chain_length(1) == 1 assert multilist.chain_length(2) == 0 multilist.append(new_node, 2) assert multilist.chain_length(0) == 2 assert multilist.chain_length(1) == 1 assert multilist.chain_length(2) == 1
def test_extend() -> None: dim = 1 multilist = VectorLinkedList(dimension=dim) another_multilist = VectorLinkedList(dimension=dim) new_node = VectorNode(dim) another_node = VectorNode(dim) multilist.append(new_node, 0) multilist.append(another_node, 0) another_multilist.extend([new_node, another_node], 0) assert another_multilist.chain_length(0) == 2 assert another_multilist.sentinel.next[0] is multilist.sentinel.next[0] assert another_multilist.sentinel.next[0].next[ 0] is multilist.sentinel.next[0].next[0]