-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefixes_spec.rb
337 lines (250 loc) · 7.85 KB
/
prefixes_spec.rb
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
require 'spec_helper'
require 'features/feature_helper'
describe "prefixes" do
include_context "braingasm features"
describe "numbers" do
it "work as expected" do
run "69+"
expect(@machine.cell).to be == 69
end
end
describe "$" do
describe "without a prefix" do
it "returns the value of the current cell" do
@machine.cell = 21
run "$+"
expect(@machine.cell).to be == 42
end
it "returns negative values too" do
@machine.cell = -5
run "$+"
expect(@machine.cell).to be == -10
end
end
it "can be prefixed by itself" do
@machine.tape = [3, 0, 0, 42]
run "$$,"
expect(@machine.cell).to be == 42
end
describe "given an integer" do
it "returns the value of the cell in that position" do
@machine.tape = [0, 0, 0, 99]
run "3$+"
expect(@machine.cell).to be == 99
end
it "works also when the tape is expanded backwards" do
@machine.cell = 2
run "6< 0$+ +"
expect(@machine.pos).to be == -6
expect(@machine.cell).to be == 3
expect(@machine.tape[6]).to be == 2
end
it "works with negative indices" do
run "<<<+++++>>> --- $$,"
expect(@machine.cell).to be == 5
end
end
end
describe "#" do
it "returns the value of the data pointer (current position on the tape)" do
@machine.tape[0] = 99
run "#, >>>>> #,"
expect(@machine.tape[0]).to be == 0
expect(@machine.tape[5]).to be == 5
end
it "returns a negative value if moving left from the starting position" do
run "7< #,"
expect(@machine.cell).to be == -7
end
it "always returns zero on the original tape position" do
run "<<<+++>>> #,"
expect(@machine.pos).to be == 0
expect(@machine.cell).to be == 0
end
end
describe "z" do
context "without prefix" do
it "returns 1 if the current cell is zero, 0 if not" do
@machine.tape = [1, 0]
run "z, > z,"
expect(@machine.tape[0..1]).to be == [0, 1]
end
end
context "with integer prefix" do
it "checks the prefix instead" do
run "1z, > 0z,"
expect(@machine.tape[0..1]).to be == [0, 1]
end
end
end
describe "n" do
context "without prefix" do
it "returns 1 if the current cell is non-zero, 0 if its zero" do
@machine.tape = [1, 0, -1]
run "n+ > n+ > n+"
expect(@machine.tape[0..2]).to be == [2, 0, 0]
end
end
context "with integer prefix" do
it "checks the prefix instead" do
run "<#n+ >#n+ >#n+"
expect(@machine.tape[0..2]).to be == [1, 0, 1]
end
end
end
describe "s" do
context "without prefix" do
it "returns 1 if the current cell is negative (signed), 0 otherwise" do
@machine.tape = [-2, -1, 0, 1, 2]
run "s, > s, > s, > s, > s,"
expect(@machine.tape).to be == [1, 1, 0, 0, 0]
end
end
context "with integer prefix" do
it "returns 0 if the given prefix is zero or positive" do
run "#s, > #s,"
expect(@machine.tape[0..1]).to be == [0, 0]
end
it "returns 1 if the given prefix is negative" do
run "< #s,"
expect(@machine.cell).to be == 1
end
end
end
describe "p (parity)" do
context "without prefix" do
it "returns 1 if the value of the current cell is even, 0 if it's odd" do
@machine.tape = [42, 13]
run "p, > p,"
expect(@machine.tape[0]).to be == 1
expect(@machine.tape[1]).to be == 0
end
end
context "with prefix yielding an integer" do
it "evaulates the prefix instead" do
run "4> #p, > #p,"
expect(@machine.tape[4]).to be == 1
expect(@machine.tape[5]).to be == 0
end
end
context "with an integer literal prefix" do
it "returns 1 if the current cell is divisble by the given integer, 0 otherwise" do
@machine.tape = [0, 1, 2, 3]
run "4[ 3p, > ]"
expect(@machine.tape[0]).to be == 1
expect(@machine.tape[1]).to be == 0
expect(@machine.tape[2]).to be == 0
expect(@machine.tape[3]).to be == 1
end
end
context "with two integer prefixes, the second an integer literal" do
it "checks the first, modulo the second" do
run "7> #4p, > #4p,"
expect(@machine.tape[7]).to be == 0
expect(@machine.tape[8]).to be == 1
end
end
context "with two integer prefixes" do
it "checks the first, modulo the second" do
run "6> 7#p, > 7#p,"
expect(@machine.tape[6]).to be == 0
expect(@machine.tape[7]).to be == 1
end
end
end
describe "o (oddity)" do
context "without prefix" do
it "returns 1 if the value of the current cell is odd, 0 if it's even" do
@machine.tape = [42, 13]
run "o, > o,"
expect(@machine.tape[0]).to be == 0
expect(@machine.tape[1]).to be == 1
end
end
context "with prefix yielding an integer" do
it "evaulates the prefix instead" do
run "7> #o, > #o,"
expect(@machine.tape[7]).to be == 1
expect(@machine.tape[8]).to be == 0
end
end
context "with an integer literal prefix" do
it "returns 0 if the current cell is divisble by the given integer, 1 otherwise" do
@machine.tape = [0, 1, 2, 3]
run "4[ 3o, > ]"
expect(@machine.tape[0]).to be == 0
expect(@machine.tape[1]).to be == 1
expect(@machine.tape[2]).to be == 1
expect(@machine.tape[3]).to be == 0
end
end
context "with two integer prefixes, the second an integer literal" do
it "checks the first, modulo the second" do
run "3> #4o, > #4o,"
expect(@machine.tape[3]).to be == 1
expect(@machine.tape[4]).to be == 0
end
end
context "with two integer prefixes" do
it "checks the first, modulo the second" do
run "6> 7#o, > 7#o,"
expect(@machine.tape[6]).to be == 1
expect(@machine.tape[7]).to be == 0
end
end
end
describe "q" do
context "without prefix" do
it "checks if the current cell value is a prime" do
@machine.tape = [ 2, 4, 7 ]
run "q, > q, > q,"
expect(@machine.tape).to be == [ 1, 0, 1 ]
end
end
context "given an integer" do
it "checks if it's s a prime" do
run "7q+ > 9q+"
expect(@machine.tape[0..1]).to be == [ 1, 0 ]
end
end
end
describe "r" do
context "without prefix" do
it "returns a random integer between 0 inclusive and 256 exclusive" do
srand 999 # Seed the RNG to make sure we get the same each time, for testing purposes
run "r+ > r+ > r+"
expect(@machine.tape[0]).to be == 192
expect(@machine.tape[1]).to be == 92
expect(@machine.tape[2]).to be == 101
end
end
context "with one integer prefix" do
it "returns a random integer below that number" do
run "100[ 3r+ > ]"
expect(@machine.tape).to all(be < 3)
expect(@machine.tape).to include(0)
expect(@machine.tape).to include(1)
expect(@machine.tape).to include(2)
end
it "works with non-literal integers too" do
srand 3
run "50> #r+"
expect(@machine.cell).to be == 42
end
end
context "with two integer prefixes" do
it "returns a random integer between 0 and that number" do
run "999[ 2 5r+ > ]"
expect(@machine.tape[0...100]).to all(be >= 2)
expect(@machine.tape[0...100]).to all(be <= 5)
expect(@machine.tape).to include(2)
expect(@machine.tape).to include(5)
end
it "works with non-literal integers too" do
srand 100
run "25> 3#r+"
expect(@machine.cell).to be == 11
end
end
end
end