Test.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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),
  75. new HashMap<>(), new HashMap<>(), new HashMap<>(),
  76. new HashMap<>());
  77. for(Instruction i : instr) {
  78. LOGGER.print(i.toString(), null, null, null, null, null);
  79. }
  80. if(LOGGER.check(checkFile)) {
  81. done++;
  82. }
  83. }
  84. } catch(Exception ex) {
  85. System.out.println("_________________________________________");
  86. System.out.println(inFile + " failed:");
  87. System.out.println(ex.getMessage());
  88. ex.printStackTrace();
  89. }
  90. });
  91. System.out.println(String.format("%d / %d compiler tests succeeded", done, tests));
  92. }
  93. private static void forEachFile(File f, String ending, BiConsumer<File, File> bc) {
  94. if(f.isFile()) {
  95. if(!f.getName().contains(".")) {
  96. File checkFile = new File(f.getPath() + ending);
  97. if(!checkFile.exists()) {
  98. try {
  99. checkFile.createNewFile();
  100. } catch(IOException ex) {
  101. }
  102. }
  103. bc.accept(f, checkFile);
  104. }
  105. } else if(f.isDirectory()) {
  106. for(File fi : f.listFiles()) {
  107. forEachFile(fi, ending, bc);
  108. }
  109. }
  110. }
  111. }