-
Notifications
You must be signed in to change notification settings - Fork 0
/
turmin.test.js
281 lines (248 loc) · 8.3 KB
/
turmin.test.js
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
const turmin = require('./turmin')
test('error: invalid syntax', () => {
expect(() => turmin('x')).toThrow('Syntax error: invalid code')
expect(() => turmin('s')).toThrow('Syntax error: invalid code')
expect(() => turmin('ds')).toThrow('Syntax error: invalid code')
expect(() => turmin('ls')).toThrow('Syntax error: invalid code')
expect(() => turmin('rs')).toThrow('Syntax error: invalid code')
expect(() => turmin('rs')).toThrow('Syntax error: invalid code')
expect(() => turmin('j')).toThrow('Syntax error: invalid code')
expect(() => turmin('js')).toThrow('Syntax error: invalid code')
expect(() => turmin('jx001')).toThrow('Syntax error: invalid code')
expect(() => turmin('jxs')).toThrow('Syntax error: invalid code')
expect(() => turmin('jxl')).toThrow('Syntax error: invalid code')
expect(() => turmin('jxr')).toThrow('Syntax error: invalid code')
expect(() => turmin(':')).toThrow('Syntax error: invalid code')
expect(() => turmin(':x')).toThrow('Syntax error: invalid code')
expect(() => turmin(':x1')).toThrow('Syntax error: invalid code')
expect(() => turmin(':1x1')).toThrow('Syntax error: invalid code')
expect(() => turmin(':0')).toThrow('Syntax error: invalid code')
expect(() => turmin(':00')).toThrow('Syntax error: invalid code')
expect(() => turmin(':001')).toThrow('Syntax error: invalid code')
})
test('comments', () => {
expect(turmin('/')).toEqual('')
expect(turmin('/\\')).toEqual('')
expect(turmin('/ sx')).toEqual('')
expect(turmin('/ sx \\')).toEqual('')
expect(turmin('sx / sy \\')).toEqual('x')
expect(turmin('sx / sy \\ r sz')).toEqual('xz')
})
test('set/right/left', () => {
expect(turmin('ss')).toEqual('s')
expect(turmin('ssss')).toEqual('s')
expect(turmin('sxss')).toEqual('s')
expect(turmin('sssx')).toEqual('x')
expect(turmin('ssrsx')).toEqual('sx')
expect(turmin('s ', '123')).toEqual('23')
expect(turmin('s rs ', '123')).toEqual('3')
expect(turmin('ssrsxlsy')).toEqual('yx')
expect(turmin(' ss r sx l sy ')).toEqual('yx')
expect(turmin(`
ss r sx l
sy
`)).toEqual('yx')
})
test('jump', () => {
expect(turmin('j 2 sy')).toEqual('')
expect(turmin('sx jx3 sy')).toEqual('x')
expect(turmin('sx jy3 sy')).toEqual('y')
expect(turmin('jx2 sx', 'y')).toEqual('x')
expect(turmin('jx2 sx', 'x')).toEqual('x')
expect(turmin('jy4 sy r jx0', 'xxx')).toEqual('yyy')
})
test('debug', () => {
let mem = [], head = [], step = []
const onDebug = (m, h, s) => {
mem.push(m)
head.push(h)
step.push(s)
}
turmin('sA r sB d l sX d', null, null, onDebug)
expect(mem).toStrictEqual([['A', 'B'], ['X', 'B']])
expect(head).toStrictEqual([1, 0])
expect(step).toStrictEqual([3, 5])
})
test('debug 2', () => {
let mem = [], head = [], step = []
const onDebug = (m, h, s) => {
mem.push(m)
head.push(h)
step.push(s)
}
turmin('d sA r sB d l sX d', null, null, onDebug)
expect(mem).toStrictEqual([[], ['A', 'B'], ['X', 'B']])
expect(head).toStrictEqual([0, 1, 0])
expect(step).toStrictEqual([0, 3, 5])
})
test('labels', () => {
expect(() => turmin('j 01')).toThrow('Syntax error: invalid label')
expect(turmin('j 01sxr:01sy')).toEqual('y')
expect(turmin('j 0789sxr:0789sy')).toEqual('y')
expect(turmin('j 0789 sxr :0789 sy')).toEqual('y')
expect(turmin('j 02 :01 sxr j 03 :02 syr j 01 :03 sz')).toEqual('yxz')
})
test('output tape', () => {
expect(turmin('sxrsy')).toEqual('xy')
expect(turmin('sxrrsy')).toEqual('x y')
expect(turmin('sxrrrsy')).toEqual('x y')
expect(turmin('sxlsy')).toEqual('yx')
expect(turmin('sxllsy')).toEqual('y x')
expect(turmin('sxlllsy')).toEqual('y x')
})
test('empty program', () => {
expect(turmin('')).toEqual('')
})
test('infinite loop', () => {
expect(() => turmin('j 0 ', null, 100)).toThrow('Maximal steps exceeded')
expect(() => turmin('sxjx0 ', null, 100)).toThrow('Maximal steps exceeded')
expect(() => turmin('sxjx1 ', null, 100)).toThrow('Maximal steps exceeded')
})
test('addition', () => {
const add = `
j 3 r j|0 / move to the next number
s| / replace a space with a tally mark
r j|4 / to the end of the second number
l s / erase the last tally mark
`
expect(turmin(add, '| |')).toEqual('||')
expect(turmin(add, '|| |')).toEqual('|||')
expect(turmin(add, '|| |||')).toEqual('|||||')
expect(turmin(add, '||| |||||')).toEqual('||||||||')
})
test('palindrome', () => { // prints 1 if a palindrome is on tape
const pal = `
j 27
l jx1 jy1 / to begininng
r jx7 jy17 / check the rightmost symbol
/ check x
s
r jx8jy8 //8
l jy29 s l jx0jy0 //11
/ check y
s
r jx18jy18 //18
l jx29 s l jx0jy0 //21
/ accept (print 1)
s1 j130 //27
/ erase tape
s l jx29jy29 //29
`
expect(turmin(pal, 'x')).toEqual('1')
expect(turmin(pal, 'y')).toEqual('1')
expect(turmin(pal, 'xx')).toEqual('1')
expect(turmin(pal, 'yy')).toEqual('1')
expect(turmin(pal, 'xyx')).toEqual('1')
expect(turmin(pal, 'yxy')).toEqual('1')
expect(turmin(pal, 'xyxyx')).toEqual('1')
expect(turmin(pal, 'xyxxyx')).toEqual('1')
expect(turmin(pal, 'yyxyy')).toEqual('1')
expect(turmin(pal, 'xxxyyxyyxxx')).toEqual('1')
expect(turmin(pal, 'xy')).toEqual('')
expect(turmin(pal, 'yx')).toEqual('')
expect(turmin(pal, 'yxyx')).toEqual('')
expect(turmin(pal, 'yxyxyx')).toEqual('')
expect(turmin(pal, 'xyxyxy')).toEqual('')
expect(turmin(pal, 'xxxy')).toEqual('')
})
test('fibonacci sequence', () => { // on tape: unary count of iterations
const fib = `
/ decrement iteration
rj|0 //0
rj|2 //2
r j 999 l / halt if no iterations left
rj|7 //7
l s //9
/ back to the left tape begin
lj|11 //11
lj|13 //13
lj|15 //15
r
/ to the second number
/ mark last digit
rj|18 //18
rj|20 //20
ls+ //22
/ back to the left tape begin
/ add a new digit to the begin
lj|24 //24
lj|26 //26
lj 32 //28
lj|30 //30
s| //32
/ to the third number
rj|33 //33
rj|35 //35
r j+43 l / all digits marked
rj|40 //40
j+22 / repeat
/ remove marks
s|rj+43 //43
/ join the second and third numbers (add)
sx l s
lj|49 //49
s|
/ shift the iterations number
rj|52 //52
rs|
rj|56 //56
ls
/ to the left tape begin
lj|60 //60
lj|62 //62
lj|64 //64
/ new iteration
r j|0
`
expect(turmin(fib, '| | |')).toEqual('| ||') // 1 2
expect(turmin(fib, '| | ||')).toEqual('|| |||') // 1 2
expect(turmin(fib, '| | |||')).toEqual('||| |||||') // 3 5
expect(turmin(fib, '| || |||')).toEqual('||||| ||||||||') // 5 8
expect(turmin(fib, '|| ||| |')).toEqual('||| |||||') // 3 5
expect(turmin(fib, '|| ||| ||')).toEqual('||||| ||||||||') // 5 8
expect(turmin(fib, '|| ||| |||')).toEqual('|||||||| |||||||||||||') // 8 13
expect(turmin(fib, '|| ||| ||||')).toEqual('||||||||||||| |||||||||||||||||||||') // 13 21
})
test('Hello World', () => {
expect(turmin('sHrserslrslrsors,rs rsWrsorsrrslrsdrs!')).toEqual('Hello, World!')
})
// ┌─A/X/R──[S1]──B/B/L──>[S2]
// └─────────^
test('Turing machine', () => {
expect(turmin(`
/ S1
jB3 sX r jA0
/ S2
l
`, 'AAAB')).toEqual('XXXB')
})
// productions: (011, 10, 101)
test('cyclic tag system', () => {
const cts = `
/ 011
j 51 / halt on empty
j014 / next production
rj02j12 / move rightmost
s0rs1rs1 / append 011
lj010j110r / move leftmost
s r d / delete + debug
/ 10
j 51 / halt on empty
j029 / next production
rj019j119 / move rightmost
s1rs0 / append 10
lj025j125r / move leftmost
s r d / delete + debug
/ 101
j 51 / halt on empty
j046 / next production
rj034j134 / move rightmost
s1rs0rs1 / append 101
lj042j142r / move leftmost
s r d / delete + debug
j00j10 / repeat
`
const mem = []
turmin(cts, '1', 1000, m => mem.push(m.join('').trim()))
expect(mem.slice(0, 6)).toStrictEqual(['011', '11', '1101', '101011', '0101110', '101110'])
})