-
Notifications
You must be signed in to change notification settings - Fork 0
/
reduce_test.go
200 lines (191 loc) · 5.57 KB
/
reduce_test.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
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
package asyncpi
import (
"strings"
"testing"
)
// Tests reduction of (send | recv)
func TestReduceSendRecv(t *testing.T) {
const proc = `(new a)(a<b> | a(x).x().0)`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
p, err = SimplifyBySC(p)
if err != nil {
t.Fatalf("cannot simplify process: %v", err)
}
t.Logf("%s reduces to %s", proc, p.Calculi())
p2, ok := p.(*Recv)
if !ok {
t.Fatalf("expects *Recv but got %s (type %T)", p.Calculi(), p)
}
if want, got := "b", p2.Chan.Ident(); want != got {
t.Fatalf("expects receive on %s but got %s", want, got)
}
_, ok = p2.Cont.(*NilProcess)
if !ok {
t.Fatalf("expects *NilProcess but got %s (type %T)", p2.Cont.Calculi(), p2.Cont)
}
}
// Test reduction of (recv | send)
func TestReduceRecvSend(t *testing.T) {
const proc = `(new a)(a(x).x<> | a<b>)`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
p, err = SimplifyBySC(p)
if err != nil {
t.Fatalf("cannot simplify process: %v", err)
}
t.Logf("%s reduces to %s", proc, p.Calculi())
p2, ok := p.(*Send)
if !ok {
t.Fatalf("expects *Send but got %s (type %T)", p.Calculi(), p)
}
if want, got := "b", p2.Chan.Ident(); want != got {
t.Fatalf("expects send on %s but got %s", want, got)
}
}
// Test reduction (and simplify) where all names are bound.
func TestReduceBoundRecvSend(t *testing.T) {
const proc = `(new a)(new b)(a(x).x<> | a<b>)`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
p, err = SimplifyBySC(p)
if err != nil {
t.Fatalf("cannot simplify process: %v", err)
}
t.Logf("%s reduces to %s", proc, p.Calculi())
p2, ok := p.(*Restrict)
if !ok {
t.Fatalf("expects a *Restrict but got %s (type %T)", p.Calculi(), p)
}
if want, got := "b", p2.Name.Ident(); want != got {
t.Fatalf("expects restrict on %s but got %s", want, got)
}
p3, ok := p2.Proc.(*Send)
if !ok {
t.Fatalf("expects *Send but got %s (type %T)", p2.Calculi(), p2)
}
if want, got := "b", p3.Chan.Ident(); want != got {
t.Fatalf("expects send on %s but got %s", want, got)
}
}
func TestReduceFreeSendRecv(t *testing.T) {
const proc = `a<b> | a(x).0`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
t.Logf("%s reduces to %s", proc, p.Calculi())
}
func TestReduceNone(t *testing.T) {
const proc = `(new a)(a<b> | b(x).0)`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if changed {
t.Fatalf("expects %s to not reduce but reduced to %s", proc, p.Calculi())
}
t.Logf("%s reduces to %s (no change)", proc, p.Calculi())
}
func TestReduceBoundRecvRecv(t *testing.T) {
const proc = `(new a)(a(z).0 | a(x).0)`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if changed {
t.Fatalf("expects %s to not reduce but reduced to %s", proc, p.Calculi())
}
t.Logf("%s reduces to %s (no change)", proc, p.Calculi())
}
// Non-shared name cannot reduce.
func TestReduceFreeRecvRecv(t *testing.T) {
const proc = `a(z).0 | a(x).0`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if changed {
t.Fatalf("expects %s to note reduce but it changed to %s", proc, p.Calculi())
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if changed {
t.Fatalf("expects %s to not reduce but reduced to %s", proc, p.Calculi())
}
t.Logf("%s reduces to %s (no change)", proc, p.Calculi())
}
func TestReduceMultiple(t *testing.T) {
const proc = `(new a)(a<b,c>|a(x,y).x(z).z<y> | b<d> | d(z).0)`
p, err := Parse(strings.NewReader(proc))
if err != nil {
t.Fatal(err)
}
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
p, err = SimplifyBySC(p)
if err != nil {
t.Fatalf("cannot simplify process: %v", err)
}
t.Logf("%s reduces to %s", proc, p.Calculi())
procPrev := p.Calculi()
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
p, err = SimplifyBySC(p)
if err != nil {
t.Fatalf("cannot simplify process: %v", err)
}
t.Logf("%s reduces to %s", procPrev, p.Calculi())
procPrev = p.Calculi()
if changed, err := reduceOnce(p); err != nil {
t.Fatalf("cannot reduce: %v", err)
} else if !changed {
t.Fatalf("expects %s to reduce but unchanged", p.Calculi())
}
p, err = SimplifyBySC(p)
if err != nil {
t.Fatalf("cannot simplify process: %v", err)
}
t.Logf("%s reduces to %s", procPrev, p.Calculi())
if _, ok := p.(*NilProcess); !ok {
t.Fatalf("expects *NilProcess but got %s (type %T)", p.Calculi(), p)
}
}