-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathut_test.go
91 lines (75 loc) · 1.92 KB
/
ut_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
package ut
import (
"fmt"
"testing"
)
func TestTokenize(t *testing.T) {
x, y := "p(f(X),h(Y,f(a)),Y)", "p(X,h(Z,Y),f(a))"
n, ix, iy := 15, 0, 8
tokens := Tokenize(x, y)
if len(tokens) != n {
t.Fatalf("len(tokens) Got: %d, Expected: %d", len(tokens), n)
}
if x != tokens[ix].Term {
t.Fatalf("tokens[%d] Got: %s, Expected: %s", ix, tokens[ix].Term, x)
}
if y != tokens[iy].Term {
t.Fatalf("tokens[%d] Got: %s, Expected: %s", iy, tokens[iy].Term, y)
}
}
func TestUT(t *testing.T) {
x, y := "p(Z,h(Z,W),f(W))", "p(f(X),h(Y,f(a)),Y)"
n, ix, iy := 12, 11, 6
ut := New(Tokenize(x, y))
if len(ut.Entries) != n {
for i, e := range ut.Entries {
t.Logf("[%d]: %s\n", i, e.Term)
}
t.Fatalf("len(ut.Entries) Got: %d, Expected: %d", len(ut.Entries), n)
}
if ut.Lookup[x] != ix {
t.Fatalf("ut.Lookup[%s] Got: %v, Expected: %d", x, ut.Lookup[x], ix)
}
if ut.Lookup[y] != iy {
t.Fatalf("ut.Lookup[%s] Got: %v, Expected: %d", y, ut.Lookup[y], iy)
}
}
func TestUnify(t *testing.T) {
x, y := "p(Z,h(Z,W),f(W))", "p(f(X),h(Y,f(a)),Y)"
ut := New(Tokenize(x, y))
ix, iy := ut.Lookup[x], ut.Lookup[y]
if !ut.Unify(ix, iy) {
t.Fatalf("ut.Unify(%d, %d) failed", ix, iy)
}
mguW := ut.MGU("W")
if mguW != "f(a)" {
t.Fatalf("Got W => %s Expected: f(a)", mguW)
}
mguX := ut.MGU("X")
if mguX != "f(a)" {
t.Fatalf("Got X => %s Expected: f(a)", mguX)
}
mguY := ut.MGU("Y")
if mguY != "f(f(a))" {
t.Fatalf("Got Y => %s Expected: f(f(a))", mguY)
}
mguZ := ut.MGU("Z")
if mguZ != "f(f(a))" {
t.Fatalf("Got Z => %s Expected: f(f(a))", mguZ)
}
}
func ExampleUnify() {
x, y := "f(X1,g(X2,X3),X2,b)", "f(g(h(a,X5),X2),X1,h(a,X4),X4)"
mgu := Unify(x, y)
fmt.Println("X1 = " + mgu["X1"])
fmt.Println("X2 = " + mgu["X2"])
fmt.Println("X3 = " + mgu["X3"])
fmt.Println("X4 = " + mgu["X4"])
fmt.Println("X5 = " + mgu["X5"])
// Output:
// X1 = g(h(a,b),h(a,b))
// X2 = h(a,b)
// X3 = h(a,b)
// X4 = b
// X5 = b
}