libmrbesen/tests/filestests.cpp

171 lines
3.6 KiB
C++

#include "test.h"
#include "files.h"
#include "util.h"
#include <fstream>
#include <list>
#include <set>
#include <string>
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
#define NONEXISTENT_FILE "tests/blabla.exe"
using namespace mrbesen;
int testFiles_parent() {
std::string out, out2;
std::string testpath1 = "/asdf1/asdf2/test.png";
files::parent(testpath1, out);
CMPASSERT(out, "/asdf1/asdf2/");
files::parent(out, out2);
CMPASSERT(out2, "/asdf1/");
files::parent(out2, out);
CMPASSERT(out, "/");
testpath1 = "assets/tex/img1.png";
files::parent(testpath1, out);
CMPASSERT(out, "assets/tex/");
files::parent("asd", out);
CMPASSERT(out, "");
return TESTGOOD;
}
int testFiles_file() {
std::string a;
files::file("/abc/def/123.txt", a);
ASSERT(a == "123.txt", "normalfile" << a);
files::file("/abc/def/.gitignore", a);
ASSERT(a == ".gitignore", "dotfiles" << a);
files::file("/abc/def/executeable", a);
ASSERT(a == "executeable", "no extention" << a);
files::file("123.txt", a);
ASSERT(a == "123.txt", "normalfile - nopath" << a);
files::file(".gitignore", a);
ASSERT(a == ".gitignore", "dotfiles - nopath " << a);
files::file("executeable", a);
ASSERT(a == "executeable", "no extention - nopath " << a);
return TESTGOOD;
}
int testFiles_extention() {
std::string a;
files::extention("abc/asd/1.txt", a);
ASSERT(a == "txt", a);
files::extention("1.txt", a);
ASSERT(a == "txt", a);
files::extention("/1.txt", a);
ASSERT(a == "txt", "");
files::extention("/a.b.c/ad./1.txt", a);
ASSERT(a == "txt", "");
files::extention("a.b.c/ad./1.txt", a);
ASSERT(a == "txt", "");
files::extention("./1.txt", a);
ASSERT(a == "txt", "");
files::extention("/a/ad/1", a);
ASSERT(a == "", "");
files::extention("/a/ad/", a);
ASSERT(a == "", "");
files::extention("/", a);
ASSERT(a == "", "");
files::extention("/a", a);
ASSERT(a == "", "");
files::extention("/a.", a);
ASSERT(a == "", "");
files::extention("/a.b", a);
ASSERT(a == "b", "");
files::extention("/sad.asd/ab", a);
ASSERT(a == "", "");
files::extention("sad.asd/ab", a);
ASSERT(a == "", "");
return TESTGOOD;
}
int testFiles_iterateFile() {
std::ifstream f1(NONEXISTENT_FILE);
ASSERT(!files::iterateFile(f1, [](const std::string&) {}), "");
//TODO: more tests
return TESTGOOD;
}
int testFiles_scan() {
std::string path = "./tests";
std::list<std::string> out, out2;
ASSERT(files::scan(path, std::inserter(out, out.begin()), false), "");
ASSERT(files::scan(path, std::inserter(out2, out2.begin()), true), "");
ASSERT(out.size() == out2.size(), out.size() << " " << out2.size());
std::set<std::string> out3;
ASSERT(files::scan(path, std::inserter(out3, out3.begin()), false, [](const std::string& str, files::FileType t){ return mrbesen::util::startsWith(str, "."); }), "");
ASSERT(out3.size() == 2, out3.size());
ASSERT(out3.find(".") != out3.end(), "");
ASSERT(out3.find("..") != out3.end(), "");
return TESTGOOD;
}
int testFiles_readFile() {
std::ostringstream buf;
ASSERT(!files::readFile(NONEXISTENT_FILE, buf), "this file should not be read"); //try to read non existing file
const char* testfile = "tests/filestests.cpp";
bool r = files::readFile(std::string(testfile), buf);
ASSERT(r, "failed to read");
//alternative read
int fd = open(testfile, O_RDONLY);
const unsigned int size = 1024*1024; //1MB
char* buf2 = new char[size];
ssize_t rc = read(fd, buf2, size);
ASSERT(rc > 0, rc);
close(fd);
//compare
ASSERT(rc == buf.tellp(), "rc: " << rc << " buf.tellp(): " << buf.tellp());
ASSERT(std::memcmp(buf2, buf.str().c_str(), rc) == 0, "")
delete[] buf2;
return TESTGOOD;
}