#include #include #include #include #include "test/Test.h" #include "tokenizer/TokenStream.h" #include "tokenizer/Tokenizer.h" #include "utils/String.h" static unsigned int done = 0; static unsigned int tests = 0; static bool checkPath(const String& path, const char* ending, bool (*f)(const String&, const String&)) { DIR* dir = opendir(path); if(dir == nullptr) { std::cout << "cannot open '" << path << "': " << strerror(errno) << "\n"; return true; } while(true) { struct dirent* entry = readdir(dir); if(entry == nullptr) { break; } else if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } else if(entry->d_type == DT_DIR) { checkPath(path + "/" + entry->d_name, ending, f); } else if(entry->d_type == DT_REG && strchr(entry->d_name, '.') == nullptr) { if(f(path + "/" + entry->d_name, path + "/" + entry->d_name + ending)) { return true; } } } if(closedir(dir)) { std::cout << "cannot close '" << path << "': " << strerror(errno) << "\n"; return true; } return false; } static String readLine(std::ifstream& f) { String s; while(true) { char c = f.get(); if(!f.good() || c == '\n') { break; } s += c; } return s; } static bool testTokenizer(const String& input, const String& output) { tests++; std::ifstream oStream; oStream.open(output); if(!oStream.good()) { std::cout << "cannot open file '" << output << "'\n"; return true; } TokenStream tokenStream; if(Tokenizer::tokenize(tokenStream, input)) { return true; } while(true) { String expected = readLine(oStream); if(expected.getLength() == 0) { break; } else if(!tokenStream.hasToken()) { std::cout << "error in '" << input << "\n'out of tokens\n"; return false; } String buffer = tokenStream.nextTokenString(); if(strchr(buffer, '\n') != nullptr) { expected += '\n'; expected += readLine(oStream); } if(strcmp(buffer, expected) != 0) { std::cout << "error in '" << input << "\n'" << buffer << "' should be '" << expected << "'\n"; return false; } } done++; return false; } static void startTokenizerTests(const char* path) { done = 0; tests = 0; checkPath(path, ".tout", testTokenizer); std::cout << done << " / " << tests << " tokenizer tests succeeded\n"; } void Test::start(const char* path) { startTokenizerTests(path); }