1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
// Tests for flat.h - directory traversal utilities
#define FLAT_IMPLEMENTATION
#include "../include/flat.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#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;
}
|