12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- #include <cstring>
- #include <fstream>
- #include <iostream>
- #include <dirent.h>
- #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);
- }
|