Test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <vector>
  5. #include <dirent.h>
  6. #include "test/Test.h"
  7. #include "test/TestLogger.h"
  8. #include "tokenizer/Token.h"
  9. #include "tokenizer/Tokenizer.h"
  10. static unsigned int done = 0;
  11. static unsigned int tests = 0;
  12. static TestLogger logger;
  13. static void forEachFile(const std::string& path, const std::string& ending, void (*f) (const std::string&, const std::string&))
  14. {
  15. DIR* dir;
  16. dir = opendir(path.c_str());
  17. struct dirent* entry = nullptr;
  18. if(dir != nullptr)
  19. {
  20. while((entry = readdir(dir)) != nullptr)
  21. {
  22. if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
  23. {
  24. continue;
  25. }
  26. if(entry->d_type == DT_DIR) // Folder
  27. {
  28. forEachFile(path + "/" + entry->d_name, ending, f);
  29. }
  30. else if(entry->d_type == DT_REG) // File
  31. {
  32. if(strchr(entry->d_name, '.') == nullptr)
  33. {
  34. std::string pathInputFile = path + "/" + entry->d_name;
  35. std::string pathOutputFile = pathInputFile + ending;
  36. f(pathInputFile, pathOutputFile);
  37. }
  38. }
  39. }
  40. closedir(dir);
  41. }
  42. }
  43. static void testTokenizer(const char* path)
  44. {
  45. done = 0;
  46. tests = 0;
  47. forEachFile(path, ".tout", [](const std::string& input, const std::string& output)
  48. {
  49. tests++;
  50. std::ifstream iStream;
  51. iStream.open(input);
  52. std::ifstream oStream;
  53. oStream.open(output);
  54. if(!iStream.good() || !oStream.good())
  55. {
  56. return;
  57. }
  58. std::vector<Token> tokens;
  59. try
  60. {
  61. Tokenizer::tokenize(tokens, iStream);
  62. }
  63. catch(std::exception& ex)
  64. {
  65. return;
  66. }
  67. logger.reset();
  68. for(Token& token : tokens)
  69. {
  70. std::string s = token.toString();
  71. logger.print(&s);
  72. }
  73. if(logger.check(input, oStream))
  74. {
  75. done++;
  76. }
  77. });
  78. std::cout << done << " / " << tests << " tokenizer tests succeeded" << std::endl;
  79. }
  80. //void Test::testCompiler()
  81. //{
  82. // done = 0;
  83. // tests = 0;
  84. // final Compiler c = new Compiler();
  85. // forEachFile(new File("./test"), ".cout", (inFile, checkFile) ->
  86. // {
  87. // tests++;
  88. // try
  89. // {
  90. // try(FileInputStream in = new FileInputStream(inFile))
  91. // {
  92. // Tokenizer tokenizer = new Tokenizer();
  93. // LOGGER.reset();
  94. // Instruction[] instr = c.compile(tokenizer.tokenize(in),
  95. // new HashMap<>(), new HashMap<>(), new HashMap<>(),
  96. // new HashMap<>());
  97. // for(Instruction i : instr)
  98. // {
  99. // LOGGER.print(i.toString(), null, null, null, null, -1);
  100. // }
  101. // if(LOGGER.check(checkFile))
  102. // {
  103. // done++;
  104. // }
  105. // }
  106. // }
  107. // catch(Exception ex)
  108. // {
  109. // System.out.println("_________________________________________");
  110. // System.out.println(inFile + " failed:");
  111. // System.out.println(ex.getMessage());
  112. // ex.printStackTrace();
  113. // }
  114. // });
  115. // System.out.println(String.format("%d / %d compiler tests succeeded", done, tests));
  116. //}
  117. //
  118. //void Test::testOutput()
  119. //{
  120. // done = 0;
  121. // tests = 0;
  122. // forEachFile(new File("./test"), ".out", (inFile, checkFile) ->
  123. // {
  124. // tests++;
  125. //
  126. // LOGGER.reset();
  127. //
  128. // Script sc = new Script(PARSER, null, null, inFile.getName(), inFile.getPath());
  129. // sc.run();
  130. //
  131. // if(LOGGER.check(checkFile))
  132. // {
  133. // done++;
  134. // }
  135. // });
  136. // System.out.println(String.format("%d / %d output tests succeeded", done, tests));
  137. //}
  138. void Test::start(const char* path)
  139. {
  140. testTokenizer(path);
  141. //testCompiler();
  142. //testOutput();
  143. }