PageRenderTime 32ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/versions/raek_levsa/UnitTests.hs

http://github.com/rickardlindberg/brainfuck
Haskell | 51 lines | 47 code | 4 blank | 0 comment | 0 complexity | d79b7985a26e75bd7dc8a6da6044d6e0 MD5 | raw file
  1. import Brainfuck
  2. import Test.Hspec.HUnit
  3. import Test.Hspec.Monadic
  4. import Test.HUnit
  5. import qualified Data.Map as M
  6. main = hspecX $ do
  7. describe "the parser" $ do
  8. it "recognized supported operations" $ do
  9. parseProgram "<>+-.," @?=
  10. [MLeft, MRight, Inc, Dec, Out, In]
  11. describe "the operation interpreter" $ do
  12. it "can execute <" $ do
  13. executeOp MLeft (Machine [] 123 M.empty) @?=
  14. ((Machine [] 122 M.empty), Nothing)
  15. it "can execute >" $ do
  16. executeOp MRight (Machine [] 123 M.empty) @?=
  17. ((Machine [] 124 M.empty), Nothing)
  18. it "can execute +" $ do
  19. executeOp Inc (Machine [] 123 (M.singleton 123 456)) @?=
  20. ((Machine [] 123 (M.singleton 123 457)), Nothing)
  21. it "can execute + on untouched cell" $ do
  22. executeOp Inc (Machine [] 123 M.empty) @?=
  23. ((Machine [] 123 (M.singleton 123 1)), Nothing)
  24. it "can execute -" $ do
  25. executeOp Dec (Machine [] 123 (M.singleton 123 456)) @?=
  26. ((Machine [] 123 (M.singleton 123 455)), Nothing)
  27. it "can execute ." $ do
  28. executeOp In (Machine [789] 123 (M.singleton 123 456)) @?=
  29. ((Machine [] 123 (M.singleton 123 789)), Nothing)
  30. it "can execute . on untouched" $ do
  31. executeOp In (Machine [789] 123 M.empty) @?=
  32. ((Machine [] 123 (M.singleton 123 789)), Nothing)
  33. it "can execute ," $ do
  34. executeOp Out (Machine [] 123 (M.singleton 123 456)) @?=
  35. ((Machine [] 123 (M.singleton 123 456)), Just 456)
  36. it "can execute ," $ do
  37. executeOp Out (Machine [] 123 M.empty) @?=
  38. ((Machine [] 123 M.empty), Just 0)
  39. describe "the interpreter" $ do
  40. it "interprets the empty program" $ do
  41. executeProgram (initialMachine []) [] @?= []
  42. it "interprets a program with one inc operation" $ do
  43. executeProgram (initialMachine []) [Inc] @?= []
  44. it "interprets a program with one out operation" $ do
  45. executeProgram (initialMachine []) [Out] @?= [0]
  46. it "interprets a program from a string" $ do
  47. execute' ",+." "a" @?= "b"