ocarina/tests/test.c

178 lines
3.4 KiB
C

/*
* Copyright 2015 (c) Anna Schumaker.
*/
#include <tests/test.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static unsigned int test_num;
unsigned int tests_failed = 0;
static void test_print_result(bool passed, unsigned int line)
{
printf("\t%u: ", test_num++);
if (passed)
printf("Success!\n");
else {
printf("Failed at line %u.\n", line);
tests_failed++;
}
}
static void test_loop_print_result(bool passed, unsigned int i,
unsigned int line)
{
if (!passed) {
printf("\t%u: ", test_num++);
printf("Failed loop at line: %u (i = %u)\n", line, i);
tests_failed++;
}
}
static void test_equal_common(gchar *lhs, gchar *rhs, bool passed)
{
if (!passed) {
printf("\t\t Actual: %s\n", lhs);
printf("\t\tExpected: %s\n", rhs);
}
g_free(lhs);
g_free(rhs);
}
static void test_not_equal_common(gchar *lhs, gchar *rhs, bool passed)
{
if (!passed)
printf("\t\tUnexpected: %s\n", lhs);
g_free(lhs);
g_free(rhs);
}
/*
* lhs and rhs should be newly allocated strings so we can
* free them in test{_not}_equal_common().
*/
void test_strings_equal(gchar *lhs, gchar *rhs, unsigned int line)
{
bool passed = strcmp(lhs, rhs) == 0;
test_print_result(passed, line);
test_equal_common(lhs, rhs, passed);
}
void test_strings_not_equal(gchar *lhs, gchar *rhs, unsigned int line)
{
bool passed = strcmp(lhs, rhs) != 0;
test_print_result(passed, line);
test_not_equal_common(lhs, rhs, passed);
}
void loop_strings_equal(gchar *lhs, gchar *rhs, unsigned int i,
unsigned int line)
{
bool passed = strcmp(lhs, rhs) == 0;
test_loop_print_result(passed, i, line);
test_equal_common(lhs, rhs, passed);
}
void loop_strings_not_equal(gchar *lhs, gchar *rhs, unsigned int i,
unsigned int line)
{
bool passed = strcmp(lhs, rhs) != 0;
test_loop_print_result(passed, i, line);
test_not_equal_common(lhs, rhs, passed);
}
static void run_test(struct UnitTest *test)
{
test_num = 0;
if (!test->t_name || !test->t_func)
return;
printf("Testing %s\n", test->t_name);
test->t_func();
printf("\n");
if (tests_failed > 0)
printf("\n%u tests failed =(\n\n", tests_failed);
}
gchar *test_data_file(const gchar *name)
{
return g_strjoin("/", g_get_user_data_dir(), "ocarina-test", name, NULL);
}
gchar *test_cache_file(const gchar *name)
{
return g_strjoin("/", g_get_user_cache_dir(), "ocarina-test", name, NULL);
}
bool test_data_file_exists(const gchar *name)
{
GFileTest test = G_FILE_TEST_EXISTS;
gchar *dir;
bool ret;
if (!name)
test = G_FILE_TEST_IS_DIR;
dir = g_strjoin("/", g_get_user_data_dir(), "ocarina-test", name, NULL);
ret = g_file_test(dir, test);
g_free(dir);
return ret;
}
static void test_rm_dir(const gchar *path)
{
const gchar *file = NULL;
GDir *dir = g_dir_open(path, 0, NULL);
if (!dir)
return;
while ((file = g_dir_read_name(dir)) != NULL) {
gchar *fullpath = g_strjoin("/", path, file, NULL);
if (g_file_test(fullpath, G_FILE_TEST_IS_DIR))
test_rm_dir(fullpath);
g_remove(fullpath);
g_free(fullpath);
}
g_dir_close(dir);
g_rmdir(path);
}
static void test_rm_dirs()
{
gchar *data = test_data_file(NULL);
gchar *cache = test_cache_file(NULL);
test_rm_dir(data);
test_rm_dir(cache);
g_free(data);
g_free(cache);
}
int main(int argc, char **argv)
{
unsigned int i;
if (test_data_file_exists(NULL))
test_rm_dirs();
for (i = 0; i < unit_tests_size; i++) {
run_test(&unit_tests[i]);
if (tests_failed != 0)
break;
}
printf("\n");
return tests_failed;
}