-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser_test.go
More file actions
218 lines (190 loc) · 5.47 KB
/
parser_test.go
File metadata and controls
218 lines (190 loc) · 5.47 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
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
package boolgebra
import "testing"
func TestScan(t *testing.T) {
data := []struct {
src string
want []token
}{
{"", nil},
{"abc", []token{{0, tkIdentifier, "abc"}}},
{"a b", []token{{0, tkIdentifier, "a"}, {2, tkIdentifier, "b"}}},
{"a and b", []token{{0, tkIdentifier, "a"}, {2, tkLongAnd, "and"}, {6, tkIdentifier, "b"}}},
{"a & b", []token{{0, tkIdentifier, "a"}, {2, tkAnd, "&"}, {4, tkIdentifier, "b"}}},
{"a | b", []token{{0, tkIdentifier, "a"}, {2, tkOr, "|"}, {4, tkIdentifier, "b"}}},
{"a ^ b", []token{{0, tkIdentifier, "a"}, {2, tkXor, "^"}, {4, tkIdentifier, "b"}}},
{"a not b", []token{{0, tkIdentifier, "a"}, {2, tkNot, "not"}, {6, tkIdentifier, "b"}}},
{"not a b", []token{{0, tkNot, "not"}, {4, tkIdentifier, "a"}, {6, tkIdentifier, "b"}}},
{"a != b", []token{{0, tkIdentifier, "a"}, {2, tkNeq, "!="}, {5, tkIdentifier, "b"}}},
{"a => b", []token{{0, tkIdentifier, "a"}, {2, tkImpl, "=>"}, {5, tkIdentifier, "b"}}},
{"a <=> b", []token{{0, tkIdentifier, "a"}, {2, tkEq, "<=>"}, {6, tkIdentifier, "b"}}},
{"a () b", []token{{0, tkIdentifier, "a"}, {2, tkLParen, "("}, {3, tkRParen, ")"}, {5, tkIdentifier, "b"}}},
//lit
{"true", []token{{0, tkTrue, "true"}}},
{"false", []token{{0, tkFalse, "false"}}},
}
for _, td := range data {
t.Run(td.src, func(t *testing.T) {
got := make([]token, 0, len(td.want))
s := newParser([]byte(td.src))
for {
next := s.peek()
tk := s.next()
if tk.kind == tkEOF {
break
}
got = append(got, tk)
if next != tk {
t.Errorf("Unexpected peek function behavior got %v want %v", next, tk)
}
}
// Compare results
if len(td.want) != len(got) {
t.Errorf("Error in scan(%q) size mismatch, got %v want %v", td.src, got, td.want)
return
}
for i := range td.want {
w, g := td.want[i], got[i]
if w != g {
t.Errorf("Error in scan(%q) content mismatch, got %v want %v", td.src, got, td.want)
return
}
}
})
}
}
func TestParse(t *testing.T) {
a := ID("a")
b := ID("b")
c := ID("c")
data := []struct {
src string
want Expr
}{
{"a b c", ID("a b c")},
{"a b c | c d e", Or(ID("a b c"), ID("c d e"))},
{"a & b", And(a, b)},
{"a ^ b", Xor(a, b)},
{"a | b", Or(a, b)},
{"a <=> b", Eq(a, b)},
{"a != b", Xor(a, b)},
{"a => b", Impl(a, b)},
// Composiing ops
{"a & b ^ c", Xor(And(a, b), c)},
{"a & b | c", Or(And(a, b), c)},
{"a & b <=> a & b", Eq(And(a, b), And(a, b))},
{"a & b != a & b", Xor(And(a, b), And(a, b))},
{"a & b => a & b", Impl(And(a, b), And(a, b))},
{"a & (b | c)", And(a, Or(b, c))},
// not case
{"a not b c", Not(ID("a b c"))},
{"a b not c", Not(ID("a b c"))},
{"not a b c", Not(ID("a b c"))},
{"not (a & b)", Not(And(a, b))},
{"not a & b", And(Not(a), b)},
{"a & (b | c)", And(a, Or(b, c))},
// command call (ID is the first builtin function)
{"Reduce a => a", Lit(true)},
{"Reduce (a => a)", Lit(true)},
{"(Reduce a => a) & b", b},
{"Reduce a => b and b => a", Eq(a,b) },
{"Ascertain a | b", Lit(true)},
{"Ascertain a&b | a&c", a},
//{"Exactly 1 in a&b , a&c", a},
// lit
{"true", Lit(true)},
{"false", Lit(false)},
{"a & false", And(a, Lit(false))},
}
for _, td := range data {
t.Run(td.src, func(t *testing.T) {
l := newParser([]byte(td.src))
got, err := l.parse(0)
if err != nil {
t.Fatalf("parse error: %v", err)
}
eq := Simplify(Xor(td.want, got))
t.Logf("got %v \nwant %v \nequ %v", got, td.want, eq)
if !eq.Is(false) {
t.Errorf("parse(%q) error: got %v want %v", td.src, got, td.want)
}
})
}
}
func TestSmullyan(t *testing.T) {
// A Knight is someone that always tells the truth.
// A Knaves is someone that always tells a lie.
// The hero meets three guys 'a','b', 'c', each one can be a Knight or a Knaves.
// Our hero asked 'a': "are 'b' and 'c' knights ?"
// but 'a' also said that 'b' was a Knaves:
pb, err := Parse(`
( (b is knight & c is knight) <=> a is knight)
& ( a is knight <=> b is not knight )
`)
if err != nil {
t.Fatalf("parse error: %v", err)
}
res := Simplify(pb)
want, _ := Parse("a is not knight & b is knight & c is not knight")
cmp := Simplify(Xor(res, want))
if !cmp.Is(false) {
t.Errorf("invalid solution got %v want %v equ %v", res, want, cmp)
}
// Another example
// https://en.wikipedia.org/wiki/Knights_and_Knaves#Examples
// A says, "We are both knaves."
pb, err = Parse("(a is not knight & b is not knight) <=> a is knight")
if err != nil {
t.Fatalf("parse error: %v", err)
}
res = Simplify(pb)
want, _ = Parse("a is not knight & b is knight")
cmp = Simplify(Xor(res, want))
if !cmp.Is(false) {
t.Errorf("invalid solution got %v want %v equ %v", res, want, cmp)
}
}
func TestFormat(t *testing.T) {
data := []struct {
src string
}{
{"a b c"},
{"a b c | c d e"},
{"a & b"},
{"a ^ b"},
{"a | b"},
{"a <=> b"},
{"a != b"},
{"a => b"},
// Composing ops
{"a & b ^ c"},
{"a & b | c"},
{"a & b <=> a & b"},
{"a & b != a & b"},
{"a & b => a & b"},
{"a & (b | c)"},
// not case
{"a not b c"},
{"a b not c"},
{"not a b c"},
{"not (a & b)"},
// lit
{"true"},
{"false"},
{"a & false"},
}
for _, td := range data {
t.Run(td.src, func(t *testing.T) {
want, err := Parse(td.src)
if err != nil {
t.Fatalf("parse error: %v", err)
}
got, err := Parse(want.String())
if err != nil {
t.Fatalf("parse error: %v", err)
}
if got.String() != want.String() {
t.Errorf("String(%q) error: got %v want %v", td.src, got, want)
}
})
}
}