assertsource_idx>=0andsource_idx<nb_nodes,f"The source idx does not have the correct format: {source_idx}"
assertdestination_idx>=0anddestination_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.
assertpath_len>=-1andpath_len<nb_nodes
assertpath_len>=1andpath_len<=nb_nodes,f"The path length is too long or nul: {path_len}"
ifpath_len>0:
for_inrange(path_len):
foriinrange(path_len):
data=fd.read(4)
hop_idx,=struct.unpack(">l",data)
# Same... the node index lies within the limits.
asserthop_idx>=0andhop_idx<nb_nodes
asserthop_idx>=0andhop_idx<nb_nodes,f"A hop of the path does not have the correct format {hop_idx}"
# The first node should be the source.
ifi==0:
asserthop_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.
ifi==path_len-1:
asserthop_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
assertfd.read()==b""
assertfd.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...")