-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstructions_spec.rb
200 lines (142 loc) · 4.16 KB
/
instructions_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
require 'spec_helper'
require 'features/feature_helper'
describe "basic instructions" do
include_context "braingasm features"
describe ">" do
before do
@machine.tape = [21, 42]
expect(@machine.dp).to be == 0
expect(@machine.cell).to be == 21
end
it "increases the data pointer, moving to the next cell" do
run ">"
expect(@machine.dp).to be == 1
expect(@machine.cell).to be == 42
end
it "takes an optional integer parameter and moves by that much" do
run "7>"
expect(@machine.dp).to be == 7
expect(@machine.cell).to be == 0
end
it "can move beyond the current end of the tape" do
@machine.tape = []
run "99>"
expect(@machine.dp).to be == 99
end
end
describe "<" do
it "decreases the data pointer, moving to the previous cell" do
@machine.tape = [1, 2, 4, 8, 16, 32]
@machine.dp = 5
expect(@machine.cell).to be == 32
run "<"
expect(@machine.dp).to be == 4
expect(@machine.cell).to be == 16
end
it "takes an optional integer parameter and moves by that much" do
@machine.dp = 10
run "3<"
expect(@machine.dp).to be == 7
end
it "can move past the current start of the tape" do
run "99<"
end
context "with # prefix" do
it "moves back to original start of tape" do
run "99>#<"
expect(@machine.dp).to be == 0
end
it "moves back to original start of tape also from the left" do
@machine.cell = 13
run "7<+#<"
expect(@machine.cell).to be == 13
end
end
end
describe "+" do
it "increases the value of current cell by one" do
run "+"
expect(@machine.cell).to be == 1
end
it "takes an optional integer parameter and increases by that much" do
run "42+"
expect(@machine.cell).to be == 42
end
end
describe "-" do
it "decreases the value of the current cell by one" do
@machine.cell = 100
run "-"
expect(@machine.cell).to be == 99
end
it "takes an optional integer parameter and decreases by that much" do
@machine.cell = 123
run "23-"
expect(@machine.cell).to be == 100
end
end
describe "*" do
it "multiplies the value of the current cell by two" do
@machine.cell = 9
run "*"
expect(@machine.cell).to be == 18
end
it "takes an optional integer parameter and multiplies with that much" do
@machine.cell = 3
run "5*"
expect(@machine.cell).to be == 15
end
end
describe "/" do
it "divides the value of the current cell by two" do
@machine.cell = 100
run "/"
expect(@machine.cell).to be == 50
end
it "takes an optional integer parameter and divides by that much" do
@machine.cell = 60
run "12/"
expect(@machine.cell).to be == 5
end
end
end
describe "tape limit (L instruction)" do
include_context "braingasm features"
describe "without a prefix" do
it "makes the tape wrap around after the current position" do
run "17+ 5> L >"
expect(@machine.dp).to be == 0
expect(@machine.cell).to be == 17
end
describe "from a negative position" do
it "wraps around to start position when going further left" do
run "7+ 5< L <"
expect(@machine.pos).to be == 0
expect(@machine.cell).to be == 7
end
it "stays within negative indices" do
run "5< L 2<"
expect(@machine.pos).to be == -1
end
it "can reach the start position moving right" do
run "5< L 5>"
expect(@machine.pos).to be == 0
end
it "wraps around when moving to the right past start position" do
run "5< L 6>"
expect(@machine.pos).to be == -5
end
end
end
describe "given an integer prefix" do
it "limits the tape to that length" do
run "5L 12[+>]"
expect(@machine.tape.take(5)).to be == [3, 3, 2, 2, 2]
expect(@machine.tape.drop(5)).to all (be 0)
end
it "makes the tape wrap back to the last cell when moving left from start position" do
run "9L <"
expect(@machine.dp).to be == 8
end
end
end