-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
154 lines (133 loc) · 6.16 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import time
import random
import subprocess
import sys
solver_executable_path = os.path.join(os.getcwd(), "a.out")
def print_tetravex(data):
data = data.split("\n")
size = int(len(data) ** 0.5)
print(("+" + "-" * 5) * size + "+")
for i in range(size):
for tile_orientation in range(3):
print("|", end=" ")
for j in range(size):
if tile_orientation == 0:
print(" " + data[i * size + j][0], end=" ")
elif tile_orientation == 1:
print(data[i * size + j][1], end=" ")
print(data[i * size + j][2], end=" ")
elif tile_orientation == 2:
print(" " + data[i * size + j][3], end=" ")
print("|", end=" ")
print()
if (i != size - 1):
print(("+" + "-" * 5) * size + "+")
print(("+" + "-" * 5) * size + "+")
def test_solver_on_file(input_path, expected_output_path):
output_folder = "output"
if not os.path.exists(output_folder):
os.makedirs(output_folder)
else:
for file in os.listdir(output_folder):
os.remove(os.path.join(output_folder, file))
input_filename = input_path.split("/")[-1]
# Path to the output file
output_path = os.path.join(
output_folder, input_filename.split(".")[0] + "-out.txt")
# Execute the solver
subprocess.run([solver_executable_path, input_path, output_path])
# Compare the output with the expected output
with open(output_path, "r") as output_file:
output = output_file.read()
with open(expected_output_path, "r") as expected_output_file:
expected_output = expected_output_file.read()
# Remove trailing newlines
output = output.rstrip()
expected_output = expected_output.rstrip()
if output == expected_output:
print("\033[92m" + input_filename + " : OK" + "\033[0m")
else:
print("\033[91m" + input_filename + " : KO" + "\033[0m")
print("Expected output :")
print_tetravex(expected_output)
print()
print("Output :")
print_tetravex(output)
def test_solver_on_folder(input_folder, expected_output_folder):
# List of input files sorted by name
input_files = sorted(os.listdir(input_folder))
# List of expected output files
expected_output_paths = os.listdir(expected_output_folder)
# Execute the solver on each input file and compare the output with the expected output
for input_file in input_files:
if os.path.isdir(os.path.join(input_folder, input_file)):
continue
input_path = os.path.join(input_folder, input_file)
expected_output_path = None
for e_path in expected_output_paths:
cpy_e_path = e_path.split("/")[-1]
if cpy_e_path.split("-ref")[0] == input_file.split(".")[0]:
expected_output_path = os.path.join(expected_output_folder, e_path)
break
if expected_output_path == None:
print("\033[91m" + input_file + " : KO" + "\033[0m")
print("Expected output file not found")
continue
test_solver_on_file(input_path, expected_output_path)
def generate_tetravex(size, nb):
for k in range(nb):
# Generate a random tetravex
tiles = [[0, 0, 0, 0] for _ in range(size * size)]
for i in range(size):
for j in range(size):
if i > 0:
tiles[i * size + j][0] = tiles[(i - 1) * size + j][3]
else:
tiles[i * size + j][0] = random.randint(0, 9)
if j > 0:
tiles[i * size + j][1] = tiles[i * size + j - 1][2]
else:
tiles[i * size + j][1] = random.randint(0, 9)
tiles[i * size + j][3] = random.randint(0, 9)
tiles[i * size + j][2] = random.randint(0, 9)
# Shuffle the tiles
cpy_tiles = tiles.copy()
random.shuffle(tiles)
# Generate the input file
generated_input_folder = "generated_input/" + str(size) + "x" + str(size)
if not os.path.exists(generated_input_folder):
os.makedirs(generated_input_folder)
# Input file path with the format size-timestamp.txt
input_path = os.path.join(generated_input_folder, "s" + str(size) + "-" + str(k) + ".txt")
with open(input_path, "w") as input_file:
for i, tile in enumerate(tiles):
input_file.write(str(tile[0]) + str(tile[1]) + str(tile[2]) + str(tile[3]))
if cpy_tiles[i] == tile:
input_file.write("@")
input_file.write("\n")
# Generate the expected output file
generated_expected_output_folder = "generated_expected_output/" + str(size) + "x" + str(size)
if not os.path.exists(generated_expected_output_folder):
os.makedirs(generated_expected_output_folder)
# Expected output file path with the format size-timestamp-ref.txt
expected_output_path = os.path.join(generated_expected_output_folder, "s" + str(size) + "-" + str(k) +"-ref.txt")
with open(expected_output_path, "w") as expected_output_file:
for tile in cpy_tiles:
expected_output_file.write(str(tile[0]) + str(tile[1]) + str(tile[2]) + str(tile[3]) + "\n")
# Main function
if __name__ == "__main__":
# Option handling: Usage: python script.py [--folder] [--file] input expected_output
if len(sys.argv) == 4:
if sys.argv[1] == "--folder":
test_solver_on_folder(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "--file":
test_solver_on_file(sys.argv[2], sys.argv[3])
elif sys.argv[1] == "--generate":
generate_tetravex(int(sys.argv[2]), int(sys.argv[3]))
else:
print("Usage: python script.py [--folder] [--file] input expected_output")
print("Or: python script.py --generate size nb")
else:
print("Usage: python script.py [--folder] [--file] input expected_output")
print("Or: python script.py --generate size nb")