Skip to content

Instantly share code, notes, and snippets.

@omnibs
Last active August 23, 2016 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omnibs/428a8ce8c3f01206b1b8689e2812b77f to your computer and use it in GitHub Desktop.
Save omnibs/428a8ce8c3f01206b1b8689e2812b77f to your computer and use it in GitHub Desktop.
defmodule BrainfuckTest do
use ExUnit.Case
import BF
@doc "
BF is our Brainfuck turing machine
Output will be a map
%{
tape: map #our tape, in the format %{0=> 0, 1=> 0, 2=> 0 ...}
ptr: int, #pointer position
output: string #brainfuck output
}
"
test "moves right" do
assert (BF.new() |> BF.run(">")).ptr == 1
end
@tag :pending
test "moves left" do
assert (BF.new() |> BF.run(">><")).ptr == 1
end
@tag :pending
test "doesnt overflow moving left" do
assert (BF.new() |> BF.run("<")).ptr == 0
end
@tag :pending
test "increments at pointer" do
assert (BF.new() |> BF.run("+")).tape[0] == 1
end
@tag :pending
test "decrements at pointer" do
assert (BF.new() |> BF.run("++-")).tape[0] == 1
end
@tag :pending
test "doesnt overflow decrements" do
assert (BF.new() |> BF.run("-")).tape[0] == 0
end
@tag :pending
test "doesnt overflow increments" do
code = String.duplicate("+", 300)
assert (BF.new() |> BF.run(code)).tape[0] == 255
end
@tag :pending
test "outputs oi" do
code = String.duplicate("+", 104)
<> ".>"
<> String.duplicate("+", 105)
<> "."
assert (BF.new() |> BF.run(code)).output == "hi"
end
@tag :pending
test "loops" do
code = "++++[>+<-]"
assert (BF.new() |> BF.run(code)).tape[1] == 4
end
@tag :pending
test "hello world" do
code = "++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++."
assert (BF.new() |> BF.run(code)).output == "Hello World!"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment