Skip to content

Commit

Permalink
Wrap around data cell [GH-1]
Browse files Browse the repository at this point in the history
  • Loading branch information
krekoten committed Apr 8, 2012
1 parent 9b67d43 commit 7949b72
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/brain_love/vm.rb
Expand Up @@ -66,8 +66,10 @@ def execute
when DEC_DP when DEC_DP
@dp -= 1 @dp -= 1
when INC_BYTE when INC_BYTE
@data[@dp] = 0 if @data[@dp] == 255
@data[@dp] += 1 @data[@dp] += 1
when DEC_BYTE when DEC_BYTE
@data[@dp] = 256 if @data[@dp] == 0
@data[@dp] -= 1 @data[@dp] -= 1
when PUTC when PUTC
@output.putc(@data[@dp].chr) @output.putc(@data[@dp].chr)
Expand Down
12 changes: 12 additions & 0 deletions spec/brain_love/vm_spec.rb
Expand Up @@ -40,6 +40,12 @@
it "increments the byte at the pointer by one" do it "increments the byte at the pointer by one" do
expect { vm.execute }.to change { vm.data[1] }.by(1) expect { vm.execute }.to change { vm.data[1] }.by(1)
end end

it "wraps around value" do
code = Array.new(256, BrainLove::VM::INC_BYTE).map(&:chr).join
vm = BrainLove::VM.new(code, input, output)
expect { vm.execute }.to change { vm.data[0] }.by(1)
end
end end


describe "DEC_BYTE" do describe "DEC_BYTE" do
Expand All @@ -48,6 +54,12 @@
it "decrements the byte at the pointer by one" do it "decrements the byte at the pointer by one" do
expect { vm.execute }.to change { vm.data[1] }.by(1) expect { vm.execute }.to change { vm.data[1] }.by(1)
end end

it "wraps around value" do
code = BrainLove::VM::DEC_BYTE.chr
vm = BrainLove::VM.new(code, input, output)
expect { vm.execute }.to change { vm.data[0] }.by(255)
end
end end


describe "PUTC" do describe "PUTC" do
Expand Down

0 comments on commit 7949b72

Please sign in to comment.