PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Test.hx

http://github.com/andyli/hxBF
Haxe | 146 lines | 123 code | 20 blank | 3 comment | 0 complexity | c15c50210d9ebbf6f61490cb53f89e55 MD5 | raw file
  1. import hxBF.BrainFuck;
  2. import haxe.io.BytesOutput;
  3. import haxe.io.StringInput;
  4. /*
  5. Tests are from http://www.lordalcol.com/brainfuckjs/
  6. */
  7. class Test extends haxe.unit.TestCase{
  8. function testHelloWorld():Void {
  9. var p =
  10. "++++++++++
  11. [
  12. >+++++++>++++++++++>+++>+<<<<-
  13. ]
  14. >++. Loop iniziale (dopo viene stampata una H)
  15. >+. e
  16. +++++++. l
  17. . l
  18. +++. o
  19. >++.
  20. <<+++++++++++++++.
  21. >.
  22. +++.
  23. ------.
  24. --------.
  25. >+.
  26. >.";
  27. var out = new BytesOutput();
  28. var bf = new BrainFuck(p, out).run();
  29. this.assertEquals("Hello World!\n", out.getBytes().toString());
  30. }
  31. function testAddition():Void {
  32. var p = ",>++++++[<-------->-],,[<+>-]<.";
  33. var out = new BytesOutput();
  34. var bf = new BrainFuck(p, new StringInput("3+2"), out).run();
  35. this.assertEquals("5", out.getBytes().toString());
  36. }
  37. function testMultiplication():Void {
  38. var p =
  39. ",>,,>++++++++[<------<------>>-]
  40. <<[>[>+>+<<-]>>[<<+>>-]<<<-]
  41. >>>++++++[<++++++++>-]<.";
  42. var out = new BytesOutput();
  43. var bf = new BrainFuck(p, new StringInput("2*4"), out).run();
  44. this.assertEquals("8", out.getBytes().toString());
  45. }
  46. function testDivision():Void {
  47. var p =
  48. ",>,,>++++++[-<--------<-------->>] Store 2 numbers from keyboard in (0) and (1); and subtract 48 from each
  49. <<[ This is the main loop which continues until the dividend in (0) is zero
  50. >[->+>+<<] Destructively copy the divisor from (1) to (2) and (3); setting (1) to zero
  51. >[-<<- Subtract the divisor in (2) from the dividend in (0); the difference is stored in (0) and (2) is cleared
  52. [>]>>>[<[>>>-<<<[-]]>>]<<] If the dividend in (0) is zero; exit the loop
  53. >>>+ Add one to the quotient in (5)
  54. <<[-<<+>>] Destructively copy the divisor in (3) to (1)
  55. <<<] Move the stack pointer to (0) and go back to the start of the main loop
  56. >[-]>>>>[-<<<<<+>>>>>] Destructively copy the quotient in (5) to (0) (not necessary; but cleaner)
  57. <<<<++++++[-<++++++++>]<. Add 48 and print result";
  58. var out = new BytesOutput();
  59. var bf = new BrainFuck(p, new StringInput("6/2"), out).run();
  60. this.assertEquals("3", out.getBytes().toString());
  61. }
  62. function testUpperCaseAString():Void {
  63. var p = ",----------[----------------------.,----------]";
  64. var out = new BytesOutput();
  65. var bf = new BrainFuck(p, new StringInput("lordalcol\n"), out).run();
  66. this.assertEquals("LORDALCOL", out.getBytes().toString());
  67. }
  68. function testBFInterpreter():Void {
  69. var p =
  70. ">>>+[[-]>>[-]++>+>+++++++[<++++>>++<-]++>>+>+>+++++[>++>++++++<<-]+>>>,<++[[>[
  71. ->>]<[>>]<<-]<[<]<+>>[>]>[<+>-[[<+>-]>]<[[[-]<]++<-[<+++++++++>[<->-]>>]>>]]<<
  72. ]<]<[[<]>[[>]>>[>>]+[<<]<[<]<+>>-]>[>]+[->>]<<<<[[<<]<[<]+<<[+>+<<-[>-->+<<-[>
  73. +<[>>+<<-]]]>[<+>-]<]++>>-->[>]>>[>>]]<<[>>+<[[<]<]>[[<<]<[<]+[-<+>>-[<<+>++>-
  74. [<->[<<+>>-]]]<[>+<-]>]>[>]>]>[>>]>>]<<[>>+>>+>>]<<[->>>>>>>>]<<[>.>>>>>>>]<<[
  75. >->>>>>]<<[>,>>>]<<[>+>]<<[+<<]<]
  76. input a brainfuck program and its input separated by an exclamation point";
  77. var out = new BytesOutput();
  78. var bf = new BrainFuck(p, new StringInput(",.+.>,++.>,+++.<<+++.+. !000"), out).run();
  79. this.assertEquals("012345", out.getBytes().toString());
  80. }
  81. function testEndOfProgramError():Void {
  82. var out = new BytesOutput();
  83. var error = null;
  84. try {
  85. var bf = new BrainFuck("[", out).run();
  86. } catch (e:EndOfProgramError) {
  87. error = e;
  88. }
  89. this.assertEquals(EndOfProgramError, Type.getClass(error));
  90. var error = null;
  91. try {
  92. var bf = new BrainFuck("+]", out).run();
  93. } catch (e:EndOfProgramError) {
  94. error = e;
  95. }
  96. this.assertEquals(EndOfProgramError, Type.getClass(error));
  97. }
  98. function testEndOfInputError():Void {
  99. var p = ",";
  100. var out = new BytesOutput();
  101. var error = null;
  102. try {
  103. var bf = new BrainFuck(p, out).run();
  104. } catch (e:EndOfInputError) {
  105. error = e;
  106. }
  107. this.assertEquals(EndOfInputError, Type.getClass(error));
  108. }
  109. function testInvalidMemoryAccessError():Void {
  110. var p = "<.";
  111. var out = new BytesOutput();
  112. var error = null;
  113. try {
  114. var bf = new BrainFuck(p, out).run();
  115. } catch (e:InvalidMemoryAccessError) {
  116. error = e;
  117. }
  118. this.assertEquals(InvalidMemoryAccessError, Type.getClass(error));
  119. }
  120. static public function main():Void {
  121. var runner = new haxe.unit.TestRunner();
  122. runner.add(new Test());
  123. runner.run();
  124. }
  125. }