1111import java .io .ByteArrayOutputStream ;
1212import java .io .FileNotFoundException ;
1313import java .io .IOException ;
14+ import java .io .InputStream ;
15+ import java .io .OutputStream ;
16+ import java .nio .charset .StandardCharsets ;
1417import java .util .Arrays ;
1518import java .util .HashMap ;
1619import java .util .Map ;
1720
18- public class TestMycology {
21+ public class TestInterpreter {
1922 private static final Interpreter .FileIOSupplier fakeSupplier = new Interpreter .FileIOSupplier () {
2023
2124 private final Map <String , byte []> files = new HashMap <>();
@@ -26,7 +29,7 @@ public byte[] readFile(String file) throws IOException {
2629 val b = files .get (file );
2730 return Arrays .copyOf (b , b .length );
2831 } else {
29- val s = TestMycology .class .getResourceAsStream ("/" + file );
32+ val s = TestInterpreter .class .getResourceAsStream ("/" + file );
3033 if (s == null ) {
3134 throw new FileNotFoundException ("Could not find resource " + file );
3235 }
@@ -49,31 +52,63 @@ public boolean writeFile(String file, byte[] data) throws IOException {
4952 }
5053 };
5154
52- @ Test
53- public void testMycology () {
54- val input = new ByteArrayInputStream (new byte [0 ]);
55- val checkingOutput = new ByteArrayOutputStream ();
56- val output = new TeeOutputStream (checkingOutput , System .out );
55+ private static byte [] readProgram (String path ) {
5756 val program = new ByteArrayOutputStream ();
5857 Assertions .assertDoesNotThrow (() -> {
59- val reader = TestMycology .class .getResourceAsStream ("/mycology.b98" );
58+ val reader = TestInterpreter .class .getResourceAsStream (path );
6059 Assertions .assertNotNull (reader );
6160 var read = 0 ;
6261 val b = new byte [4096 ];
6362 while ((read = reader .read (b )) > 0 ) {
6463 program .write (b , 0 , read );
6564 }
6665 });
67- val returnCode = Assertions .assertDoesNotThrow (() -> Interpreter .executeProgram (false , new String []{"mycology.b98" }, program .toByteArray (), 300000 , input , output , fakeSupplier ));
68- val txt = output .toString ();
66+ return program .toByteArray ();
67+ }
68+
69+ private static int interpret (String [] args , byte [] code , int iterLimit , InputStream input , OutputStream output ) {
70+ return Assertions .assertDoesNotThrow (() -> Interpreter .executeProgram (false , args , code , iterLimit , input , output , fakeSupplier ));
71+ }
72+
73+ private static InputStream nullStream () {
74+ return new ByteArrayInputStream (new byte [0 ]);
75+ }
76+
77+ @ Test
78+ public void testMycology () {
79+ val checkingOutput = new ByteArrayOutputStream ();
80+ val output = new TeeOutputStream (checkingOutput , System .out );
81+ val program = readProgram ("/mycology.b98" );
82+ val returnCode = interpret (new String []{"mycology.b98" }, program , 300000 , nullStream (), output );
83+ val txt = checkingOutput .toString ();
6984 Assertions .assertTrue (Arrays .stream (txt .split ("\n " )).noneMatch ((line ) -> {
7085 if (line .startsWith ("BAD" )) {
71- System .out .println (line );
86+ System .err .println ("Found BAD check in Mycology! Interpreter is NOT standard-compliant" );
7287 return true ;
7388 } else {
7489 return false ;
7590 }
7691 }));
7792 Assertions .assertEquals (15 , returnCode );
7893 }
94+
95+ @ Test
96+ public void testSemicolonAtStart () {
97+ System .out .println ("Testing edge case ;;.@" );
98+ val output = new ByteArrayOutputStream ();
99+ val returnCode = interpret (new String [0 ], ";;.@" .getBytes (StandardCharsets .UTF_8 ), 50 , nullStream (), output );
100+ val txt = output .toString ();
101+ Assertions .assertEquals ("0 " , txt );
102+ Assertions .assertEquals (0 , returnCode );
103+ }
104+
105+ @ Test
106+ public void testPutCharAtStart () {
107+ System .out .println ("Testing edge case 'a,@" );
108+ val output = new ByteArrayOutputStream ();
109+ val returnCode = interpret (new String [0 ], "'a,@" .getBytes (StandardCharsets .UTF_8 ), 50 , nullStream (), output );
110+ val txt = output .toString ();
111+ Assertions .assertEquals ("a" , txt );
112+ Assertions .assertEquals (0 , returnCode );
113+ }
79114}
0 commit comments