2014-04-13 12:26:32 -04:00
|
|
|
/*
|
|
|
|
* Copyright 2014 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
2014-04-26 13:31:22 -04:00
|
|
|
#include <glib.h>
|
2014-04-13 12:26:32 -04:00
|
|
|
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
static unsigned int test_num;
|
2014-04-26 11:59:12 -04:00
|
|
|
static unsigned int failed;
|
2014-04-13 12:26:32 -04:00
|
|
|
|
|
|
|
void new_test(const std::string &name)
|
|
|
|
{
|
|
|
|
std::cout << name << std::endl;
|
|
|
|
test_num = 0;
|
2014-04-26 11:59:12 -04:00
|
|
|
failed = 0;
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void begin()
|
|
|
|
{
|
|
|
|
std::cout << " " << test_num << ": ";
|
|
|
|
test_num++;
|
|
|
|
}
|
|
|
|
|
2014-04-26 11:00:44 -04:00
|
|
|
void end()
|
|
|
|
{
|
|
|
|
std::cout << std::endl;
|
2014-04-26 11:59:12 -04:00
|
|
|
if (failed > 0) {
|
|
|
|
std::cout << failed << " tests failed =(" << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
exit(failed);
|
|
|
|
}
|
2014-04-26 11:00:44 -04:00
|
|
|
}
|
|
|
|
|
2014-04-13 12:26:32 -04:00
|
|
|
template <class T>
|
2014-04-26 13:31:22 -04:00
|
|
|
void check_equal(const T &lhs, const T &rhs, unsigned int line)
|
2014-04-13 12:26:32 -04:00
|
|
|
{
|
|
|
|
if (lhs == rhs)
|
|
|
|
std::cout << "Success!" << std::endl;
|
|
|
|
else {
|
2014-04-26 11:59:12 -04:00
|
|
|
std::cout << "Failed at line " << line << ":" << std::endl;
|
2014-04-13 12:26:32 -04:00
|
|
|
std::cout << " Actual: " << lhs << std::endl;
|
|
|
|
std::cout << " Expected: " << rhs << std::endl;
|
2014-04-26 11:59:12 -04:00
|
|
|
failed++;
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
template <class T>
|
|
|
|
void equal(const T &lhs, const T &rhs, unsigned int line = 0)
|
|
|
|
{
|
|
|
|
begin();
|
|
|
|
check_equal(lhs, rhs, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string data_dir()
|
|
|
|
{
|
|
|
|
std::string res = g_get_user_data_dir();
|
|
|
|
res += "/ocarina-test";
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string data_file(const std::string &name)
|
|
|
|
{
|
|
|
|
return data_dir() + "/" + name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool data_dir_exists()
|
|
|
|
{
|
|
|
|
return g_file_test(data_dir().c_str(), G_FILE_TEST_IS_DIR);
|
|
|
|
}
|
2014-04-13 12:26:32 -04:00
|
|
|
}
|
2014-04-26 11:59:12 -04:00
|
|
|
|
|
|
|
#define run_test(name, func, ...) \
|
|
|
|
do { \
|
|
|
|
test :: new_test(name); \
|
|
|
|
func(##__VA_ARGS__); \
|
|
|
|
test :: end(); \
|
|
|
|
} while (0)
|
|
|
|
|
2014-04-26 13:31:22 -04:00
|
|
|
#define test_equal(lhs, rhs) \
|
|
|
|
do { \
|
|
|
|
test :: equal(lhs, rhs, __LINE__); \
|
2014-04-26 11:59:12 -04:00
|
|
|
} while (0)
|