-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasyncpi_test.go
226 lines (207 loc) · 5.61 KB
/
asyncpi_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
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
package asyncpi
import (
"fmt"
"sort"
"strings"
"testing"
"go.nickng.io/asyncpi/internal/name"
)
type TestCase struct {
Input string
Output string
FreeNames []Name
}
var TestCases map[string]TestCase
func init() {
TestCases = map[string]TestCase{
"NilProcess": {
Input: ` 0 `,
Output: `inact`,
FreeNames: []Name{},
},
"Par": {
Input: `b().a().0 | b<> | (new x)x(a,b,c).0`,
Output: `par[ par[ recv(b,[]).recv(a,[]).inact | send(b,[]) ] | restrict(x,recv(x,[a b c]).inact) ]`,
FreeNames: newNames("a", "b"),
},
"Recv": {
Input: `a(b, c,d__). 0 `,
Output: `recv(a,[b c d__]).inact`,
FreeNames: newNames("a"),
},
"Rep": {
Input: `! a().0`,
Output: `repeat(recv(a,[]).inact)`,
FreeNames: []Name{name.New("a")},
},
"Res": {
Input: `(new x) x().0 `,
Output: `restrict(x,recv(x,[]).inact)`,
FreeNames: []Name{},
},
"Send": {
Input: `a<b, e_, b> `,
Output: `send(a,[b e_ b])`,
FreeNames: []Name{name.New("a"), name.New("b"), name.New("e_")},
},
}
}
// Tests fn(a) is a
func TestFreeName(t *testing.T) {
n := name.New("a")
freeNames := FreeNames(n)
if len(freeNames) == 1 && freeNames[0].Ident() != n.Ident() {
t.Errorf("FreeName: fn(a) does not match a: `%s` vs `%s`", freeNames, n)
}
}
// Tests fn(a) U fn(b) is a U b
func TestFreeNames(t *testing.T) {
piNames := []Name{name.New("a"), name.New("c"), name.New("b")}
freeNames := []Name{}
for _, name := range piNames {
freeNames = append(freeNames, FreeNames(name)...)
}
sort.Slice(freeNames, names(freeNames).Less)
sort.Slice(piNames, names(piNames).Less)
if len(piNames) != len(freeNames) {
t.Errorf("FreeNames: fn(a...) and a... have different sizes")
t.Fail()
}
for i := range piNames {
if piNames[i].Ident() != freeNames[i].Ident() {
t.Errorf("FreeNames: fn(a...) does not match a...: `%s` vs `%s`", freeNames, piNames)
}
}
}
// Tests parsing of nil process.
func TestParseNilProcess(t *testing.T) {
test := TestCases["NilProcess"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as nil process: `%s`\nparsed: %s",
test.Input, test.Output, proc)
}
}
// Tests parsing of parallel composition.
func TestParsePar(t *testing.T) {
test := TestCases["Par"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as par: `%s`\nparsed: %s",
test.Input, test.Output, proc)
}
}
// Tests FreeVar calculation of parallel composition.
func TestParFreeVar(t *testing.T) {
test := TestCases["Par"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if len(proc.FreeNames()) != len(test.FreeNames) {
t.Errorf("FreeNames(par): parsed and test case have different sizes: `%s` vs `%s`", proc.FreeNames(), test.FreeNames)
t.Fail()
}
for i := range test.FreeNames {
fn := proc.FreeNames()[i]
if fn.Ident() != test.FreeNames[i].Ident() {
t.Errorf("FreeNames(par): parsed and test case do not match: `%s` vs `%s`",
fn, test.FreeNames[i])
}
}
}
// Tests parsing of receive.
func TestParseRecv(t *testing.T) {
test := TestCases["Recv"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as receive: `%s`\nparsed: %s",
test.Input, test.Output, proc)
}
}
func TestParsedRepeat(t *testing.T) {
test := TestCases["Rep"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as repeat: `%s`\nparsed: %s",
test.Input, test.Output, proc)
}
}
// Tests parsing of restrict.
func TestParseRestrict(t *testing.T) {
test := TestCases["Res"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as restrict: `%s`\nparsed: %s",
test.Input, test.Output, proc)
}
}
// Tests parsing of send.
func TestParseSend(t *testing.T) {
test := TestCases["Send"]
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as send `%s`.\nparsed: %s",
test.Input, test.Output, proc)
}
}
// Tests parsing with comment.
func TestParseComment(t *testing.T) {
test := TestCase{
Input: `
a(). # Receive
# blank line
0 #end`,
Output: `recv(a,[]).inact`,
FreeNames: newNames("a"),
}
proc, err := Parse(strings.NewReader(test.Input))
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(proc.String()) != test.Output {
t.Errorf("Parse: `%s` not parsed as recv `%s`.\nparsed: %s",
test.Input, test.Output, proc)
}
}
// Tests syntax error.
func TestParseFailed(t *testing.T) {
incomplete := `(new a`
_, err := Parse(strings.NewReader(incomplete))
if err != nil {
if _, ok := err.(*ParseError); !ok {
t.Errorf("Parse: `%s` expecting parse error but got %s",
incomplete, err)
}
return
}
t.Errorf("Parse `%s` is syntactically incorrect and should return error",
incomplete)
}
// This example shows how the parser should be invoked.
func ExampleParse() {
proc, err := Parse(strings.NewReader("(new a) (a<v> | a(x).b(y).0 | b<u>)"))
if err != nil {
fmt.Println(err) // Parse failed
}
fmt.Println(proc.String())
// Output: restrict(a,par[ par[ send(a,[v]) | recv(a,[x]).recv(b,[y]).inact ] | send(b,[u]) ])
}