forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplify_test_case.py
107 lines (99 loc) · 3.23 KB
/
simplify_test_case.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
import math
import sys
import subprocess
import re
from shutil import copyfile
import random
KEY_ERROR_REGEX = re.compile(r"KeyError: [0-9]+$")
def execute():
print("Command 1")
cmd1 = subprocess.run(["./python", "./tests/test.py"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
cmd1stdout = cmd1.stdout.decode("utf-8")
if cmd1stdout:
print(cmd1stdout)
cmd1stderr = cmd1.stderr.decode("utf-8")
if cmd1stderr:
print(cmd1stderr)
if cmd1.returncode != 0:
return False
print("Command 2")
cmd2 = subprocess.run(
["../recreate/recreate.bin", "../cpython/tests/test.rewind"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
cwd="../recreate"
)
cmd2stdout = cmd2.stdout.decode("utf-8")
if cmd2stdout:
print(cmd2stdout)
cmd2stderr = cmd2.stderr.decode("utf-8")
if cmd2stderr:
print(cmd2stderr)
stderr = cmd2.stderr.decode("utf-8")
errlines = stderr.split("\n")
return len(errlines) > 1 and "Cannot find Ref ID for container" in errlines[-2]
def main():
# print(execute())
simplify_test_case(execute, "tests/test.py")
def simplify_test_case(execute, input_file):
copyfile(input_file, input_file + ".original")
print("Backed up " + input_file + " to " + input_file + ".original")
f = open(input_file)
file_lines = open(input_file).readlines()
prev_file_lines = file_lines
chunk_idx = 0
divisor = 2
f.close()
no_tries = 0
result = execute()
assert result, "should have problem initially"
while True:
prev_file_lines = file_lines
file_lines = file_lines.copy()
chunk_size = len(file_lines) / divisor
if chunk_size < 1:
break
low = math.floor(chunk_idx * chunk_size)
del file_lines[low:low + math.floor(chunk_size)]
print("Try no", no_tries)
print("Divisor", divisor)
print("Chunk size", chunk_size)
print("Removing chuck", chunk_idx)
print("Reduced lines from", len(prev_file_lines), "to", len(file_lines))
new_file_content = "".join(file_lines)
f = open(input_file, "w")
f.write(new_file_content)
f.close()
result = execute()
no_tries += 1
if result:
print("Problem persists. Keeping change.")
# keep this, reset divisor to 2
f = open(input_file + ".last_good", "w")
f.write(new_file_content)
f.close()
print("Saved to " + input_file + ".last_good")
divisor = 2
chunk_idx = 0
no_tries = 0
else:
# undo
print("Output changed. Undoing.")
chunk_idx += 1
if chunk_idx >= divisor - 1:
print("Increase divisor to", divisor)
divisor *= 2
chunk_idx = 0
print("Reset part index to", chunk_idx)
else:
print("Increased part index to", chunk_idx)
file_lines = prev_file_lines
prev_file_content = "".join(file_lines)
f = open(input_file, "w")
f.write(prev_file_content)
f.close()
print("Complete")
if __name__ == "__main__":
main()