138 lines
2.9 KiB
C++
138 lines
2.9 KiB
C++
/*
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#include <tests/test.h>
|
|
|
|
#include <glib.h>
|
|
#include <iostream>
|
|
#include <stdlib.h>
|
|
|
|
static unsigned int test_num;
|
|
unsigned int test :: failed;
|
|
|
|
|
|
template <class T>
|
|
static void generic_equal(const T &a, const T &b, const unsigned int line)
|
|
{
|
|
std::cout << " " << test_num++ << ": ";
|
|
if (a == b)
|
|
std::cout << "Success!" << std::endl;
|
|
else {
|
|
std::cout << "Failed at line " << line << ":" << std::endl;
|
|
std::cout << " Actual: " << a << std::endl;
|
|
std::cout << " Expected: " << b << std::endl;
|
|
test :: failed++;
|
|
}
|
|
}
|
|
|
|
template <class T>
|
|
static void generic_not_equal(const T &a, const T &b, const unsigned int line)
|
|
{
|
|
std::cout << " " << test_num++ << ": ";
|
|
if (a != b)
|
|
std::cout << "Success!" << std::endl;
|
|
else {
|
|
std::cout << "Failed at line " << line << ":" << std::endl;
|
|
std::cout << " Unexpected: " << a << std::endl;
|
|
test :: failed++;
|
|
}
|
|
}
|
|
|
|
|
|
void test :: run(const std::string &name, void (*func)())
|
|
{
|
|
failed = 0;
|
|
test_num = 0;
|
|
std::cout << name << std::endl;
|
|
|
|
func();
|
|
|
|
std::cout << std::endl;
|
|
if (failed > 0) {
|
|
std::cout << failed << " tests failed =(" << std::endl << std::endl;
|
|
exit(failed);
|
|
}
|
|
}
|
|
|
|
void test :: for_each(unsigned int init, unsigned int max, unsigned int inc,
|
|
unsigned int (*func)(unsigned int), unsigned int line)
|
|
{
|
|
std::cout << " " << test_num++ << ": ";
|
|
for (unsigned int i = init; i < max; i += inc) {
|
|
int ret = func(i);
|
|
if (ret != 0) {
|
|
std::cout << "Failed loop at line: " << ret;
|
|
std::cout << " (i = " << i << ")" << std::endl;
|
|
std::cout << " Called from line: " << line << std::endl;
|
|
test :: failed++;
|
|
return;
|
|
}
|
|
}
|
|
std::cout << "Success!" << std::endl;
|
|
}
|
|
|
|
#define DEFINE_COMPARE_FUNC(name, type) \
|
|
void test :: name(const type a, const type b, unsigned int line) \
|
|
{ generic_##name(a, b, line); }
|
|
|
|
#define DEFINE_COMPARE(type) \
|
|
DEFINE_COMPARE_FUNC(equal, type) \
|
|
DEFINE_COMPARE_FUNC(not_equal, type)
|
|
|
|
DEFINE_COMPARE(int)
|
|
DEFINE_COMPARE(std::string &)
|
|
DEFINE_COMPARE(void *)
|
|
|
|
std::string test :: data_dir()
|
|
{
|
|
std::string res = g_get_user_data_dir();
|
|
res += "/ocarina-test";
|
|
return res;
|
|
}
|
|
|
|
std::string test :: data_file(const std::string &name)
|
|
{
|
|
return data_dir() + "/" + name;
|
|
}
|
|
|
|
bool test :: data_dir_exists()
|
|
{
|
|
return g_file_test(data_dir().c_str(), G_FILE_TEST_IS_DIR);
|
|
}
|
|
|
|
bool test :: data_file_exists(const std::string &name)
|
|
{
|
|
return g_file_test(data_file(name).c_str(), G_FILE_TEST_EXISTS);
|
|
}
|
|
|
|
void test :: rm_data_dir()
|
|
{
|
|
std::string cmd = "rm -r " + data_dir() + " 2>/dev/null";
|
|
if (data_dir_exists())
|
|
system(cmd.c_str());
|
|
}
|
|
|
|
void test :: reset_data_dir()
|
|
{
|
|
std::string cmd = "mkdir -p " + data_dir();
|
|
rm_data_dir();
|
|
system(cmd.c_str());
|
|
}
|
|
|
|
void test :: cp_data_dir()
|
|
{
|
|
reset_data_dir();
|
|
std::string cmd = "cp -r tests/Data/* " + data_dir();
|
|
system(cmd.c_str());
|
|
}
|
|
|
|
void test :: gen_library()
|
|
{
|
|
system("tests/gen_library.sh");
|
|
}
|
|
|
|
void test :: rm_library_dirs()
|
|
{
|
|
system("rm -r /tmp/ocarina/dir2 /tmp/ocarina/dir4");
|
|
}
|