-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecompile_test.go
More file actions
175 lines (148 loc) · 3.58 KB
/
Copy pathdecompile_test.go
File metadata and controls
175 lines (148 loc) · 3.58 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
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
package decompile
import (
"fmt"
"testing"
"github.com/nukilabs/decompile/dominator"
"github.com/nukilabs/decompile/graph"
)
func TestComputeIntervals(t *testing.T) {
// Create a simple graph with root 1.
g := graph.New[int]()
// Set the root node.
a := g.Node(1)
g.SetRoot(a)
// Add additional nodes.
b := g.Node(2)
c := g.Node(3)
d := g.Node(4)
e := g.Node(5)
f := g.Node(6)
// Add edges to form the control flow graph:
// 1 -> 2, 2 -> 3, 3 -> 4, 4 -> 2, 2 -> 5, 5 -> 6, 6 -> 1.
g.SetEdge(a, b)
g.SetEdge(b, c)
g.SetEdge(c, d)
g.SetEdge(d, b)
g.SetEdge(b, e)
g.SetEdge(e, f)
g.SetEdge(f, a)
// Compute the intervals.
intervals := Intervals(g)
if len(intervals) != 2 {
t.Fatalf("expected 2 intervals, got %d", len(intervals))
}
// Check the first interval.
t.Log(intervals[0])
items1 := []*graph.Node[int]{a}
for _, node := range items1 {
if !intervals[0].Contains(node) {
t.Fatalf("interval 1 does not contain node %v", node)
}
}
// Check the second interval.
t.Log(intervals[1])
items2 := []*graph.Node[int]{b, c, d, e, f}
for _, node := range items2 {
if !intervals[1].Contains(node) {
t.Fatalf("interval 2 does not contain node %v", node)
}
}
}
func TestDerivedSequence(t *testing.T) {
// Create a simple graph with root 1.
g := graph.New[int]()
// Set the root node.
a := g.Node(1)
g.SetRoot(a)
// Add additional nodes.
b := g.Node(2)
c := g.Node(3)
d := g.Node(4)
e := g.Node(5)
f := g.Node(6)
// Add edges to form the control flow graph:
// 1 -> 2, 2 -> 3, 3 -> 4, 4 -> 2, 2 -> 5, 5 -> 6, 6 -> 1.
g.SetEdge(a, b)
g.SetEdge(b, c)
g.SetEdge(c, d)
g.SetEdge(d, b)
g.SetEdge(b, e)
g.SetEdge(e, f)
g.SetEdge(f, a)
// Compute the derived sequence.
graphs, intervals := DerivedSequence(g)
// Check the number of graphs.
if len(graphs) != len(intervals) {
t.Fatalf("expected same number of graphs and corresponding intervals, got %d and %d", len(graphs), len(intervals))
}
for _, graph := range graphs {
println(graph.String())
}
}
func TestStructureLoops(t *testing.T) {
// Create a simple graph with root 1.
g := graph.New[int]()
// Set the root node.
n1 := g.Node(1)
g.SetRoot(n1)
// Add additional nodes.
n2 := g.Node(2)
n3 := g.Node(3)
n4 := g.Node(4)
n5 := g.Node(5)
n6 := g.Node(6)
n7 := g.Node(7)
n8 := g.Node(8)
n9 := g.Node(9)
n10 := g.Node(10)
n11 := g.Node(11)
n12 := g.Node(12)
n13 := g.Node(13)
n14 := g.Node(14)
n15 := g.Node(15)
// Add edges to form the control flow graph:
g.SetEdge(n1, n2)
g.SetEdge(n1, n5)
g.SetEdge(n2, n3)
g.SetEdge(n2, n4)
g.SetEdge(n3, n5)
g.SetEdge(n4, n5)
g.SetEdge(n5, n6)
g.SetEdge(n6, n7)
g.SetEdge(n7, n8)
g.SetEdge(n7, n9)
g.SetEdge(n8, n9)
g.SetEdge(n8, n10)
g.SetEdge(n9, n10)
g.SetEdge(n10, n11)
g.SetEdge(n6, n12)
g.SetEdge(n12, n13)
g.SetEdge(n13, n14)
g.SetEdge(n14, n13)
g.SetEdge(n14, n15)
g.SetEdge(n15, n6)
// Compute the derived sequence.
graphs, intervals := DerivedSequence(g)
for _, graph := range graphs {
fmt.Println(graph)
}
for _, iis := range intervals {
for _, interval := range iis {
fmt.Println(interval)
}
}
// Compute the dominator tree.
dom := dominator.New(g)
// Init DFS numbering.
g.InitOrder()
// Compute the structure loops.
loops, _ := StructureLoops(g, dom)
conds := StructureTwoWayConditionals(g, dom)
// Check the structure loop.
for _, loop := range loops {
fmt.Println(loop)
}
for _, cond := range conds {
fmt.Println(cond)
}
}