-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmachine_test.go
81 lines (73 loc) · 2.36 KB
/
machine_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
package turing
import (
"strings"
"testing"
)
func TestMachineExample1(t *testing.T) {
m := NewMachine(MachineInput{
MConfigurations: []MConfiguration{
{"b", []string{" "}, []string{"P0", "R"}, "c"},
{"c", []string{" "}, []string{"R"}, "e"},
{"e", []string{" "}, []string{"P1", "R"}, "k"},
{"k", []string{" "}, []string{"R"}, "b"},
},
})
m.MoveN(50)
checkTape(t, m.TapeString(), "0 1 0 1 0 1 0 1 0 1 0 1")
}
func TestMachineExample1Short(t *testing.T) {
m := NewMachine(MachineInput{
MConfigurations: []MConfiguration{
{"b", []string{" "}, []string{"P0"}, "b"},
{"b", []string{"0"}, []string{"R", "R", "P1"}, "b"},
{"b", []string{"1"}, []string{"R", "R", "P0"}, "b"},
},
})
m.MoveN(50)
checkTape(t, m.TapeString(), "0 1 0 1 0 1 0 1 0 1 0 1")
}
func TestMachineExample2(t *testing.T) {
m := NewMachine(MachineInput{
MConfigurations: []MConfiguration{
{"b", []string{"*", " "}, []string{"Pe", "R", "Pe", "R", "P0", "R", "R", "P0", "L", "L"}, "o"},
{"o", []string{"1"}, []string{"R", "Px", "L", "L", "L"}, "o"},
{"o", []string{"0"}, []string{}, "q"},
{"q", []string{"0", "1"}, []string{"R", "R"}, "q"},
{"q", []string{" "}, []string{"P1", "L"}, "p"},
{"p", []string{"x"}, []string{"E", "R"}, "q"},
{"p", []string{"e"}, []string{"R"}, "f"},
{"p", []string{" "}, []string{"L", "L"}, "p"},
{"f", []string{"*"}, []string{"R", "R"}, "f"},
{"f", []string{" "}, []string{"P0", "L", "L"}, "o"},
},
})
m.Move()
checkCompleteConfiguration(t, m.CompleteConfiguration(), "eeo0 0")
m.Move()
checkCompleteConfiguration(t, m.CompleteConfiguration(), "eeq0 0")
m.Move()
checkCompleteConfiguration(t, m.CompleteConfiguration(), "ee0 q0")
m.Move()
checkCompleteConfiguration(t, m.CompleteConfiguration(), "ee0 0 q")
m.Move()
checkCompleteConfiguration(t, m.CompleteConfiguration(), "ee0 0p 1")
// ...
m.MoveN(200)
checkTape(t, m.TapeString(), "ee0 0 1 0 1 1 0 1 1 1 0 1 1 1 1")
}
func checkTape(t *testing.T, tape string, expectedStart string) {
if !strings.HasPrefix(tape, expectedStart) {
var actual string
if len(expectedStart)+10 <= len(tape) {
actual = tape[0 : len(expectedStart)+10]
} else {
actual = tape
}
t.Errorf("got %s, want %s", actual, expectedStart)
}
}
func checkCompleteConfiguration(t *testing.T, actual string, expected string) {
if actual != expected {
t.Errorf("got %s, want %s", actual, expected)
}
}