1 |
/* |
2 |
recursive-dir-list.c - test `make_recursive_dir_list' function |
3 |
Copyright (C) 2007, 2008, 2009 siliconforks.com |
4 |
|
5 |
This program is free software; you can redistribute it and/or modify |
6 |
it under the terms of the GNU General Public License as published by |
7 |
the Free Software Foundation; either version 2 of the License, or |
8 |
(at your option) any later version. |
9 |
|
10 |
This program is distributed in the hope that it will be useful, |
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
GNU General Public License for more details. |
14 |
|
15 |
You should have received a copy of the GNU General Public License along |
16 |
with this program; if not, write to the Free Software Foundation, Inc., |
17 |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
18 |
*/ |
19 |
|
20 |
#include <assert.h> |
21 |
#include <stdlib.h> |
22 |
#include <string.h> |
23 |
|
24 |
#include "util.h" |
25 |
|
26 |
struct Expected { |
27 |
int count; |
28 |
const char * name; |
29 |
}; |
30 |
|
31 |
void cleanup(void) { |
32 |
system("rm -fr DIR"); |
33 |
} |
34 |
|
35 |
void touch(const char * file) { |
36 |
FILE * f = fopen(file, "w"); |
37 |
if (f == NULL) { |
38 |
fatal("cannot open file: %s", file); |
39 |
} |
40 |
fclose(f); |
41 |
} |
42 |
|
43 |
void verify(struct Expected * expected, struct DirListEntry * actual, int length) { |
44 |
struct DirListEntry * p = actual; |
45 |
while (p != NULL) { |
46 |
char * name = p->name; |
47 |
for (int i = 0; i < length; i++) { |
48 |
if (strcmp(expected[i].name, name) == 0) { |
49 |
expected[i].count++; |
50 |
break; |
51 |
} |
52 |
} |
53 |
p = p->next; |
54 |
} |
55 |
|
56 |
/* now verify the totals */ |
57 |
for (int i = 0; i < length; i++) { |
58 |
assert(expected[i].count == 1); |
59 |
} |
60 |
} |
61 |
|
62 |
int main(void) { |
63 |
atexit(cleanup); |
64 |
|
65 |
system("rm -fr DIR"); |
66 |
|
67 |
/* simple case */ |
68 |
xmkdir("DIR"); |
69 |
xmkdir("DIR/a"); |
70 |
xmkdir("DIR/a/b"); |
71 |
xmkdir("DIR/c"); |
72 |
xmkdir("DIR/d"); |
73 |
touch("DIR/0"); |
74 |
touch("DIR/a/1"); |
75 |
touch("DIR/a/b/2"); |
76 |
touch("DIR/c/3"); |
77 |
touch("DIR/c/4"); |
78 |
/* DIR/d is empty */ |
79 |
|
80 |
struct Expected expected[] = { |
81 |
{0, "c/4"}, |
82 |
{0, "c/3"}, |
83 |
{0, "a/b/2"}, |
84 |
{0, "a/1"}, |
85 |
{0, "0"}, |
86 |
}; |
87 |
|
88 |
struct DirListEntry * list = make_recursive_dir_list("DIR"); |
89 |
verify(expected, list, sizeof(expected) / sizeof(expected[0])); |
90 |
free_dir_list(list); |
91 |
|
92 |
exit(EXIT_SUCCESS); |
93 |
} |