156 lines
3.4 KiB
C++
156 lines
3.4 KiB
C++
#include "test.h"
|
|
#include "files.h"
|
|
#include "util.h"
|
|
|
|
#include <list>
|
|
#include <set>
|
|
#include <string>
|
|
#include <sstream>
|
|
|
|
#include <cstring>
|
|
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
using namespace mrbesen;
|
|
|
|
int testFiles_parent() {
|
|
std::string out, out2;
|
|
std::string testpath1 = "/asdf1/asdf2/test.png";
|
|
|
|
files::parent(testpath1, out);
|
|
ASSERT(out == "/asdf1/asdf2/", out);
|
|
|
|
files::parent(out, out2);
|
|
ASSERT(out2 == "/asdf1/", out2);
|
|
|
|
files::parent(out2, out);
|
|
ASSERT(out == "/", out);
|
|
|
|
testpath1 = "assets/tex/img1.png";
|
|
files::parent(testpath1, out);
|
|
ASSERT(out == "assets/tex/", out);
|
|
|
|
files::parent("asd", out);
|
|
ASSERT(out == "", 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_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("tests/blabla.exe", buf), "this file should not be read"); //try to read non existing file
|
|
|
|
bool r = files::readFile(std::string("tests/filestests.cpp"), buf);
|
|
ASSERT(r, "failed to read");
|
|
|
|
//alternative read
|
|
int fd = open("tests/filestests.cpp", 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;
|
|
}
|