// Tests for flat.h - directory traversal utilities #define FLAT_IMPLEMENTATION #include "../include/flat.h" #include #include #include #include #include #include #define ASSERT(cond, msg) \ do { \ if (!(cond)) { \ fprintf(stderr, "FAIL: %s (test_flat.c:%d)\n", msg, __LINE__); \ failures++; \ } \ } while (0) static int failures = 0; void test_path() { char *p = flat_path(3, "a", "b", "c"); ASSERT(p != NULL, "path returns non-NULL"); ASSERT(strcmp(p, "a/b/c") == 0, "path a/b/c"); free(p); p = flat_path(2, "home", "user"); ASSERT(strcmp(p, "home/user") == 0, "path home/user"); free(p); p = flat_path(1, "single"); ASSERT(strcmp(p, "single") == 0, "path single component"); free(p); } void test_path_zero() { char *p = flat_path(0, NULL); ASSERT(p == NULL, "zero components returns NULL"); } void test_entry_count() { ASSERT(mkdir("/tmp/test_flat_dir", 0777) == 0 || errno == EEXIST, "mkdir test_flat_dir"); FILE *f1 = fopen("/tmp/test_flat_dir/file1.txt", "w"); ASSERT(f1 != NULL, "fopen file1.txt"); if (!f1) { rmdir("/tmp/test_flat_dir"); return; } fclose(f1); FILE *f2 = fopen("/tmp/test_flat_dir/file2.txt", "w"); ASSERT(f2 != NULL, "fopen file2.txt"); if (!f2) { unlink("/tmp/test_flat_dir/file1.txt"); rmdir("/tmp/test_flat_dir"); return; } fclose(f2); int64_t count = flat_directory_entry_count("/tmp/test_flat_dir"); ASSERT(count >= 2, "entry count >= 2"); unlink("/tmp/test_flat_dir/file1.txt"); unlink("/tmp/test_flat_dir/file2.txt"); rmdir("/tmp/test_flat_dir"); } void test_entries() { ASSERT(mkdir("/tmp/test_flat_dir2", 0777) == 0 || errno == EEXIST, "mkdir test_flat_dir2"); FILE *f1 = fopen("/tmp/test_flat_dir2/alpha.txt", "w"); ASSERT(f1 != NULL, "fopen alpha.txt"); if (!f1) { rmdir("/tmp/test_flat_dir2"); return; } fclose(f1); FILE *f2 = fopen("/tmp/test_flat_dir2/beta.txt", "w"); ASSERT(f2 != NULL, "fopen beta.txt"); if (!f2) { unlink("/tmp/test_flat_dir2/alpha.txt"); rmdir("/tmp/test_flat_dir2"); return; } fclose(f2); char **entries = flat_directory_entries("/tmp/test_flat_dir2"); ASSERT(entries != NULL, "entries returns non-NULL"); if (!entries) { unlink("/tmp/test_flat_dir2/alpha.txt"); unlink("/tmp/test_flat_dir2/beta.txt"); rmdir("/tmp/test_flat_dir2"); return; } int found_alpha = 0, found_beta = 0; for (int64_t i = 0; entries[i]; i++) { if (strcmp(entries[i], "alpha.txt") == 0) found_alpha = 1; if (strcmp(entries[i], "beta.txt") == 0) found_beta = 1; } ASSERT(found_alpha, "found alpha.txt"); ASSERT(found_beta, "found beta.txt"); flat_free_entries(entries); unlink("/tmp/test_flat_dir2/alpha.txt"); unlink("/tmp/test_flat_dir2/beta.txt"); rmdir("/tmp/test_flat_dir2"); } void test_entries_invalid() { char **entries = flat_directory_entries("/nonexistent/path"); ASSERT(entries == NULL, "invalid path returns NULL"); } void test_free_entries_null() { flat_free_entries(NULL); } void test_entry_count_invalid() { int64_t count = flat_directory_entry_count("/nonexistent/path"); ASSERT(count == 0, "invalid path returns 0 count"); } void test_path_with_empty() { char *p = flat_path(3, "a", "", "c"); ASSERT(p != NULL, "path with empty returns non-NULL"); ASSERT(strcmp(p, "a//c") == 0, "path a//c with empty"); free(p); } int main() { test_path(); test_path_zero(); test_entry_count(); test_entries(); test_entries_invalid(); test_free_entries_null(); test_entry_count_invalid(); test_path_with_empty(); printf("test_flat: %d failures\n", failures); return failures; }