-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
151 lines (132 loc) · 5.43 KB
/
main.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
import sys
import os
from io import StringIO
from gcms import GCMS
from object import Color
from exceptions import NoBinFoundException
import time
import traceback
reverse_color_dict = {0: Color.BLUE, 1: Color.YELLOW, 2: Color.RED, 3: Color.GREEN}
PATH = "C:\Users\sanju\Downloads\Compress_tc" # put the path of test case directory in this string
testcases = {
PATH + "complex/tc0.txt": PATH + "complex/tc0_sol.txt",
PATH + "complex/tc1.txt": PATH + "complex/tc1_sol.txt",
PATH + "complex/tc2.txt": PATH + "complex/tc2_sol.txt",
PATH + "add_bin/tc0.txt": PATH + "add_bin/tc0_sol.txt",
PATH + "add_bin/tc1.txt": PATH + "add_bin/tc1_sol.txt",
PATH + "add_bin/tc2.txt": PATH + "add_bin/tc2_sol.txt",
PATH + "add_bin_object/tc0.txt": PATH + "add_bin_object/tc0_sol.txt",
PATH + "add_bin_object/tc1.txt": PATH + "add_bin_object/tc1_sol.txt",
PATH + "add_bin_object/tc2.txt": PATH + "add_bin_object/tc2_sol.txt",
PATH + "add_object/tc0.txt": PATH + "add_object/tc0_sol.txt",
PATH + "add_object/tc1.txt": PATH + "add_object/tc1_sol.txt",
PATH + "add_object/tc2.txt": PATH + "add_object/tc2_sol.txt",
PATH + "bin_object_del/tc0.txt": PATH + "bin_object_del/tc0_sol.txt",
PATH + "bin_object_del/tc1.txt": PATH + "bin_object_del/tc1_sol.txt",
PATH + "bin_object_del/tc2.txt": PATH + "bin_object_del/tc2_sol.txt",
PATH + "exception/tc0.txt": PATH + "exception/tc0_sol.txt",
PATH + "exception/tc1.txt": PATH + "exception/tc1_sol.txt",
}
timeouts = {
PATH + "complex/tc0.txt": 1,
PATH + "complex/tc1.txt": 2,
PATH + "complex/tc2.txt": 15,
PATH + "add_bin/tc0.txt": 1,
PATH + "add_bin/tc1.txt": 1,
PATH + "add_bin/tc2.txt": 15,
PATH + "add_bin_object/tc0.txt": 1,
PATH + "add_bin_object/tc1.txt": 3,
PATH + "add_bin_object/tc2.txt": 15,
PATH + "add_object/tc0.txt": 1,
PATH + "add_object/tc1.txt": 5,
PATH + "add_object/tc2.txt": 25,
PATH + "bin_object_del/tc0.txt": 1,
PATH + "bin_object_del/tc1.txt": 3,
PATH + "bin_object_del/tc2.txt": 15,
PATH + "exception/tc0.txt": 1,
PATH + "exception/tc1.txt": 1,
}
def run_testcase(testcase_file_path, testcase_sol_file_path):
sol_ind = 0
try:
gcms = GCMS()
with open(testcase_file_path, "r") as tf, open(testcase_sol_file_path, "r") as tsf:
lines = tf.readlines()
sol_lines = tsf.readlines()
sol_ind = 0
for line in lines:
line = line.strip().split()
if line[0] == "0":
gcms.add_bin(int(line[1]), int(line[2]))
elif line[0] == "1":
try:
gcms.add_object(int(line[1]), int(line[2]), reverse_color_dict[int(line[3])])
except:
continue
elif line[0] == "2":
gcms.delete_object(int(line[1]))
elif line[0] == "3":
if f"{gcms.bin_info(int(line[1]))}\n" != sol_lines[sol_ind]:
return 0 # Incorrect
sol_ind += 1
elif line[0] == "4":
if f"{gcms.object_info(int(line[1]))}\n" != sol_lines[sol_ind]:
return 0 # Incorrect
sol_ind += 1
return 1 # Correct
except Exception as e:
# print(e)
return -2 # Import or other error
import resource
import multiprocessing
import time
def run_testcase_with_dynamic_timeout(testcase, sol_file, timeout):
def target_function(result_queue):
try:
start_time = resource.getrusage(resource.RUSAGE_SELF).ru_utime
result = run_testcase(testcase, sol_file)
end_time = resource.getrusage(resource.RUSAGE_SELF).ru_utime
cpu_time_used = end_time - start_time
result_queue.put((result, cpu_time_used))
except Exception as e:
result_queue.put((-2, 0))
result_queue = multiprocessing.Queue()
process = multiprocessing.Process(target=target_function, args=(result_queue,))
process.start()
start = time.time()
while process.is_alive():
current_time = time.time()
if (current_time - start) >= timeout:
process.terminate()
process.join()
return -1
time.sleep(0.1)
# Retrieve result if available
if not result_queue.empty():
return result_queue.get()[0]
else:
return -2
def run_all_testcases(testcases, timeouts):
results = {}
original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
try:
for testcase in testcases.keys():
start_time = time.time()
timeout = timeouts.get(testcase, 5) # Default to 5 seconds
try:
result = run_testcase_with_dynamic_timeout(testcase, testcases[testcase], timeout)
except Exception as e:
# print(e)
result = -2 # Import or other error
end_time = time.time()
time_taken = 0
else:
end_time = time.time()
time_taken = end_time - start_time
results[testcase] = (result, time_taken)
finally:
sys.stdout.close()
sys.stdout = original_stdout
return results
print(run_all_testcases(testcases, timeouts))