Brainfuck tests

This commit is contained in:
Robin Jadoul 2017-01-30 17:21:00 +01:00
parent 4a24fd1589
commit acc2dd2ae6
19 changed files with 125 additions and 0 deletions

View File

@ -26,6 +26,7 @@ import unittest, subprocess, filecmp, argparse, os.path, os, shutil
REFERENCE_DIR = os.path.join(os.path.abspath(os.path.dirname(__file__)), "test_data")
JSON_TESTS = [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1]
BF_TESTS = [0, 0, 0, 0, 0, 0]
def make_json_test(idx, should_fail=0):
def test(self):
@ -60,6 +61,28 @@ def make_json_test(idx, should_fail=0):
return test
def make_bf_test(idx, should_fail=False):
def test(self):
argpath = os.path.join(REFERENCE_DIR, "bf%03d.bf" % idx)
inpath = os.path.join(REFERENCE_DIR, "bf%03d.in" % idx)
infile = open(inpath)
exppath = os.path.join(REFERENCE_DIR, "bf%03d.exp" % idx)
outpath = os.path.join(data_dir, "bf%03d.out" % idx)
outfile = open(outpath, "w")
p = subprocess.Popen([os.path.join(args.builddir, "examples", "brainfuck", "bf"), argpath], stdin=infile, stdout=outfile, stderr=subprocess.DEVNULL)
p.communicate()
infile.close()
outfile.close()
if should_fail:
self.assertNotEqual(0, p.returncode)
return
self.assertEqual(0, p.returncode)
self.assertTrue(filecmp.cmp(exppath, outpath), "Testcase %d for bf example failed" % idx)
return test
class Tests(unittest.TestCase):
pass
@ -78,4 +101,7 @@ if __name__ == "__main__":
for i, should_fail in enumerate(JSON_TESTS):
setattr(Tests, "test_json_%03d" % (i + 1), make_json_test(i + 1, should_fail))
for i, should_fail in enumerate(BF_TESTS):
setattr(Tests, "test_bf_%03d" % (i + 1), make_bf_test(i + 1, should_fail))
unittest.main()

1
test_data/bf001.bf Normal file
View File

@ -0,0 +1 @@
++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++>++++++++++++++++<<<<<<<<<<<<<<<<-]>>>>>>>>>>.<<<<<<<<<<>>>>>>>>>>>>+.-<<<<<<<<<<<<>>>>>>>>>>>>>>++.--<<<<<<<<<<<<<<>>>>>>>>>>>>>>+++.---<<<<<<<<<<<<<<>>>>>>>>>>>>>>-.+<<<<<<<<<<<<<<>>>>>>>>>>>>>----.++++<<<<<<<<<<<<<>>>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<<<>>>>>>>>>>>>>>+++.---<<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>>----.++++<<<<<<<<<<<<<<<>>>>>>>>>>>>>>-.+<<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>>----.++++<<<<<<<<<<<<<<<>>>>>>>>>>>>>.<<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>++.--<<<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>>>>>>>>>>>+++.---<<<<<<<<<<<<<<>>>>>>>>>>>>+++.---<<<<<<<<<<<<>>>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>+.-<<<<>++.--<.

BIN
test_data/bf001.exp Normal file

Binary file not shown.

0
test_data/bf001.in Normal file
View File

20
test_data/bf002.bf Normal file
View File

@ -0,0 +1,20 @@
++ Cell c0 = 2
> +++++ Cell c1 = 5
[ Start your loops with your cell pointer on the loop counter (c1 in our case)
< + Add 1 to c0
> - Subtract 1 from c1
] End your loops with the cell pointer on the loop counter
At this point our program has added 5 to 2 leaving 7 in c0 and 0 in c1
BUT we cannot output this value to the terminal since it's not ASCII encoded!
To display the ASCII character "7" we must add 48 to the value 7!
48 = 6 * 8 so let's use another loop to help us!
++++ ++++ c1 = 8 and this will be our loop counter again
[
< +++ +++ Add 6 to c0
> - Subtract 1 from c1
]
< . Print out c0 which has the value 55 which translates to "7"!

1
test_data/bf002.exp Normal file
View File

@ -0,0 +1 @@
7

0
test_data/bf002.in Normal file
View File

1
test_data/bf003.bf Normal file
View File

@ -0,0 +1 @@
++++++++[>+++++++++<-]>.<+++++[>++++++<-]>-.+++++++..+++.<++++++++[>>++++<<-]>>.<<++++[>------<-]>.<++++[>++++++<-]>.+++.------.--------.>+.

1
test_data/bf003.exp Normal file
View File

@ -0,0 +1 @@
Hello World!

