Initial commit

This commit is contained in:
2023-12-06 11:49:25 +01:00
parent 15831dabd6
commit bc54546bc2
12 changed files with 558 additions and 1 deletions

42
lib/testlib.cpp Normal file
View File

@@ -0,0 +1,42 @@
/** @file testlib.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Implementation of the testing library
*
* @bug No known bugs
* */
#include "testlib.hpp"
namespace details {
void m_assert(bool expr, std::string expr_str, std::string f, std::string file,
int line, std::string msg)
{
std::function<void(const std::string &)> print_message =
[](const std::string &msg) {
if (msg.size() > 0) {
std::cout << "message: " << msg << "\n\n";
}
else {
std::cout << "\n";
}
};
std::string new_assert(f.size() + (expr ? 4 : 6), '-');
std::cout << "\x1B[36m" << new_assert << "\033[0m\n";
std::cout << f << ": ";
if (expr) {
std::cout << "\x1B[32mOK\033[0m\n";
print_message(msg);
}
else {
std::cout << "\x1B[31mFAIL\033[0m\n";
print_message(msg);
std::cout << file << " " << line << ": Assertion \"" << expr_str
<< "\" Failed\n\n";
abort();
}
}
} // namespace details

73
lib/utils.cpp Normal file
View File

@@ -0,0 +1,73 @@
/** @file utils.cpp
*
* @author Cory Alexander Balaton (coryab)
* @author Janita Ovidie Sandtrøen Willumsen (janitaws)
*
* @version 1.0
*
* @brief Implementation of the utils
*
* @bug No known bugs
* */
#include "utils.hpp"
namespace utils {
std::string scientific_format(double d, int width, int prec)
{
std::stringstream ss;
ss << std::setw(width) << std::setprecision(prec) << std::scientific << d;
return ss.str();
}
std::string scientific_format(const std::vector<double> &v, int width, int prec)
{
std::stringstream ss;
for (double elem : v) {
ss << scientific_format(elem, width, prec);
}
return ss.str();
}
bool mkpath(std::string path, int mode)
{
std::string cur_dir;
std::string::size_type pos = -1;
struct stat buf;
if (path.back() != '/') {
path += '/';
}
while (true) {
pos++;
pos = path.find('/', pos);
if (pos != std::string::npos) {
cur_dir = path.substr(0, pos);
if (mkdir(cur_dir.c_str(), mode) != 0
&& stat(cur_dir.c_str(), &buf) != 0) {
return -1;
}
}
else {
break;
}
}
return 0;
}
std::string dirname(const std::string &path)
{
return path.substr(0, path.find_last_of("/"));
}
std::string concatpath(const std::string &left, const std::string &right)
{
if (left.back() != '/' and right.front() != '/') {
return left + '/' + right;
}
else {
return left + right;
}
}
} // namespace utils