Browse files

Wrap around data cell [GH-1]

  • Loading branch information...
1 parent 9b67d43 commit 7949b7293725d45fe4aaa234052498230ed13e77 @krekoten committed Apr 8, 2012
Showing with 14 additions and 0 deletions.
  1. +2 −0 lib/brain_love/vm.rb
  2. +12 −0 spec/brain_love/vm_spec.rb
View
2 lib/brain_love/vm.rb
@@ -66,8 +66,10 @@ def execute
when DEC_DP
@dp -= 1
when INC_BYTE
+ @data[@dp] = 0 if @data[@dp] == 255
@data[@dp] += 1
when DEC_BYTE
+ @data[@dp] = 256 if @data[@dp] == 0
@data[@dp] -= 1
when PUTC
@output.putc(@data[@dp].chr)
View
12 spec/brain_love/vm_spec.rb
@@ -40,6 +40,12 @@
it "increments the byte at the pointer by one" do
expect { vm.execute }.to change { vm.data[1] }.by(1)
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
describe "DEC_BYTE" do
@@ -48,6 +54,12 @@
it "decrements the byte at the pointer by one" do
expect { vm.execute }.to change { vm.data[1] }.by(1)
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
describe "PUTC" do

0 comments on commit 7949b72

Please sign in to comment.