diff --git a/verify_output.py b/verify_output.py index f8a7e655ef9a03b808b6df76cbb3f73f1bc7e549..57222ff4c6953bce81c0fed0bcb1467368c781ab 100644 --- a/verify_output.py +++ b/verify_output.py @@ -4,36 +4,44 @@ import struct def verify_output(file): with open(file, "rb") as fd: - # First 4 bytes should be the number of nodes + # First 4 bytes should be the number of nodes. data = fd.read(4) nb_nodes, = struct.unpack(">l", data) - # The file should contain exactly nb_nodes entries + # The file should contain exactly nb_nodes entries. for _ in range(nb_nodes): - # An entry is 4 + 4 + 4 + 4 + len(path) * 4 bytes - data = fd.read(16) + # An entry is 4 + 4 + 8 + 4 + len(path) * 4 bytes. + data = fd.read(20) - # Index of the node, distance value, path length (number of hops) - source_idx, node_idx, a, path_len = struct.unpack(">llll", data) + # Index of the node, distance value, path length (number of hops). + source_idx, destination_idx, _cost, path_len = struct.unpack(">llql", data) # The node index lies within the limits. - assert source_idx >= 0 and source_idx < nb_nodes - assert node_idx >= 0 and node_idx < nb_nodes + assert source_idx >= 0 and source_idx < nb_nodes, f"The source idx does not have the correct format: {source_idx}" + assert destination_idx >= 0 and destination_idx < nb_nodes, f"The destination idx does not have the correct format: {destination_idx}" # The path len can be nul if there is no path. # The shortest path cannot contain loops. - assert path_len >= -1 and path_len < nb_nodes + assert path_len >= 1 and path_len <= nb_nodes, f"The path length is too long or nul: {path_len}" if path_len > 0: - for _ in range(path_len): + for i in range(path_len): data = fd.read(4) hop_idx, = struct.unpack(">l", data) # Same... the node index lies within the limits. - assert hop_idx >= 0 and hop_idx < nb_nodes + assert hop_idx >= 0 and hop_idx < nb_nodes, f"A hop of the path does not have the correct format {hop_idx}" + + # The first node should be the source. + if i == 0: + assert hop_idx == source_idx, f"The first node of the path is not the source: {hop_idx} (and source is {source_idx})" + + # The last node should be the destination. + if i == path_len - 1: + assert hop_idx == destination_idx, f"The first node of the path is not the destination: {hop_idx} (and destination is {destination_idx})" # The file does not contain anymore bytes - assert fd.read() == b"" + assert fd.read() == b"", "The file is not empty" print("The file has the correct format!\nThis does not mean that it solves the shortest path problem, but at least it contains readable information...")