|
| 1 | +package com.falsepattern.jfunge.storage; |
| 2 | + |
| 3 | +import com.falsepattern.jfunge.interpreter.Interpreter; |
| 4 | +import lombok.val; |
| 5 | +import lombok.var; |
| 6 | +import org.apache.commons.io.output.TeeOutputStream; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.ByteArrayOutputStream; |
| 12 | +import java.io.FileNotFoundException; |
| 13 | +import java.io.IOException; |
| 14 | +import java.io.InputStream; |
| 15 | +import java.io.OutputStream; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +public class TestInterpreter { |
| 22 | + private static final Interpreter.FileIOSupplier fakeSupplier = new Interpreter.FileIOSupplier() { |
| 23 | + |
| 24 | + private final Map<String, byte[]> files = new HashMap<>(); |
| 25 | + |
| 26 | + @Override |
| 27 | + public byte[] readFile(String file) throws IOException { |
| 28 | + if (files.containsKey(file)) { |
| 29 | + val b = files.get(file); |
| 30 | + return Arrays.copyOf(b, b.length); |
| 31 | + } else { |
| 32 | + try (val s = TestInterpreter.class.getResourceAsStream("/" + file)) { |
| 33 | + if (s == null) { |
| 34 | + throw new FileNotFoundException("Could not find resource " + file); |
| 35 | + } |
| 36 | + val ret = new ByteArrayOutputStream(); |
| 37 | + val b = new byte[4096]; |
| 38 | + var r = 0; |
| 39 | + while ((r = s.read(b)) > 0) { |
| 40 | + ret.write(b, 0, r); |
| 41 | + } |
| 42 | + val bytes = ret.toByteArray(); |
| 43 | + files.put(file, bytes); |
| 44 | + return Arrays.copyOf(bytes, bytes.length); |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean writeFile(String file, byte[] data) { |
| 51 | + files.put(file, Arrays.copyOf(data, data.length)); |
| 52 | + return true; |
| 53 | + } |
| 54 | + }; |
| 55 | + |
| 56 | + @SuppressWarnings("SameParameterValue") |
| 57 | + private static byte[] readProgram(String path) { |
| 58 | + val program = new ByteArrayOutputStream(); |
| 59 | + Assertions.assertDoesNotThrow(() -> { |
| 60 | + val reader = TestInterpreter.class.getResourceAsStream(path); |
| 61 | + Assertions.assertNotNull(reader); |
| 62 | + var read = 0; |
| 63 | + val b = new byte[4096]; |
| 64 | + while ((read = reader.read(b)) > 0) { |
| 65 | + program.write(b, 0, read); |
| 66 | + } |
| 67 | + }); |
| 68 | + return program.toByteArray(); |
| 69 | + } |
| 70 | + |
| 71 | + private static int interpret(String[] args, byte[] code, int iterLimit, InputStream input, OutputStream output) { |
| 72 | + return Assertions.assertDoesNotThrow(() -> Interpreter.executeProgram(false, args, code, iterLimit, input, output, fakeSupplier)); |
| 73 | + } |
| 74 | + |
| 75 | + private static InputStream nullStream() { |
| 76 | + return new ByteArrayInputStream(new byte[0]); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testMycology() { |
| 81 | + val checkingOutput = new ByteArrayOutputStream(); |
| 82 | + val output = new TeeOutputStream(checkingOutput, System.out); |
| 83 | + val program = readProgram("/mycology.b98"); |
| 84 | + val returnCode = interpret(new String[]{"mycology.b98"}, program, 300000, nullStream(), output); |
| 85 | + val txt = checkingOutput.toString(); |
| 86 | + String currentlyActiveFingerprint = null; |
| 87 | + boolean fingerprintHadError = false; |
| 88 | + boolean good = true; |
| 89 | + for (val line: txt.split("\n")) { |
| 90 | + if (line.startsWith("Testing fingerprint ")) { |
| 91 | + int start = "Testing fingerprint ".length(); |
| 92 | + currentlyActiveFingerprint = line.substring(start, start + 4); |
| 93 | + fingerprintHadError = false; |
| 94 | + } else if (line.equals("About to test detailed () behaviour with two fingerprints.")) { |
| 95 | + //Fingerprint core checks are over, stop tracking. |
| 96 | + currentlyActiveFingerprint = null; |
| 97 | + fingerprintHadError = false; |
| 98 | + } |
| 99 | + if (line.startsWith("BAD")) { |
| 100 | + if (good) { |
| 101 | + System.err.println("Found BAD check(s) in Mycology! Interpreter is NOT standard-compliant."); |
| 102 | + good = false; |
| 103 | + } |
| 104 | + if (currentlyActiveFingerprint != null) { |
| 105 | + if (!fingerprintHadError) { |
| 106 | + System.err.println("Broken fingerprint: " + currentlyActiveFingerprint); |
| 107 | + fingerprintHadError = true; |
| 108 | + } |
| 109 | + } else { |
| 110 | + System.err.println("Not inside a fingerprint test, base language spec is broken. Fix urgently!"); |
| 111 | + } |
| 112 | + System.err.print(" "); |
| 113 | + System.err.println(line); |
| 114 | + } |
| 115 | + } |
| 116 | + Assertions.assertTrue(good); |
| 117 | + Assertions.assertEquals(15, returnCode); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testSemicolonAtStart() { |
| 122 | + System.out.println("Testing edge case ;;.@"); |
| 123 | + val output = new ByteArrayOutputStream(); |
| 124 | + val returnCode = interpret(new String[0], ";;.@".getBytes(StandardCharsets.UTF_8), 50, nullStream(), output); |
| 125 | + val txt = output.toString(); |
| 126 | + Assertions.assertEquals("0 ", txt); |
| 127 | + Assertions.assertEquals(0, returnCode); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void testPutCharAtStart() { |
| 132 | + System.out.println("Testing edge case 'a,@"); |
| 133 | + val output = new ByteArrayOutputStream(); |
| 134 | + val returnCode = interpret(new String[0], "'a,@".getBytes(StandardCharsets.UTF_8), 50, nullStream(), output); |
| 135 | + val txt = output.toString(); |
| 136 | + Assertions.assertEquals("a", txt); |
| 137 | + Assertions.assertEquals(0, returnCode); |
| 138 | + } |
| 139 | +} |
0 commit comments