Test.cpp 4.1 KB

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