123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include <dirent.h>
- #include <string.h>
- #include <unistd.h>
- #include "test/Test.h"
- #include "utils/String.h"
- #include "tokenizer/TokenStream.h"
- #include "tokenizer/Tokenizer.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 == NULL) {
- std::cout << "cannot open '" << path << "': " << strerror(errno) << "\n";
- return true;
- }
- struct dirent* entry = nullptr;
- while(true) {
- 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, '.') == NULL) {
- 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;
- bool b = tokenize(&tokenStream, input);
- if(!b) {
- done++;
- }
- while(tokenStream.hasToken()) {
- String buffer = tokenStream.nextTokenString();
- String expected = readLine(oStream);
- if(strchr(buffer, '\n') != NULL) {
- expected += '\n';
- expected += readLine(oStream);
- }
- if(strcmp(buffer, expected) != 0) {
- std::cout << "error in '" << input << "\n'" << buffer << "' should be '" << expected << "'\n";
- done--;
- break;
- }
- }
- return b;
- }
- static void test_testTokenizer(const char* path) {
- done = 0;
- tests = 0;
- checkPath(path, ".tout", testTokenizer);
- std::cout << done << " / " << tests << " tokenizer tests succeeded" << std::endl;
- }
- void Test::start(const char* path) {
- test_testTokenizer(path);
- //testCompiler();
- //testOutput();
- }
|