Test.cpp 4.1 KB

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