-
Notifications
You must be signed in to change notification settings - Fork 1
/
ddmin.py
295 lines (247 loc) · 7.15 KB
/
ddmin.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import sys
import re
import pdb
import time
import os
from argparse import ArgumentParser
from optparse import OptionParser
from testcase.infiniteLoop import InfiniteLoopException
class Result:
Pass = 1
Fail = 2
Unresolved = 3
class BreakoutException(Exception):
def __init__(self, message):
self.message = message
###########################################
# array input minimization
def Minimize(data, f):
if (f([]) == Result.Fail):
return []
if (f(data) == Result.Pass):
raise ValueError('ddmin: function must fail on data')
return ddmin(data, f, 2)
def ddmin(data, f, granularity):
while (len(data) >= 2):
try:
subsets = makeSubsets(data, granularity)
# pdb.set_trace()
for subset in subsets:
#print(subset)
if (f(subset) == Result.Fail):
data = subset
granularity = 2
raise BreakoutException('continue while loop')
b = [0 for i in range(len(data))]
for i in range(len(subsets)):
complement = makeComplement(subsets, i, b[:0])
if (f(complement) == Result.Fail):
granularity -= 1
if (granularity < 2):
granularity = 2
data = complement
raise BreakoutException('continue while loop')
if (granularity == len(data)):
return data
granularity *= 2 # can try different values to divide into different pieces
if (granularity > len(data)):
granularity = len(data)
except BreakoutException:
pass
return data
def makeSubsets(s, n):
ret = []
while(n > 0):
i = len(s) // n
copyS = s
s = copyS[i:]
ret.append(copyS[:i])
n -= 1 # bug!!!
# print('ret is:{0}\n'.format(ret))
return ret
def makeComplement(subsets, n, b):
for i in range(len(subsets)):
s = subsets[i]
if (i == n):
continue
b += s
# print('b is:{0}\n'.format(b))
return b
def f(d):
seen1, seen2, seen3 = False, False, False
for v in d:
if (v == 1):
seen1 = True
if (v == 7):
seen2 = True
if (v == 8):
seen3 = True
if (seen1 and seen2 and seen3):
return Result.Fail
else:
return Result.Pass
def ExampleMinimize():
data = [1, 2, 3, 4, 5, 6, 7, 8]
m = Minimize(data, f)
print(m)
#####################################################
# python program testcase minimization
def ddmin(data, granularity):
while (len(data) >= 2):
try:
subsets = findSubsets(data, granularity)
for subset in subsets:
print("Testing Subsets")
print(subset)
if (executeTest(subset) == Result.Fail):
data = subset
granularity = 2
raise BreakoutException('continue while loop')
for i in range(len(subsets)):
complement = findComplement(subsets, i)
print("Testing Complement")
print(complement)
if (executeTest(complement) == Result.Fail):
granularity -= 1
if (granularity < 2):
granularity = 2
data = complement
raise BreakoutException('continue while loop')
if (granularity == len(data)):
return data
granularity *= 2
if (granularity > len(data)):
granularity = len(data)
except BreakoutException:
pass
return data
def findSubsets(set, n):
ret = []
while(n > 0):
i = len(set) // n
copy = set
set = copy[i:]
ret.append("".join(copy[:i]))
n -= 1
return ret
def findComplement(subsets, n):
b = []
for i in range(len(subsets)):
s = subsets[i]
if (i == n):
continue
b.append(s)
return "".join(b)
def executeTest(code):
# arguments = "t.py".encode('utf-8')
# filename = "handleInput.py"
# result = subprocess.run(["python", filename, "t.py"], stderr=subprocess.PIPE)
# print(result.stdout.decode('utf-8'))
try:
exec(code)
except Exception:
ex = sys.exc_info()[0]
if (str(ex) == stre):
print("Designated Err Detected. Catch")
return Result.Fail
try:
exec(code)
except SyntaxError as e1:
print("Syntax Err Detected. Ignored")
return Result.Unresolved
except e as e2:
print("Designated Err Detected. Catch")
return Result.Fail
except NameError as e3:
print("Name Err Detected. Ignored")
# print(e3)
return Result.Unresolved
except ModuleNotFoundError as e4:
print("ModuleNotFound Err Detected. Ignored")
# print(e4)
return Result.Unresolved
except TypeError as e5:
print("Type Err Detected. Ignored")
#print(e5)
return Result.Unresolved
except ImportError as e6:
print("Import Err Detected. Ignored")
#print(e6)
return Result.Unresolved
print("Pass")
return Result.Pass
def findMin(data):
if (executeTest('') == Result.Fail):
print("trivial failure")
return []
try:
# print(data)
exec(data)
except Exception as e:
ex = sys.exc_info()[0]
# pdb.set_trace()
if ((str(ex) == "<class '__main__.InfiniteLoopException'>") or
(ex is StopIteration) or (ex is OverflowError) or (ex is FloatingPointError) or (ex is ZeroDivisionError) or
(ex is AssertionError) or (ex is AttributeError) or (ex is IndexError) or (ex is KeyError) or (ex is UnboundLocalError) or
(ex is NotImplementedError)):
setGlobal(ex)
return ddmin(data, 2)
else:
raise Exception('unsupported error')
raise Exception('function must fail')
def startDdmin():
filename = sys.argv[1]
f = open(filename, "r")
code = ''.join(f.readlines())
print(findMin(code))
def startDdmin_r(args):
file_path = args[0]
# create a new file
f_tmp = open("tmp.py", "w")
f_file = open(file_path, "r")
file_path_array = file_path.split("/")
file_path_pre = "/".join(file_path_array[0:-1])
customized_module_imported = []
for line in f_file:
keywords = line.split(" ")
is_module = False
for module_imported in customized_module_imported:
if line.find(module_imported + ".") > 0:
f_tmp.write(line.replace(module_imported + ".", ""))
is_module = True
break
if is_module:
continue
if keywords[0] == "import" and os.path.isfile(file_path_pre + "/" + keywords[1][0:-1] + ".py"):
# read the code, whether there is import of local module, copy into new file
library_path = file_path_pre + "/" + keywords[1][0:-1] + ".py"
customized_module_imported.append(keywords[1][0:-1])
f_tmp.write(open(library_path, "r").read())
else:
f_tmp.write(line)
f_tmp.close()
with open('tmp.py', 'r') as myfile:
code = myfile.read()
os.remove("tmp.py")
m = findMin(code)
print(m)
def setGlobal(ex):
global e, stre
e = ex
stre = str(e)
print("Catching "+ str(ex))
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-r", action="store_true", dest="recursive")
(options, args) = parser.parse_args()
# print options
if args == []:
print ("Usage: python ddmin.py [-r] filepath")
exit()
start = time.time()
if options.recursive:
startDdmin_r(args)
else:
startDdmin()
end = time.time()
print("Time elapsed: "+str(end-start))