Skip to content
Snippets Groups Projects
Select Git revision
  • c018d88b73c247abf58a9d3857ab4119ca549a88
  • master default
2 results

spiframe.cpp

Blame
  • Main.java 5.80 KiB
    package tester;
    
    import com.google.gson.Gson;
    
    import java.io.*;
    import java.nio.charset.StandardCharsets;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.concurrent.atomic.AtomicBoolean;
    import java.util.stream.Stream;
    
    public class Main {
        public static void main(String[] args) throws FileNotFoundException {
            if (args.length < 2) {
                System.out.println("usage: <tested_app.jar> <testcase.json>");
                System.exit(1);
            }
            
            Gson gson = new Gson();
    
            TestCase test = gson.fromJson(new FileReader(args[1]), TestCase.class);
    
            if (System.getProperty("os.name").toLowerCase().contains("win")){
                try {
                    ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/c", "@chcp", "65001>nul").inheritIO();
                    Process p = pb.start();
                    p.waitFor();
                    System.setOut(new PrintStream(System.out, true, StandardCharsets.UTF_8));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    
            Process proc;
            try {
                proc = Runtime.getRuntime().exec(String.format("java -jar %s --testing", args[0]));
            } catch (Exception e) {
                System.out.print("❌ ");
                System.out.printf("Nem sikerület elindítani a folyamatot, kérlek ellenőrizd, hogy tesztelendő program tényleg itt van-e: \"%s\"\n", args[0]);
                System.exit(1);
                return;
            }
            
            var in = proc.getOutputStream();
            var out = proc.getInputStream();
    
            try {
                in.write(test.input.getBytes(StandardCharsets.UTF_8));
                in.flush();
                in.close();
            } catch (Exception e) {
                System.out.print("❌ ");
                System.out.println("A program nem fogadott annyi inputot, amennyi meg volt adva.");
                System.exit(1);
                return;
            }
    
            try {
                proc.waitFor();
            } catch (Exception e) {
                System.out.print("❌ ");
                System.out.println("Nem sikerült a programnak helyesen bezáródni.");
                System.exit(1);
                return;
            }
            
            String o = "";
            try {
                byte[] by = out.readAllBytes();
                o = new String(by, StandardCharsets.UTF_8);
            } catch (Exception e){
                System.out.print("❌ ");
                System.out.println("Nem sikerült kiolvasni a program outputját.");
            }
    
            System.out.println("--------------");
            System.out.println("Kimenet tesztelése");
            System.out.println();
    
            AtomicBoolean error = new AtomicBoolean(compareStrings(o, test.output));
            if (!error.get()) {
                System.out.print("✔️ ");
                System.out.println("Siker.");
            }
    
            test.files.forEach((f, e) -> {
                System.out.println();
                System.out.println();
                System.out.println("--------------");
                System.out.printf("File: %s\n", f);
                System.out.println();
                try {
                    String got = fileToString(f);
                    boolean er = compareStrings(got, e);
                    if (er) {
                        error.set(true);
                    } else {
                        System.out.print("✔️ ");
                        System.out.println("Siker.");
                    }
                } catch (Exception exception) {
                    error.set(true);
                    System.out.print("❌ ");
                    System.out.printf("Nem sikerült megnyitni a \"%s\" fájlt.\n", f);
                }
            });
    
            System.out.println();
            System.out.println("--------------");
    
    
            if (error.get()) {
                System.out.print("\uD83D\uDD25 ");
            } else {
                System.out.print("✅ ");
            }
            
            System.out.print("A program tesztelése siker");
            if (error.get()) {
                System.out.print("telenül");
            } else {
                System.out.print("esen");
            }
            System.out.println(" végződött.");
            
            if (error.get()) {
                System.exit(1);
            }
        }
        
        private static boolean compareStrings(String gotOut, String expected) {
            String[] expectedLines = expected.split("\n");
            String[] gotLines = gotOut.split("\n");
    
            boolean error = false;
            int i = 0;
            for (; i < expectedLines.length; i++) {
                String exp = expectedLines[i].trim();
                if (gotLines.length <= i) {
                    error = true;
                    System.out.print("❌ ");
                    System.out.println("A program rövidebb kimenetet produkált, mint az elvárt.");
                    System.out.printf("sor %d:\n", i);
                    System.out.printf("  várt: %s\n", exp);
                } else {
                    String got = gotLines[i].trim();
                    if (!exp.equals(got)) {
                        error = true;
                        System.out.print("❌ ");
                        System.out.printf("sor %d:\n", i);
                        System.out.printf("  várt: %s\n", exp);
                        System.out.printf("kapott: %s\n", got);
                    }
                }
            }
    
            for (; i < gotLines.length; i++) {
                error = true;
                String got = gotLines[i].trim();
                System.out.print("❌ ");
                System.out.println("A program hosszabb kimenetet produkált, mint az elvárt.");
                System.out.printf("sor %d:\n", i);
                System.out.printf("kapott: %s\n", got);
            }
            return error;
        }
        
        private static String fileToString(String path) throws IOException {
            StringBuilder build = new StringBuilder();
            Stream<String> stream = Files.lines(Paths.get(path), StandardCharsets.UTF_8);
            stream.forEach(s -> build.append(s).append("\n"));
            return build.toString();
        }
    }