-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
120 lines (99 loc) · 3.03 KB
/
main.go
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
package main
import (
"fmt"
"os"
"regexp"
"strings"
)
var FailingInput = readInput(os.Args[2])
func main() {
/* 1. Start with n = 2 and 𝝙 (CF ) as test
2. Test each 𝝙i and each ∇i
3. Three possible outcomes:
a. Some 𝝙i causes failure: Goto 1 with 𝝙 = 𝝙i and n = 2
b. Some ∇i causes failure: Goto 1 with 𝝙 = ∇i and n = n - 1
c. No test causes failure:
If granularity can be redefined (n*2 <= | 𝝙 |) go to step 1 with 𝝙 = 𝝙 and n = n * 2
Otherwise: Done, return the 1-minimum test
*/
input := readInput(os.Args[1])
result := executeDeltaDebugging(input, 2)
fmt.Println("1-minimum test success. The failing input is:")
fmt.Println(result)
}
func isTestFailing(input string, failingInput string) bool {
match, _ := regexp.MatchString(failingInput, input)
return match
}
func divideInput(input string, n int) []string {
inputLength := len(input)
pieceLength := inputLength / n
pieces := make([]string, n)
for i := 0; i < n-1; i++ {
pieces[i] = input[i*pieceLength : i*pieceLength+pieceLength]
}
pieces[n-1] = input[(n-1)*pieceLength : inputLength]
return pieces
}
func mergeExceptElementAt(elements []string, skippingElementIndex int) string {
var sb strings.Builder
for i, v := range elements {
// Jump over element to skip
if i == skippingElementIndex {
continue
}
sb.WriteString(v)
}
return sb.String()
}
func executeDeltaDebugging(delta string, n int) string {
pieces := divideInput(delta, n)
deltas, nablas := divideIntoDeltasNablas(pieces)
for i, v := range deltas {
if isTestFailing(v, FailingInput) {
fmt.Printf("test fails on delta[%d] %s\n", i, v)
fmt.Println("---------------------------------------------")
return executeDeltaDebugging(v, 2)
} else if isTestFailing(nablas[i], FailingInput) {
fmt.Printf("test fails on nabla[%d] %s\n", i, nablas[i])
fmt.Println("---------------------------------------------")
return executeDeltaDebugging(nablas[i], n-1)
}
}
if canRedefineGranularity(delta, n) {
fmt.Println("test does not fail and granularity can be redefined")
fmt.Println("---------------------------------------------")
return executeDeltaDebugging(delta, n*2)
} else {
fmt.Println("test does not fail and granularity cannot be redefined")
fmt.Println("---------------------------------------------")
return delta
}
return ""
}
func canRedefineGranularity(delta string, n int) bool {
return len(delta) >= n*2
}
func divideIntoDeltasNablas(pieces []string) (deltas []string, nablas []string) {
// We need to assign to deltas and nablas
n := len(pieces)
deltas = make([]string, n)
nablas = make([]string, n)
for i, v := range pieces {
deltas[i] = v
nablas[i] = mergeExceptElementAt(pieces, i)
}
// debug
for i := 0; i < n; i++ {
fmt.Printf("delta %d \t%s\n", i, deltas[i])
fmt.Printf("nabla %d \t%s\n", i, nablas[i])
}
return deltas, nablas
}
func readInput(filepath string) string {
content, err := os.ReadFile(filepath)
if err != nil {
fmt.Println("error opening file: " + err.Error())
}
return string(content)
}