Test.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. {
  15. private static final TestScheduler SCHEDULER = new TestScheduler();
  16. private static final TestLogger LOGGER = new TestLogger();
  17. private static final ScriptManager PARSER = new ScriptManager(LOGGER, SCHEDULER);
  18. private static int done = 0;
  19. private static int tests = 0;
  20. public static void test()
  21. {
  22. testTokenizer();
  23. testCompiler();
  24. testOutput();
  25. LOGGER.reset();
  26. PARSER.startScript(true, "test", "./test/test.test");
  27. LOGGER.printAll();
  28. }
  29. private static void testOutput()
  30. {
  31. done = 0;
  32. tests = 0;
  33. forEachFile(new File("./test"), ".out", (inFile, checkFile) ->
  34. {
  35. tests++;
  36. LOGGER.reset();
  37. Script sc = new Script(PARSER, null, null, inFile.getName(), inFile.getPath());
  38. sc.run();
  39. if(LOGGER.check(checkFile))
  40. {
  41. done++;
  42. }
  43. });
  44. System.out.println(String.format("%d / %d output tests succeeded", done, tests));
  45. }
  46. private static void testTokenizer()
  47. {
  48. done = 0;
  49. tests = 0;
  50. forEachFile(new File("./test"), ".tout", (inFile, checkFile) ->
  51. {
  52. try
  53. {
  54. try(FileInputStream in = new FileInputStream(inFile))
  55. {
  56. tests++;
  57. Tokenizer tokenizer = new Tokenizer();
  58. LOGGER.reset();
  59. for(Token t : tokenizer.tokenize(in))
  60. {
  61. LOGGER.print(t.toString(), null, null, null, null, -1);
  62. }
  63. if(LOGGER.check(checkFile))
  64. {
  65. done++;
  66. }
  67. }
  68. }
  69. catch(Exception ex)
  70. {
  71. ex.printStackTrace();
  72. }
  73. });
  74. System.out.println(String.format("%d / %d tokenizer tests succeeded", done, tests));
  75. }
  76. private static void testCompiler()
  77. {
  78. done = 0;
  79. tests = 0;
  80. final Compiler c = new Compiler();
  81. forEachFile(new File("./test"), ".cout", (inFile, checkFile) ->
  82. {
  83. tests++;
  84. try
  85. {
  86. try(FileInputStream in = new FileInputStream(inFile))
  87. {
  88. Tokenizer tokenizer = new Tokenizer();
  89. LOGGER.reset();
  90. Instruction[] instr = c.compile(tokenizer.tokenize(in),
  91. new HashMap<>(), new HashMap<>(), new HashMap<>(),
  92. new HashMap<>());
  93. for(Instruction i : instr)
  94. {
  95. LOGGER.print(i.toString(), null, null, null, null, -1);
  96. }
  97. if(LOGGER.check(checkFile))
  98. {
  99. done++;
  100. }
  101. }
  102. }
  103. catch(Exception ex)
  104. {
  105. System.out.println("_________________________________________");
  106. System.out.println(inFile + " failed:");
  107. System.out.println(ex.getMessage());
  108. ex.printStackTrace();
  109. }
  110. });
  111. System.out.println(String.format("%d / %d compiler tests succeeded", done, tests));
  112. }
  113. private static void forEachFile(File f, String ending, BiConsumer<File, File> bc)
  114. {
  115. if(f.isFile())
  116. {
  117. if(!f.getName().contains("."))
  118. {
  119. File checkFile = new File(f.getPath() + ending);
  120. if(!checkFile.exists())
  121. {
  122. try
  123. {
  124. checkFile.createNewFile();
  125. }
  126. catch(IOException ex)
  127. {
  128. }
  129. }
  130. bc.accept(f, checkFile);
  131. }
  132. }
  133. else if(f.isDirectory())
  134. {
  135. for(File fi : f.listFiles())
  136. {
  137. forEachFile(fi, ending, bc);
  138. }
  139. }
  140. }
  141. }