This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpectedOutput.java
213 lines (150 loc) · 3.69 KB
/
ExpectedOutput.java
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
package tests;
import static org.junit.Assert.*;
import org.junit.Test;
import java.io.IOException;
import Exceptions.RuntimeError;
import Exceptions.SyntaxError;
import Machines.TM;
public class ExpectedOutput {
@Test
public void Example1() throws SyntaxError, IOException, RuntimeError {
String code = """
// a + b
{q011111011}; // 4 + 1
#define F = {q2};
(q0, 1, 0, R, q1);
(q1, 1, 1, R, q1);
(q1, 0, 0, R, q2);
(q2, 1, 0, H, q2);
(q2, 0, 0, H, q2);
""";
assertEquals("5", run(code));
}
@Test
public void Example2() throws SyntaxError, IOException, RuntimeError {
String code = """
//Number x2
{q01111}; // 3
#define F = {q1};
(q0, 1, 0, R, q1);
(q1, 1, 0, R, q2);
(q1, 0, 0, H, q1);
(q2, 1, 1, R, q2);
(q2, 0, 0, R, q3);
(q3, 1, 1, R, q3);
(q3, 0, 1, L, q4);
(q4, 1, 1, L, q4);
(q4, 0, 0, L, q5);
(q5, 1, 1, L, q5);
(q5, 0, 1, R, q1);
""";
assertEquals("6", run(code));
}
@Test
public void Example3() throws SyntaxError, IOException, RuntimeError {
String code = """
// x - y
{0q0111110111100}; // (4, 3)
#define F = {f};
//Search for the separator
(q0, 1, 1, R, q0);
(q0, 0, 0, R, q1);
//Go to the end of y
(q1, 1, 1, R, q1);
(q1, 0, 0, L, q2);
//Substracting 1 from y
(q2, 1, 0, L, q3);
//If there is no more y, we stop
(q2, 0, 0, H, f);
//Find the separator
(q3, 1, 1, L, q3);
(q3, 0, 0, L, q4);
//Go to the beginning of x
(q4, 1, 1, L, q4);
(q4, 0, 0, R, q5);
//Start all over again
(q5, 1, 0, R, q0);
(q5, 0, 0, R, q6); //This means that x>y
//Find all the ones and delete them
(q6, 1, 0, R, q6);
(q6, 0, 0, H, f);
(f, 0, 0, H, f);
(f, 1, 1, H, f);
""";
assertEquals("1", run(code));
}
@Test
public void Example4() throws SyntaxError, IOException, RuntimeError {
String code = """
//SUMATORY FROM 1 TO X
{q01111}; // 3
#define F = {f};
//Eliminate the first one
(q0, 1, 0, R, q1);
//Slip a one
(q1, 1, 1, R, q2);
//STOP
(q1, 0, 0, H, f);
//Leave a mark
(q2, 1, 0, R, q3);
(q2, 0, 0, R, q1);
//Go to the end of x
(q3, 1, 1, R, q3);
(q3, 0, 0, R, q4);
//Go to the end and add 1
(q4, 0, 1, L, q5);
(q4, 1, 1, R, q4);
//Go to the separator
(q5, 1, 1, L, q5);
(q5, 0, 0, L, q6);
//Search for the mark, remove it and start over
(q6, 1, 1, L, q6);
(q6, 0, 1, R, q2);
(f, 0, 0, H, f);
(f, 1, 1, H, f);
""";
assertEquals("6", run(code));
}
@Test
public void Example5() throws SyntaxError, IOException, RuntimeError {
String code = """
//SUMATORY FROM 1 TO X
{q01111}; // 3
#define F = {f};
//Eliminate the first one
(q0, 1, 0, R, q1);
//Slip a one
(q1, 1, 1, R, q2);
//STOP
(q1, 0, 0, H, f);
//Leave a mark
(q2, 1, 0, R, q3);
(q2, 0, 0, R, q1);
//Go to the end of x
(q3, 1, 1, R, q3);
(q3, 0, 0, R, q4);
//Go to the end and add 1
(q4, 0, 1, L, q5);
(q4, 1, 1, R, q4);
//Go to the separator
(q5, 1, 1, L, q5);
(q5, 0, 0, L, q6);
//Search for the mark, remove it and start over
(q6, 1, 1, L, q6);
(q6, 0, 1, R, q2);
(f, 0, 0, H, f);
(f, 1, 1, H, f);
""";
assertEquals("6", run(code));
}
public static String run(String code) throws SyntaxError, IOException, RuntimeError {
TM machine = new TM(code);
int execution_code = 1;
do {
execution_code = machine.update();
if (execution_code == -1)
throw new RuntimeError("A Halt state was reached, but it wasn't a final state!");
} while (execution_code != 0);
return machine.output();
}
}