-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshowcase3.py
More file actions
61 lines (52 loc) · 2.21 KB
/
showcase3.py
File metadata and controls
61 lines (52 loc) · 2.21 KB
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
# @authors: skcheongbrian, sevenseasofbri
from humandeltadebug import DD
import argparse
'''
This example contains a test that demonstrates the use of dd to find a logic bug in Python code.
The test case will fail if the function does not return the sum of two numbers.
'''
class WrongLogic(DD):
def _test(self, c):
candidate = DD.config_to_string(c)
try:
# Execute the candidate code in a sandboxed namespace
ns = {}
exec(candidate, ns)
result = ns["add"](2, 3)
if result == 5:
return self.PASS
else:
return self.FAIL
except Exception as e:
if DD.verbose:
print("Execution error:", e)
return self.UNRESOLVED
def coerce(self, c):
return DD.config_to_string(c)
# Logic bug: the function subtracts instead of adds
failing_py = "def add(a, b):\n return a - b"
passing_py = "def add(a, b):\n return a + b"
failing_config = DD.string_to_config(failing_py)
passing_config = DD.string_to_config(passing_py)
parser = argparse.ArgumentParser(description="Run delta debugging with verbosity control.")
parser.add_argument('--verbose', action='store_true', help='Enable verbose output')
args = parser.parse_args()
logic_bug_dd = WrongLogic()
logic_bug_dd.verbose = 1 if args.verbose else 0
# Minimizing the bug
minimal_config = logic_bug_dd.dd(failing_config)
minimal_failing = DD.config_to_string(minimal_config[0])
print("**********")
print("* Test 1 *")
print("**********************************************")
print("Failing test case to be minimised: \n " + failing_py)
print("**********************************************")
print("Minimal failing test case: \n" + minimal_failing)
print("**********************************************")
print("Passing test case to be maximised with respect to: \n" + passing_py)
# Maximizing it w.r.t the passing config
max_config = logic_bug_dd.ddmax(DD.string_to_config(minimal_failing), passing_config, 2)
maximal_failing = DD.config_to_string(max_config[1])
print("**********************************************")
print("Maximal failing test case: \n" + maximal_failing)
print("**********************************************")