| 1 | |
| 2 | from rpython.jit.tl.braininterp import BrainInterpreter |
| 3 | |
| 4 | from StringIO import StringIO |
| 5 | |
| 6 | def run_code(code, inp): |
| 7 | inp_s = StringIO(inp) |
| 8 | out = StringIO() |
| 9 | b = BrainInterpreter() |
| 10 | b.interpret(code, inp_s, out) |
| 11 | return out.getvalue() |
| 12 | |
| 13 | def test_braintone(): |
| 14 | assert run_code("+++.","") == chr(3) |
| 15 | assert run_code("+++>+++.","") == chr(3) |
| 16 | assert run_code("""++++++++++ |
| 17 | [ The initial loop to set up useful values in the array |
| 18 | >+++++++>++++++++++>+++>+<<<<- |
| 19 | ] |
| 20 | >++. print 'H' |
| 21 | >+. print 'e' |
| 22 | +++++++. 'l' |
| 23 | . 'l' |
| 24 | +++. 'o' |
| 25 | >++. space |
| 26 | <<+++++++++++++++. 'W' |
| 27 | >. 'o' |
| 28 | +++. 'r' |
| 29 | ------. 'l' |
| 30 | --------. 'd' |
| 31 | >+. '!' |
| 32 | >. newline |
| 33 | """, "") == "Hello World!\n" |
| 34 | |
| 35 | code = ",----------[----------------------.,----------]" |
| 36 | assert run_code(code, "\x0a") == "" |
| 37 | assert run_code(code, "a\x0a") == "A" |
| 38 | assert run_code(code, "aa\x0a") == "AA" |
| 39 | assert run_code(code, "lowercase\x0a") == "LOWERCASE" |
| 40 | |
| 41 | add_code = ",>++++++[<-------->-],[<+>-],<.>." |
| 42 | |
| 43 | assert run_code(add_code, "43 ") == "7 " |
| 44 | assert run_code(add_code, "12 ") == "3 " |
| 45 |