From acc2dd2ae6caaa09b58d656668a0bc0c3bb32384 Mon Sep 17 00:00:00 2001 From: Robin Jadoul Date: Mon, 30 Jan 2017 17:21:00 +0100 Subject: [PATCH] Brainfuck tests --- run_tests.py | 26 ++++++++++++++++++++++++++ test_data/bf001.bf | 1 + test_data/bf001.exp | Bin 0 -> 25 bytes test_data/bf001.in | 0 test_data/bf002.bf | 20 ++++++++++++++++++++ test_data/bf002.exp | 1 + test_data/bf002.in | 0 test_data/bf003.bf | 1 + test_data/bf003.exp | 1 + test_data/bf003.in | 0 test_data/bf004.bf | 0 test_data/bf004.exp | 1 + test_data/bf004.in | 0 test_data/bf005.bf | 29 +++++++++++++++++++++++++++++ test_data/bf005.exp | 4 ++++ test_data/bf005.in | 4 ++++ test_data/bf006.bf | 29 +++++++++++++++++++++++++++++ test_data/bf006.exp | 4 ++++ test_data/bf006.in | 4 ++++ 19 files changed, 125 insertions(+) create mode 100644 test_data/bf001.bf create mode 100644 test_data/bf001.exp create mode 100644 test_data/bf001.in create mode 100644 test_data/bf002.bf create mode 100644 test_data/bf002.exp create mode 100644 test_data/bf002.in create mode 100644 test_data/bf003.bf create mode 100644 test_data/bf003.exp create mode 100644 test_data/bf003.in create mode 100644 test_data/bf004.bf create mode 100644 test_data/bf004.exp create mode 100644 test_data/bf004.in create mode 100644 test_data/bf005.bf create mode 100644 test_data/bf005.exp create mode 100644 test_data/bf005.in create mode 100644 test_data/bf006.bf create mode 100644 test_data/bf006.exp create mode 100644 test_data/bf006.in diff --git a/run_tests.py b/run_tests.py index 2102c5e..2c0d2d2 100755 --- a/run_tests.py +++ b/run_tests.py @@ -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() diff --git a/test_data/bf001.bf b/test_data/bf001.bf new file mode 100644 index 0000000..444d5c5 --- /dev/null +++ b/test_data/bf001.bf @@ -0,0 +1 @@ +++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++>++++++++++++++++<<<<<<<<<<<<<<<<-]>>>>>>>>>>.<<<<<<<<<<>>>>>>>>>>>>+.-<<<<<<<<<<<<>>>>>>>>>>>>>>++.--<<<<<<<<<<<<<<>>>>>>>>>>>>>>+++.---<<<<<<<<<<<<<<>>>>>>>>>>>>>>-.+<<<<<<<<<<<<<<>>>>>>>>>>>>>----.++++<<<<<<<<<<<<<>>>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<<<>>>>>>>>>>>>>>+++.---<<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>>----.++++<<<<<<<<<<<<<<<>>>>>>>>>>>>>>-.+<<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>>----.++++<<<<<<<<<<<<<<<>>>>>>>>>>>>>.<<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>.<<<<>>>>>>>>>>>>>>++.--<<<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>>>>>>>>>>>+++.---<<<<<<<<<<<<<<>>>>>>>>>>>>+++.---<<<<<<<<<<<<>>>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<<<>>>>>>>>>>>>>---.+++<<<<<<<<<<<<<>>>>+.-<<<<>++.--<. diff --git a/test_data/bf001.exp b/test_data/bf001.exp new file mode 100644 index 0000000000000000000000000000000000000000..b1fcbd7918ef46162c702df73e434c06ddbab7e3 GIT binary patch literal 25 gcmWGYEGo`VDJ@ng$yX@JNL46GElw^?Rpep-0C~a)hyVZp literal 0 HcmV?d00001 diff --git a/test_data/bf001.in b/test_data/bf001.in new file mode 100644 index 0000000..e69de29 diff --git a/test_data/bf002.bf b/test_data/bf002.bf new file mode 100644 index 0000000..c50dc5e --- /dev/null +++ b/test_data/bf002.bf @@ -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"! diff --git a/test_data/bf002.exp b/test_data/bf002.exp new file mode 100644 index 0000000..c793025 --- /dev/null +++ b/test_data/bf002.exp @@ -0,0 +1 @@ +7 \ No newline at end of file diff --git a/test_data/bf002.in b/test_data/bf002.in new file mode 100644 index 0000000..e69de29 diff --git a/test_data/bf003.bf b/test_data/bf003.bf new file mode 100644 index 0000000..4a423f2 --- /dev/null +++ b/test_data/bf003.bf @@ -0,0 +1 @@ +++++++++[>+++++++++<-]>.<+++++[>++++++<-]>-.+++++++..+++.<++++++++[>>++++<<-]>>.<<++++[>------<-]>.<++++[>++++++<-]>.+++.------.--------.>+. diff --git a/test_data/bf003.exp b/test_data/bf003.exp new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/test_data/bf003.exp @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/test_data/bf003.in b/test_data/bf003.in new file mode 100644 index 0000000..e69de29 diff --git a/test_data/bf004.bf b/test_data/bf004.bf new file mode 100644 index 0000000..e69de29 diff --git a/test_data/bf004.exp b/test_data/bf004.exp new file mode 100644 index 0000000..c57eff5 --- /dev/null +++ b/test_data/bf004.exp @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/test_data/bf004.in b/test_data/bf004.in new file mode 100644 index 0000000..e69de29 diff --git a/test_data/bf005.bf b/test_data/bf005.bf new file mode 100644 index 0000000..b4f4f4e --- /dev/null +++ b/test_data/bf005.bf @@ -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 + diff --git a/test_data/bf005.exp b/test_data/bf005.exp new file mode 100644 index 0000000..d58385b --- /dev/null +++ b/test_data/bf005.exp @@ -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. diff --git a/test_data/bf005.in b/test_data/bf005.in new file mode 100644 index 0000000..eba6da5 --- /dev/null +++ b/test_data/bf005.in @@ -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. diff --git a/test_data/bf006.bf b/test_data/bf006.bf new file mode 100644 index 0000000..b4f4f4e --- /dev/null +++ b/test_data/bf006.bf @@ -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 + diff --git a/test_data/bf006.exp b/test_data/bf006.exp new file mode 100644 index 0000000..eba6da5 --- /dev/null +++ b/test_data/bf006.exp @@ -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. diff --git a/test_data/bf006.in b/test_data/bf006.in new file mode 100644 index 0000000..d58385b --- /dev/null +++ b/test_data/bf006.in @@ -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.