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(true, "test", "./test/test.test");
  25. LOGGER.printAll();
  26. }
  27. private static void testOutput() {
  28. done = 0;
  29. tests = 0;
  30. forEachFile(new File("./test"), ".out", (inFile, checkFile) -> {
  31. tests++;
  32. LOGGER.reset();
  33. Script sc = new Script(PARSER, null, null, inFile.getName(), inFile.getPath());
  34. sc.run();
  35. if(LOGGER.check(checkFile)) {
  36. done++;
  37. }
  38. });
  39. System.out.println(String.format("%d / %d output tests succeeded", done, tests));
  40. }
  41. private static void testTokenizer() {
  42. done = 0;
  43. tests = 0;
  44. forEachFile(new File("./test"), ".tout", (inFile, checkFile) -> {
  45. try {
  46. try(FileInputStream in = new FileInputStream(inFile)) {
  47. tests++;
  48. Tokenizer tokenizer = new Tokenizer();
  49. LOGGER.reset();
  50. for(Token t : tokenizer.tokenize(in)) {
  51. LOGGER.print(t.toString(), null, null, null, null, null);
  52. }
  53. if(LOGGER.check(checkFile)) {
  54. done++;
  55. }
  56. }
  57. } catch(Exception ex) {
  58. ex.printStackTrace();
  59. }
  60. });
  61. System.out.println(String.format("%d / %d tokenizer tests succeeded", done, tests));
  62. }
  63. private static void testCompiler() {
  64. done = 0;
  65. tests = 0;
  66. final Compiler c = new Compiler();
  67. forEachFile(new File("./test"), ".cout", (inFile, checkFile) -> {
  68. tests++;
  69. try {
  70. try(FileInputStream in = new FileInputStream(inFile)) {
  71. Tokenizer tokenizer = new Tokenizer();
  72. LOGGER.reset();
  73. Instruction[] instr = c.compile(tokenizer.tokenize(in),
  74. new HashMap<>(), new HashMap<>(), new HashMap<>(),
  75. 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. }