libmrbesen/tests/filestests.cpp

171 lines
3.6 KiB
C++
Raw Permalink Normal View History

2020-09-28 23:49:14 +02:00
#include "test.h"
#include "files.h"
2020-10-02 15:54:12 +02:00
#include "util.h"
2020-09-28 23:49:14 +02:00
2021-02-07 21:25:58 +01:00
#include <fstream>
2020-10-02 15:54:12 +02:00
#include <list>
#include <set>
2020-09-28 23:49:14 +02:00
#include <string>
2020-10-08 10:34:12 +02:00
#include <sstream>
#include <cstring>
#include <unistd.h>
#include <fcntl.h>
2020-09-28 23:49:14 +02:00
2021-02-07 21:25:58 +01:00
#define NONEXISTENT_FILE "tests/blabla.exe"
2020-09-28 23:49:14 +02:00
using namespace mrbesen;
int testFiles_parent() {
std::string out, out2;
std::string testpath1 = "/asdf1/asdf2/test.png";
2020-10-07 13:03:07 +02:00
files::parent(testpath1, out);
2021-02-07 21:25:58 +01:00
CMPASSERT(out, "/asdf1/asdf2/");
2020-09-28 23:49:14 +02:00
2020-10-07 13:03:07 +02:00
files::parent(out, out2);
2021-02-07 21:25:58 +01:00
CMPASSERT(out2, "/asdf1/");
2020-09-28 23:49:14 +02:00
2020-10-07 13:03:07 +02:00
files::parent(out2, out);
2021-02-07 21:25:58 +01:00
CMPASSERT(out, "/");
2020-09-28 23:49:14 +02:00
testpath1 = "assets/tex/img1.png";
2020-10-07 13:03:07 +02:00
files::parent(testpath1, out);
2021-02-07 21:25:58 +01:00
CMPASSERT(out, "assets/tex/");
2020-09-28 23:49:14 +02:00
2020-10-07 13:03:07 +02:00
files::parent("asd", out);
2021-02-07 21:25:58 +01:00
CMPASSERT(out, "");
2020-09-28 23:49:14 +02:00
return TESTGOOD;
}
int testFiles_file() {
std::string a;
2020-10-07 13:03:07 +02:00
files::file("/abc/def/123.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "123.txt", "normalfile" << a);
2020-10-07 13:03:07 +02:00
files::file("/abc/def/.gitignore", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == ".gitignore", "dotfiles" << a);
2020-10-07 13:03:07 +02:00
files::file("/abc/def/executeable", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "executeable", "no extention" << a);
2020-10-07 13:03:07 +02:00
files::file("123.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "123.txt", "normalfile - nopath" << a);
2020-10-07 13:03:07 +02:00
files::file(".gitignore", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == ".gitignore", "dotfiles - nopath " << a);
2020-10-07 13:03:07 +02:00
files::file("executeable", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "executeable", "no extention - nopath " << a);
return TESTGOOD;
}
int testFiles_extention() {
std::string a;
2020-10-07 13:03:07 +02:00
files::extention("abc/asd/1.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "txt", a);
2020-10-07 13:03:07 +02:00
files::extention("1.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "txt", a);
2020-10-07 13:03:07 +02:00
files::extention("/1.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "txt", "");
2020-10-07 13:03:07 +02:00
files::extention("/a.b.c/ad./1.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "txt", "");
2020-10-07 13:03:07 +02:00
files::extention("a.b.c/ad./1.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "txt", "");
2020-10-07 13:03:07 +02:00
files::extention("./1.txt", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "txt", "");
2020-10-07 13:03:07 +02:00
files::extention("/a/ad/1", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "", "");
2020-10-07 13:03:07 +02:00
files::extention("/a/ad/", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "", "");
2020-10-07 13:03:07 +02:00
files::extention("/", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "", "");
2020-10-07 13:03:07 +02:00
files::extention("/a", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "", "");
2020-10-07 13:03:07 +02:00
files::extention("/a.", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "", "");
2020-10-07 13:03:07 +02:00
files::extention("/a.b", a);
2020-09-28 23:49:14 +02:00
ASSERT(a == "b", "");
2020-10-07 13:03:07 +02:00
files::extention("/sad.asd/ab", a);
2020-09-29 14:04:19 +02:00
ASSERT(a == "", "");
2020-10-07 13:03:07 +02:00
files::extention("sad.asd/ab", a);
2020-09-29 14:04:19 +02:00
ASSERT(a == "", "");
2020-09-28 23:49:14 +02:00
return TESTGOOD;
}
2020-10-02 15:54:12 +02:00
2021-02-07 21:25:58 +01:00
int testFiles_iterateFile() {
std::ifstream f1(NONEXISTENT_FILE);
ASSERT(!files::iterateFile(f1, [](const std::string&) {}), "");
//TODO: more tests
return TESTGOOD;
}
2020-10-02 15:54:12 +02:00
int testFiles_scan() {
std::string path = "./tests";
std::list<std::string> out, out2;
2020-10-07 13:03:07 +02:00
ASSERT(files::scan(path, std::inserter(out, out.begin()), false), "");
ASSERT(files::scan(path, std::inserter(out2, out2.begin()), true), "");
2020-10-02 15:54:12 +02:00
ASSERT(out.size() == out2.size(), out.size() << " " << out2.size());
std::set<std::string> out3;
2020-10-07 13:03:07 +02:00
ASSERT(files::scan(path, std::inserter(out3, out3.begin()), false, [](const std::string& str, files::FileType t){ return mrbesen::util::startsWith(str, "."); }), "");
2020-10-02 15:54:12 +02:00
ASSERT(out3.size() == 2, out3.size());
ASSERT(out3.find(".") != out3.end(), "");
ASSERT(out3.find("..") != out3.end(), "");
return TESTGOOD;
}
2020-10-08 10:34:12 +02:00
int testFiles_readFile() {
std::ostringstream buf;
2021-02-07 21:25:58 +01:00
ASSERT(!files::readFile(NONEXISTENT_FILE, buf), "this file should not be read"); //try to read non existing file
2020-10-08 10:34:12 +02:00
2021-09-07 02:44:50 +02:00
const char* testfile = "tests/filestests.cpp";
bool r = files::readFile(std::string(testfile), buf);
2020-10-08 10:34:12 +02:00
ASSERT(r, "failed to read");
//alternative read
2021-09-07 02:44:50 +02:00
int fd = open(testfile, O_RDONLY);
2020-10-08 10:34:12 +02:00
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;
}