Test.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package me.hammerle.snuviscript.test;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.HashMap;
  6. import java.util.function.BiConsumer;
  7. import me.hammerle.snuviscript.code.Script;
  8. import me.hammerle.snuviscript.code.ScriptManager;
  9. import me.hammerle.snuviscript.tokenizer.Tokenizer;
  10. import me.hammerle.snuviscript.tokenizer.Token;
  11. import me.hammerle.snuviscript.code.Compiler;
  12. import me.hammerle.snuviscript.instructions.Instruction;
  13. public class Test {
  14. private static final TestScheduler SCHEDULER = new TestScheduler();
  15. private static final TestLogger LOGGER = new TestLogger();
  16. private static final ScriptManager PARSER = new ScriptManager(LOGGER, SCHEDULER);
  17. private static int done = 0;
  18. private static int tests = 0;
  19. public static void test() {
  20. testTokenizer();
  21. testCompiler();
  22. testOutput();
  23. // LOGGER.reset();
  24. // PARSER.startScript("test", "./test/test.test");
  25. // SCHEDULER.execute();
  26. // LOGGER.printAll();
  27. }
  28. private static void testOutput() {
  29. done = 0;
  30. tests = 0;
  31. forEachFile(new File("./test"), ".out", (inFile, checkFile) -> {
  32. tests++;
  33. LOGGER.reset();
  34. Script sc = new Script(PARSER, null, inFile.getName(), inFile.getPath());
  35. sc.run();
  36. if(LOGGER.check(checkFile)) {
  37. done++;
  38. }
  39. });
  40. System.out.println(String.format("%d / %d output tests succeeded", done, tests));
  41. }
  42. private static void testTokenizer() {
  43. done = 0;
  44. tests = 0;
  45. forEachFile(new File("./test"), ".tout", (inFile, checkFile) -> {
  46. try {
  47. try(FileInputStream in = new FileInputStream(inFile)) {
  48. tests++;
  49. Tokenizer tokenizer = new Tokenizer();
  50. LOGGER.reset();
  51. for(Token t : tokenizer.tokenize(in)) {
  52. LOGGER.print(t.toString(), null, null, null, null, null);
  53. }
  54. if(LOGGER.check(checkFile)) {
  55. done++;
  56. }
  57. }
  58. } catch(Exception ex) {
  59. ex.printStackTrace();
  60. }
  61. });
  62. System.out.println(String.format("%d / %d tokenizer tests succeeded", done, tests));
  63. }
  64. private static void testCompiler() {
  65. done = 0;
  66. tests = 0;
  67. final Compiler c = new Compiler();
  68. forEachFile(new File("./test"), ".cout", (inFile, checkFile) -> {
  69. tests++;
  70. try {
  71. try(FileInputStream in = new FileInputStream(inFile)) {
  72. Tokenizer tokenizer = new Tokenizer();
  73. LOGGER.reset();
  74. Instruction[] instr = c.compile(tokenizer.tokenize(in), new HashMap<>(),
  75. new HashMap<>(), new HashMap<>(), new HashMap<>());
  76. for(Instruction i : instr) {
  77. LOGGER.print(i.toString(), null, null, null, null, null);
  78. }
  79. if(LOGGER.check(checkFile)) {
  80. done++;
  81. }
  82. }
  83. } catch(Exception ex) {
  84. System.out.println("_________________________________________");
  85. System.out.println(inFile + " failed:");
  86. System.out.println(ex.getMessage());
  87. ex.printStackTrace();
  88. }
  89. });
  90. System.out.println(String.format("%d / %d compiler tests succeeded", done, tests));
  91. }
  92. private static void forEachFile(File f, String ending, BiConsumer<File, File> bc) {
  93. if(f.isFile()) {
  94. if(!f.getName().contains(".")) {
  95. File checkFile = new File(f.getPath() + ending);
  96. if(!checkFile.exists()) {
  97. try {
  98. checkFile.createNewFile();
  99. } catch(IOException ex) {
  100. }
  101. }
  102. bc.accept(f, checkFile);
  103. }
  104. } else if(f.isDirectory()) {
  105. for(File fi : f.listFiles()) {
  106. forEachFile(fi, ending, bc);
  107. }
  108. }
  109. }
  110. }