/* * Copyright 2015 (c) Anna Schumaker. */ #include #include #include #include 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++; } } /* * lhs and rhs should be newly allocated strings so we can * free them at the end of this function. */ void test_strings_equal(gchar *lhs, gchar *rhs, unsigned int line) { bool passed = strcmp(lhs, rhs) == 0; test_print_result(passed, line); if (!passed) { printf("\t\t Actual: %s\n", lhs); printf("\t\tExpected: %s\n", rhs); } g_free(lhs); g_free(rhs); } void test_strings_not_equal(gchar *lhs, gchar *rhs, unsigned int line) { bool passed = strcmp(lhs, rhs) != 0; test_print_result(passed, line); if (!passed) printf("\t\tUnexpected: %s\n", lhs); g_free(lhs); g_free(rhs); } 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); if (!passed) { printf("\t\t Actual: %s\n", lhs); printf("\t\tExpected: %s\n", rhs); } g_free(lhs); g_free(rhs); } 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); if (!passed) printf("\t\tUnexpected: %s\n", lhs); g_free(lhs); g_free(rhs); } static void run_test(struct UnitTest *test) { test_num = 0; 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); } 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; } void test_cp_data_dir() { const gchar *file = NULL; gchar *dst_dir = test_data_file(NULL); GDir *dir = g_dir_open("tests/Data", 0, NULL); if (!dir) goto out; g_mkdir_with_parents(dst_dir, 0755); /* TODO: This could be a good place to use vfs_copy_file_range() */ while((file = g_dir_read_name(dir)) != NULL) { gchar *orig = g_strjoin("/", "tests/Data", file, NULL); gchar *dst = g_strjoin("/", dst_dir, file, NULL); gchar *cmd = g_strjoin(" ", "cp", orig, dst, NULL); system(cmd); g_free(orig); g_free(dst); g_free(cmd); } g_dir_close(dir); out: g_free(dst_dir); } void test_rm_data_dir() { const gchar *file = NULL; gchar *path = test_data_file(NULL); GDir *dir = g_dir_open(path, 0, NULL); if (!dir) goto out; while ((file = g_dir_read_name(dir)) != NULL) { gchar *fullpath = g_strjoin("/", path, file, NULL); g_remove(fullpath); g_free(fullpath); } g_dir_close(dir); g_rmdir(path); out: g_free(path); } void test_generate_library() { system("tests/gen_library.sh"); } void test_rm_library_dirs() { system("rm -r /tmp/ocarina/dir2 /tmp/ocarina/dir4"); } int main(int argc, char **argv) { unsigned int i; if (test_data_file_exists(NULL)) test_rm_data_dir(); for (i = 0; i < unit_tests_size; i++) { run_test(&unit_tests[i]); if (tests_failed != 0) break; } printf("\n"); return tests_failed; }