Test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include <dirent.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "test/Test.h"
  10. #include "utils/String.h"
  11. #include "tokenizer/TokenStream.h"
  12. #include "tokenizer/Tokenizer.h"
  13. static unsigned int done = 0;
  14. static unsigned int tests = 0;
  15. static bool checkPath(const String& path, const char* ending, bool(*f) (const String&, const String&)) {
  16. DIR* dir = opendir(path);
  17. if(dir == NULL) {
  18. std::cout << "cannot open '" << path << "': " << strerror(errno) << "\n";
  19. return true;
  20. }
  21. struct dirent* entry = nullptr;
  22. while(true) {
  23. entry = readdir(dir);
  24. if(entry == nullptr) {
  25. break;
  26. } else if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
  27. continue;
  28. } else if(entry->d_type == DT_DIR) {
  29. checkPath(path + "/" + entry->d_name, ending, f);
  30. } else if(entry->d_type == DT_REG && strchr(entry->d_name, '.') == NULL) {
  31. if(f(path + "/" + entry->d_name, path + "/" + entry->d_name + ending)) {
  32. return true;
  33. }
  34. }
  35. }
  36. if(closedir(dir)) {
  37. std::cout << "cannot close '" << path << "': " << strerror(errno) << "\n";
  38. return true;
  39. }
  40. return false;
  41. }
  42. static String readLine(std::ifstream& f) {
  43. String s;
  44. while(true) {
  45. char c = f.get();
  46. if(!f.good() || c == '\n') {
  47. break;
  48. }
  49. s += c;
  50. }
  51. return s;
  52. }
  53. static bool testTokenizer(const String& input, const String& output) {
  54. tests++;
  55. std::ifstream oStream;
  56. oStream.open(output);
  57. if(!oStream.good()) {
  58. std::cout << "cannot open file '" << output << "'\n";
  59. return true;
  60. }
  61. TokenStream tokenStream;
  62. if(Tokenizer::tokenize(tokenStream, input)) {
  63. return true;
  64. }
  65. while(true) {
  66. String expected = readLine(oStream);
  67. if(expected.getLength() == 0) {
  68. break;
  69. } else if(!tokenStream.hasToken()) {
  70. std::cout << "error in '" << input << "\n'out of tokens\n";
  71. return false;
  72. }
  73. String buffer = tokenStream.nextTokenString();
  74. if(strchr(buffer, '\n') != nullptr) {
  75. expected += '\n';
  76. expected += readLine(oStream);
  77. }
  78. if(strcmp(buffer, expected) != 0) {
  79. std::cout << "error in '" << input << "\n'" << buffer << "' should be '" << expected << "'\n";
  80. return false;
  81. }
  82. }
  83. done++;
  84. return false;
  85. }
  86. static void test_testTokenizer(const char* path) {
  87. done = 0;
  88. tests = 0;
  89. checkPath(path, ".tout", testTokenizer);
  90. std::cout << done << " / " << tests << " tokenizer tests succeeded" << std::endl;
  91. }
  92. void Test::start(const char* path) {
  93. test_testTokenizer(path);
  94. //testCompiler();
  95. //testOutput();
  96. }