File: test_brainfuck.py

package info (click to toggle)
pypy 7.3.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 113,660 kB
  • sloc: python: 1,419,707; ansic: 64,313; cpp: 3,290; sh: 2,763; makefile: 540; xml: 256; asm: 213; lisp: 45; awk: 4
file content (45 lines) | stat: -rw-r--r-- 1,316 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

from rpython.jit.tl.braininterp import BrainInterpreter

from StringIO import StringIO

def run_code(code, inp):
    inp_s = StringIO(inp)
    out = StringIO()
    b = BrainInterpreter()
    b.interpret(code, inp_s, out)
    return out.getvalue()

def test_braintone():
    assert run_code("+++.","") == chr(3)
    assert run_code("+++>+++.","") == chr(3)
    assert run_code("""++++++++++
[                   The initial loop to set up useful values in the array
   >+++++++>++++++++++>+++>+<<<<-
]
>++.                print 'H'
>+.                 print 'e'
+++++++.                  'l'
.                         'l'
+++.                      'o'
>++.                      space
<<+++++++++++++++.        'W'
>.                        'o'
+++.                      'r'
------.                   'l'
--------.                 'd'
>+.                       '!'
>.                        newline
""", "") == "Hello World!\n"

    code = ",----------[----------------------.,----------]"
    assert run_code(code, "\x0a") == ""
    assert run_code(code, "a\x0a") == "A"
    assert run_code(code, "aa\x0a") == "AA"
    assert run_code(code, "lowercase\x0a") == "LOWERCASE"

    add_code = ",>++++++[<-------->-],[<+>-],<.>."

    assert run_code(add_code, "43 ") == "7 "
    assert run_code(add_code, "12 ") == "3 "