123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include <iostream>
- #include <fstream>
- #include <cstring>
- #include <vector>
- #include <dirent.h>
- #include "test/Test.h"
- #include "test/TestLogger.h"
- #include "tokenizer/Token.h"
- #include "tokenizer/Tokenizer.h"
-
- static unsigned int done = 0;
- static unsigned int tests = 0;
- static TestLogger logger;
- static void forEachFile(const std::string& path, const std::string& ending, void (*f) (const std::string&, const std::string&))
- {
- DIR* dir;
- dir = opendir(path.c_str());
- struct dirent* entry = nullptr;
- if(dir != nullptr)
- {
- while((entry = readdir(dir)) != nullptr)
- {
- if(strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
- {
- continue;
- }
- if(entry->d_type == DT_DIR)
- {
- forEachFile(path + "/" + entry->d_name, ending, f);
- }
- else if(entry->d_type == DT_REG)
- {
- if(strchr(entry->d_name, '.') == nullptr)
- {
- std::string pathInputFile = path + "/" + entry->d_name;
- std::string pathOutputFile = pathInputFile + ending;
- f(pathInputFile, pathOutputFile);
- }
- }
- }
- closedir(dir);
- }
- }
- static void testTokenizer(const char* path)
- {
- done = 0;
- tests = 0;
- forEachFile(path, ".tout", [](const std::string& input, const std::string& output)
- {
- tests++;
-
- std::ifstream iStream;
- iStream.open(input);
-
- std::ifstream oStream;
- oStream.open(output);
-
- if(!iStream.good() || !oStream.good())
- {
- return;
- }
-
- std::vector<Token> tokens;
- try
- {
- Tokenizer::tokenize(tokens, iStream);
- }
- catch(std::exception& ex)
- {
- return;
- }
-
- logger.reset();
- for(Token& token : tokens)
- {
- std::string s = token.toString();
- logger.print(&s);
- }
-
- if(logger.check(input, oStream))
- {
- done++;
- }
- });
- std::cout << done << " / " << tests << " tokenizer tests succeeded" << std::endl;
- }
- void Test::start(const char* path)
- {
- testTokenizer(path);
-
-
- }
|