0
test_data/bf003.in Normal file
View File

0
test_data/bf004.bf Normal file
View File

1
test_data/bf004.exp Normal file
View File

@ -0,0 +1 @@
Hello World!

0
test_data/bf004.in Normal file
View File

29
test_data/bf005.bf Normal file
View File

@ -0,0 +1,29 @@
-,+[ Read first character and start outer character reading loop
-[ Skip forward if character is 0
>>++++[>++++++++<-] Set up divisor (32) for division loop
(MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
<+<-[ Set up dividend (x minus 1) and enter division loop
>+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward
<[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient
<<<<<- Decrement dividend
] End division loop
]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag
>--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag
++++++++++++<[ If flag then set up divisor (13) for second division loop
(MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
>-[>+>>] Reduce divisor; Normal case: increase remainder
>[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient
<<<<<- Decrease dividend
] End division loop
>>[<+>-] Add remainder back to divisor to get a useful 13
>[ Skip forward if quotient was 0
-[ Decrement quotient and skip forward if quotient was 1
-<<[-]>> Zero quotient and divisor if quotient was 2
]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1
]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0
] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
<[-] Clear remainder from first division if second division was skipped
<.[-] Output ROT13ed character from copy and clear it
<-,+ Read next character
] End character reading loop

4
test_data/bf005.exp Normal file
View File

@ -0,0 +1,4 @@
Yberz vcfhz qbybe fvg nzrg, pbafrgrghe fnqvcfpvat ryvge, frq qvnz abahzl rvezbq
grzcbe vaivqhag hg ynober rg qbyber zntan nyvdhlnz reng, frq qvnz ibyhcghn. Ng
ireb rbf rg npphfnz rg whfgb qhb qbyberf rg rn erohz. Fgrg pyvgn xnfq thoretera,
ab frn gnxvzngn fnapghf rfg Yberz vcfhz qbybe fvg nzrg.

4
test_data/bf005.in Normal file
View File

@ -0,0 +1,4 @@
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet.

29
test_data/bf006.bf Normal file
View File

@ -0,0 +1,29 @@
-,+[ Read first character and start outer character reading loop
-[ Skip forward if character is 0
>>++++[>++++++++<-] Set up divisor (32) for division loop
(MEMORY LAYOUT: dividend copy remainder divisor quotient zero zero)
<+<-[ Set up dividend (x minus 1) and enter division loop
>+>+>-[>>>] Increase copy and remainder / reduce divisor / Normal case: skip forward
<[[>+<-]>>+>] Special case: move remainder back to divisor and increase quotient
<<<<<- Decrement dividend
] End division loop
]>>>[-]+ End skip loop; zero former divisor and reuse space for a flag
>--[-[<->+++[-]]]<[ Zero that flag unless quotient was 2 or 3; zero quotient; check flag
++++++++++++<[ If flag then set up divisor (13) for second division loop
(MEMORY LAYOUT: zero copy dividend divisor remainder quotient zero zero)
>-[>+>>] Reduce divisor; Normal case: increase remainder
>[+[<+>-]>+>>] Special case: increase remainder / move it back to divisor / increase quotient
<<<<<- Decrease dividend
] End division loop
>>[<+>-] Add remainder back to divisor to get a useful 13
>[ Skip forward if quotient was 0
-[ Decrement quotient and skip forward if quotient was 1
-<<[-]>> Zero quotient and divisor if quotient was 2
]<<[<<->>-]>> Zero divisor and subtract 13 from copy if quotient was 1
]<<[<<+>>-] Zero divisor and add 13 to copy if quotient was 0
] End outer skip loop (jump to here if ((character minus 1)/32) was not 2 or 3)
<[-] Clear remainder from first division if second division was skipped
<.[-] Output ROT13ed character from copy and clear it
<-,+ Read next character
] End character reading loop

4
test_data/bf006.exp Normal file
View File

@ -0,0 +1,4 @@
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren,
no sea takimata sanctus est Lorem ipsum dolor sit amet.

4
test_data/bf006.in Normal file
View File

@ -0,0 +1,4 @@
Yberz vcfhz qbybe fvg nzrg, pbafrgrghe fnqvcfpvat ryvge, frq qvnz abahzl rvezbq
grzcbe vaivqhag hg ynober rg qbyber zntan nyvdhlnz reng, frq qvnz ibyhcghn. Ng
ireb rbf rg npphfnz rg whfgb qhb qbyberf rg rn erohz. Fgrg pyvgn xnfq thoretera,
ab frn gnxvzngn fnapghf rfg Yberz vcfhz qbybe fvg nzrg.